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 <algorithm>
20
21 #include "control/controller.hpp"
22 #include "gui/mousecursor.hpp"
23 #include "math/util.hpp"
24 #include "supertux/colorscheme.hpp"
25 #include "supertux/globals.hpp"
26 #include "supertux/resources.hpp"
27 #include "video/drawing_context.hpp"
28 #include "video/renderer.hpp"
29 #include "video/video_system.hpp"
30 #include "video/viewport.hpp"
31
Dialog(bool passive,bool auto_clear_dialogs)32 Dialog::Dialog(bool passive, bool auto_clear_dialogs) :
33 m_text(),
34 m_buttons(),
35 m_selected_button(),
36 m_cancel_button(-1),
37 m_passive(passive),
38 m_clear_diags(auto_clear_dialogs),
39 m_text_size()
40 {
41 }
42
~Dialog()43 Dialog::~Dialog()
44 {
45 }
46
47 void
set_text(const std::string & text)48 Dialog::set_text(const std::string& text)
49 {
50 m_text = text;
51
52 m_text_size = Sizef(Resources::normal_font->get_text_width(m_text),
53 Resources::normal_font->get_text_height(m_text));
54
55 }
56
57 void
clear_buttons()58 Dialog::clear_buttons()
59 {
60 m_buttons.clear();
61 m_selected_button = 0;
62 m_cancel_button = -1;
63 }
64
65 void
add_default_button(const std::string & text,const std::function<void ()> & callback)66 Dialog::add_default_button(const std::string& text, const std::function<void ()>& callback)
67 {
68 add_button(text, callback);
69 m_selected_button = static_cast<int>(m_buttons.size()) - 1;
70 }
71
72 void
add_cancel_button(const std::string & text,const std::function<void ()> & callback)73 Dialog::add_cancel_button(const std::string& text, const std::function<void ()>& callback)
74 {
75 add_button(text, callback);
76 m_cancel_button = static_cast<int>(m_buttons.size() - 1);
77 }
78
79 void
add_button(const std::string & text,const std::function<void ()> & callback)80 Dialog::add_button(const std::string& text, const std::function<void ()>& callback)
81 {
82 m_buttons.push_back({text, callback});
83 }
84
85 int
get_button_at(const Vector & mouse_pos) const86 Dialog::get_button_at(const Vector& mouse_pos) const
87 {
88 Rectf bg_rect(Vector(static_cast<float>(SCREEN_WIDTH) / 2.0f - m_text_size.width / 2.0f,
89 static_cast<float>(SCREEN_HEIGHT) / 2.0f - m_text_size.height / 2.0f),
90 Sizef(m_text_size.width,
91 m_text_size.height + 44));
92
93 for (int i = 0; i < static_cast<int>(m_buttons.size()); ++i)
94 {
95 float segment_width = bg_rect.get_width() / static_cast<float>(m_buttons.size());
96 float button_width = segment_width;
97 float button_height = 24.0f;
98 Vector pos(bg_rect.get_left() + segment_width/2.0f + static_cast<float>(i) * segment_width,
99 bg_rect.get_bottom() - 12);
100 Rectf button_rect(Vector(pos.x - button_width/2, pos.y - button_height/2),
101 Vector(pos.x + button_width/2, pos.y + button_height/2));
102 if (button_rect.contains(mouse_pos))
103 {
104 return i;
105 }
106 }
107 return -1;
108 }
109
110 void
event(const SDL_Event & ev)111 Dialog::event(const SDL_Event& ev)
112 {
113 if (m_passive) // Passive dialogs don't accept events
114 return;
115
116 switch (ev.type) {
117 case SDL_MOUSEBUTTONDOWN:
118 if (ev.button.button == SDL_BUTTON_LEFT)
119 {
120 Vector mouse_pos = VideoSystem::current()->get_viewport().to_logical(ev.motion.x, ev.motion.y);
121 int new_button = get_button_at(mouse_pos);
122 if (new_button != -1)
123 {
124 m_selected_button = new_button;
125 on_button_click(m_selected_button);
126 }
127 }
128 break;
129
130 case SDL_MOUSEMOTION:
131 {
132 Vector mouse_pos = VideoSystem::current()->get_viewport().to_logical(ev.motion.x, ev.motion.y);
133 int new_button = get_button_at(mouse_pos);
134 if (new_button != -1)
135 {
136 m_selected_button = new_button;
137 if (MouseCursor::current())
138 MouseCursor::current()->set_state(MouseCursorState::LINK);
139 }
140 else
141 {
142 if (MouseCursor::current())
143 MouseCursor::current()->set_state(MouseCursorState::NORMAL);
144 }
145 }
146 break;
147
148 default:
149 break;
150 }
151 }
152
153 void
process_input(const Controller & controller)154 Dialog::process_input(const Controller& controller)
155 {
156 if (m_passive) // Passive dialogs don't accept events
157 return;
158
159 if (controller.pressed(Control::LEFT))
160 {
161 m_selected_button -= 1;
162 m_selected_button = std::max(m_selected_button, 0);
163 }
164
165 if (controller.pressed(Control::RIGHT))
166 {
167 m_selected_button += 1;
168 m_selected_button = std::min(m_selected_button, static_cast<int>(m_buttons.size()) - 1);
169 }
170
171 if (controller.pressed(Control::ACTION) ||
172 controller.pressed(Control::JUMP) ||
173 controller.pressed(Control::MENU_SELECT))
174 {
175 on_button_click(m_selected_button);
176 }
177
178 if (m_cancel_button != -1 &&
179 (controller.pressed(Control::ESCAPE) ||
180 controller.pressed(Control::MENU_BACK)))
181 {
182 on_button_click(m_cancel_button);
183 }
184 }
185
186 void
draw(DrawingContext & context)187 Dialog::draw(DrawingContext& context)
188 {
189 Rectf bg_rect(Vector(static_cast<float>(m_passive ?
190 (static_cast<float>(context.get_width()) - m_text_size.width - 20.0f) :
191 static_cast<float>(context.get_width()) / 2.0f - m_text_size.width / 2.0f),
192 static_cast<float>(m_passive ?
193 (static_cast<float>(context.get_height()) - m_text_size.height - 65.0f) :
194 (static_cast<float>(context.get_height()) / 2.0f - m_text_size.height / 2.0f))),
195 Sizef(m_text_size.width,
196 m_text_size.height + 44));
197
198 // draw background rect
199 context.color().draw_filled_rect(bg_rect.grown(12.0f),
200 Color(0.2f, 0.3f, 0.4f, m_passive ? 0.3f : 0.8f),
201 16.0f,
202 LAYER_GUI-10);
203
204 context.color().draw_filled_rect(bg_rect.grown(8.0f),
205 Color(0.6f, 0.7f, 0.8f, m_passive ? 0.2f : 0.5f),
206 16.0f,
207 LAYER_GUI-10);
208
209 // draw text
210 context.color().draw_text(Resources::normal_font, m_text,
211 Vector(bg_rect.get_left() + bg_rect.get_width()/2.0f,
212 bg_rect.get_top()),
213 ALIGN_CENTER, LAYER_GUI);
214 if (m_passive)
215 return;
216
217 // draw HL line
218 context.color().draw_filled_rect(Rectf(Vector(bg_rect.get_left(), bg_rect.get_bottom() - 35),
219 Sizef(bg_rect.get_width(), 4)),
220 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
221 context.color().draw_filled_rect(Rectf(Vector(bg_rect.get_left(), bg_rect.get_bottom() - 35),
222 Sizef(bg_rect.get_width(), 2)),
223 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
224
225 // draw buttons
226 for (int i = 0; i < static_cast<int>(m_buttons.size()); ++i)
227 {
228 float segment_width = bg_rect.get_width() / static_cast<float>(m_buttons.size());
229 float button_width = segment_width;
230 Vector pos(bg_rect.get_left() + segment_width/2.0f + static_cast<float>(i) * segment_width,
231 bg_rect.get_bottom() - 12);
232
233 if (i == m_selected_button)
234 {
235 float button_height = 24.0f;
236 float blink = (sinf(g_real_time * math::PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
237 context.color().draw_filled_rect(Rectf(Vector(pos.x - button_width/2, pos.y - button_height/2),
238 Vector(pos.x + button_width/2, pos.y + button_height/2)).grown(2.0f),
239 Color(1.0f, 1.0f, 1.0f, blink),
240 14.0f,
241 LAYER_GUI-10);
242 context.color().draw_filled_rect(Rectf(Vector(pos.x - button_width/2, pos.y - button_height/2),
243 Vector(pos.x + button_width/2, pos.y + button_height/2)),
244 Color(1.0f, 1.0f, 1.0f, 0.5f),
245 12.0f,
246 LAYER_GUI-10);
247 }
248
249 context.color().draw_text(Resources::normal_font, m_buttons[i].text,
250 Vector(pos.x, pos.y - static_cast<float>(int(Resources::normal_font->get_height() / 2))),
251 ALIGN_CENTER, LAYER_GUI,
252 i == m_selected_button ? ColorScheme::Menu::active_color : ColorScheme::Menu::default_color);
253 }
254 }
255
256 void
on_button_click(int button) const257 Dialog::on_button_click(int button) const
258 {
259 if (m_buttons[button].callback)
260 {
261 m_buttons[button].callback();
262 }
263 if (m_clear_diags || button == m_cancel_button)
264 {
265 MenuManager::instance().set_dialog({});
266 }
267 }
268
269 /* EOF */
270