Navigation Menu

Skip to content

Commit

Permalink
Added callback functions to Dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Grumbel committed Aug 25, 2014
1 parent e906147 commit aae0f6e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
51 changes: 29 additions & 22 deletions src/gui/dialog.cpp
Expand Up @@ -24,7 +24,8 @@
Dialog::Dialog() :
m_text(),
m_buttons(),
m_selected_button()
m_selected_button(),
m_text_size()
{
}

Expand All @@ -36,12 +37,21 @@ void
Dialog::set_text(const std::string& text)
{
m_text = text;

m_text_size = Sizef(Resources::normal_font->get_text_width(m_text),
Resources::normal_font->get_text_height(m_text));

}

void
Dialog::add_button(const std::string& text)
Dialog::add_button(const std::string& text, const std::function<void ()>& callback, bool focus)
{
m_buttons.push_back(text);
m_buttons.push_back({text, callback});

if (focus)
{
m_selected_button = m_buttons.size() - 1;
}
}

void
Expand All @@ -62,7 +72,7 @@ Dialog::process_input(const Controller& controller)
if (controller.pressed(Controller::ACTION) ||
controller.pressed(Controller::MENU_SELECT))
{
on_select(m_selected_button);
on_button_click(m_selected_button);

// warning: this will "delete this"
MenuManager::instance().set_dialog({});
Expand All @@ -72,17 +82,10 @@ Dialog::process_input(const Controller& controller)
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;
Rectf bg_rect(Vector(SCREEN_WIDTH/2 - m_text_size.width/2,
SCREEN_HEIGHT/2 - m_text_size.height/2),
Sizef(m_text_size.width,
m_text_size.height + 44));

// draw background rect
ctx.draw_filled_rect(bg_rect.grown(12.0f),
Expand All @@ -103,17 +106,17 @@ Dialog::draw(DrawingContext& ctx)

// 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);
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);
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<int>(m_buttons.size()); ++i)
{
float segment_width = bg_rect.get_width() / m_buttons.size();
float button_width = segment_width * 0.95;
float button_width = segment_width;
float button_height = 24.0f;
Vector pos(bg_rect.p1.x + segment_width/2.0f + i * segment_width,
bg_rect.p2.y - 12);
Expand All @@ -133,15 +136,19 @@ Dialog::draw(DrawingContext& ctx)
LAYER_GUI-10);
}

ctx.draw_text(Resources::normal_font, m_buttons[i],
ctx.draw_text(Resources::normal_font, m_buttons[i].text,
Vector(pos.x, pos.y - int(Resources::normal_font->get_height()/2)),
ALIGN_CENTER, LAYER_GUI);
}
}

void
Dialog::on_select(int id)
Dialog::on_button_click(int button)
{
if (m_buttons[button].callback)
{
m_buttons[button].callback();
}
}

/* EOF */
19 changes: 16 additions & 3 deletions src/gui/dialog.hpp
Expand Up @@ -17,30 +17,43 @@
#ifndef HEADER_SUPERTUX_GUI_DIALOG_HPP
#define HEADER_SUPERTUX_GUI_DIALOG_HPP

#include <functional>
#include <string>
#include <vector>

#include "math/sizef.hpp"

class Controller;
class DrawingContext;

class Dialog
{
private:
struct Button
{
std::string text;
std::function<void ()> callback;
};

std::string m_text;
std::vector<std::string> m_buttons;
std::vector<Button> m_buttons;
int m_selected_button;

Sizef m_text_size;

public:
Dialog();
virtual ~Dialog();

void set_text(const std::string& text);
void add_button(const std::string& text);
void add_button(const std::string& text, const std::function<void ()>& callback = {},
bool focus = false);

void process_input(const Controller& controller);
void draw(DrawingContext& context);

virtual void on_select(int id);
private:
void on_button_click(int button);

private:
Dialog(const Dialog&) = delete;
Expand Down

0 comments on commit aae0f6e

Please sign in to comment.