supertux
menu_manager.hpp
1 // SuperTux
2 // Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
3 // 2023 Vankata453
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef HEADER_SUPERTUX_GUI_MENU_MANAGER_HPP
19 #define HEADER_SUPERTUX_GUI_MENU_MANAGER_HPP
20 
21 #include "util/currenton.hpp"
22 
23 #include <memory>
24 #include <vector>
25 
26 #include "control/controller.hpp"
27 #include "gui/menu_action.hpp"
28 #include "gui/menu_transition.hpp"
29 #include "math/rectf.hpp"
30 
31 class Controller;
32 class Dialog;
33 class Menu;
34 class Notification;
35 union SDL_Event;
36 
37 class MenuManager final : public Currenton<MenuManager>
38 {
39 private:
40  static const float s_menu_repeat_initial;
41  static const float s_menu_repeat_rate;
42 
43 public:
44  static MenuManager& instance();
45 
46 private:
47  template<typename T>
48  struct SettableItem {
49  std::unique_ptr<T> current = nullptr;
50  std::unique_ptr<T> next = nullptr;
51  bool has_next = false;
52  };
53 
54 private:
55  std::vector<std::unique_ptr<Menu> > m_menu_stack;
56  std::unique_ptr<MenuTransition> m_transition;
57 
58  float m_menu_repeat_time;
59 
60  SettableItem<Dialog> m_dialog;
61  SettableItem<Notification> m_notification;
62 
63 public:
64  MenuManager();
65  ~MenuManager() override;
66 
67  void event(const SDL_Event& event);
68  void process_input(const Controller& controller);
69  void refresh();
70 
71  void draw(DrawingContext& context);
72 
73  void set_dialog(std::unique_ptr<Dialog> dialog);
74  void set_notification(std::unique_ptr<Notification> notification);
75 
76  void set_menu(int id, bool skip_transition = false);
77  void set_menu(std::unique_ptr<Menu> menu, bool skip_transition = false);
78  void push_menu(int id, bool skip_transition = false);
79  void push_menu(std::unique_ptr<Menu> menu, bool skip_transition = false);
80  void pop_menu(bool skip_transition = false);
81  void clear_menu_stack(bool skip_transition = false);
82 
83  void on_window_resize();
84 
85  bool is_active() const;
86  bool has_dialog() const;
87 
88  Menu* current_menu() const;
89  Menu* previous_menu() const;
90 
91 private:
92  void check_input_action(Control control, MenuAction action,
93  const Controller& controller, MenuAction& result);
94 
95  template<typename T>
96  Rectf to_rect(T& menu)
97  {
98  return Rectf(menu.get_center_pos().x - menu.get_width() / 2,
99  menu.get_center_pos().y - menu.get_height() / 2,
100  menu.get_center_pos().x + menu.get_width() / 2,
101  menu.get_center_pos().y + menu.get_height() / 2);
102  }
103 
104  template<typename S, typename T>
105  Rectf to_transition_rect(S* from, T* to)
106  {
107  if (from)
108  return to_rect(*from);
109  else
110  return Rectf(to->get_center_pos(), Sizef(0, 0));
111  }
112 
113  template<typename S, typename T>
114  void transition(S* from, T* to)
115  {
116  if (!from && !to)
117  return;
118 
119  m_transition->start(to_transition_rect(from, to),
120  to_transition_rect(to, from));
121  }
122 
123 private:
124  MenuManager(const MenuManager&) = delete;
125  MenuManager& operator=(const MenuManager&) = delete;
126 };
127 
128 #endif
129 
130 /* EOF */
Definition: dialog.hpp:32
Definition: controller.hpp:57
Definition: notification.hpp:28
Definition: rectf.hpp:31
Definition: menu_manager.hpp:37
A &#39;Currenton&#39; allows access to the currently active instance of a class via the static current() func...
Definition: currenton.hpp:30
Definition: menu.hpp:55
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
Definition: sizef.hpp:26