Started upgrading things to new state
This commit is contained in:
@ -7,14 +7,9 @@
|
||||
#include "assert/assert.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class StateOwner;
|
||||
|
||||
template<class V>
|
||||
class StateProperty {
|
||||
private:
|
||||
StateOwner *owner;
|
||||
V value;
|
||||
|
||||
/**
|
||||
* Method that is invoked every time that the value of this state property
|
||||
* is updated.
|
||||
@ -22,25 +17,45 @@ namespace Dawn {
|
||||
* @param val Value that is to be used for this property.
|
||||
*/
|
||||
void setInternal(V val) {
|
||||
if(val == this->value) return;// TODO: can I omit this? kinda bad tbh.
|
||||
assertNotNull(this->owner);
|
||||
auto old = this->value;
|
||||
this->value = val;
|
||||
this->owner->_statePropertyUpdated(this, val, old);
|
||||
if(val == this->_realValue) return;// TODO: can I omit this? kinda bad tbh.
|
||||
this->previous = this->_realValue;
|
||||
this->_realValue = val;
|
||||
|
||||
// Notify the effect listeners
|
||||
auto itEffect = this->_effectListners.begin();
|
||||
while(itEffect != this->_effectListners.end()) {
|
||||
(*itEffect)();
|
||||
++itEffect;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
V _realValue;
|
||||
std::vector<std::function<void()>> _effectListners;
|
||||
V previous;
|
||||
|
||||
/**
|
||||
* Creates a new state property and listens for its change.
|
||||
*/
|
||||
StateProperty() {}
|
||||
|
||||
/**
|
||||
* Creates a new state property and listens for its change.
|
||||
* @param initial The initial value of this state.
|
||||
*/
|
||||
StateProperty(V initial) : _realValue(initial) {}
|
||||
|
||||
const StateProperty& operator += (const V &value) {
|
||||
this->setInternal(this->value + value);
|
||||
this->setInternal(this->_realValue + value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
const bool_t operator != (const V &value) {
|
||||
return value != this->value;
|
||||
return value != this->_realValue;
|
||||
}
|
||||
|
||||
const bool_t operator == (const V &value) {
|
||||
return value == this->value;
|
||||
return value == this->_realValue;
|
||||
}
|
||||
|
||||
const StateProperty& operator = (const V &val) {
|
||||
@ -49,15 +64,13 @@ namespace Dawn {
|
||||
}
|
||||
|
||||
operator V() const {
|
||||
return this->value;
|
||||
return this->_realValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor for StateProperty.
|
||||
*/
|
||||
~StateProperty() {
|
||||
this->owner->_statePropertyDestroyed(this);
|
||||
}
|
||||
~StateProperty() {}
|
||||
|
||||
friend class StateOwner;
|
||||
};
|
||||
|
Reference in New Issue
Block a user