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