auria fad4cb4c01 Updated menu singleton code, there was a bug in its logic (that had no consequence that i know but better fix it anyway) + some code style cleanup along the way
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@5023 178a84e3-b1eb-0310-8ba1-8eac791a3b58
2010-03-18 18:18:19 +00:00

164 lines
5.9 KiB
C++

// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2009 Marianne Gagnon
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef HEADER_SCREEN_HPP
#define HEADER_SCREEN_HPP
#include <map>
#include <string>
#include "irrlicht.h"
#include "guiengine/engine.hpp"
#include "guiengine/widget.hpp"
#include "input/input.hpp"
#include "utils/ptr_vector.hpp"
namespace GUIEngine
{
#define DEFINE_SCREEN_SINGLETON( ClassName ) template<> ClassName* GUIEngine::ScreenSingleton< ClassName >::singleton = NULL
/**
* Declares a class to be a singleton. Normally, all screens will be singletons.
* Note that you need to use the 'DEFINE_SCREEN_SINGLETON' macro in a .cpp file to
* actually define the instance (as this can't be done in a .h)
*/
template<typename SCREEN>
class ScreenSingleton
{
static SCREEN* singleton;
public:
~ScreenSingleton()
{
singleton = NULL;
}
static SCREEN* getInstance()
{
if (singleton == NULL)
{
singleton = new SCREEN();
GUIEngine::addScreenToList(singleton);
}
return singleton;
}
};
void parseScreenFileDiv(irr::io::IrrXMLReader* xml, ptr_vector<Widget>& append_to);
/**
* Represents a single screen. Mainly responsible of its children widgets; Screen lays them
* out, asks them to add themselves, asks them to remove themselves, etc.
*
* Also initiates the read of GUI files, even though most of that work is done in "screen_loader.cpp"
*/
class Screen
{
friend class Skin;
bool m_loaded;
std::string m_filename;
void loadFromFile();
static void addWidgetsRecursively(ptr_vector<Widget>& widgets, Widget* parent=NULL);
void calculateLayout(ptr_vector<Widget>& widgets, Widget* parent=NULL);
/** Will be called to determine if the 3D scene must be rendered when at this screen. */
bool m_render_3d;
unsigned int m_magic_number;
public:
bool throttleFPS;
ptr_vector<Widget, HOLD> m_widgets;
// current mouse position, read-only...
int m_mouse_x, m_mouse_y;
/** this variable is not used by the Screen object itself; it's the routines creating
* screens that may use it to perform some operations only once. initialized to false.
*/
bool m_inited;
/** 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) */
virtual void forgetWhatWasLoaded();
Screen(); /**< creates a dummy incomplete object; only use to override behaviour in sub-class */
Screen(const char* filename);
virtual ~Screen();
bool operator ==(const char* filename) const { return m_filename == filename; }
/** returns an object by name, or NULL if not found */
Widget* getWidget(const char* name);
/** returns an object by name, casted to specified type, or NULL if not found/wrong type */
template <typename T> T* getWidget(const char* name)
{
return dynamic_cast<T*>( getWidget(name) );
}
static Widget* getWidget(const char* name, ptr_vector<Widget>* within_vector);
static Widget* getWidget(const int id, ptr_vector<Widget>* within_vector);
Widget* getFirstWidget(ptr_vector<Widget>* within_vector=NULL);
Widget* getLastWidget(ptr_vector<Widget>* within_vector=NULL);
virtual void addWidgets();
virtual void calculateLayout();
void manualAddWidget(Widget* w);
void manualRemoveWidget(Widget* w);
const std::string& getName() const { return m_filename; }
void elementsWereDeleted(ptr_vector<Widget>* within_vector = NULL);
/** Will be called to determine if the 3D scene must be rendered when at this screen */
bool needs3D() { return m_render_3d; }
void setNeeds3D(bool needs3D) { m_render_3d = needs3D; }
virtual void init() = 0;
virtual void tearDown() = 0;
/** Called when escape is pressed.
* @return true if the screen should be closed, false if you handled the press another way */
virtual bool onEscapePressed() { return true; }
/**
* will be called everytime sometimes happens.
* Events are generally a widget state change. In this case, a pointer to the said widget is passed along its
* name, so you get its new state and/or act. There are two special events, passed with a NULL widget, and which
* bear the anmes "init" and "tearDown", called respectively when a screen is being made visible and when it's
* being left, allowing for setup/clean-up.
*/
virtual void eventCallback(Widget* widget, const std::string& name, const int playerID) = 0;
virtual void onUpdate(float dt, irr::video::IVideoDriver*) { };
};
}
#endif