fixed bug in menus when hitting escape to go back to the previous menu

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/irrlicht@3348 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
auria 2009-04-04 18:59:42 +00:00
parent 325fe78fae
commit 59ba8f1e44
6 changed files with 72 additions and 28 deletions

View File

@ -49,6 +49,7 @@ void switchToScreen(const char* screen_name)
{ {
// clean what was left by the previous screen // clean what was left by the previous screen
g_env->clear(); g_env->clear();
if(g_current_screen != NULL) g_current_screen->elementsWereDeleted();
g_current_screen = NULL; g_current_screen = NULL;
Widget::resetIDCounters(); Widget::resetIDCounters();

View File

@ -29,7 +29,7 @@ Screen::Screen(const char* file)
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void Screen::loadFromFile() void Screen::loadFromFile()
{ {
std::cout << "loading GUI screen from file " << (file_manager->getGUIDir() + "/" + m_filename).c_str() << std::endl; std::cout << "*** loading GUI screen from file " << (file_manager->getGUIDir() + "/" + m_filename).c_str() << std::endl;
IrrXMLReader* xml = irr::io::createIrrXMLReader( (file_manager->getGUIDir() + "/" + m_filename).c_str() ); IrrXMLReader* xml = irr::io::createIrrXMLReader( (file_manager->getGUIDir() + "/" + m_filename).c_str() );
parseScreenFileDiv(xml, m_widgets); parseScreenFileDiv(xml, m_widgets);
m_loaded = true; m_loaded = true;
@ -204,6 +204,28 @@ void Screen::addWidgetsRecursively(ptr_vector<Widget>& widgets, Widget* parent)
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
/**
* Called when screen is removed. This means all irrlicht widgets this screen has pointers
* to are now gone. Set all references to NULL to avoid problems.
*/
void Screen::elementsWereDeleted(ptr_vector<Widget>* within_vector)
{
if(within_vector == NULL) within_vector = &m_widgets;
const unsigned short widgets_amount = within_vector->size();
for(int n=0; n<widgets_amount; n++)
{
Widget& widget = (*within_vector)[n];
widget.m_element = NULL;
if(widget.m_children.size() > 0)
{
elementsWereDeleted( &(widget.m_children) );
}
}
}
// -----------------------------------------------------------------------------
Widget* Screen::getWidget(const char* name) Widget* Screen::getWidget(const char* name)
{ {
return getWidget(name, &m_widgets); return getWidget(name, &m_widgets);
@ -244,6 +266,7 @@ Widget* Screen::getWidget(const int id, ptr_vector<Widget>* within_vector)
if(widget.m_children.size() > 0) if(widget.m_children.size() > 0)
{ {
// std::cout << "widget = <" << widget.m_properties[PROP_ID].c_str() << "> widget.m_children.size()=" << widget.m_children.size() << std::endl;
Widget* el = getWidget(id, &(widget.m_children)); Widget* el = getWidget(id, &(widget.m_children));
if(el != NULL) return el; if(el != NULL) return el;
} }

View File

@ -48,6 +48,8 @@ namespace GUIEngine
const std::string& getName() const { return m_filename; } const std::string& getName() const { return m_filename; }
void elementsWereDeleted(ptr_vector<Widget>* within_vector = NULL);
virtual bool OnEvent(const SEvent& event); virtual bool OnEvent(const SEvent& event);
}; };

View File

@ -59,6 +59,7 @@ void Skin::draw3DButtonPaneStandard (IGUIElement *element, const core::rect< s32
const int id = element->getID(); const int id = element->getID();
if(id != -1) if(id != -1)
{ {
//std::cout << "searching for a widget in screen " << GUIEngine::getCurrentScreen()->getName().c_str() << std::endl;
Widget* widget = GUIEngine::getCurrentScreen()->getWidget(id); Widget* widget = GUIEngine::getCurrentScreen()->getWidget(id);
if(widget != NULL) if(widget != NULL)
@ -92,7 +93,7 @@ void Skin::draw3DButtonPaneStandard (IGUIElement *element, const core::rect< s32
} }
} }
} }
if(!draw_border && !mark_selected) return; if(!draw_border && !mark_selected) return;
if(mark_selected) if(mark_selected)

View File

@ -63,23 +63,28 @@ namespace StateManager
void menuEventKarts(GUIEngine::Widget* widget, std::string& name) void menuEventKarts(GUIEngine::Widget* widget, std::string& name)
{ {
static bool karts_menu_inited = false; static bool karts_menu_inited = false;
if(name == "init" && !karts_menu_inited) if(name == "init" /*&& !karts_menu_inited*/)
{ {
std::cout << "filling kart menu..." << std::endl;
GUIEngine::RibbonGridWidget* w = dynamic_cast<GUIEngine::RibbonGridWidget*>(GUIEngine::getCurrentScreen()->getWidget("karts")); GUIEngine::RibbonGridWidget* w = dynamic_cast<GUIEngine::RibbonGridWidget*>(GUIEngine::getCurrentScreen()->getWidget("karts"));
assert( w != NULL ); assert( w != NULL );
w->addItem("Gnu","k1","gnu.png"); if(!karts_menu_inited)
w->addItem("Wilber","k2","gnu.png"); {
w->addItem("Tux","k3","gnu.png"); w->addItem("Gnu","k1","gnu.png");
w->addItem("Puffy","k4","gnu.png"); w->addItem("Wilber","k2","gnu.png");
w->addItem("Hexley","k5","gnu.png"); w->addItem("Tux","k3","gnu.png");
w->addItem("Sushi","k6","gnu.png"); w->addItem("Puffy","k4","gnu.png");
w->addItem("Nolok","k7","gnu.png"); w->addItem("Hexley","k5","gnu.png");
w->addItem("Mozilla","k8","gnu.png"); w->addItem("Sushi","k6","gnu.png");
w->addItem("Nolok","k7","gnu.png");
w->addItem("Mozilla","k8","gnu.png");
karts_menu_inited = true;
}
w->updateItemDisplay(); w->updateItemDisplay();
GUIEngine::SpinnerWidget* w2 = dynamic_cast<GUIEngine::SpinnerWidget*> GUIEngine::SpinnerWidget* w2 = dynamic_cast<GUIEngine::SpinnerWidget*>
(GUIEngine::getCurrentScreen()->getWidget("player")); (GUIEngine::getCurrentScreen()->getWidget("player"));
assert( w2 != NULL ); assert( w2 != NULL );
w2->addLabel("Hiker"); w2->addLabel("Hiker");
w2->addLabel("Auria"); w2->addLabel("Auria");
@ -99,7 +104,8 @@ namespace StateManager
w3->setModel(test); w3->setModel(test);
karts_menu_inited = true; //karts_menu_inited = true;
std::cout << "filled kart menu... done" << std::endl;
} }
// TODO - actually check which kart was selected // TODO - actually check which kart was selected
else if(name == "karts") else if(name == "karts")
@ -196,23 +202,26 @@ namespace StateManager
void menuEventTracks(GUIEngine::Widget* widget, std::string& name) void menuEventTracks(GUIEngine::Widget* widget, std::string& name)
{ {
static bool track_menu_inited = false; static bool track_menu_inited = false;
if(name == "init" && !track_menu_inited) if(name == "init")
{ {
GUIEngine::RibbonGridWidget* w = dynamic_cast<GUIEngine::RibbonGridWidget*> GUIEngine::RibbonGridWidget* w = dynamic_cast<GUIEngine::RibbonGridWidget*>
(GUIEngine::getCurrentScreen()->getWidget("tracks")); (GUIEngine::getCurrentScreen()->getWidget("tracks"));
assert( w != NULL ); assert( w != NULL );
w->addItem("Track 1","t1","track1.png"); if(!track_menu_inited)
w->addItem("Track 2","t2","track2.png"); {
w->addItem("Track 3","t3","track3.png"); w->addItem("Track 1","t1","track1.png");
w->addItem("Track 4","t4","track4.png"); w->addItem("Track 2","t2","track2.png");
w->addItem("Track 5","t5","track5.png"); w->addItem("Track 3","t3","track3.png");
w->addItem("Track 6","t6","track6.png"); w->addItem("Track 4","t4","track4.png");
w->addItem("Track 7","t7","track7.png"); w->addItem("Track 5","t5","track5.png");
w->addItem("Track 8","t8","track8.png"); w->addItem("Track 6","t6","track6.png");
w->addItem("Track 7","t7","track7.png");
w->addItem("Track 8","t8","track8.png");
track_menu_inited = true;
}
w->updateItemDisplay(); w->updateItemDisplay();
track_menu_inited = true;
} }
// -- track seelction screen // -- track seelction screen
if(name == "tracks") if(name == "tracks")
@ -378,7 +387,10 @@ namespace StateManager
} }
g_game_mode = g_menu_stack[g_menu_stack.size()-1] == "race"; g_game_mode = g_menu_stack[g_menu_stack.size()-1] == "race";
std::cout << "-- switching to screen " << g_menu_stack[g_menu_stack.size()-1].c_str() << std::endl;
GUIEngine::switchToScreen(g_menu_stack[g_menu_stack.size()-1].c_str()); GUIEngine::switchToScreen(g_menu_stack[g_menu_stack.size()-1].c_str());
std::cout << "-- switched to screen " << g_menu_stack[g_menu_stack.size()-1].c_str() << std::endl;
eventCallback(NULL, g_init_event); eventCallback(NULL, g_init_event);
} }

View File

@ -660,6 +660,10 @@ RibbonGridWidget::RibbonGridWidget()
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void RibbonGridWidget::add() void RibbonGridWidget::add()
{ {
// Work-around for FIXME below... first clear children to avoid duplicates since we're adding everything again everytime
m_children.clearAndDeleteAll();
m_rows.clearWithoutDeleting();
m_has_label = m_properties[PROP_TEXT].size() > 0; m_has_label = m_properties[PROP_TEXT].size() > 0;
const int label_height = m_has_label ? 25 : 0; const int label_height = m_has_label ? 25 : 0;
@ -689,6 +693,7 @@ void RibbonGridWidget::add()
// add rows // add rows
for(int n=0; n<row_amount; n++) for(int n=0; n<row_amount; n++)
{ {
// FIXME - only add the first time, not everytime
RibbonWidget* ribbon = new RibbonWidget(RIBBON_TOOLBAR); RibbonWidget* ribbon = new RibbonWidget(RIBBON_TOOLBAR);
ribbon->x = x; ribbon->x = x;
ribbon->y = y + (int)(n*row_height); ribbon->y = y + (int)(n*row_height);
@ -724,14 +729,14 @@ void RibbonGridWidget::add()
m_label->setTextAlignment( EGUIA_CENTER, EGUIA_CENTER ); m_label->setTextAlignment( EGUIA_CENTER, EGUIA_CENTER );
} }
// add arrow butotns on each side // add arrow buttons on each side
// FIXME? these arrow buttons are outside of the widget's boundaries // FIXME? these arrow buttons are outside of the widget's boundaries
// create sub-widgets if they don't already exist // create sub-widgets if they don't already exist
if(m_left_widget == NULL) //if(m_left_widget == NULL)
{ //{
m_left_widget = new Widget(); m_left_widget = new Widget();
m_right_widget = new Widget(); m_right_widget = new Widget();
} //}
const int average_y = y + (h-label_height)/2; const int average_y = y + (h-label_height)/2;
const int button_w = 30, button_h = 50; const int button_w = 30, button_h = 50;