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