Split gui/menu.?pp
[supertux.git] / src / gui / menu.hpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #ifndef HEADER_SUPERTUX_GUI_MENU_HPP
18 #define HEADER_SUPERTUX_GUI_MENU_HPP
19
20 #include <list>
21 #include <memory>
22 #include <SDL.h>
23
24 #include "gui/mousecursor.hpp"
25 #include "video/font.hpp"
26
27 bool confirm_dialog(Surface* background, std::string text);
28
29 class MenuItem;
30
31 class Menu
32 {
33   static Color default_color;
34   static Color active_color;
35   static Color inactive_color;
36   static Color label_color;
37   static Color field_color;
38 private:
39   static std::vector<Menu*> last_menus;
40
41   /** Pointers to all currently available menus, used to handle repositioning on window resize */
42   static std::list<Menu*>   all_menus;
43
44   static Menu* previous;
45   static Menu* current_;
46
47 public:
48   /** Set the current menu, if pmenu is NULL, hide the current menu */
49   static void set_current(Menu* pmenu);
50
51   static void push_current(Menu* pmenu);
52   static void pop_current();
53
54   static void recalc_pos();
55
56   /** Return the current active menu or NULL if none is active */
57   static Menu* current()
58   {
59     return current_;
60   }
61
62 private:
63   /* Action done on the menu */
64   enum MenuAction {
65     MENU_ACTION_NONE = -1,
66     MENU_ACTION_UP,
67     MENU_ACTION_DOWN,
68     MENU_ACTION_LEFT,
69     MENU_ACTION_RIGHT,
70     MENU_ACTION_HIT,
71     MENU_ACTION_INPUT,
72     MENU_ACTION_REMOVE,
73     MENU_ACTION_BACK
74   };
75
76 public:
77   Menu();
78   virtual ~Menu();
79
80   MenuItem* add_hl();
81   MenuItem* add_label(const std::string& text);
82   MenuItem* add_entry(int id, const std::string& text);
83   MenuItem* add_toggle(int id, const std::string& text, bool toggled = false);
84   MenuItem* add_inactive(int id, const std::string& text);
85   MenuItem* add_back(const std::string& text);
86   MenuItem* add_submenu(const std::string& text, Menu* submenu, int id = -1);
87   MenuItem* add_controlfield(int id, const std::string& text,
88                              const std::string& mapping = "");
89   MenuItem* add_string_select(int id, const std::string& text);
90
91   virtual void menu_action(MenuItem* item);
92
93   void update();
94
95   /** Remove all entries from the menu */
96   void clear();
97
98   /** Return the index of the menu item that was 'hit' (ie. the user
99       clicked on it) in the last event() call */
100   int check ();
101
102   MenuItem& get_item(int index)
103   {
104     return *(items[index]);
105   }
106
107   MenuItem& get_item_by_id(int id);
108   const MenuItem& get_item_by_id(int id) const;
109
110   int get_active_item_id();
111   void set_active_item(int id);
112
113   void draw(DrawingContext& context);
114   void set_pos(float x, float y, float rw = 0, float rh = 0);
115
116   void event(const SDL_Event& event);
117
118   bool is_toggled(int id) const;
119   void set_toggled(int id, bool toggled);
120
121   Menu* get_parent() const;
122
123 protected:
124   void additem(MenuItem* pmenu_item);
125   float get_width() const;
126   float get_height() const;
127
128 private:
129   void check_controlfield_change_event(const SDL_Event& event);
130   void draw_item(DrawingContext& context, int index);
131
132 private:
133   /** Number of the item that got 'hit' (ie. pressed) in the last
134       event()/update() call, -1 if none */
135   int hit_item;
136
137   // position of the menu (ie. center of the menu, not top/left)
138   float pos_x;
139   float pos_y;
140
141   /** input event for the menu (up, down, left, right, etc.) */
142   MenuAction menuaction;
143
144   /* input implementation variables */
145   int   delete_character;
146   char  mn_input_char;
147   float menu_repeat_time;
148
149   bool close;
150
151 public:
152   std::vector<MenuItem*> items;
153
154 private:
155   float effect_progress;
156   float effect_start_time;
157   int arrange_left;
158   int active_item;
159
160   std::auto_ptr<Surface> checkbox;
161   std::auto_ptr<Surface> checkbox_checked;
162   std::auto_ptr<Surface> back;
163   std::auto_ptr<Surface> arrow_left;
164   std::auto_ptr<Surface> arrow_right;
165 };
166
167 #endif
168
169 /* EOF */