Added a joystick control dialog.
[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 /* Kinds of menu items */
31 enum MenuItemKind {
32   MN_ACTION,
33   MN_GOTO,
34   MN_TOGGLE,
35   MN_BACK,
36   MN_DEACTIVE,
37   MN_TEXTFIELD,
38   MN_NUMFIELD,
39   MN_CONTROLFIELD,
40   MN_STRINGSELECT,
41   MN_LABEL,
42   MN_HL, /* horizontal line */
43 };
44
45 class Menu;
46
47 class MenuItem
48 {
49 public:
50   MenuItemKind kind;
51   int toggled;
52   char *text;
53   char *input;
54   int *int_p;   // used for setting keys (can be used for more stuff...)
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, int* int_p);
62 };
63
64 class Menu
65 {
66 private:  
67   static std::vector<Menu*> last_menus;
68   static Menu* current_;
69
70   static void push_current(Menu* pmenu);
71   static void pop_current();
72
73 public:
74   /** Set the current menu, if pmenu is NULL, hide the current menu */
75   static void set_current(Menu* pmenu);
76
77   /** Return the current active menu or NULL if none is active */
78   static Menu* current() { return current_; }
79
80 private:
81   /* Action done on the menu */
82   enum MenuAction {
83     MENU_ACTION_NONE = -1,
84     MENU_ACTION_UP,
85     MENU_ACTION_DOWN,
86     MENU_ACTION_LEFT,
87     MENU_ACTION_RIGHT,
88     MENU_ACTION_HIT,
89     MENU_ACTION_INPUT,
90     MENU_ACTION_REMOVE
91   };
92
93   /** Number of the item that got 'hit' (ie. pressed) in the last
94       event()/action() call, -1 if none */
95   int hit_item;
96
97   // position of the menu (ie. center of the menu, not top/left)
98   int pos_x;
99   int pos_y;
100   bool has_backitem;
101
102   /** input event for the menu (up, down, left, right, etc.) */
103   MenuAction menuaction;
104
105   /* input implementation variables */
106   int delete_character;
107   char mn_input_char;
108   
109 public:
110   Timer effect;
111   int arrange_left;
112   int active_item;
113
114   std::vector<MenuItem> item;
115
116   Menu();
117   ~Menu();
118
119   void additem(MenuItem* pmenu_item);
120   void additem(MenuItemKind kind, const std::string& text, int init_toggle, Menu* target_menu, int *int_p = NULL);
121   
122   void  action ();
123   
124   /** Remove all entries from the menu */
125   void clear();
126
127   /** Return the index of the menu item that was 'hit' (ie. the user
128       clicked on it) in the last event() call */
129   int  check  ();
130
131   MenuItem& get_item(int index) { return item[index]; }
132
133   void Menu::get_controlfield_key_into_input(MenuItem *item);
134
135   void draw   ();
136   void draw_item(int index, int menu_width, int menu_height);
137   void set_pos(int x, int y, float rw = 0, float rh = 0);
138
139   /** translate a SDL_Event into a menu_action */
140   void event(SDL_Event& event);
141
142   int get_width() const;
143   int get_height() const;
144 };
145
146 extern Surface* checkbox;
147 extern Surface* checkbox_checked;
148 extern Surface* back;
149 extern Surface* arrow_left;
150 extern Surface* arrow_right;
151
152 extern Menu* contrib_menu;
153 extern Menu* contrib_subset_menu;
154 extern Menu* main_menu;
155 extern Menu* game_menu;
156 extern Menu* worldmap_menu;
157 extern Menu* options_menu;
158 extern Menu* options_keys_menu;
159 extern Menu* options_joystick_menu;
160 extern Menu* highscore_menu;
161 extern Menu* load_game_menu;
162 extern Menu* save_game_menu;
163
164 #endif /*SUPERTUX_MENU_H*/
165
166 /* Local Variables: */
167 /* mode: c++ */
168 /* End: */