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