Added z order support and draw_part().
[supertux.git] / lib / gui / menu.h
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.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 <set>
25 #include <string>
26 #include <utility>
27
28 #include "SDL.h"
29
30 #include "../video/surface.h"
31 #include "../video/font.h"
32 #include "../special/timer.h"
33 #include "../special/base.h"
34 #include "../gui/mousecursor.h"
35
36 namespace SuperTux
37   {
38
39   /* Joystick menu delay */
40 #define JOYSTICK_MENU_DELAY 500
41
42   /* IDs for menus */
43
44   bool confirm_dialog(Surface* background, std::string text);
45
46   /* Kinds of menu items */
47   enum MenuItemKind {
48     MN_ACTION,
49     MN_GOTO,
50     MN_TOGGLE,
51     MN_BACK,
52     MN_DEACTIVE,
53     MN_TEXTFIELD,
54     MN_NUMFIELD,
55     MN_CONTROLFIELD_KB,
56     MN_CONTROLFIELD_JS,
57     MN_STRINGSELECT,
58     MN_LABEL,
59     MN_HL, /* horizontal line */
60   };
61
62   class Menu;
63
64   class MenuItem
65     {
66     public:
67       MenuItem() {};
68       MenuItem(MenuItemKind kind, int id = -1);
69       MenuItem(MenuItemKind kind, int id, const std::string& text);
70       MenuItemKind kind;
71       int id;   // item id      
72       int toggled;
73       std::string text;
74       std::string input;
75       int *int_p;   // used for setting keys (can be used for more stuff...)
76       std::pair<std::set<std::string>, std::set<std::string>::iterator> list;
77       Menu* target_menu;
78
79       void change_text (const std::string& text);
80       void change_input(const std::string& text);
81
82       static MenuItem* create(MenuItemKind kind, const std::string& text, int init_toggle, Menu* target_menu, int id, int* int_p);
83
84       std::string get_input_with_symbol(bool active_item);   // returns the text with an input symbol
85     private:
86       bool input_flickering;
87       Timer input_flickering_timer;
88     };
89
90   class Menu
91     {
92     private:
93       static std::vector<Menu*> last_menus;
94       static Menu* current_;
95
96       static void push_current(Menu* pmenu);
97       static void pop_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       /** Return the current active menu or NULL if none is active */
104       static Menu* current()
105       {
106         return current_;
107       }
108
109     private:
110       /* Action done on the menu */
111       enum MenuAction {
112         MENU_ACTION_NONE = -1,
113         MENU_ACTION_UP,
114         MENU_ACTION_DOWN,
115         MENU_ACTION_LEFT,
116         MENU_ACTION_RIGHT,
117         MENU_ACTION_HIT,
118         MENU_ACTION_INPUT,
119         MENU_ACTION_REMOVE
120       };
121
122       /** Number of the item that got 'hit' (ie. pressed) in the last
123           event()/action() call, -1 if none */
124       int hit_item;
125
126       // position of the menu (ie. center of the menu, not top/left)
127       int pos_x;
128       int pos_y;
129
130       /** input event for the menu (up, down, left, right, etc.) */
131       MenuAction menuaction;
132
133       /* input implementation variables */
134       int delete_character;
135       char mn_input_char;
136       Timer joystick_timer;
137
138     public:
139       static Font* default_font;
140       static Font* active_font;
141       static Font* deactive_font;
142       static Font* label_font;
143       static Font* field_font;
144     
145       Timer effect;
146       int arrange_left;
147       int active_item;
148
149       std::vector<MenuItem> item;
150
151       Menu();
152       ~Menu();
153
154       void additem(const MenuItem& menu_item);      
155       void additem(MenuItem* pmenu_item);
156       void additem(MenuItemKind kind, const std::string& text, int init_toggle, Menu* target_menu, int id = -1, int *int_p = NULL);
157
158       void  action ();
159
160       /** Remove all entries from the menu */
161       void clear();
162
163       /** Return the index of the menu item that was 'hit' (ie. the user
164           clicked on it) in the last event() call */
165       int  check  ();
166
167       MenuItem& get_item(int index)
168       {
169         return item[index];
170       }
171       MenuItem& get_item_by_id(int id);
172
173       int get_active_item_id();
174
175       bool isToggled(int id);
176
177       void Menu::get_controlfield_key_into_input(MenuItem *item);
178       void Menu::get_controlfield_js_into_input(MenuItem *item);
179
180       void draw(DrawingContext& context);
181       void draw_item(DrawingContext& context,
182                      int index, int menu_width, int menu_height);
183       void set_pos(int x, int y, float rw = 0, float rh = 0);
184
185       /** translate a SDL_Event into a menu_action */
186       void event(SDL_Event& event);
187
188       int get_width() const;
189       int get_height() const;
190
191       bool is_toggled(int id) const;
192     };
193
194   extern Surface* checkbox;
195   extern Surface* checkbox_checked;
196   extern Surface* back;
197   extern Surface* arrow_left;
198   extern Surface* arrow_right;
199
200 } //namespace SuperTux
201
202 #endif /*SUPERTUX_MENU_H*/
203
204 /* Local Variables: */
205 /* mode: c++ */
206 /* End: */