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