-Started to move stuff from library back to main game
[supertux.git] / src / gui / 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 #ifndef SUPERTUX_MENU_H
20 #define SUPERTUX_MENU_H
21
22 #include <vector>
23 #include <set>
24 #include <string>
25 #include <utility>
26
27 #include "SDL.h"
28
29 #include "video/surface.h"
30 #include "video/font.h"
31 #include "special/timer.h"
32 #include "mousecursor.h"
33
34 bool confirm_dialog(Surface* background, std::string text);
35
36 /* Kinds of menu items */
37 enum MenuItemKind {
38   MN_ACTION,
39   MN_GOTO,
40   MN_TOGGLE,
41   MN_BACK,
42   MN_DEACTIVE,
43   MN_TEXTFIELD,
44   MN_NUMFIELD,
45   MN_CONTROLFIELD,
46   MN_STRINGSELECT,
47   MN_LABEL,
48   MN_HL, /* horizontal line */
49 };
50
51 class Menu;
52
53 class MenuItem
54 {
55 public:
56   MenuItem(MenuItemKind kind, int id = -1);
57   MenuItemKind kind;
58   int id;   // item id      
59   bool toggled;
60   std::string text;
61   std::string input;
62   
63   std::vector<std::string> list; // list of values for a STRINGSELECT item
64   size_t selected; // currently selected item
65   
66   Menu* target_menu;
67   
68   void change_text (const std::string& text);
69   void change_input(const std::string& text);
70   
71   static MenuItem* create(MenuItemKind kind, const std::string& text,
72                           int init_toggle, Menu* target_menu, int id, int key);
73   
74   std::string get_input_with_symbol(bool active_item);   // returns the text with an input symbol
75
76 private:
77   /// copy-construction not allowed
78   MenuItem(const MenuItem& other) { assert(false); }
79   /// assignment not allowed
80   void operator= (const MenuItem& other) { assert(false); }
81
82   /// keyboard key or joystick button
83   bool input_flickering;
84   Timer input_flickering_timer;
85 };
86
87 class Menu
88 {
89 private:
90   static std::vector<Menu*> last_menus;
91   static Menu* current_;
92   
93   static void push_current(Menu* pmenu);
94   static void pop_current();
95   
96 public:
97   /** Set the current menu, if pmenu is NULL, hide the current menu */
98   static void set_current(Menu* pmenu);
99   
100   /** Return the current active menu or NULL if none is active */
101   static Menu* current()
102   {
103     return current_;
104   }
105   
106 private:
107   /* Action done on the menu */
108   enum MenuAction {
109     MENU_ACTION_NONE = -1,
110     MENU_ACTION_UP,
111     MENU_ACTION_DOWN,
112     MENU_ACTION_LEFT,
113     MENU_ACTION_RIGHT,
114     MENU_ACTION_HIT,
115     MENU_ACTION_INPUT,
116     MENU_ACTION_REMOVE,
117     MENU_ACTION_BACK
118   };
119   
120   /** Number of the item that got 'hit' (ie. pressed) in the last
121       event()/action() call, -1 if none */
122   int hit_item;
123   
124   // position of the menu (ie. center of the menu, not top/left)
125   int pos_x;
126   int pos_y;
127   
128   /** input event for the menu (up, down, left, right, etc.) */
129   MenuAction menuaction;
130   
131   /* input implementation variables */
132   int delete_character;
133   char mn_input_char;
134   Timer repeat_timer;
135
136 public:
137   static Font* default_font;
138   static Font* active_font;
139   static Font* deactive_font;
140   static Font* label_font;
141   static Font* field_font;
142
143   std::vector<MenuItem*> items;
144   
145   Menu();
146   ~Menu();
147   
148   void add_hl();
149   void add_label(const std::string& text);
150   void add_entry(int id, const std::string& text);
151   void add_toggle(int id, const std::string& text, bool toggled = false);
152   void add_deactive(int id, const std::string& text);
153   void add_back(const std::string& text);
154   void add_submenu(const std::string& text, Menu* submenu, int id = -1);
155   void add_controlfield(int id, const std::string& text,
156                         const std::string& mapping = "");
157
158   virtual void menu_action(MenuItem* item);
159   
160   void action();
161   
162   /** Remove all entries from the menu */
163   void clear();
164   
165   /** Return the index of the menu item that was 'hit' (ie. the user
166       clicked on it) in the last event() call */
167   int check ();
168   
169   MenuItem& get_item(int index)
170   {
171     return *(items[index]);
172   }
173   MenuItem& get_item_by_id(int id);
174   const MenuItem& get_item_by_id(int id) const;
175   
176   int get_active_item_id();
177   void set_active_item(int id);
178   
179   void draw(DrawingContext& context);  
180   void set_pos(int x, int y, float rw = 0, float rh = 0);
181   
182   void event(const SDL_Event& event);
183
184   bool is_toggled(int id) const;
185
186 protected:
187   void additem(MenuItem* pmenu_item);  
188   int get_width() const;
189   int get_height() const;
190
191 private:
192   void check_controlfield_change_event(const SDL_Event& event);  
193   void draw_item(DrawingContext& context, int index);
194   Timer effect;
195   int arrange_left;
196   int active_item;
197 };
198
199 extern Surface* checkbox;
200 extern Surface* checkbox_checked;
201 extern Surface* back;
202 extern Surface* arrow_left;
203 extern Surface* arrow_right;
204
205 #endif