From 33899210b2818102bdb2f5d4a6ac4fb5940dfdab Mon Sep 17 00:00:00 2001 From: auria Date: Sun, 28 Jun 2009 20:07:06 +0000 Subject: [PATCH] Implemented 'press esc to cancel' buttons in some modal dialogs git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/irrlicht@3663 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/gui/modaldialog.cpp | 55 ++++++++++++++++++++++++++++++++--------- src/gui/modaldialog.hpp | 3 +++ src/gui/screen.cpp | 6 +++++ 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/gui/modaldialog.cpp b/src/gui/modaldialog.cpp index f3abdd53e..2a1f8aa49 100644 --- a/src/gui/modaldialog.cpp +++ b/src/gui/modaldialog.cpp @@ -21,6 +21,7 @@ #include "gui/options_screen.hpp" #include "gui/state_manager.hpp" #include "gui/widget.hpp" +#include "input/input_manager.hpp" #include "network/network_manager.hpp" #include "race/race_manager.hpp" #include "utils/translation.hpp" @@ -108,19 +109,30 @@ PressAKeyDialog::PressAKeyDialog(const float w, const float h) : widget->add(); + IGUIFont* font = GUIEngine::getFont(); + const int textHeight = font->getDimension(L"X").Height; + ButtonWidget* widget2 = new ButtonWidget(); widget2->m_type = WTYPE_BUTTON; // FIXME : shouldn't constructor set type? - widget2->m_properties[PROP_TEXT] = _("Press ESC to cancel"); // TODO : pressing this button should cancel + widget2->m_properties[PROP_ID] = "cancel"; + widget2->m_properties[PROP_TEXT] = _("Press ESC to cancel"); widget2->x = 15; - widget2->y = m_area.getHeight() - 60; + widget2->y = m_area.getHeight() - textHeight - 12; widget2->w = m_area.getWidth() - 30; - widget2->h = 50; + widget2->h = textHeight + 6; widget2->setParent(m_irrlicht_window); m_children.push_back(widget2); widget2->add(); } - +void PressAKeyDialog::processEvent(std::string& eventSource) +{ + if(eventSource == "cancel") + { + input_manager->setMode(InputManager::MENU); + dismiss(); + } +} // ------------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------------ @@ -142,7 +154,7 @@ EnterPlayerNameDialog::EnterPlayerNameDialog(const float w, const float h) : widget->x = 0; widget->y = 0; widget->w = m_area.getWidth(); - widget->h = m_area.getHeight()/2; + widget->h = m_area.getHeight()/3; widget->setParent(m_irrlicht_window); m_children.push_back(widget); @@ -153,10 +165,7 @@ EnterPlayerNameDialog::EnterPlayerNameDialog(const float w, const float h) : IGUIFont* font = GUIEngine::getFont(); const int textHeight = font->getDimension(L"X").Height; - const int bottomYFrom = m_area.getHeight()/2; - const int bottomYTo = m_area.getHeight(); - const int bottomHeight = bottomYTo - bottomYFrom; - const int textAreaYFrom = bottomYFrom + bottomHeight/2 - textHeight/2; + const int textAreaYFrom = m_area.getHeight()/2 - textHeight/2; textCtrl = new TextBoxWidget(); textCtrl->m_type = WTYPE_BUTTON; @@ -169,14 +178,38 @@ EnterPlayerNameDialog::EnterPlayerNameDialog(const float w, const float h) : m_children.push_back(textCtrl); textCtrl->add(); GUIEngine::getGUIEnv()->setFocus( textCtrl->m_element ); + + ButtonWidget* widget2 = new ButtonWidget(); + widget2->m_type = WTYPE_BUTTON; // FIXME : shouldn't constructor set type? + widget2->m_properties[PROP_ID] = "cancel"; + widget2->m_properties[PROP_TEXT] = _("Press ESC to cancel"); + widget2->x = 15; + widget2->y = m_area.getHeight() - textHeight - 12; + widget2->w = m_area.getWidth() - 30; + widget2->h = textHeight + 6; + widget2->setParent(m_irrlicht_window); + + m_children.push_back(widget2); + widget2->add(); + + // don't allow navigating there with keyboard; pressing 'enter' will accept the current name + // no matter where focus is + widget2->m_element->setTabStop(false); + } EnterPlayerNameDialog::~EnterPlayerNameDialog() { - std::cout << "Rmoving text control element\n"; textCtrl->m_element->remove(); } - +void EnterPlayerNameDialog::processEvent(std::string& eventSource) +{ + if(eventSource == "cancel") + { + input_manager->setMode(InputManager::MENU); + dismiss(); + } +} // ------------------------------------------------------------------------------------------------------ void EnterPlayerNameDialog::onEnterPressedInternal() diff --git a/src/gui/modaldialog.hpp b/src/gui/modaldialog.hpp index e7174e209..350447933 100644 --- a/src/gui/modaldialog.hpp +++ b/src/gui/modaldialog.hpp @@ -48,6 +48,7 @@ public: ptr_vector m_children; virtual ~ModalDialog(); + virtual void processEvent(std::string& eventSource){} static void dismiss(); static void onEnterPressed(); @@ -62,6 +63,7 @@ public: * Creates a modal dialog with given percentage of screen width and height */ PressAKeyDialog(const float percentWidth, const float percentHeight); + void processEvent(std::string& eventSource); }; class EnterPlayerNameDialog : public ModalDialog @@ -75,6 +77,7 @@ public: ~EnterPlayerNameDialog(); void onEnterPressedInternal(); + void processEvent(std::string& eventSource); }; class TrackInfoDialog : public ModalDialog diff --git a/src/gui/screen.cpp b/src/gui/screen.cpp index 59adab812..cd0268990 100644 --- a/src/gui/screen.cpp +++ b/src/gui/screen.cpp @@ -651,6 +651,12 @@ bool Screen::OnEvent(const SEvent& event) Widget* w = getWidget(id); if(w == NULL) break; + if(ModalDialog::isADialogActive()) + { + ModalDialog::getCurrent()->processEvent(w->m_properties[PROP_ID]); + return false; + } + Widget* parent = w->m_event_handler; if(w->m_event_handler != NULL) {