2 // Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
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.
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.
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/>.
17 #include "gui/dialog.hpp"
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"
42 Dialog::set_text(const std::string& text)
46 m_text_size = Sizef(Resources::normal_font->get_text_width(m_text),
47 Resources::normal_font->get_text_height(m_text));
52 Dialog::add_button(const std::string& text, const std::function<void ()>& callback, bool focus)
54 m_buttons.push_back({text, callback});
58 m_selected_button = m_buttons.size() - 1;
63 Dialog::get_button_at(const Vector& mouse_pos) const
65 Rectf bg_rect(Vector(SCREEN_WIDTH/2 - m_text_size.width/2,
66 SCREEN_HEIGHT/2 - m_text_size.height/2),
67 Sizef(m_text_size.width,
68 m_text_size.height + 44));
70 for(int i = 0; i < static_cast<int>(m_buttons.size()); ++i)
72 float segment_width = bg_rect.get_width() / m_buttons.size();
73 float button_width = segment_width;
74 float button_height = 24.0f;
75 Vector pos(bg_rect.p1.x + segment_width/2.0f + i * segment_width,
77 Rectf button_rect(Vector(pos.x - button_width/2, pos.y - button_height/2),
78 Vector(pos.x + button_width/2, pos.y + button_height/2));
79 if (button_rect.contains(mouse_pos))
88 Dialog::event(const SDL_Event& ev)
91 case SDL_MOUSEBUTTONDOWN:
92 if(ev.button.button == SDL_BUTTON_LEFT)
94 Vector mouse_pos = VideoSystem::current()->get_renderer().to_logical(ev.motion.x, ev.motion.y);
95 int new_button = get_button_at(mouse_pos);
98 m_selected_button = new_button;
99 on_button_click(m_selected_button);
101 // warning: this will "delete this"
102 MenuManager::instance().set_dialog({});
107 case SDL_MOUSEMOTION:
109 Vector mouse_pos = VideoSystem::current()->get_renderer().to_logical(ev.motion.x, ev.motion.y);
110 int new_button = get_button_at(mouse_pos);
111 if (new_button != -1)
113 m_selected_button = new_button;
114 if(MouseCursor::current())
115 MouseCursor::current()->set_state(MC_LINK);
119 if(MouseCursor::current())
120 MouseCursor::current()->set_state(MC_NORMAL);
131 Dialog::process_input(const Controller& controller)
133 if (controller.pressed(Controller::LEFT))
135 m_selected_button -= 1;
136 m_selected_button = std::max(m_selected_button, 0);
139 if (controller.pressed(Controller::RIGHT))
141 m_selected_button += 1;
142 m_selected_button = std::min(m_selected_button, static_cast<int>(m_buttons.size()) - 1);
145 if (controller.pressed(Controller::ACTION) ||
146 controller.pressed(Controller::MENU_SELECT))
148 on_button_click(m_selected_button);
150 // warning: this will "delete this"
151 MenuManager::instance().set_dialog({});
156 Dialog::draw(DrawingContext& ctx)
158 Rectf bg_rect(Vector(SCREEN_WIDTH/2 - m_text_size.width/2,
159 SCREEN_HEIGHT/2 - m_text_size.height/2),
160 Sizef(m_text_size.width,
161 m_text_size.height + 44));
163 // draw background rect
164 ctx.draw_filled_rect(bg_rect.grown(12.0f),
165 Color(0.2f, 0.3f, 0.4f, 0.8f),
169 ctx.draw_filled_rect(bg_rect.grown(8.0f),
170 Color(0.6f, 0.7f, 0.8f, 0.5f),
175 ctx.draw_text(Resources::normal_font, m_text,
176 Vector(bg_rect.p1.x + bg_rect.get_width()/2.0f,
178 ALIGN_CENTER, LAYER_GUI);
181 ctx.draw_filled_rect(Vector(bg_rect.p1.x, bg_rect.p2.y - 35),
182 Vector(bg_rect.get_width(), 4),
183 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
184 ctx.draw_filled_rect(Vector(bg_rect.p1.x, bg_rect.p2.y - 35),
185 Vector(bg_rect.get_width(), 2),
186 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
189 for(int i = 0; i < static_cast<int>(m_buttons.size()); ++i)
191 float segment_width = bg_rect.get_width() / m_buttons.size();
192 float button_width = segment_width;
193 float button_height = 24.0f;
194 Vector pos(bg_rect.p1.x + segment_width/2.0f + i * segment_width,
197 if (i == m_selected_button)
199 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
200 ctx.draw_filled_rect(Rectf(Vector(pos.x - button_width/2, pos.y - button_height/2),
201 Vector(pos.x + button_width/2, pos.y + button_height/2)).grown(2.0f),
202 Color(1.0f, 1.0f, 1.0f, blink),
205 ctx.draw_filled_rect(Rectf(Vector(pos.x - button_width/2, pos.y - button_height/2),
206 Vector(pos.x + button_width/2, pos.y + button_height/2)),
207 Color(1.0f, 1.0f, 1.0f, 0.5f),
212 ctx.draw_text(Resources::normal_font, m_buttons[i].text,
213 Vector(pos.x, pos.y - int(Resources::normal_font->get_height()/2)),
214 ALIGN_CENTER, LAYER_GUI,
215 i == m_selected_button ? ColorScheme::Menu::active_color : ColorScheme::Menu::default_color);
220 Dialog::on_button_click(int button) const
222 if (m_buttons[button].callback)
224 m_buttons[button].callback();