supertux
menu.hpp
1 // SuperTux
2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HEADER_SUPERTUX_GUI_MENU_HPP
18 #define HEADER_SUPERTUX_GUI_MENU_HPP
19 
20 #include <functional>
21 #include <memory>
22 #include <SDL.h>
23 
24 #include "gui/menu_action.hpp"
25 #include "math/vector.hpp"
26 #include "video/color.hpp"
27 #include "video/drawing_context.hpp"
28 
29 class ItemAction;
30 class ItemBack;
31 class ItemColor;
34 class ItemColorDisplay;
35 class ItemControlField;
36 class ItemFloatField;
37 class ItemGoTo;
38 class ItemHorizontalLine;
39 class ItemHorizontalMenu;
40 class ItemInactive;
41 class ItemIntField;
42 class ItemLabel;
43 class ItemPaths;
44 class ItemScript;
45 class ItemScriptLine;
46 class ItemList;
47 class ItemStringSelect;
48 class ItemTextField;
49 class ItemToggle;
50 class ItemStringArray;
51 class ItemImages;
52 class MenuItem;
53 class PathObject;
54 
55 class Menu
56 {
57 public:
58  Menu();
59  virtual ~Menu();
60 
61  virtual void menu_action(MenuItem& item) = 0;
62 
65  virtual bool on_back_action() { return true; }
66 
68  virtual void refresh() {}
69 
70  virtual void on_window_resize();
71 
72  void draw(DrawingContext& context);
73 
74  virtual void event(const SDL_Event& event);
75  virtual void process_action(const MenuAction& action);
76 
77  ItemHorizontalLine& add_hl();
78  ItemLabel& add_label(const std::string& text);
79  ItemAction& add_entry(int id, const std::string& text);
80  ItemAction& add_entry(const std::string& text, const std::function<void()>& callback);
81  ItemToggle& add_toggle(int id, const std::string& text, bool* toggled, bool center_text = false);
82  ItemToggle& add_toggle(int id, const std::string& text,
83  const std::function<bool()>& get_func,
84  const std::function<void(bool)>& set_func,
85  bool center_text = false);
86  ItemInactive& add_inactive(const std::string& text, bool default_color = false);
87  ItemBack& add_back(const std::string& text, int id = -1);
88  ItemGoTo& add_submenu(const std::string& text, int submenu, int id = -1);
89  ItemControlField& add_controlfield(int id, const std::string& text, const std::string& mapping = "");
90  ItemStringSelect& add_string_select(int id, const std::string& text, int* selected, const std::vector<std::string>& strings);
91  ItemStringSelect& add_string_select(int id, const std::string& text, int default_item, const std::vector<std::string>& strings);
92  ItemTextField& add_textfield(const std::string& text, std::string* input, int id = -1);
93  ItemScript& add_script(const std::string& text, std::string* script, int id = -1);
94  ItemScriptLine& add_script_line(std::string* input, int id = -1);
95  ItemIntField& add_intfield(const std::string& text, int* input, int id = -1, bool positive = false);
96  ItemFloatField& add_floatfield(const std::string& text, float* input, int id = -1, bool positive = false);
97  ItemAction& add_file(const std::string& text, std::string* input, const std::vector<std::string>& extensions,
98  const std::string& basedir, bool path_relative_to_basedir,
99  const std::function<void (MenuItem&)>& item_processor = {}, int id = -1);
100 
101  ItemColor& add_color(const std::string& text, Color* color, int id = -1);
102  ItemColorDisplay& add_color_display(Color* color, int id = -1);
103  ItemColorChannelRGBA& add_color_channel_rgba(float* input, Color channel, int id = -1,
104  bool is_linear = false);
105  ItemColorChannelOKLab& add_color_channel_oklab(Color* color, int channel);
106  ItemPaths& add_path_settings(const std::string& text, PathObject& target, const std::string& path_ref);
107  ItemStringArray& add_string_array(const std::string& text, std::vector<std::string>& items, int id = -1);
108  ItemImages& add_images(const std::string& image_path, int max_image_width = 0, int max_image_height = 0, int id = -1);
109  ItemImages& add_images(const std::vector<std::string>& image_paths, int max_image_width = 0, int max_image_height = 0, int id = -1);
110  ItemList& add_list(const std::string& text, const std::vector<std::string>& items, std::string* value_ptr, int id = -1);
111  ItemHorizontalMenu& add_horizontalmenu(int id, float height, float min_item_width = -1.f);
112 
114  void clear();
115 
116  MenuItem& get_item(int index) { return *(m_items[index]); }
117 
118  MenuItem& get_item_by_id(int id);
119  const MenuItem& get_item_by_id(int id) const;
120 
121  int get_active_item_id() const;
122  void set_active_item(int id);
123 
124  Vector get_center_pos() const { return m_pos; }
125  void set_center_pos(float x, float y);
126 
127  float get_width() const;
128  float get_height() const;
129 
131  virtual bool is_sensitive() const { return false; }
132 
133 protected:
134  MenuItem& add_item(std::unique_ptr<MenuItem> menu_item);
135  MenuItem& add_item(std::unique_ptr<MenuItem> menu_item, int pos_);
136  template<typename T, typename... Args>
137  T& add_item(Args&&... args)
138  {
139  auto item = std::make_unique<T>(std::forward<Args>(args)...);
140  auto item_ptr = item.get();
141  add_item(std::move(item));
142  return *item_ptr;
143  }
144  void delete_item(int pos_);
145 
147  void calculate_width();
149  void calculate_height();
150 
151 private:
152  void check_controlfield_change_event(const SDL_Event& event);
153  void draw_item(DrawingContext& context, int index, float y_pos);
154 
155 private:
157  Vector m_pos;
158 
159  /* input implementation variables */
160  int m_delete_character;
161  char m_mn_input_char;
162  float m_menu_width;
163  float m_menu_height;
164  float m_menu_help_height;
165 
166 public:
167  std::vector<std::unique_ptr<MenuItem> > m_items;
168 
169 private:
170  int m_arrange_left;
171 
172 protected:
173  int m_active_item;
174 
175 private:
176  Menu(const Menu&) = delete;
177  Menu& operator=(const Menu&) = delete;
178 };
179 
180 #endif
181 
182 /* EOF */
Definition: item_stringselect.hpp:24
Definition: item_script.hpp:22
void calculate_width()
Recalculates the width for this menu.
Definition: menu.cpp:452
Definition: item_floatfield.hpp:23
Definition: item_colorchannel.hpp:26
Definition: item_toggle.hpp:24
Definition: item_string_array.hpp:23
Definition: item_label.hpp:22
Definition: item_colordisplay.hpp:24
Definition: item_colorchannel.hpp:69
Definition: item_script_line.hpp:23
Definition: item_textfield.hpp:23
Definition: item_paths.hpp:24
Definition: menu_item.hpp:23
Definition: item_images.hpp:23
Definition: item_color.hpp:24
Definition: item_horizontalmenu.hpp:29
Definition: item_controlfield.hpp:22
virtual bool on_back_action()
Executed before the menu is exited.
Definition: menu.hpp:65
Definition: item_inactive.hpp:24
Definition: item_action.hpp:22
void calculate_height()
Recalculates the height for this menu.
Definition: menu.cpp:467
void clear()
Remove all entries from the menu.
Definition: menu.cpp:325
Definition: menu.hpp:55
Definition: item_back.hpp:22
virtual void refresh()
Perform actions to bring the menu up to date with configuration changes.
Definition: menu.hpp:68
virtual bool is_sensitive() const
returns true when the text is more important than action
Definition: menu.hpp:131
Definition: item_intfield.hpp:23
Definition: color.hpp:26
A base class for all objects that contain, or make use of a path.
Definition: path_object.hpp:36
Definition: item_list.hpp:25
Definition: item_goto.hpp:22
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
Definition: item_hl.hpp:22