a first implementation of doors to switch between sectors
[supertux.git] / src / 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 <SDL.h>
24 #include <vector>
25 #include "screen/texture.h"
26 #include "timer.h"
27 #include "type.h"
28 #include "mousecursor.h"
29
30 /* IDs for menus */
31
32 enum MainMenuIDs {
33   MNID_STARTGAME,
34   MNID_CONTRIB,
35   MNID_OPTIONMENU,
36   MNID_LEVELEDITOR,
37   MNID_CREDITS,
38   MNID_QUITMAINMENU
39   };
40
41 enum OptionsMenuIDs {
42   MNID_OPENGL,
43   MNID_FULLSCREEN,
44   MNID_SOUND,
45   MNID_MUSIC,
46   MNID_SHOWFPS
47   };
48
49 enum GameMenuIDs {
50   MNID_CONTINUE,
51   MNID_ABORTLEVEL
52   };
53
54 enum WorldMapMenuIDs {
55   MNID_RETURNWORLDMAP,
56   MNID_QUITWORLDMAP
57   };
58
59 enum LevelEditorMainMenuIDs {
60   MNID_RETURNLEVELEDITOR,
61   MNID_SUBSETSETTINGS,
62   MNID_QUITLEVELEDITOR
63   };
64   
65 enum LevelEditorSubsetSettingsIDs {
66   MNID_SUBSETTITLE,
67   MNID_SUBSETDESCRIPTION,
68   MNID_SUBSETSAVECHANGES
69   };
70   
71 enum LevelEditorSubsetNewIDs {
72  MNID_SUBSETNAME,
73  MNID_CREATESUBSET
74 };
75
76 enum LevelEditorSettingsMenuIDs {
77   MNID_NAME,
78   MNID_AUTHOR,
79   MNID_SONG,
80   MNID_BGIMG,
81   MNID_PARTICLE,
82   MNID_LENGTH,
83   MNID_HEIGHT,
84   MNID_TIME,
85   MNID_GRAVITY,
86   MNID_BGSPEED,
87   MNID_TopRed,
88   MNID_TopGreen,
89   MNID_TopBlue,
90   MNID_BottomRed,
91   MNID_BottomGreen,
92   MNID_BottomBlue,
93   MNID_APPLY
94   };
95
96 bool confirm_dialog(std::string text);
97
98 /* Kinds of menu items */
99 enum MenuItemKind {
100   MN_ACTION,
101   MN_GOTO,
102   MN_TOGGLE,
103   MN_BACK,
104   MN_DEACTIVE,
105   MN_TEXTFIELD,
106   MN_NUMFIELD,
107   MN_CONTROLFIELD_KB,
108   MN_CONTROLFIELD_JS,
109   MN_STRINGSELECT,
110   MN_LABEL,
111   MN_HL, /* horizontal line */
112 };
113
114 class Menu;
115
116 class MenuItem
117 {
118 public:
119   MenuItemKind kind;
120   int toggled;
121   char *text;
122   char *input;
123   int *int_p;   // used for setting keys (can be used for more stuff...)
124   int id;   // item id
125   string_list_type* list;
126   Menu* target_menu;
127
128   void change_text (const char *text);
129   void change_input(const char *text);
130
131   static MenuItem* create(MenuItemKind kind, const char *text, int init_toggle, Menu* target_menu, int id, int* int_p);
132
133   std::string get_input_with_symbol(bool active_item);   // returns the text with an input symbol
134 private:
135   bool input_flickering;
136   Timer input_flickering_timer;
137 };
138
139 class Menu
140 {
141 private:  
142   static std::vector<Menu*> last_menus;
143   static Menu* current_;
144
145   static void push_current(Menu* pmenu);
146   static void pop_current();
147
148 public:
149   /** Set the current menu, if pmenu is NULL, hide the current menu */
150   static void set_current(Menu* pmenu);
151
152   /** Return the current active menu or NULL if none is active */
153   static Menu* current() { return current_; }
154
155 private:
156   /* Action done on the menu */
157   enum MenuAction {
158     MENU_ACTION_NONE = -1,
159     MENU_ACTION_UP,
160     MENU_ACTION_DOWN,
161     MENU_ACTION_LEFT,
162     MENU_ACTION_RIGHT,
163     MENU_ACTION_HIT,
164     MENU_ACTION_INPUT,
165     MENU_ACTION_REMOVE
166   };
167
168   /** Number of the item that got 'hit' (ie. pressed) in the last
169       event()/action() call, -1 if none */
170   int hit_item;
171
172   // position of the menu (ie. center of the menu, not top/left)
173   int pos_x;
174   int pos_y;
175
176   /** input event for the menu (up, down, left, right, etc.) */
177   MenuAction menuaction;
178
179   /* input implementation variables */
180   int delete_character;
181   char mn_input_char;
182   Timer joystick_timer;
183   
184 public:
185   Timer effect;
186   int arrange_left;
187   int active_item;
188
189   std::vector<MenuItem> item;
190
191   Menu();
192   ~Menu();
193
194   void additem(MenuItem* pmenu_item);
195   void additem(MenuItemKind kind, const std::string& text, int init_toggle, Menu* target_menu, int id = -1, int *int_p = NULL);
196   
197   void  action ();
198   
199   /** Remove all entries from the menu */
200   void clear();
201
202   /** Return the index of the menu item that was 'hit' (ie. the user
203       clicked on it) in the last event() call */
204   int  check  ();
205
206   MenuItem& get_item(int index) { return item[index]; }
207   MenuItem& get_item_by_id(int id);
208
209   int get_active_item_id();
210
211   bool isToggled(int id);
212
213   void Menu::get_controlfield_key_into_input(MenuItem *item);
214   void Menu::get_controlfield_js_into_input(MenuItem *item);
215
216   void draw(DrawingContext& context);
217   void draw_item(DrawingContext& context,
218       int index, int menu_width, int menu_height);
219   void set_pos(int x, int y, float rw = 0, float rh = 0);
220
221   /** translate a SDL_Event into a menu_action */
222   void event(SDL_Event& event);
223
224   int get_width() const;
225   int get_height() const;
226
227   bool is_toggled(int id) const;
228 };
229
230 extern Surface* checkbox;
231 extern Surface* checkbox_checked;
232 extern Surface* back;
233 extern Surface* arrow_left;
234 extern Surface* arrow_right;
235
236 extern Menu* contrib_menu;
237 extern Menu* contrib_subset_menu;
238 extern Menu* main_menu;
239 extern Menu* game_menu;
240 extern Menu* worldmap_menu;
241 extern Menu* options_menu;
242 extern Menu* options_keys_menu;
243 extern Menu* options_joystick_menu;
244 extern Menu* highscore_menu;
245 extern Menu* load_game_menu;
246 extern Menu* save_game_menu;
247
248 #endif /*SUPERTUX_MENU_H*/
249
250 /* Local Variables: */
251 /* mode: c++ */
252 /* End: */