From: Ingo Ruhnke Date: Wed, 27 Aug 2014 07:37:56 +0000 (+0200) Subject: Added default and cancel button to the Dialog X-Git-Url: https://git.octo.it/?p=supertux.git;a=commitdiff_plain;h=7831e810062da90ebb745f8e1a4346bc1e80e3ed Added default and cancel button to the Dialog --- diff --git a/src/gui/dialog.cpp b/src/gui/dialog.cpp index b10f29918..d48991152 100644 --- a/src/gui/dialog.cpp +++ b/src/gui/dialog.cpp @@ -30,6 +30,7 @@ Dialog::Dialog() : m_text(), m_buttons(), m_selected_button(), + m_cancel_button(-1), m_text_size() { } @@ -53,17 +54,27 @@ Dialog::clear_buttons() { m_buttons.clear(); m_selected_button = 0; + m_cancel_button = -1; } void -Dialog::add_button(const std::string& text, const std::function& callback, bool focus) +Dialog::add_default_button(const std::string& text, const std::function& callback) { - m_buttons.push_back({text, callback}); + add_button(text, callback); + m_selected_button = m_buttons.size() - 1; +} - if (focus) - { - m_selected_button = m_buttons.size() - 1; - } +void +Dialog::add_cancel_button(const std::string& text, const std::function& callback) +{ + add_button(text, callback); + m_cancel_button = m_buttons.size() - 1; +} + +void +Dialog::add_button(const std::string& text, const std::function& callback) +{ + m_buttons.push_back({text, callback}); } int @@ -104,9 +115,6 @@ Dialog::event(const SDL_Event& ev) { m_selected_button = new_button; on_button_click(m_selected_button); - - // warning: this will "delete this" - MenuManager::instance().set_dialog({}); } } break; @@ -153,9 +161,12 @@ Dialog::process_input(const Controller& controller) controller.pressed(Controller::MENU_SELECT)) { on_button_click(m_selected_button); + } - // warning: this will "delete this" - MenuManager::instance().set_dialog({}); + if (m_cancel_button != -1 && + controller.pressed(Controller::MENU_BACK)) + { + on_button_click(m_cancel_button); } } @@ -230,6 +241,7 @@ Dialog::on_button_click(int button) const { m_buttons[button].callback(); } + MenuManager::instance().set_dialog({}); } /* EOF */ diff --git a/src/gui/dialog.hpp b/src/gui/dialog.hpp index 86c499a2f..2f8b73589 100644 --- a/src/gui/dialog.hpp +++ b/src/gui/dialog.hpp @@ -39,6 +39,7 @@ private: std::string m_text; std::vector