c3b200075c3ca8111ed5ff6b3c88126c73ffc308
[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 "supertux/resources.hpp"
22 #include "video/drawing_context.hpp"
23
24 Dialog::Dialog() :
25   m_text(),
26   m_buttons(),
27   m_selected_button(),
28   m_text_size()
29 {
30 }
31
32 Dialog::~Dialog()
33 {
34 }
35
36 void
37 Dialog::set_text(const std::string& text)
38 {
39   m_text = text;
40
41   m_text_size = Sizef(Resources::normal_font->get_text_width(m_text),
42                       Resources::normal_font->get_text_height(m_text));
43
44 }
45
46 void
47 Dialog::add_button(const std::string& text, const std::function<void ()>& callback, bool focus)
48 {
49   m_buttons.push_back({text, callback});
50
51   if (focus)
52   {
53     m_selected_button = m_buttons.size() - 1;
54   }
55 }
56
57 void
58 Dialog::process_input(const Controller& controller)
59 {
60   if (controller.pressed(Controller::LEFT))
61   {
62     m_selected_button -= 1;
63     m_selected_button = std::max(m_selected_button, 0);
64   }
65
66   if (controller.pressed(Controller::RIGHT))
67   {
68     m_selected_button += 1;
69     m_selected_button = std::min(m_selected_button, static_cast<int>(m_buttons.size()) - 1);
70   }
71
72   if (controller.pressed(Controller::ACTION) ||
73       controller.pressed(Controller::MENU_SELECT))
74   {
75     on_button_click(m_selected_button);
76
77     // warning: this will "delete this"
78     MenuManager::instance().set_dialog({});
79   }
80 }
81
82 void
83 Dialog::draw(DrawingContext& ctx)
84 {
85   Rectf bg_rect(Vector(SCREEN_WIDTH/2 - m_text_size.width/2,
86                        SCREEN_HEIGHT/2 - m_text_size.height/2),
87                 Sizef(m_text_size.width,
88                       m_text_size.height + 44));
89
90   // draw background rect
91   ctx.draw_filled_rect(bg_rect.grown(12.0f),
92                        Color(0.2f, 0.3f, 0.4f, 0.8f),
93                        16.0f,
94                        LAYER_GUI-10);
95
96   ctx.draw_filled_rect(bg_rect.grown(8.0f),
97                        Color(0.6f, 0.7f, 0.8f, 0.5f),
98                        16.0f,
99                        LAYER_GUI-10);
100
101   // draw text
102   ctx.draw_text(Resources::normal_font, m_text,
103                 Vector(bg_rect.p1.x + bg_rect.get_width()/2.0f,
104                        bg_rect.p1.y),
105                 ALIGN_CENTER, LAYER_GUI);
106
107   // draw HL line
108   ctx.draw_filled_rect(Vector(bg_rect.p1.x, bg_rect.p2.y - 35),
109                        Vector(bg_rect.get_width(), 4),
110                        Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
111   ctx.draw_filled_rect(Vector(bg_rect.p1.x, bg_rect.p2.y - 35),
112                        Vector(bg_rect.get_width(), 2),
113                        Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
114
115   // draw buttons
116   for(int i = 0; i < static_cast<int>(m_buttons.size()); ++i)
117   {
118     float segment_width = bg_rect.get_width() / m_buttons.size();
119     float button_width = segment_width;
120     float button_height = 24.0f;
121     Vector pos(bg_rect.p1.x + segment_width/2.0f + i * segment_width,
122                bg_rect.p2.y - 12);
123
124     if (i == m_selected_button)
125     {
126       float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
127       ctx.draw_filled_rect(Rectf(Vector(pos.x - button_width/2, pos.y - button_height/2),
128                                  Vector(pos.x + button_width/2, pos.y + button_height/2)).grown(2.0f),
129                            Color(1.0f, 1.0f, 1.0f, blink),
130                            14.0f,
131                            LAYER_GUI-10);
132       ctx.draw_filled_rect(Rectf(Vector(pos.x - button_width/2, pos.y - button_height/2),
133                                  Vector(pos.x + button_width/2, pos.y + button_height/2)),
134                            Color(1.0f, 1.0f, 1.0f, 0.5f),
135                            12.0f,
136                            LAYER_GUI-10);
137     }
138
139     ctx.draw_text(Resources::normal_font, m_buttons[i].text,
140                   Vector(pos.x, pos.y - int(Resources::normal_font->get_height()/2)),
141                   ALIGN_CENTER, LAYER_GUI);
142   }
143 }
144
145 void
146 Dialog::on_button_click(int button)
147 {
148   if (m_buttons[button].callback)
149   {
150     m_buttons[button].callback();
151   }
152 }
153
154 /* EOF */