light: simplify settings logic a bit

This commit is contained in:
Eric Wasylishen 2016-07-27 01:53:57 -06:00
parent 6da4ab87ae
commit cbfe1e8ebf
2 changed files with 59 additions and 67 deletions

View File

@ -107,7 +107,6 @@ public:
spotangle { "angle", 40.0f },
spotangle2 { "softangle", 0.0f },
style { "style", 0.0f },
bleed { "bleed", false },
anglescale {strings{"anglesense", "anglescale"}, -1.0f }, // fallback to worldspawn
dirtscale { "dirtscale", 0.0f },
dirtgain { "dirtgain", 0 },
@ -115,6 +114,7 @@ public:
deviance { "deviance", 0 },
samples { "samples", 16 },
projfov { "project_fov", 90 },
bleed { "bleed", false },
origin { "origin", 0, 0, 0 },
color { "color", 255.0f, 255.0f, 255.0f, vec3_transformer_t::NORMALIZE_COLOR_TO_255 },
mangle { "mangle", 0, 0, 0 }, // not transformed to vec

View File

@ -213,22 +213,32 @@ Face_EdgeIndexSmoothed(const bsp2_t *bsp, const bsp2_dface_t *f, const int edgei
/* command-line options */
enum class setting_source_t {
DEFAULT,
MAP,
COMMANDLINE
DEFAULT = 0,
MAP = 1,
COMMANDLINE = 2
};
class lockable_setting_t {
protected:
bool _locked;
setting_source_t _source;
bool _registered;
std::vector<std::string> _names;
lockable_setting_t(std::vector<std::string> names)
: _locked(false), _registered(false), _names(names) {
: _source(setting_source_t::DEFAULT), _registered(false), _names(names) {
assert(_names.size() > 0);
}
bool changeSource(setting_source_t newSource) {
assert(_registered);
if (static_cast<int>(newSource) >= static_cast<int>(_source)) {
_source = newSource;
return true;
}
return false;
}
public:
const std::string &primaryName() const { return _names.at(0); }
const std::vector<std::string> &names() const { return _names; }
@ -236,8 +246,8 @@ public:
virtual void setStringValue(const std::string &str, bool locked = false) = 0;
virtual std::string stringValue() const = 0;
virtual bool isChanged() const = 0;
bool isLocked() const { return _locked; }
bool isChanged() const { return _source != setting_source_t::DEFAULT; }
bool isLocked() const { return _source != setting_source_t::COMMANDLINE; }
bool isRegistered() { return _registered; }
void setRegistered() { _registered = true; }
@ -247,22 +257,23 @@ class lockable_bool_t : public lockable_setting_t {
private:
bool _default, _value;
void setBoolInternal(bool f) {
assert(_registered);
void setBoolValueInternal(bool f, setting_source_t newsource) {
if (f == _value) {
return;
}
if (changeSource(newsource)) {
_value = f;
}
}
public:
void setBoolValueLocked(bool f) {
setBoolInternal(f);
_locked = true;
setBoolValueInternal(f, setting_source_t::COMMANDLINE);
}
void setBoolValue(bool f) {
if (!_locked) {
setBoolInternal(f);
}
setBoolValueInternal(f, setting_source_t::MAP);
}
bool boolValue() const {
@ -281,10 +292,6 @@ public:
return _value ? "1" : "0";
}
virtual bool isChanged() const {
return _value != _default;
}
lockable_bool_t(std::vector<std::string> names, bool v)
: lockable_setting_t(names), _default(v), _value(v) {}
@ -296,8 +303,11 @@ class lockable_vec_t : public lockable_setting_t {
private:
float _default, _value, _min, _max;
void setFloatInternal(float f) {
assert(_registered);
void setFloatInternal(float f, setting_source_t newsource) {
if (f == _value) {
return;
}
if (changeSource(newsource)) {
if (f < _min) {
logprint("WARNING: '%s': %f is less than minimum value %f.\n",
primaryName().c_str(), f, _min);
@ -310,6 +320,7 @@ private:
}
_value = f;
}
}
public:
bool boolValue() const {
@ -325,14 +336,11 @@ public:
}
void setFloatValue(float f) {
if (!_locked) {
setFloatInternal(f);
}
setFloatInternal(f, setting_source_t::MAP);
}
void setFloatValueLocked(float f) {
setFloatInternal(f);
_locked = true;
setFloatInternal(f, setting_source_t::COMMANDLINE);
}
virtual void setStringValue(const std::string &str, bool locked = false) {
@ -345,10 +353,6 @@ public:
return std::to_string(_value);
}
virtual bool isChanged() const {
return _value != _default;
}
lockable_vec_t(std::vector<std::string> names, float v,
float minval=-std::numeric_limits<float>::infinity(),
float maxval=std::numeric_limits<float>::infinity())
@ -371,12 +375,10 @@ private:
public:
virtual void setStringValue(const std::string &str, bool locked = false) {
if (!_locked || locked) {
if (str == _value)
return;
if (changeSource(locked ? setting_source_t::COMMANDLINE : setting_source_t::MAP)) {
_value = str;
if (locked) {
_locked = true;
}
}
}
@ -384,10 +386,6 @@ public:
return _value;
}
virtual bool isChanged() const {
return _value != _default;
}
lockable_string_t(std::vector<std::string> names, std::string v)
: lockable_setting_t(names), _default(v), _value(v) {}
@ -429,8 +427,16 @@ private:
}
}
void transformAndSetVec3Value(const vec3_t val) {
transformVec3Value(val, _value);
void transformAndSetVec3Value(const vec3_t val, setting_source_t newsource) {
vec3_t tmp;
transformVec3Value(val, tmp);
if (VectorCompare(tmp, _value)) {
return;
}
if (changeSource(newsource)) {
VectorCopy(tmp, _value);
}
}
public:
@ -452,16 +458,11 @@ public:
}
void setVec3Value(const vec3_t val) {
assert(_registered);
if (!_locked) {
transformAndSetVec3Value(val);
}
transformAndSetVec3Value(val, setting_source_t::MAP);
}
void setVec3ValueLocked(const vec3_t val) {
assert(_registered);
transformAndSetVec3Value(val);
_locked = true;
transformAndSetVec3Value(val, setting_source_t::COMMANDLINE);
}
virtual void setStringValue(const std::string &str, bool locked = false) {
@ -486,15 +487,6 @@ public:
<< _value[2];
return ss.str();
}
virtual bool isChanged() const {
for (int i=0; i<3; i++) {
if (_default[i] != _value[i]) {
return true;
}
}
return false;
}
};
void SetGlobalSetting(std::string name, std::string value, bool cmdline);