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