5c97fa1fbb18710451eb5ff1de2871c3723e7024
[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 "video/color.hpp"
25 #include "video/surface_ptr.hpp"
26
27 class DrawingContext;
28 class MenuItem;
29
30 bool confirm_dialog(Surface* background, std::string text);
31
32 class Menu
33 {
34   static Color default_color;
35   static Color active_color;
36   static Color inactive_color;
37   static Color label_color;
38   static Color field_color;
39 private:
40   /* Action done on the menu */
41   enum MenuAction {
42     MENU_ACTION_NONE = -1,
43     MENU_ACTION_UP,
44     MENU_ACTION_DOWN,
45     MENU_ACTION_LEFT,
46     MENU_ACTION_RIGHT,
47     MENU_ACTION_HIT,
48     MENU_ACTION_INPUT,
49     MENU_ACTION_REMOVE,
50     MENU_ACTION_BACK
51   };
52
53 public:
54   Menu();
55   virtual ~Menu();
56
57   MenuItem* add_hl();
58   MenuItem* add_label(const std::string& text);
59   MenuItem* add_entry(int id, const std::string& text);
60   MenuItem* add_toggle(int id, const std::string& text, bool toggled = false);
61   MenuItem* add_inactive(int id, const std::string& text);
62   MenuItem* add_back(const std::string& text);
63   MenuItem* add_submenu(const std::string& text, Menu* submenu, int id = -1);
64   MenuItem* add_controlfield(int id, const std::string& text,
65                              const std::string& mapping = "");
66   MenuItem* add_string_select(int id, const std::string& text);
67
68   virtual void menu_action(MenuItem* item);
69
70   void update();
71
72   /** Remove all entries from the menu */
73   void clear();
74
75   /** Return the index of the menu item that was 'hit' (ie. the user
76       clicked on it) in the last event() call */
77   int check ();
78
79   virtual void check_menu() =0;
80
81   MenuItem& get_item(int index)
82   {
83     return *(items[index]);
84   }
85
86   MenuItem& get_item_by_id(int id);
87   const MenuItem& get_item_by_id(int id) const;
88
89   int get_active_item_id();
90   void set_active_item(int id);
91
92   void draw(DrawingContext& context);
93   void set_pos(float x, float y, float rw = 0, float rh = 0);
94
95   void event(const SDL_Event& event);
96
97   bool is_toggled(int id) const;
98   void set_toggled(int id, bool toggled);
99
100   Menu* get_parent() const;
101
102 protected:
103   void additem(MenuItem* pmenu_item);
104   float get_width() const;
105   float get_height() const;
106
107 private:
108   void check_controlfield_change_event(const SDL_Event& event);
109   void draw_item(DrawingContext& context, int index);
110
111 private:
112   /** Number of the item that got 'hit' (ie. pressed) in the last
113       event()/update() call, -1 if none */
114   int hit_item;
115
116   // position of the menu (ie. center of the menu, not top/left)
117   float pos_x;
118   float pos_y;
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 */