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
This commit is contained in:
auria 2010-08-15 23:24:08 +00:00
parent f03c8b94fe
commit f43d9d4d35
9 changed files with 119 additions and 118 deletions

View File

@ -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;

View File

@ -52,29 +52,13 @@ ModalDialog::ModalDialog(const float percentWidth, const float percentHeight)
// ----------------------------------------------------------------------------
void traceChildren(const ptr_vector<Widget>& widgets, int indent=0)
void ModalDialog::loadFromFile(const char* xmlFile)
{
for (int n=0; n<widgets.size(); n++)
{
for (int i=0; i<indent; i++) std::cout << " ";
std::cout << " Type " << widgets[n].getType() << " : "
<< const_cast<Widget*>(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<Widget>& widgets, Widget* par
} // addWidgetsRecursively
// ----------------------------------------------------------------------------
bool isMyChildHelperFunc(const ptr_vector<Widget>* 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; n<count; n++)
{
if (isMyChildHelperFunc(&within->getConst(n)->getChildren(), widget))
{
return true;
}
}
return false;
}
bool ModalDialog::isMyChild(Widget* widget) const
{
return isMyChildHelperFunc(&m_children, widget);
}

View File

@ -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<Widget>& 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<Widget> 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();

View File

@ -160,6 +160,22 @@ void Widget::setDeactivated()
}
}
// -----------------------------------------------------------------------------
bool Widget::deleteChild(const char* id)
{
const int count = m_children.size();
for (int n=0; n<count; n++)
{
if (m_children[n].m_properties[PROP_ID] == id)
{
m_children.erase(n);
return true;
}
}
return false;
}
// -----------------------------------------------------------------------------
namespace GUIEngine
{

View File

@ -377,6 +377,13 @@ namespace GUIEngine
const ptr_vector<Widget>& 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)

View File

@ -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");
}
// ------------------------------------------------------------------------------------------------------

View File

@ -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("

View File

@ -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

View File

@ -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<amount; n++)
for (unsigned int n=0; n<amount; n++)
{
TYPE * pointer = contentsVector[n];
if(pointer == instance) return true;
const TYPE * pointer = contentsVector[n];
if (pointer == instance) return true;
}
return false;