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