StateStack.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #ifndef FREESHOP_STATESTACK_HPP
  2. #define FREESHOP_STATESTACK_HPP
  3. #include "State.hpp"
  4. #include "StateIdentifiers.hpp"
  5. #include <cpp3ds/System/NonCopyable.hpp>
  6. #include <cpp3ds/System/Time.hpp>
  7. #include <vector>
  8. #include <utility>
  9. #include <functional>
  10. #include <map>
  11. namespace FreeShop {
  12. class StateStack : private cpp3ds::NonCopyable
  13. {
  14. public:
  15. enum Action {
  16. Push,
  17. Pop,
  18. Clear,
  19. ClearUnder,
  20. };
  21. public:
  22. explicit StateStack(State::Context context);
  23. template <typename T>
  24. void registerState(States::ID stateID);
  25. void update(float delta);
  26. void renderTopScreen(cpp3ds::Window& window);
  27. void renderBottomScreen(cpp3ds::Window& window);
  28. void processEvent(const cpp3ds::Event& event);
  29. void pushState(States::ID stateID, bool renderAlone = false, StateCallback callback = nullptr);
  30. void popState();
  31. void clearStates();
  32. void clearStatesUnder();
  33. bool isEmpty() const;
  34. private:
  35. State::Ptr createState(States::ID stateID, StateCallback callback);
  36. void applyPendingChanges();
  37. private:
  38. struct PendingChange
  39. {
  40. explicit PendingChange(Action action, States::ID stateID = States::None, bool renderAlone = false, StateCallback = nullptr);
  41. Action action;
  42. States::ID stateID;
  43. bool renderAlone;
  44. StateCallback callback;
  45. };
  46. struct StateStackItem
  47. {
  48. States::ID id;
  49. State::Ptr pointer;
  50. bool renderAlone;
  51. bool renderEnabled;
  52. };
  53. void updateRenderConfig();
  54. private:
  55. std::vector<StateStackItem> m_stack;
  56. std::vector<PendingChange> m_pendingList;
  57. State::Context m_context;
  58. std::map<States::ID, std::function<State::Ptr(StateCallback)>> m_factories;
  59. };
  60. template <typename T>
  61. void StateStack::registerState(States::ID stateID)
  62. {
  63. m_factories[stateID] = [this] (StateCallback callback)
  64. {
  65. return State::Ptr(new T(*this, m_context, callback));
  66. };
  67. }
  68. } // namespace FreeShop
  69. #endif // FREESHOP_STATESTACK_HPP