Fixed MN_STRINGSELECT menu item
[supertux.git] / src / gui / menu.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #ifndef SUPERTUX_MENU_H
21 #define SUPERTUX_MENU_H
22
23 #include <vector>
24 #include <memory>
25 #include <set>
26 #include <string>
27 #include <utility>
28 #include <assert.h>
29
30 #include <SDL.h>
31
32 #include "video/surface.hpp"
33 #include "video/font.hpp"
34 #include "mousecursor.hpp"
35
36 bool confirm_dialog(Surface* background, std::string text);
37
38 /* Kinds of menu items */
39 enum MenuItemKind {
40   MN_ACTION,
41   MN_GOTO,
42   MN_TOGGLE,
43   MN_BACK,
44   MN_DEACTIVE,
45   MN_TEXTFIELD,
46   MN_NUMFIELD,
47   MN_CONTROLFIELD,
48   MN_STRINGSELECT,
49   MN_LABEL,
50   MN_HL, /* horizontal line */
51 };
52
53 class Menu;
54 \f
55 class MenuItem
56 {
57 public:
58   MenuItem(MenuItemKind kind, int id = -1);
59   MenuItemKind kind;
60   int id;   // item id
61   bool toggled;
62   std::string text;
63   std::string input;
64   std::string help;
65
66   std::vector<std::string> list; // list of values for a STRINGSELECT item
67   size_t selected; // currently selected item
68
69   Menu* target_menu;
70
71   void set_help(const std::string& help_text);
72
73   void change_text (const std::string& text);
74   void change_input(const std::string& text);
75
76   static MenuItem* create(MenuItemKind kind, const std::string& text,
77                           int init_toggle, Menu* target_menu, int id, int key);
78
79   std::string get_input_with_symbol(bool active_item);   // returns the text with an input symbol
80
81 private:
82   /// copy-construction not allowed
83   MenuItem(const MenuItem& ) { assert(false); }
84   /// assignment not allowed
85   void operator= (const MenuItem& ) { assert(false); }
86
87   /// keyboard key or joystick button
88   bool input_flickering;
89 };
90 \f
91 class Menu
92 {
93 private:
94   static std::vector<Menu*> last_menus;
95   static Menu* previous;
96   static Menu* current_;
97
98   static void pop_current();
99
100 public:
101   /** Set the current menu, if pmenu is NULL, hide the current menu */
102   static void set_current(Menu* pmenu);
103
104   static void push_current(Menu* pmenu);
105
106   /** Return the current active menu or NULL if none is active */
107   static Menu* current()
108   {
109     return current_;
110   }
111
112 private:
113   /* Action done on the menu */
114   enum MenuAction {
115     MENU_ACTION_NONE = -1,
116     MENU_ACTION_UP,
117     MENU_ACTION_DOWN,
118     MENU_ACTION_LEFT,
119     MENU_ACTION_RIGHT,
120     MENU_ACTION_HIT,
121     MENU_ACTION_INPUT,
122     MENU_ACTION_REMOVE,
123     MENU_ACTION_BACK
124   };
125
126   /** Number of the item that got 'hit' (ie. pressed) in the last
127       event()/update() call, -1 if none */
128   int hit_item;
129
130   // position of the menu (ie. center of the menu, not top/left)
131   float pos_x;
132   float pos_y;
133
134   /** input event for the menu (up, down, left, right, etc.) */
135   MenuAction menuaction;
136
137   /* input implementation variables */
138   int   delete_character;
139   char  mn_input_char;
140   float menu_repeat_time;
141
142 public:
143   static Font* default_font;
144   static Font* active_font;
145   static Font* deactive_font;
146   static Font* label_font;
147   static Font* field_font;
148
149   std::vector<MenuItem*> items;
150
151   Menu();
152   virtual ~Menu();
153
154   MenuItem* add_hl();
155   MenuItem* add_label(const std::string& text);
156   MenuItem* add_entry(int id, const std::string& text);
157   MenuItem* add_toggle(int id, const std::string& text, bool toggled = false);
158   MenuItem* add_deactive(int id, const std::string& text);
159   MenuItem* add_back(const std::string& text);
160   MenuItem* add_submenu(const std::string& text, Menu* submenu, int id = -1);
161   MenuItem* add_controlfield(int id, const std::string& text,
162                              const std::string& mapping = "");
163   MenuItem* add_string_select(int id, const std::string& text);
164
165   virtual void menu_action(MenuItem* item);
166
167   void update();
168
169   /** Remove all entries from the menu */
170   void clear();
171
172   /** Return the index of the menu item that was 'hit' (ie. the user
173       clicked on it) in the last event() call */
174   int check ();
175
176   MenuItem& get_item(int index)
177   {
178     return *(items[index]);
179   }
180   MenuItem& get_item_by_id(int id);
181   const MenuItem& get_item_by_id(int id) const;
182
183   int get_active_item_id();
184   void set_active_item(int id);
185
186   void draw(DrawingContext& context);
187   void set_pos(float x, float y, float rw = 0, float rh = 0);
188
189   void event(const SDL_Event& event);
190
191   bool is_toggled(int id) const;
192
193   Menu* get_parent() const;
194
195 protected:
196   void additem(MenuItem* pmenu_item);
197   float get_width() const;
198   float get_height() const;
199
200 private:
201   void check_controlfield_change_event(const SDL_Event& event);
202   void draw_item(DrawingContext& context, int index);
203   float effect_progress;
204   float effect_start_time;
205   int arrange_left;
206   int active_item;
207
208   std::auto_ptr<Surface> checkbox;
209   std::auto_ptr<Surface> checkbox_checked;
210   std::auto_ptr<Surface> back;
211   std::auto_ptr<Surface> arrow_left;
212   std::auto_ptr<Surface> arrow_right;
213 };
214
215 #endif