Started upgrading things to new state

This commit is contained in:
2023-02-27 23:36:03 -08:00
parent fc34ae94ff
commit 8e04742d5a
19 changed files with 456 additions and 495 deletions

View File

@ -1,114 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "StateEvent.hpp"
namespace Dawn {
template<class V>
class StateProperty;
class StateOwner {
private:
std::map<void*, std::vector<std::function<void()>>> effectListeners;
std::vector<std::function<void()>> stateUpdateListeners;
public:
/**
* Creates a new state property and listens for its change.
*
* @tparam V The type of the state that is held.
* @param initial The initial value of this state.
* @return The state that can then be listened for.
*/
template<class V>
StateProperty<V> useState(V initial) {
auto property = StateProperty<V>();
property.value = initial;
property.owner = this;
return property;
}
/**
* Listen for changees to a state property and involke the provided func
* when the value is changed.
*
* @tparam V The type of the state property.
* @param property Property to listen for affect changees to.
* @param fn The callback to be invoked when the state value changes.
*/
template<class V>
void useEffect(StateProperty<V> &property, const std::function<void()> &fn) {
assertFalse(property.owner == this);
this->effectListeners[(void*)&property].push_back(fn);
}
/**
* Listen for changes to any single state property managed by this state
* owner.
*
* @param fn Function to be invoked when any state property is updated.
*/
void useStateUpdated(const std::function<void()> &fn) {
this->stateUpdateListeners.push_back(fn);
}
/**
* Listen for when an event is invoked by a state event. This is intended
* to allow for cross-state-owner communication in a simple and effective
* way.
*
* @tparam A The arguments from the state event that are calledback.
* @param event The event that is being subscribed to.
* @param fn The function to be inokved on event trigger.
*/
template<typename...A>
void useEvent(StateEvent<A...> &event, const std::function<void(A...)> &fn) {
event.listeners.push_back(fn);
}
/**
* Internal method (that has to be exposed) to listen for changes for when
* a state property that belongs to this state owner is updated.
*
* @tparam V The value type of the state property.
* @param prop The property that has its value changed in question.
* @param n The new, current value of the property.
* @param o The old, previous value of the property.
*/
template<class V>
void _statePropertyUpdated(StateProperty<V> *prop, V n, V o) {
auto eff = &this->effectListeners[prop];
auto itEff = eff->begin();
while(itEff != eff->end()) {
(*itEff)();
++itEff;
}
auto itUpdate = this->stateUpdateListeners.begin();
while(itUpdate != this->stateUpdateListeners.end()) {
(*itUpdate)();
++itUpdate;
}
}
/**
* Internal method to listen for when a state property is disposed or
* destroyed so that it can be completely removed from this state owner.
*
* @tparam V Value type.
* @param prop Property that was destroyed.
*/
template<class V>
void _statePropertyDestroyed(StateProperty<V> *prop) {
this->effectListeners.erase((void*)prop);
}
virtual ~StateOwner() {
}
};
}