428244d17c0e54271edbd5a131c78e762677a194
[supertux.git] / src / gui / 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 <list>
21 #include <memory>
22 #include <SDL.h>
23
24 #include "gui/mousecursor.hpp"
25 #include "video/font.hpp"
26
27 bool confirm_dialog(Surface* background, std::string text);
28
29 /* Kinds of menu items */
30 enum MenuItemKind {
31   MN_ACTION,
32   MN_GOTO,
33   MN_TOGGLE,
34   MN_BACK,
35   MN_INACTIVE,
36   MN_TEXTFIELD,
37   MN_NUMFIELD,
38   MN_CONTROLFIELD,
39   MN_STRINGSELECT,
40   MN_LABEL,
41   MN_HL /* horizontal line */
42 };
43
44 class Menu;
45
46 class MenuItem
47 {
48 public:
49   MenuItem(MenuItemKind kind, int id = -1);
50
51   void set_help(const std::string& help_text);
52
53   void change_text (const std::string& text);
54   void change_input(const std::string& text);
55
56   static MenuItem* create(MenuItemKind kind, const std::string& text,
57                           int init_toggle, Menu* target_menu, int id, int key);
58
59   std::string get_input_with_symbol(bool active_item);   // returns the text with an input symbol
60
61 public:
62   MenuItemKind kind;
63   int id;   // item id
64   bool toggled;
65   std::string text;
66   std::string input;
67   std::string help;
68
69   std::vector<std::string> list; // list of values for a STRINGSELECT item
70   size_t selected; // currently selected item
71
72   Menu* target_menu;
73
74 private:
75   /// keyboard key or joystick button
76   bool input_flickering;
77
78 private:
79   MenuItem(const MenuItem&);
80   MenuItem& operator=(const MenuItem&);
81 };
82
83 class Menu
84 {
85   static Color default_color;
86   static Color active_color;
87   static Color inactive_color;
88   static Color label_color;
89   static Color field_color;
90 private:
91   static std::vector<Menu*> last_menus;
92
93   /** Pointers to all currently available menus, used to handle repositioning on window resize */
94   static std::list<Menu*>   all_menus;
95
96   static Menu* previous;
97   static Menu* current_;
98
99 public:
100   /** Set the current menu, if pmenu is NULL, hide the current menu */
101   static void set_current(Menu* pmenu);
102
103   static void push_current(Menu* pmenu);
104   static void pop_current();
105
106   static void recalc_pos();
107
108   /** Return the current active menu or NULL if none is active */
109   static Menu* current()
110   {
111     return current_;
112   }
113
114 private:
115   /* Action done on the menu */
116   enum MenuAction {
117     MENU_ACTION_NONE = -1,
118     MENU_ACTION_UP,
119     MENU_ACTION_DOWN,
120     MENU_ACTION_LEFT,
121     MENU_ACTION_RIGHT,
122     MENU_ACTION_HIT,
123     MENU_ACTION_INPUT,
124     MENU_ACTION_REMOVE,
125     MENU_ACTION_BACK
126   };
127
128 public:
129   Menu();
130   virtual ~Menu();
131
132   MenuItem* add_hl();
133   MenuItem* add_label(const std::string& text);
134   MenuItem* add_entry(int id, const std::string& text);
135   MenuItem* add_toggle(int id, const std::string& text, bool toggled = false);
136   MenuItem* add_inactive(int id, const std::string& text);
137   MenuItem* add_back(const std::string& text);
138   MenuItem* add_submenu(const std::string& text, Menu* submenu, int id = -1);
139   MenuItem* add_controlfield(int id, const std::string& text,
140                              const std::string& mapping = "");
141   MenuItem* add_string_select(int id, const std::string& text);
142
143   virtual void menu_action(MenuItem* item);
144
145   void update();
146
147   /** Remove all entries from the menu */
148   void clear();
149
150   /** Return the index of the menu item that was 'hit' (ie. the user
151       clicked on it) in the last event() call */
152   int check ();
153
154   MenuItem& get_item(int index)
155   {
156     return *(items[index]);
157   }
158
159   MenuItem& get_item_by_id(int id);
160   const MenuItem& get_item_by_id(int id) const;
161
162   int get_active_item_id();
163   void set_active_item(int id);
164
165   void draw(DrawingContext& context);
166   void set_pos(float x, float y, float rw = 0, float rh = 0);
167
168   void event(const SDL_Event& event);
169
170   bool is_toggled(int id) const;
171   void set_toggled(int id, bool toggled);
172
173   Menu* get_parent() const;
174
175 protected:
176   void additem(MenuItem* pmenu_item);
177   float get_width() const;
178   float get_height() const;
179
180 private:
181   void check_controlfield_change_event(const SDL_Event& event);
182   void draw_item(DrawingContext& context, int index);
183
184 private:
185   /** Number of the item that got 'hit' (ie. pressed) in the last
186       event()/update() call, -1 if none */
187   int hit_item;
188
189   // position of the menu (ie. center of the menu, not top/left)
190   float pos_x;
191   float pos_y;
192
193   /** input event for the menu (up, down, left, right, etc.) */
194   MenuAction menuaction;
195
196   /* input implementation variables */
197   int   delete_character;
198   char  mn_input_char;
199   float menu_repeat_time;
200
201   bool close;
202
203 public:
204   std::vector<MenuItem*> items;
205
206 private:
207   float effect_progress;
208   float effect_start_time;
209   int arrange_left;
210   int active_item;
211
212   std::auto_ptr<Surface> checkbox;
213   std::auto_ptr<Surface> checkbox_checked;
214   std::auto_ptr<Surface> back;
215   std::auto_ptr<Surface> arrow_left;
216   std::auto_ptr<Surface> arrow_right;
217 };
218
219 #endif
220
221 /* EOF */