From e906147104b40f670d692db861f3be27c47008ee Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Mon, 25 Aug 2014 05:43:14 +0200 Subject: [PATCH] Implemented Dialog::draw() --- src/gui/dialog.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++--- src/gui/dialog.hpp | 4 +++ 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/src/gui/dialog.cpp b/src/gui/dialog.cpp index 8c4c0ddc8..5d7b117f7 100644 --- a/src/gui/dialog.cpp +++ b/src/gui/dialog.cpp @@ -17,16 +17,27 @@ #include "gui/dialog.hpp" #include "control/controller.hpp" +#include "gui/menu_manager.hpp" #include "supertux/resources.hpp" #include "video/drawing_context.hpp" Dialog::Dialog() : - m_text(""), + m_text(), m_buttons(), m_selected_button() { } +Dialog::~Dialog() +{ +} + +void +Dialog::set_text(const std::string& text) +{ + m_text = text; +} + void Dialog::add_button(const std::string& text) { @@ -46,22 +57,91 @@ Dialog::process_input(const Controller& controller) { m_selected_button += 1; m_selected_button = std::min(m_selected_button, static_cast(m_buttons.size()) - 1); - } + } + + if (controller.pressed(Controller::ACTION) || + controller.pressed(Controller::MENU_SELECT)) + { + on_select(m_selected_button); + + // warning: this will "delete this" + MenuManager::instance().set_dialog({}); + } } void Dialog::draw(DrawingContext& ctx) { + Vector center(SCREEN_WIDTH/2, SCREEN_HEIGHT/2); + + Sizef text_size(Resources::normal_font->get_text_width(m_text), + Resources::normal_font->get_text_height(m_text)); + + Rectf text_rect(Vector(center.x - text_size.width/2, + center.y - text_size.height/2), + text_size); + + Rectf bg_rect = text_rect; + bg_rect.p2.y += 44; + + // draw background rect + ctx.draw_filled_rect(bg_rect.grown(12.0f), + Color(0.2f, 0.3f, 0.4f, 0.8f), + 16.0f, + LAYER_GUI-10); + + ctx.draw_filled_rect(bg_rect.grown(8.0f), + Color(0.6f, 0.7f, 0.8f, 0.5f), + 16.0f, + LAYER_GUI-10); + + // draw text + ctx.draw_text(Resources::normal_font, m_text, + Vector(bg_rect.p1.x + bg_rect.get_width()/2.0f, + bg_rect.p1.y), + ALIGN_CENTER, LAYER_GUI); + + // draw HL line + ctx.draw_filled_rect(Vector(bg_rect.p1.x, bg_rect.p2.y - 35), + Vector(bg_rect.get_width(), 4), + Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI); + ctx.draw_filled_rect(Vector(bg_rect.p1.x, bg_rect.p2.y - 35), + Vector(bg_rect.get_width(), 2), + Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI); + + // draw buttons for(int i = 0; i < static_cast(m_buttons.size()); ++i) { + float segment_width = bg_rect.get_width() / m_buttons.size(); + float button_width = segment_width * 0.95; + float button_height = 24.0f; + Vector pos(bg_rect.p1.x + segment_width/2.0f + i * segment_width, + bg_rect.p2.y - 12); + if (i == m_selected_button) { - // highlight + float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f; + ctx.draw_filled_rect(Rectf(Vector(pos.x - button_width/2, pos.y - button_height/2), + Vector(pos.x + button_width/2, pos.y + button_height/2)).grown(2.0f), + Color(1.0f, 1.0f, 1.0f, blink), + 14.0f, + LAYER_GUI-10); + ctx.draw_filled_rect(Rectf(Vector(pos.x - button_width/2, pos.y - button_height/2), + Vector(pos.x + button_width/2, pos.y + button_height/2)), + Color(1.0f, 1.0f, 1.0f, 0.5f), + 12.0f, + LAYER_GUI-10); } + ctx.draw_text(Resources::normal_font, m_buttons[i], - Vector(100, 100), + Vector(pos.x, pos.y - int(Resources::normal_font->get_height()/2)), ALIGN_CENTER, LAYER_GUI); } } +void +Dialog::on_select(int id) +{ +} + /* EOF */ diff --git a/src/gui/dialog.hpp b/src/gui/dialog.hpp index 272cb4bca..001a30043 100644 --- a/src/gui/dialog.hpp +++ b/src/gui/dialog.hpp @@ -32,12 +32,16 @@ private: public: Dialog(); + virtual ~Dialog(); + void set_text(const std::string& text); void add_button(const std::string& text); void process_input(const Controller& controller); void draw(DrawingContext& context); + virtual void on_select(int id); + private: Dialog(const Dialog&) = delete; Dialog& operator=(const Dialog&) = delete; -- 2.11.0