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