Some more messing around with aspect-ratio, window resize and stuff
[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 \f
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   std::string help;
65
66   std::vector<std::string> list; // list of values for a STRINGSELECT item
67   size_t selected; // currently selected item
68
69   Menu* target_menu;
70
71   void set_help(const std::string& help_text);
72
73   void change_text (const std::string& text);
74   void change_input(const std::string& text);
75
76   static MenuItem* create(MenuItemKind kind, const std::string& text,
77                           int init_toggle, Menu* target_menu, int id, int key);
78
79   std::string get_input_with_symbol(bool active_item);   // returns the text with an input symbol
80
81 private:
82   /// copy-construction not allowed
83   MenuItem(const MenuItem& ) { assert(false); }
84   /// assignment not allowed
85   void operator= (const MenuItem& ) { assert(false); }
86
87   /// keyboard key or joystick button
88   bool input_flickering;
89 };
90 \f
91 class Menu
92 {
93 private:
94   static std::vector<Menu*> last_menus;
95   static Menu* previous;
96   static Menu* current_;
97
98   static void pop_current();
99
100 public:
101   /** Set the current menu, if pmenu is NULL, hide the current menu */
102   static void set_current(Menu* pmenu);
103
104   static void push_current(Menu* pmenu);
105
106   static void recalc_pos();
107
108   /** Return the current active menu or NULL if none is active */
109   static Menu* current()
110   {
111     return current_;
112   }
113
114 private:
115   /* Action done on the menu */
116   enum MenuAction {
117     MENU_ACTION_NONE = -1,
118     MENU_ACTION_UP,
119     MENU_ACTION_DOWN,
120     MENU_ACTION_LEFT,
121     MENU_ACTION_RIGHT,
122     MENU_ACTION_HIT,
123     MENU_ACTION_INPUT,
124     MENU_ACTION_REMOVE,
125     MENU_ACTION_BACK
126   };
127
128   /** Number of the item that got 'hit' (ie. pressed) in the last
129       event()/update() call, -1 if none */
130   int hit_item;
131
132   // position of the menu (ie. center of the menu, not top/left)
133   float pos_x;
134   float pos_y;
135
136   /** input event for the menu (up, down, left, right, etc.) */
137   MenuAction menuaction;
138
139   /* input implementation variables */
140   int   delete_character;
141   char  mn_input_char;
142   float menu_repeat_time;
143
144 public:
145   static Font* default_font;
146   static Font* active_font;
147   static Font* deactive_font;
148   static Font* label_font;
149   static Font* field_font;
150
151   std::vector<MenuItem*> items;
152
153   Menu();
154   virtual ~Menu();
155
156   MenuItem* add_hl();
157   MenuItem* add_label(const std::string& text);
158   MenuItem* add_entry(int id, const std::string& text);
159   MenuItem* add_toggle(int id, const std::string& text, bool toggled = false);
160   MenuItem* add_deactive(int id, const std::string& text);
161   MenuItem* add_back(const std::string& text);
162   MenuItem* add_submenu(const std::string& text, Menu* submenu, int id = -1);
163   MenuItem* add_controlfield(int id, const std::string& text,
164                              const std::string& mapping = "");
165   MenuItem* add_string_select(int id, const std::string& text);
166
167   virtual void menu_action(MenuItem* item);
168
169   void update();
170
171   /** Remove all entries from the menu */
172   void clear();
173
174   /** Return the index of the menu item that was 'hit' (ie. the user
175       clicked on it) in the last event() call */
176   int check ();
177
178   MenuItem& get_item(int index)
179   {
180     return *(items[index]);
181   }
182   MenuItem& get_item_by_id(int id);
183   const MenuItem& get_item_by_id(int id) const;
184
185   int get_active_item_id();
186   void set_active_item(int id);
187
188   void draw(DrawingContext& context);
189   void set_pos(float x, float y, float rw = 0, float rh = 0);
190
191   void event(const SDL_Event& event);
192
193   bool is_toggled(int id) const;
194   void set_toggled(int id, bool toggled);
195
196   Menu* get_parent() const;
197
198 protected:
199   void additem(MenuItem* pmenu_item);
200   float get_width() const;
201   float get_height() const;
202
203 private:
204   void check_controlfield_change_event(const SDL_Event& event);
205   void draw_item(DrawingContext& context, int index);
206   float effect_progress;
207   float effect_start_time;
208   int arrange_left;
209   int active_item;
210
211   std::auto_ptr<Surface> checkbox;
212   std::auto_ptr<Surface> checkbox_checked;
213   std::auto_ptr<Surface> back;
214   std::auto_ptr<Surface> arrow_left;
215   std::auto_ptr<Surface> arrow_right;
216 };
217
218 #endif