From f43d9d4d3587149e09962e7f574c533e1ea26d6e Mon Sep 17 00:00:00 2001 From: auria Date: Sun, 15 Aug 2010 23:24:08 +0000 Subject: [PATCH] Ported race paused dialog to XML files + fixed many small things with dialogs-from-xml on the way git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@5741 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/guiengine/event_handler.cpp | 21 ++-- src/guiengine/modaldialog.cpp | 49 +++++--- src/guiengine/modaldialog.hpp | 18 +-- src/guiengine/widget.cpp | 16 +++ src/guiengine/widget.hpp | 7 ++ .../dialogs/press_a_key_dialog.cpp | 3 +- .../dialogs/race_paused_dialog.cpp | 109 +++++------------- .../dialogs/race_paused_dialog.hpp | 6 +- src/utils/ptr_vector.hpp | 8 +- 9 files changed, 119 insertions(+), 118 deletions(-) diff --git a/src/guiengine/event_handler.cpp b/src/guiengine/event_handler.cpp index 605a47e58..04386379c 100644 --- a/src/guiengine/event_handler.cpp +++ b/src/guiengine/event_handler.cpp @@ -479,7 +479,10 @@ EventPropagation EventHandler::onWidgetActivated(GUIEngine::Widget* w, const int // notify modal dialog too if (ModalDialog::isADialogActive()) { - if (ModalDialog::getCurrent()->processEvent(parent->m_properties[PROP_ID]) == EVENT_BLOCK) return EVENT_BLOCK; + if (ModalDialog::getCurrent()->processEvent(parent->m_properties[PROP_ID]) == EVENT_BLOCK) + { + return EVENT_BLOCK; + } } sendEventToUser(parent, parent->m_properties[PROP_ID], playerID); @@ -521,25 +524,29 @@ EventPropagation EventHandler::onGUIEvent(const SEvent& event) //else break; } case EGET_ELEMENT_HOVERED: - { + { Widget* w = GUIEngine::getWidget(id); + if (w == NULL) break; - + if (!w->m_focusable) return GUIEngine::EVENT_BLOCK; - + // When a modal dialog is shown, don't select widgets out of the dialog if (ModalDialog::isADialogActive() && !ModalDialog::getCurrent()->isMyChild(w)) { // check for parents too before discarding event if (w->m_event_handler != NULL) { - if (!ModalDialog::getCurrent()->isMyChild(w->m_event_handler)) break; + if (!ModalDialog::getCurrent()->isMyChild(w->m_event_handler)) + { + break; + } } } - + // select ribbons on hover if (w->m_event_handler != NULL && w->m_event_handler->m_type == WTYPE_RIBBON) - { + { RibbonWidget* ribbon = (RibbonWidget*)(w->m_event_handler); if (ribbon == NULL) break; diff --git a/src/guiengine/modaldialog.cpp b/src/guiengine/modaldialog.cpp index c9220b35d..f0a73ccf1 100644 --- a/src/guiengine/modaldialog.cpp +++ b/src/guiengine/modaldialog.cpp @@ -52,29 +52,13 @@ ModalDialog::ModalDialog(const float percentWidth, const float percentHeight) // ---------------------------------------------------------------------------- -void traceChildren(const ptr_vector& widgets, int indent=0) +void ModalDialog::loadFromFile(const char* xmlFile) { - for (int n=0; n(widgets.getConst(n))->m_properties[PROP_ID].c_str() << "\n"; - traceChildren (widgets[n].getChildren(), indent+1); - } -} - -ModalDialog::ModalDialog(const char* xmlFile, const float percentWidth, const float percentHeight) -{ - // FIXME: dialog are destroyed when dismissed, this means the disk - // will be seeked everytime the same dialog is opened to read its - // XML file... cache loaded dialogs somehow maybe? - doInit(percentWidth, percentHeight); IrrXMLReader* xml = irr::io::createIrrXMLReader( (file_manager->getGUIDir() + "/" + xmlFile).c_str() ); Screen::parseScreenFileDiv(xml, m_children, m_irrlicht_window); delete xml; - - std::cout << "Dialog children :\n"; - traceChildren(m_children); + + loadedFromFile(); LayoutManager::calculateLayout( m_children, this ); @@ -282,3 +266,30 @@ void ModalDialog::addWidgetsRecursively(ptr_vector& widgets, Widget* par } // addWidgetsRecursively // ---------------------------------------------------------------------------- + +bool isMyChildHelperFunc(const ptr_vector* within, const Widget* widget) +{ + if (within->size() == 0) return false; + + if (within->contains(widget)) + { + return true; + } + + const int count = within->size(); + for (int n=0; ngetConst(n)->getChildren(), widget)) + { + return true; + } + } + + return false; +} + +bool ModalDialog::isMyChild(Widget* widget) const +{ + return isMyChildHelperFunc(&m_children, widget); +} + diff --git a/src/guiengine/modaldialog.hpp b/src/guiengine/modaldialog.hpp index f2870d997..6bbe59ed1 100644 --- a/src/guiengine/modaldialog.hpp +++ b/src/guiengine/modaldialog.hpp @@ -59,18 +59,18 @@ namespace GUIEngine * \brief Creates a modal dialog with given percentage of screen width and height */ ModalDialog(const float percentWidth, const float percentHeight); - - /** - * \brief Creates a modal dialog with given percentage of screen width and height - */ - ModalDialog(const char* xmlFile, const float percentWidth, const float percentHeight); - - + + void loadFromFile(const char* xmlFile); + virtual void onEnterPressedInternal(); void clearWindow(); void addWidgetsRecursively(ptr_vector& widgets, Widget* parent=NULL); - + + /** \brief Callback invoked when the dialog was loaded from the XML file (if the constructor + * that takes a XML file as argument is used) + */ + virtual void loadedFromFile() {} public: ptr_vector m_children; @@ -80,7 +80,7 @@ namespace GUIEngine /** Returns whether to block event propagation (usually, you will want to block events you processed) */ virtual EventPropagation processEvent(const std::string& eventSource){ return EVENT_LET; } - bool isMyChild(Widget* widget) const { return m_children.contains(widget); } + bool isMyChild(Widget* widget) const; bool isMyChild(irr::gui::IGUIElement* widget) const { return m_irrlicht_window->isMyChild(widget); } Widget* getFirstWidget(); diff --git a/src/guiengine/widget.cpp b/src/guiengine/widget.cpp index 95c84291c..212d3168d 100644 --- a/src/guiengine/widget.cpp +++ b/src/guiengine/widget.cpp @@ -160,6 +160,22 @@ void Widget::setDeactivated() } } +// ----------------------------------------------------------------------------- + +bool Widget::deleteChild(const char* id) +{ + const int count = m_children.size(); + for (int n=0; n& getChildren() const { return m_children; } + /** + * \brief removes and deletes the child with the given PROP_ID + * \param id PROP_ID property of the child to remove + * \return whether deletion was successful + */ + bool deleteChild(const char* id); + /** * Override in children to possibly receive updates (you may need to register to * them first) diff --git a/src/states_screens/dialogs/press_a_key_dialog.cpp b/src/states_screens/dialogs/press_a_key_dialog.cpp index 9818856a7..e866f9c15 100644 --- a/src/states_screens/dialogs/press_a_key_dialog.cpp +++ b/src/states_screens/dialogs/press_a_key_dialog.cpp @@ -30,8 +30,9 @@ using namespace irr::gui; // ------------------------------------------------------------------------------------------------------ PressAKeyDialog::PressAKeyDialog(const float w, const float h) : - ModalDialog("press_a_key_dialog.stkgui", w, h) + ModalDialog(w, h) { + loadFromFile("press_a_key_dialog.stkgui"); } // ------------------------------------------------------------------------------------------------------ diff --git a/src/states_screens/dialogs/race_paused_dialog.cpp b/src/states_screens/dialogs/race_paused_dialog.cpp index 8dee0bf6d..a3118a44a 100644 --- a/src/states_screens/dialogs/race_paused_dialog.cpp +++ b/src/states_screens/dialogs/race_paused_dialog.cpp @@ -1,5 +1,5 @@ // SuperTuxKart - a fun racing game with go-kart -// Copyright (C) 2009 Marianne Gagnon +// Copyright (C) 2010 Marianne Gagnon // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License @@ -44,83 +44,12 @@ RacePausedDialog::RacePausedDialog(const float percentWidth, const float percentHeight) : ModalDialog(percentWidth, percentHeight) { + loadFromFile("race_paused_dialog.stkgui"); + World::getWorld()->pause(WorldStatus::IN_GAME_MENU_PHASE); - - ScalableFont* font = GUIEngine::getTitleFont(); - const int text_height = GUIEngine::getFontHeight(); - - IGUIFont* titlefont = GUIEngine::getTitleFont(); - const int title_height = font->getDimension(L"X").Height; - - - const int icon_size = (m_area.getHeight() - text_height - 150) / 2; - - // ---- Caption - core::rect< s32 > area(0, 0, m_area.getWidth(), title_height); - IGUIStaticText* caption = - GUIEngine::getGUIEnv()->addStaticText( _("Paused"), - area, /*border*/false, - /*word wrap*/ false, - m_irrlicht_window); - caption->setTabStop(false); - caption->setTextAlignment(EGUIA_CENTER, EGUIA_CENTER); - caption->setOverrideFont(titlefont); - caption->setOverrideColor(video::SColor(255,255,255,255)); - // ---- Back button - IconButtonWidget* back_btn = new IconButtonWidget(); - back_btn->m_properties[PROP_ID] = "backbtn"; - back_btn->m_properties[PROP_ICON] = "gui/back.png"; - //I18N: In the 'paused' screen - back_btn->m_text = _("Back to Race"); - back_btn->m_x = m_area.getWidth() / 2 - icon_size; - back_btn->m_y = text_height*2; - back_btn->m_w = icon_size*2; // width larger to leave room for text - back_btn->m_h = icon_size; - back_btn->setParent(m_irrlicht_window); - m_children.push_back(back_btn); - back_btn->add(); - + ButtonWidget* back_btn = (ButtonWidget*)Screen::getWidget("backbtn", &m_children); back_btn->setFocusForPlayer( PLAYER_ID_GAME_MASTER ); - - // ---- Choice ribbon - m_choice_ribbon = new RibbonWidget(RIBBON_TOOLBAR); - m_choice_ribbon->m_properties[PROP_ID] = "choiceribbon"; - - m_choice_ribbon->m_x = 0; - m_choice_ribbon->m_y = text_height*2 + icon_size + 50; - m_choice_ribbon->m_w = m_area.getWidth(); - m_choice_ribbon->m_h = icon_size + text_height; - m_choice_ribbon->setParent(m_irrlicht_window); - - if (race_manager->getMajorMode() == RaceManager::MAJOR_MODE_SINGLE) - { - //I18N: In the 'paused' screen - m_choice_ribbon->addIconChild(_("Setup New Race"), "newrace", - 128, 128, "gui/main_race.png"); - } - - if (race_manager->getMajorMode() == RaceManager::MAJOR_MODE_SINGLE) - { - //I18N: In the 'paused' screen - m_choice_ribbon->addIconChild(_("Restart Race"), "restart", - 128, 128, "gui/restart.png"); - } - - //I18N: In the 'paused' screen - m_choice_ribbon->addIconChild(_("Options"), "options", - 128, 128, "gui/main_options.png"); - - //I18N: In the 'paused' screen - m_choice_ribbon->addIconChild(_("Help"), "help", 128, 128, - "gui/main_help.png"); - - //I18N: In the 'paused' screen - m_choice_ribbon->addIconChild(_("Exit Race"), "exit", 128, 128, - "gui/main_quit.png"); - - m_children.push_back(m_choice_ribbon); - m_choice_ribbon->add(); } // RacePausedDialog // ---------------------------------------------------------------------------- @@ -130,17 +59,43 @@ RacePausedDialog::~RacePausedDialog() } // ~RacePausedDialog // ---------------------------------------------------------------------------- + +void RacePausedDialog::loadedFromFile() +{ + printf("==== RacePausedDialog::loadedFromFile() ====\n"); + + // disable the "restart" button in GPs + if (race_manager->getMajorMode() != RaceManager::MAJOR_MODE_SINGLE) + { + printf("==== REMOVING restart button ====\n"); + GUIEngine::RibbonWidget* choice_ribbon = (GUIEngine::RibbonWidget*) + Screen::getWidget("choiceribbon", &m_children); + + + const bool success = choice_ribbon->deleteChild("restart"); + assert(success); + } +} + +// ---------------------------------------------------------------------------- + void RacePausedDialog::onEnterPressedInternal() { } // onEnterPressedInternal // ---------------------------------------------------------------------------- + GUIEngine::EventPropagation RacePausedDialog::processEvent(const std::string& eventSource) { - if(UserConfigParams::m_verbosity>=5) + GUIEngine::RibbonWidget* chocie_ribbon = (GUIEngine::RibbonWidget*) + Screen::getWidget("choiceribbon", &m_children); + + if (UserConfigParams::m_verbosity>=5) + { std::cout << "RacePausedDialog::processEvent(" << eventSource.c_str() << ")\n"; + } if (eventSource == "backbtn") { @@ -151,7 +106,7 @@ GUIEngine::EventPropagation else if (eventSource == "choiceribbon") { const std::string& selection = - m_choice_ribbon->getSelectionIDString(PLAYER_ID_GAME_MASTER); + chocie_ribbon->getSelectionIDString(PLAYER_ID_GAME_MASTER); if(UserConfigParams::m_verbosity>=5) std::cout << "RacePausedDialog::processEvent(" diff --git a/src/states_screens/dialogs/race_paused_dialog.hpp b/src/states_screens/dialogs/race_paused_dialog.hpp index c40dc68d2..766a91d89 100644 --- a/src/states_screens/dialogs/race_paused_dialog.hpp +++ b/src/states_screens/dialogs/race_paused_dialog.hpp @@ -27,7 +27,11 @@ namespace GUIEngine class RacePausedDialog : public GUIEngine::ModalDialog { - GUIEngine::RibbonWidget* m_choice_ribbon; + //GUIEngine::RibbonWidget* m_choice_ribbon; + +protected: + virtual void loadedFromFile(); + public: /** * Creates a modal dialog with given percentage of screen width and height diff --git a/src/utils/ptr_vector.hpp b/src/utils/ptr_vector.hpp index 273c2b808..96596d097 100644 --- a/src/utils/ptr_vector.hpp +++ b/src/utils/ptr_vector.hpp @@ -115,13 +115,13 @@ TYPE* remove(const int ID) return out; } -bool contains( TYPE* instance ) const +bool contains( const TYPE* instance ) const { const unsigned int amount = contentsVector.size(); - for(unsigned int n=0; n