* Revert breaking changes from last revision
[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 "math/vector.hpp"
25 #include "video/color.hpp"
26 #include "video/surface_ptr.hpp"
27
28 class DrawingContext;
29 class MenuItem;
30
31 bool confirm_dialog(Surface* background, std::string text);
32
33 class Menu
34 {
35   static Color default_color;
36   static Color active_color;
37   static Color inactive_color;
38   static Color label_color;
39   static Color field_color;
40 private:
41   /* Action done on the menu */
42   enum MenuAction {
43     MENU_ACTION_NONE = -1,
44     MENU_ACTION_UP,
45     MENU_ACTION_DOWN,
46     MENU_ACTION_LEFT,
47     MENU_ACTION_RIGHT,
48     MENU_ACTION_HIT,
49     MENU_ACTION_INPUT,
50     MENU_ACTION_REMOVE,
51     MENU_ACTION_BACK
52   };
53
54 public:
55   Menu();
56   virtual ~Menu();
57
58   MenuItem* add_hl();
59   MenuItem* add_label(const std::string& text);
60   MenuItem* add_entry(int id, const std::string& text);
61   MenuItem* add_toggle(int id, const std::string& text, bool toggled = false);
62   MenuItem* add_inactive(int id, const std::string& text);
63   MenuItem* add_back(const std::string& text);
64   MenuItem* add_submenu(const std::string& text, Menu* submenu, int id = -1);
65   MenuItem* add_controlfield(int id, const std::string& text,
66                              const std::string& mapping = "");
67   MenuItem* add_string_select(int id, const std::string& text);
68
69   virtual void menu_action(MenuItem* item);
70
71   void update();
72
73   /** Remove all entries from the menu */
74   void clear();
75
76   /** Return the index of the menu item that was 'hit' (ie. the user
77       clicked on it) in the last event() call */
78   int check ();
79
80   virtual void check_menu() =0;
81
82   MenuItem& get_item(int index)
83   {
84     return *(items[index]);
85   }
86
87   MenuItem& get_item_by_id(int id);
88   const MenuItem& get_item_by_id(int id) const;
89
90   int get_active_item_id();
91   void set_active_item(int id);
92
93   void draw(DrawingContext& context);
94   void set_pos(float x, float y, float rw = 0, float rh = 0);
95
96   void event(const SDL_Event& event);
97
98   bool is_toggled(int id) const;
99   void set_toggled(int id, bool toggled);
100
101   Menu* get_parent() const;
102
103 protected:
104   void additem(MenuItem* pmenu_item);
105   float get_width() const;
106   float get_height() const;
107
108 private:
109   void check_controlfield_change_event(const SDL_Event& event);
110   void draw_item(DrawingContext& context, int index);
111
112 private:
113   /** Number of the item that got 'hit' (ie. pressed) in the last
114       event()/update() call, -1 if none */
115   int hit_item;
116
117   // position of the menu (ie. center of the menu, not top/left)
118   Vector pos;
119
120   /** input event for the menu (up, down, left, right, etc.) */
121   MenuAction menuaction;
122
123   /* input implementation variables */
124   int   delete_character;
125   char  mn_input_char;
126   float menu_repeat_time;
127
128 public:
129   bool close;
130
131   std::vector<MenuItem*> items;
132
133 public:
134   float effect_progress;
135   float effect_start_time;
136
137 private:
138   int arrange_left;
139   int active_item;
140
141   SurfacePtr checkbox;
142   SurfacePtr checkbox_checked;
143   SurfacePtr back;
144   SurfacePtr arrow_left;
145   SurfacePtr arrow_right;
146 };
147
148 #endif
149
150 /* EOF */