Difference between revisions of "CEGUI In Practice - User Data"
m (Robot: Cosmetic changes) |
|||
Line 2: | Line 2: | ||
This tutorial will be a short tutorial, as i will show how to do a small addon to the previous tutorial [[CEGUI In Practice - A Game Console]] showing you how to associate the ConsoleRoot with our GameConsoleWindow class. While not terribly useful in this situation, it can demonstrate a useful feature that you likely will use in your own game. | This tutorial will be a short tutorial, as i will show how to do a small addon to the previous tutorial [[CEGUI In Practice - A Game Console]] showing you how to associate the ConsoleRoot with our GameConsoleWindow class. While not terribly useful in this situation, it can demonstrate a useful feature that you likely will use in your own game. | ||
+ | |||
+ | |||
+ | ==== UserData ==== | ||
+ | |||
+ | User data in CEGUI is basically a void pointer. This allows you to assign a custom structure or variable to each cegui window. This is extremely useful as it allows you to greater incorporate CEGUI into your development. For the previous example we created a Class to encompass our Console window which was great as it allowed us to easily encapsulate all the CEGUI windows and their handlers into a neat and easy to use function. It was pretty easy to go from GameConsoleWindow to each of the classes (by accessing the m_ConsoleWindow in the class and getChild() method). However, its not so easy to from the CEGUI::Window back up to the class level without recompiling CEGUI and creating custom widgets, unless you use userData. | ||
+ | |||
+ | Its almost so easy to use that its a waste of wiki space to show this, but regardless I see alot of posts about it on the IRC channel. Here is a line we would add to our CreateCEGUIWindow(); function. | ||
+ | |||
+ | <source lang="cpp"> | ||
+ | void GameConsoleWindow::CreateCEGUIWindow() | ||
+ | { | ||
+ | |||
+ | ... | ||
+ | |||
+ | CEGUI::System::getSingleton().getGUISheet()->addChildWindow(m_ConsoleWindow); | ||
+ | (this)->RegisterHandlers(); | ||
+ | m_ConsoleWindow->setUserData(this); // <------ Add this line | ||
+ | ... | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | That adds a pointer to the class we're currently in, which is an instance of GameConsoleWindow. Note tho, it stores this as a void* so when we go back to this pointer later, it will not know what type of data this is, so you will need to cast it back: | ||
+ | |||
+ | <source lang="cpp"> | ||
+ | GameConsoleWindow *ConsoleClass = NULL; | ||
+ | |||
+ | ConsoleClass = static_cast<GameConsoleWindow*>(m_ConsoleWindow->getUserData()); | ||
+ | </source> | ||
+ | |||
+ | This will convert the void* we stored above, into the ConsoleClass pointer. You could probably use dynamic_cast but then you'd need to turn on RTT, which is not always required for performance reasons. Regardless you can see how easily this is done. We will use this in a later tutorial demonstrating how to implement an inventory system. | ||
+ | |||
+ | Until next time.. |
Revision as of 18:52, 7 March 2011
CEGUI InPractice 7
This tutorial will be a short tutorial, as i will show how to do a small addon to the previous tutorial CEGUI In Practice - A Game Console showing you how to associate the ConsoleRoot with our GameConsoleWindow class. While not terribly useful in this situation, it can demonstrate a useful feature that you likely will use in your own game.
UserData
User data in CEGUI is basically a void pointer. This allows you to assign a custom structure or variable to each cegui window. This is extremely useful as it allows you to greater incorporate CEGUI into your development. For the previous example we created a Class to encompass our Console window which was great as it allowed us to easily encapsulate all the CEGUI windows and their handlers into a neat and easy to use function. It was pretty easy to go from GameConsoleWindow to each of the classes (by accessing the m_ConsoleWindow in the class and getChild() method). However, its not so easy to from the CEGUI::Window back up to the class level without recompiling CEGUI and creating custom widgets, unless you use userData.
Its almost so easy to use that its a waste of wiki space to show this, but regardless I see alot of posts about it on the IRC channel. Here is a line we would add to our CreateCEGUIWindow(); function.
void GameConsoleWindow::CreateCEGUIWindow() { ... CEGUI::System::getSingleton().getGUISheet()->addChildWindow(m_ConsoleWindow); (this)->RegisterHandlers(); m_ConsoleWindow->setUserData(this); // <------ Add this line ... }
That adds a pointer to the class we're currently in, which is an instance of GameConsoleWindow. Note tho, it stores this as a void* so when we go back to this pointer later, it will not know what type of data this is, so you will need to cast it back:
GameConsoleWindow *ConsoleClass = NULL; ConsoleClass = static_cast<GameConsoleWindow*>(m_ConsoleWindow->getUserData());
This will convert the void* we stored above, into the ConsoleClass pointer. You could probably use dynamic_cast but then you'd need to turn on RTT, which is not always required for performance reasons. Regardless you can see how easily this is done. We will use this in a later tutorial demonstrating how to implement an inventory system.
Until next time..