diff --git a/src/guiengine/screen.cpp b/src/guiengine/screen.cpp index b13b6be11..e8e1995d3 100644 --- a/src/guiengine/screen.cpp +++ b/src/guiengine/screen.cpp @@ -42,6 +42,8 @@ using namespace GUIEngine; Screen::Screen(const char* file) { + m_magic_number = 0xCAFEC001; + throttleFPS = true; m_mouse_x = 0; @@ -55,6 +57,8 @@ Screen::Screen(const char* file) Screen::Screen() { + m_magic_number = 0xCAFEC001; + m_mouse_x = 0; m_mouse_y = 0; m_loaded = false; @@ -62,8 +66,20 @@ Screen::Screen() m_render_3d = false; } +Screen::~Screen() +{ + assert(m_magic_number == 0xCAFEC001); + m_magic_number = 0xDEADBEEF; +} + void Screen::forgetWhatWasLoaded() { + assert(m_magic_number == 0xCAFEC001); + for (int n=0; ngetGUIDir() + "/" + m_filename).c_str() ); parseScreenFileDiv(xml, m_widgets); m_loaded = true; @@ -86,6 +103,7 @@ void Screen::loadFromFile() /* small shortcut so this method can be called without arguments */ void Screen::calculateLayout() { + assert(m_magic_number == 0xCAFEC001); // build layout calculateLayout( m_widgets ); } @@ -97,6 +115,7 @@ void Screen::calculateLayout() */ void Screen::calculateLayout(ptr_vector& widgets, Widget* parent) { + assert(m_magic_number == 0xCAFEC001); const unsigned short widgets_amount = widgets.size(); // ----- read x/y/size parameters @@ -264,6 +283,7 @@ void Screen::calculateLayout(ptr_vector& widgets, Widget* parent) void Screen::addWidgets() { + assert(m_magic_number == 0xCAFEC001); if (!m_loaded) loadFromFile(); addWidgetsRecursively( m_widgets ); @@ -312,6 +332,7 @@ void Screen::addWidgetsRecursively(ptr_vector& widgets, Widget* parent) */ void Screen::elementsWereDeleted(ptr_vector* within_vector) { + assert(m_magic_number == 0xCAFEC001); if (within_vector == NULL) within_vector = &m_widgets; const unsigned short widgets_amount = within_vector->size(); @@ -330,11 +351,13 @@ void Screen::elementsWereDeleted(ptr_vector* within_vector) // ----------------------------------------------------------------------------- void Screen::manualAddWidget(Widget* w) { + assert(m_magic_number == 0xCAFEC001); m_widgets.push_back(w); } // ----------------------------------------------------------------------------- void Screen::manualRemoveWidget(Widget* w) { + assert(m_magic_number == 0xCAFEC001); m_widgets.remove(w); } diff --git a/src/guiengine/screen.hpp b/src/guiengine/screen.hpp index 167485454..2762ed5b8 100644 --- a/src/guiengine/screen.hpp +++ b/src/guiengine/screen.hpp @@ -92,6 +92,8 @@ namespace GUIEngine /** Will be called to determine if the 3D scene must be rendered when at this screen. */ bool m_render_3d; + + int m_magic_number; public: bool throttleFPS; @@ -107,11 +109,12 @@ namespace GUIEngine /** Next time this menu needs to be shown, don't use cached values, re-calculate everything. (useful e.g. on reschange, when sizes have changed and must be re-calculated) */ - void forgetWhatWasLoaded(); + virtual void forgetWhatWasLoaded(); Screen(); /**< creates a dummy incomplete object; only use to override behaviour in sub-class */ Screen(const char* filename); - virtual ~Screen(){} + virtual ~Screen(); + bool operator ==(const char* filename) const { return m_filename == filename; } /** returns an object by name, or NULL if not found */ diff --git a/src/guiengine/widget.cpp b/src/guiengine/widget.cpp index 550897fa5..49822f424 100644 --- a/src/guiengine/widget.cpp +++ b/src/guiengine/widget.cpp @@ -56,6 +56,8 @@ namespace GUIEngine // ----------------------------------------------------------------------------- Widget::Widget(bool reserve_id) { + m_magic_number = 0xCAFEC001; + x = -1; y = -1; w = -1; @@ -88,6 +90,8 @@ Widget::Widget(bool reserve_id) Widget::~Widget() { + assert(m_magic_number == 0xCAFEC001); + // If any player focused this widget, unset that focus for (int n=0; nunfocused(playerID); m_player_focus[playerID] = false; } @@ -176,6 +188,8 @@ void Widget::unsetFocusForPlayer(const int playerID) */ bool Widget::isFocusedForPlayer(const int playerID) { + assert(m_magic_number == 0xCAFEC001); + return m_player_focus[playerID]; } @@ -189,7 +203,7 @@ bool Widget::isFocusedForPlayer(const int playerID) * Returns false if couldn't convert to either */ bool Widget::convertToCoord(std::string& x, int* absolute /* out */, int* percentage /* out */) -{ +{ bool is_number; int i; std::istringstream myStream(x); @@ -211,6 +225,8 @@ bool Widget::convertToCoord(std::string& x, int* absolute /* out */, int* percen void Widget::move(const int x, const int y, const int w, const int h) { + assert(m_magic_number == 0xCAFEC001); + this->x = x; this->y = y; this->w = w; @@ -226,6 +242,8 @@ void Widget::move(const int x, const int y, const int w, const int h) */ void Widget::readCoords(Widget* parent) { + assert(m_magic_number == 0xCAFEC001); + /* determine widget position and size if not already done by sizers */ std::string x = m_properties[PROP_X]; std::string y = m_properties[PROP_Y]; @@ -360,6 +378,7 @@ void Widget::readCoords(Widget* parent) void Widget::setParent(IGUIElement* parent) { + assert(m_magic_number == 0xCAFEC001); m_parent = parent; } diff --git a/src/guiengine/widget.hpp b/src/guiengine/widget.hpp index 959c54c9f..22ddae8c9 100644 --- a/src/guiengine/widget.hpp +++ b/src/guiengine/widget.hpp @@ -93,6 +93,7 @@ namespace GUIEngine class Widget : public SkinWidgetContainer { protected: + int m_magic_number; friend class EventHandler; friend class RibbonWidget; diff --git a/src/states_screens/kart_selection.cpp b/src/states_screens/kart_selection.cpp index 478c2f4a0..afa18f9ae 100644 --- a/src/states_screens/kart_selection.cpp +++ b/src/states_screens/kart_selection.cpp @@ -684,13 +684,25 @@ KartSelectionScreen::KartSelectionScreen() : Screen("karts.stkgui") { g_dispatcher = new FocusDispatcher(this); } - +// ----------------------------------------------------------------------------- +void KartSelectionScreen::forgetWhatWasLoaded() +{ + Screen::forgetWhatWasLoaded(); + + // this pointer is no more valid + g_dispatcher = NULL; +} // ----------------------------------------------------------------------------- // Return true if event was handled successfully bool KartSelectionScreen::playerJoin(InputDevice* device, bool firstPlayer) { std::cout << "playerJoin() ==========\n"; + if (g_dispatcher == NULL) + { + g_dispatcher = new FocusDispatcher(this); + } + DynamicRibbonWidget* w = this->getWidget("karts"); if (w == NULL) { @@ -717,7 +729,7 @@ bool KartSelectionScreen::playerJoin(InputDevice* device, bool firstPlayer) { g_dispatcher->setRootID(kartsArea.m_reserved_id); g_dispatcher->add(); - m_widgets.push_back(g_dispatcher); + if (!m_widgets.contains(g_dispatcher)) m_widgets.push_back(g_dispatcher); // We keep the ID of the "root" widget, see comment at top g_root_id = kartsArea.m_reserved_id; diff --git a/src/states_screens/kart_selection.hpp b/src/states_screens/kart_selection.hpp index 0cbb04ca4..2fb299d8f 100644 --- a/src/states_screens/kart_selection.hpp +++ b/src/states_screens/kart_selection.hpp @@ -70,4 +70,7 @@ public: /** Standard 'Screen' callback every frame */ void onUpdate(float dt, irr::video::IVideoDriver*); + + /** overload */ + virtual void forgetWhatWasLoaded(); }; diff --git a/src/utils/ptr_vector.hpp b/src/utils/ptr_vector.hpp index 0d653d994..273c2b808 100644 --- a/src/utils/ptr_vector.hpp +++ b/src/utils/ptr_vector.hpp @@ -129,10 +129,15 @@ bool contains( TYPE* instance ) const void clearAndDeleteAll() { - for(unsigned int n=0; n