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