m_text(),
m_buttons(),
m_selected_button(),
+ m_cancel_button(-1),
m_text_size()
{
}
{
m_buttons.clear();
m_selected_button = 0;
+ m_cancel_button = -1;
}
void
-Dialog::add_button(const std::string& text, const std::function<void ()>& callback, bool focus)
+Dialog::add_default_button(const std::string& text, const std::function<void ()>& 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<void ()>& callback)
+{
+ add_button(text, callback);
+ m_cancel_button = m_buttons.size() - 1;
+}
+
+void
+Dialog::add_button(const std::string& text, const std::function<void ()>& callback)
+{
+ m_buttons.push_back({text, callback});
}
int
{
m_selected_button = new_button;
on_button_click(m_selected_button);
-
- // warning: this will "delete this"
- MenuManager::instance().set_dialog({});
}
}
break;
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);
}
}
{
m_buttons[button].callback();
}
+ MenuManager::instance().set_dialog({});
}
/* EOF */
std::string m_text;
std::vector<Button> m_buttons;
int m_selected_button;
+ int m_cancel_button;
Sizef m_text_size;
virtual ~Dialog();
void set_text(const std::string& text);
+
+ void add_button(const std::string& text, const std::function<void ()>& callback = {});
+
+ /** The default gets focused when the dialog is first shown */
+ void add_default_button(const std::string& text, const std::function<void ()>& callback = {});
+
+ /** The cancel button can not only be activated by selecting it, but
+ via the MENU_BACK button */
+ void add_cancel_button(const std::string& text, const std::function<void ()>& callback = {});
+
void clear_buttons();
- void add_button(const std::string& text, const std::function<void ()>& callback = {},
- bool focus = false);
void event(const SDL_Event& event);
void process_input(const Controller& controller);
case MNID_QUITMAINMENU:
std::unique_ptr<Dialog> dialog(new Dialog);
dialog->set_text(_("Do you really want to quit SuperTux?"));
- dialog->add_button(_("Cancel"));
- dialog->add_button(_("Quit SuperTux"), [] {
+ dialog->add_cancel_button(_("Cancel"));
+ dialog->add_default_button(_("Quit SuperTux"), [] {
MenuManager::instance().clear_menu_stack();
ScreenManager::current()->quit(std::unique_ptr<ScreenFade>(new FadeOut(0.25)));
SoundManager::current()->stop_music(0.25);
- }, true);
+ });
MenuManager::instance().set_dialog(std::move(dialog));
break;
}