CG-Project2
Loading...
Searching...
No Matches
State.hpp
1#pragma once
2
3#include <map>
4#include "../../../Opengl-Core/include/Core.hpp"
5
21class State {
22public:
30 virtual void onAttach() {
31 std::cout << "Attaching state: " << this->m_name << " to StateManager\n";
32 this->m_attached = true;
33 }
34
42 virtual void onDetach() {
43 std::cout << "Detaching state: " << this->m_name << " from StateManager\n";
44 this->m_attached = false;
45 }
46
51 virtual void onUpdate() {}
52
57 virtual void onRender() {}
58
64 virtual bool isCurrentStateEnd() { return true; }
65
70 std::string getName() const { return this->m_name; }
71
76 bool isAttached() const { return this->m_attached; }
77
85 State(const std::string &name = "State") :
86 m_name(std::move(name)) {}
87 virtual ~State() = default;
88
89protected:
91 const std::string m_name;
93 bool m_attached = false;
94};
95
105class StateManager {
106public:
107 StateManager(StateManager &other) = delete;
108
109 void operator=(const StateManager &other) = delete;
110
117 inline static Shared<StateManager> instance() {
118 if (s_pointer == nullptr) {
119 Shared<StateManager> copy(new StateManager());
120 copy.swap(s_pointer);
121 }
122 return s_pointer;
123 }
124
130 bool shouldExit() const {
131 ASSERT(this->m_currentState != nullptr);
132 return this->m_currentState->isCurrentStateEnd();
133 }
134
138 void execUpdate() { this->m_currentState->onUpdate(); }
139
143 void execRender() { this->m_currentState->onRender(); }
144
154 void sync();
155
159 void clean();
160
170 void changeState(const std::string &name, const Shared<State> &state);
171
182 inline void changeState(const Shared<State> &state) { this->changeState(state->getName(), state); }
189 void changeState(const std::string &name);
190
196 bool isCurrentStateDefined() const { return this->m_currentState != nullptr; }
197
204 void cacheState(const std::string &name, const Shared<State> &state);
205
211 inline void cacheState(const Shared<State> &state) { this->cacheState(state->getName(), state); }
212
218 void uncacheState(const std::string &name);
219
225 inline std::map<std::string, Shared<State>> getCachedStates() { return this->m_cacheStates; }
226
227 ~StateManager() = default;
228
229private:
231 inline static Shared<StateManager> s_pointer = nullptr;
232
234 Shared<State> m_currentState = nullptr;
236 Shared<State> m_queueState = nullptr;
238 std::map<std::string, Shared<State>> m_cacheStates{};
239
241 bool m_stateChanged = false;
242
243 StateManager() = default;
244};
This class manages the current State running in the application and also provides a simple cache syst...
Definition State.hpp:105
std::map< std::string, Shared< State > > getCachedStates()
Retrieves the current cache of the StateManager.
Definition State.hpp:225
void changeState(const Shared< State > &state)
This method queue the given State and tells the StateManager that the current state needs to be chang...
Definition State.hpp:182
void clean()
Cleans all the current state cache system.
Definition State.cpp:5
bool isCurrentStateDefined() const
Retrieves if the current state is defined or not.
Definition State.hpp:196
void cacheState(const std::string &name, const Shared< State > &state)
This method caches the State given with the given name.
Definition State.cpp:67
static Shared< StateManager > instance()
Retrieves the instance of the StateManager if it's not created. This function is thread safe using a ...
Definition State.hpp:117
void uncacheState(const std::string &name)
Removes the State with the given name if contained in the cache.
Definition State.cpp:75
bool shouldExit() const
Retrieves true if the current state should exit.
Definition State.hpp:130
void execRender()
Execute State::onRender() method of the current state.
Definition State.hpp:143
void changeState(const std::string &name, const Shared< State > &state)
Works as the other StateManager::changeState(const Shared<State>&) but this will use the given name w...
Definition State.cpp:55
void cacheState(const Shared< State > &state)
Caches the given State, his name will be used in the cached states.
Definition State.hpp:211
void execUpdate()
Execute State::onUpdate() method of the current state.
Definition State.hpp:138
void sync()
This method works like a barrier: after calling StateManager::changeState(), detach the current state...
Definition State.cpp:27
This class allow to create a state to be attached to the StateManager. A State works like a scene: de...
Definition State.hpp:21
State(const std::string &name="State")
Instances a state with the given name.
Definition State.hpp:85
bool m_attached
true if the state is attached
Definition State.hpp:93
virtual void onDetach()
This method is called while using sync() (read sync() documentation for more info)....
Definition State.hpp:42
virtual void onRender()
This methods contains all the operations executed at every event::loop::LOOP_RENDER if there are any.
Definition State.hpp:57
virtual bool isCurrentStateEnd()
Retrieves the current binary state of the current State
Definition State.hpp:64
std::string getName() const
Retrieves the State name.
Definition State.hpp:70
bool isAttached() const
Retrieves if the state is attached.
Definition State.hpp:76
virtual void onAttach()
This method is called after using StateManager::sync() (read StateMnaager::sync() documentation for m...
Definition State.hpp:30
const std::string m_name
state's name
Definition State.hpp:91
virtual void onUpdate()
This methods contains all the operations executed at every event::loop::LOOP_UPDATE if there are any.
Definition State.hpp:51