60d6205cc132f5890e76367974cfcc83ad1111f9
[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 "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_SAVEGAME,
57   MNID_QUITWORLDMAP
58   };
59
60 enum LevelEditorMainMenuIDs {
61   MNID_RETURNLEVELEDITOR,
62   MNID_SUBSETSETTINGS,
63   MNID_QUITLEVELEDITOR
64   };
65
66 enum LevelEditorSettingsMenuIDs {
67   MNID_APPLY
68   };
69
70 bool confirm_dialog(char *text);
71
72 /* Kinds of menu items */
73 enum MenuItemKind {
74   MN_ACTION,
75   MN_GOTO,
76   MN_TOGGLE,
77   MN_BACK,
78   MN_DEACTIVE,
79   MN_TEXTFIELD,
80   MN_NUMFIELD,
81   MN_CONTROLFIELD,
82   MN_STRINGSELECT,
83   MN_LABEL,
84   MN_HL, /* horizontal line */
85 };
86
87 class Menu;
88
89 class MenuItem
90 {
91 public:
92   MenuItemKind kind;
93   int toggled;
94   char *text;
95   char *input;
96   int *int_p;   // used for setting keys (can be used for more stuff...)
97   int id;   // item id
98   string_list_type* list;
99   Menu* target_menu;
100
101   void change_text (const char *text);
102   void change_input(const char *text);
103
104   static MenuItem* create(MenuItemKind kind, const char *text, int init_toggle, Menu* target_menu, int id, int* int_p);
105 };
106
107 class Menu
108 {
109 private:  
110   static std::vector<Menu*> last_menus;
111   static Menu* current_;
112
113   static void push_current(Menu* pmenu);
114   static void pop_current();
115
116 public:
117   /** Set the current menu, if pmenu is NULL, hide the current menu */
118   static void set_current(Menu* pmenu);
119
120   /** Return the current active menu or NULL if none is active */
121   static Menu* current() { return current_; }
122
123 private:
124   /* Action done on the menu */
125   enum MenuAction {
126     MENU_ACTION_NONE = -1,
127     MENU_ACTION_UP,
128     MENU_ACTION_DOWN,
129     MENU_ACTION_LEFT,
130     MENU_ACTION_RIGHT,
131     MENU_ACTION_HIT,
132     MENU_ACTION_INPUT,
133     MENU_ACTION_REMOVE
134   };
135
136   /** Number of the item that got 'hit' (ie. pressed) in the last
137       event()/action() call, -1 if none */
138   int hit_item;
139
140   // position of the menu (ie. center of the menu, not top/left)
141   int pos_x;
142   int pos_y;
143
144   /** input event for the menu (up, down, left, right, etc.) */
145   MenuAction menuaction;
146
147   /* input implementation variables */
148   int delete_character;
149   char mn_input_char;
150   
151 public:
152   Timer effect;
153   int arrange_left;
154   int active_item;
155
156   std::vector<MenuItem> item;
157
158   Menu();
159   ~Menu();
160
161   void additem(MenuItem* pmenu_item);
162   void additem(MenuItemKind kind, const std::string& text, int init_toggle, Menu* target_menu, int id = -1, int *int_p = NULL);
163   
164   void  action ();
165   
166   /** Remove all entries from the menu */
167   void clear();
168
169   /** Return the index of the menu item that was 'hit' (ie. the user
170       clicked on it) in the last event() call */
171   int  check  ();
172
173   MenuItem& get_item(int index) { return item[index]; }
174   MenuItem& get_item_by_id(int id);
175
176   bool isToggled(int id);
177
178   void Menu::get_controlfield_key_into_input(MenuItem *item);
179
180   void draw   ();
181   void draw_item(int index, int menu_width, int menu_height);
182   void set_pos(int x, int y, float rw = 0, float rh = 0);
183
184   /** translate a SDL_Event into a menu_action */
185   void event(SDL_Event& event);
186
187   int get_width() const;
188   int get_height() const;
189
190   bool is_toggled(int id) const;
191 };
192
193 extern Surface* checkbox;
194 extern Surface* checkbox_checked;
195 extern Surface* back;
196 extern Surface* arrow_left;
197 extern Surface* arrow_right;
198
199 extern Menu* contrib_menu;
200 extern Menu* contrib_subset_menu;
201 extern Menu* main_menu;
202 extern Menu* game_menu;
203 extern Menu* worldmap_menu;
204 extern Menu* options_menu;
205 extern Menu* options_keys_menu;
206 extern Menu* options_joystick_menu;
207 extern Menu* highscore_menu;
208 extern Menu* load_game_menu;
209 extern Menu* save_game_menu;
210
211 #endif /*SUPERTUX_MENU_H*/
212
213 /* Local Variables: */
214 /* mode: c++ */
215 /* End: */