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