b10f29918e526eccbc35e8bcab6da499d4c10ef6
[supertux.git] / src / gui / dialog.cpp
1 //  SuperTux
2 //  Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
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 #include "gui/dialog.hpp"
18
19 #include "control/controller.hpp"
20 #include "gui/menu_manager.hpp"
21 #include "gui/menu.hpp"
22 #include "gui/mousecursor.hpp"
23 #include "supertux/resources.hpp"
24 #include "supertux/colorscheme.hpp"
25 #include "video/drawing_context.hpp"
26 #include "video/renderer.hpp"
27 #include "video/video_system.hpp"
28
29 Dialog::Dialog() :
30   m_text(),
31   m_buttons(),
32   m_selected_button(),
33   m_text_size()
34 {
35 }
36
37 Dialog::~Dialog()
38 {
39 }
40
41 void
42 Dialog::set_text(const std::string& text)
43 {
44   m_text = text;
45
46   m_text_size = Sizef(Resources::normal_font->get_text_width(m_text),
47                       Resources::normal_font->get_text_height(m_text));
48
49 }
50
51 void
52 Dialog::clear_buttons()
53 {
54   m_buttons.clear();
55   m_selected_button = 0;
56 }
57
58 void
59 Dialog::add_button(const std::string& text, const std::function<void ()>& callback, bool focus)
60 {
61   m_buttons.push_back({text, callback});
62
63   if (focus)
64   {
65     m_selected_button = m_buttons.size() - 1;
66   }
67 }
68
69 int
70 Dialog::get_button_at(const Vector& mouse_pos) const
71 {
72   Rectf bg_rect(Vector(SCREEN_WIDTH/2 - m_text_size.width/2,
73                        SCREEN_HEIGHT/2 - m_text_size.height/2),
74                 Sizef(m_text_size.width,
75                       m_text_size.height + 44));
76
77   for(int i = 0; i < static_cast<int>(m_buttons.size()); ++i)
78   {
79     float segment_width = bg_rect.get_width() / m_buttons.size();
80     float button_width = segment_width;
81     float button_height = 24.0f;
82     Vector pos(bg_rect.p1.x + segment_width/2.0f + i * segment_width,
83                bg_rect.p2.y - 12);
84     Rectf button_rect(Vector(pos.x - button_width/2, pos.y - button_height/2),
85                       Vector(pos.x + button_width/2, pos.y + button_height/2));
86     if (button_rect.contains(mouse_pos))
87     {
88       return i;
89     }
90   }
91   return -1;
92 }
93
94 void
95 Dialog::event(const SDL_Event& ev)
96 {
97   switch(ev.type) {
98     case SDL_MOUSEBUTTONDOWN:
99     if(ev.button.button == SDL_BUTTON_LEFT)
100     {
101       Vector mouse_pos = VideoSystem::current()->get_renderer().to_logical(ev.motion.x, ev.motion.y);
102       int new_button = get_button_at(mouse_pos);
103       if (new_button != -1)
104       {
105         m_selected_button = new_button;
106         on_button_click(m_selected_button);
107
108         // warning: this will "delete this"
109         MenuManager::instance().set_dialog({});
110       }
111     }
112     break;
113
114     case SDL_MOUSEMOTION:
115     {
116       Vector mouse_pos = VideoSystem::current()->get_renderer().to_logical(ev.motion.x, ev.motion.y);
117       int new_button = get_button_at(mouse_pos);
118       if (new_button != -1)
119       {
120         m_selected_button = new_button;
121         if(MouseCursor::current())
122           MouseCursor::current()->set_state(MC_LINK);
123       }
124       else
125       {
126         if(MouseCursor::current())
127           MouseCursor::current()->set_state(MC_NORMAL);
128       }
129     }
130     break;
131
132     default:
133       break;
134   }
135 }
136
137 void
138 Dialog::process_input(const Controller& controller)
139 {
140   if (controller.pressed(Controller::LEFT))
141   {
142     m_selected_button -= 1;
143     m_selected_button = std::max(m_selected_button, 0);
144   }
145
146   if (controller.pressed(Controller::RIGHT))
147   {
148     m_selected_button += 1;
149     m_selected_button = std::min(m_selected_button, static_cast<int>(m_buttons.size()) - 1);
150   }
151
152   if (controller.pressed(Controller::ACTION) ||
153       controller.pressed(Controller::MENU_SELECT))
154   {
155     on_button_click(m_selected_button);
156
157     // warning: this will "delete this"
158     MenuManager::instance().set_dialog({});
159   }
160 }
161
162 void
163 Dialog::draw(DrawingContext& ctx)
164 {
165   Rectf bg_rect(Vector(SCREEN_WIDTH/2 - m_text_size.width/2,
166                        SCREEN_HEIGHT/2 - m_text_size.height/2),
167                 Sizef(m_text_size.width,
168                       m_text_size.height + 44));
169
170   // draw background rect
171   ctx.draw_filled_rect(bg_rect.grown(12.0f),
172                        Color(0.2f, 0.3f, 0.4f, 0.8f),
173                        16.0f,
174                        LAYER_GUI-10);
175
176   ctx.draw_filled_rect(bg_rect.grown(8.0f),
177                        Color(0.6f, 0.7f, 0.8f, 0.5f),
178                        16.0f,
179                        LAYER_GUI-10);
180
181   // draw text
182   ctx.draw_text(Resources::normal_font, m_text,
183                 Vector(bg_rect.p1.x + bg_rect.get_width()/2.0f,
184                        bg_rect.p1.y),
185                 ALIGN_CENTER, LAYER_GUI);
186
187   // draw HL line
188   ctx.draw_filled_rect(Vector(bg_rect.p1.x, bg_rect.p2.y - 35),
189                        Vector(bg_rect.get_width(), 4),
190                        Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
191   ctx.draw_filled_rect(Vector(bg_rect.p1.x, bg_rect.p2.y - 35),
192                        Vector(bg_rect.get_width(), 2),
193                        Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
194
195   // draw buttons
196   for(int i = 0; i < static_cast<int>(m_buttons.size()); ++i)
197   {
198     float segment_width = bg_rect.get_width() / m_buttons.size();
199     float button_width = segment_width;
200     float button_height = 24.0f;
201     Vector pos(bg_rect.p1.x + segment_width/2.0f + i * segment_width,
202                bg_rect.p2.y - 12);
203
204     if (i == m_selected_button)
205     {
206       float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
207       ctx.draw_filled_rect(Rectf(Vector(pos.x - button_width/2, pos.y - button_height/2),
208                                  Vector(pos.x + button_width/2, pos.y + button_height/2)).grown(2.0f),
209                            Color(1.0f, 1.0f, 1.0f, blink),
210                            14.0f,
211                            LAYER_GUI-10);
212       ctx.draw_filled_rect(Rectf(Vector(pos.x - button_width/2, pos.y - button_height/2),
213                                  Vector(pos.x + button_width/2, pos.y + button_height/2)),
214                            Color(1.0f, 1.0f, 1.0f, 0.5f),
215                            12.0f,
216                            LAYER_GUI-10);
217     }
218
219     ctx.draw_text(Resources::normal_font, m_buttons[i].text,
220                   Vector(pos.x, pos.y - int(Resources::normal_font->get_height()/2)),
221                   ALIGN_CENTER, LAYER_GUI,
222                   i == m_selected_button ? ColorScheme::Menu::active_color : ColorScheme::Menu::default_color);
223   }
224 }
225
226 void
227 Dialog::on_button_click(int button) const
228 {
229   if (m_buttons[button].callback)
230   {
231     m_buttons[button].callback();
232   }
233 }
234
235 /* EOF */