- fixed problem with last_menu not being able to handle menues deeper than two submenues
[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 <stack>
26 #include "texture.h"
27 #include "timer.h"
28 #include "type.h"
29 #include "mousecursor.h"
30
31 /* Kinds of menu items */
32 enum MenuItemKind {
33   MN_ACTION,
34   MN_GOTO,
35   MN_TOGGLE,
36   MN_BACK,
37   MN_DEACTIVE,
38   MN_TEXTFIELD,
39   MN_NUMFIELD,
40   MN_CONTROLFIELD,
41   MN_STRINGSELECT,
42   MN_LABEL,
43   MN_HL, /* horizontal line */
44 };
45
46 class Menu;
47
48 class MenuItem
49 {
50 public:
51   MenuItemKind kind;
52   int toggled;
53   char *text;
54   char *input;
55   string_list_type* list;
56   Menu* target_menu;
57
58   void change_text (const char *text);
59   void change_input(const char *text);
60
61   static MenuItem* create(MenuItemKind kind, const char *text, int init_toggle, Menu* target_menu);
62 };
63
64 class Menu
65 {
66 private:  
67   static std::stack<Menu*> last_menus;
68   static Menu* current_;
69 public:
70   /** Set the current menu, if pmenu is NULL, hide the current menu */
71   static void set_current(Menu* pmenu);
72
73   /** Return the current active menu or NULL if none is active */
74   static Menu* current() { return current_; }
75
76 private:
77   /* Action done on the menu */
78   enum MenuAction {
79     MENU_ACTION_NONE = -1,
80     MENU_ACTION_UP,
81     MENU_ACTION_DOWN,
82     MENU_ACTION_LEFT,
83     MENU_ACTION_RIGHT,
84     MENU_ACTION_HIT,
85     MENU_ACTION_INPUT,
86     MENU_ACTION_REMOVE
87   };
88
89   // position of the menu (ie. center of the menu, not top/left)
90   int pos_x;
91   int pos_y;
92   bool has_backitem;
93
94   /** input event for the menu (up, down, left, right, etc.) */
95   MenuAction menuaction;
96
97   /* input implementation variables */
98   int delete_character;
99   char mn_input_char;
100   
101   int width();
102   int height();
103
104 public:
105   Timer effect;
106   int arrange_left;
107   int active_item;
108
109   std::vector<MenuItem> item;
110
111   Menu();
112   ~Menu();
113
114   void additem(MenuItem* pmenu_item);
115   void additem(MenuItemKind kind, const std::string& text, int init_toggle, Menu* target_menu);
116   void action ();
117   
118   /** Remove all entries from the menu */
119   void clear();
120
121   /** Check, if the value of the active menu item has changed. FIXME:
122       Somebody should document the exact meaning of this function a
123       bit more */
124   int  check  ();
125
126   void draw   ();
127   void draw_item(int index, int menu_width, int menu_height);
128   void set_pos(int x, int y, float rw = 0, float rh = 0);
129
130   /** translate a SDL_Event into a menu_action */
131   void event(SDL_Event& event);
132 };
133
134 extern Surface* checkbox;
135 extern Surface* checkbox_checked;
136 extern Surface* back;
137 extern Surface* arrow_left;
138 extern Surface* arrow_right;
139
140 extern Menu* contrib_menu;
141 extern Menu* contrib_subset_menu;
142 extern Menu* main_menu;
143 extern Menu* game_menu;
144 extern Menu* worldmap_menu;
145 extern Menu* options_menu;
146 extern Menu* options_controls_menu;
147 extern Menu* highscore_menu;
148 extern Menu* load_game_menu;
149 extern Menu* save_game_menu;
150
151 #endif /*SUPERTUX_MENU_H*/
152
153 /* Local Variables: */
154 /* mode: c++ */
155 /* End: */