CEGUI In Practice - A Game Console
Written for CEGUI 0.7
Works with versions 0.7.x (obsolete)
CEGUI In Practice series
CEGUI InPractice 6
Welcome back! This tutorial we will put together the knowledge from the past few chapters to create a functioning Console window. The last few tutorials have given us the basics. Now its time to put that knowledge to use and make something we can use in our game.
Game Plan
Okay, now lets look at what we need to do. We will want to be able to type in commands, press the send button, and have them output into the text box. We will also probably want to acknowledge when enter is pressed as thats the 'normal' feel of typing into a chat.
We discussed earlier how to send input to CEGUI (CEGUI_In_Practice_-_Managing_input), so we will have to assume that you have that working (and probably built the application structure behind it). We will also assume you used the previous tutorial's layout file and naming for the console window ( CEGUI_In_Practice_-_Using_.layout_files).
Due to the fact that we are getting to a slightly more advanced implementation of CEGUI here, and because we may want the ConsoleWindow to do other more useful things (like parsing strings for /say commands) I'm going to create a class to encompass the CEGUI ConsoleWindow.
class GameConsoleWindow { public: GameConsoleWindow(); // Constructor private: void CreateCEGUIWindow(); // The function which will load in the CEGUI Window and register event handlers void RegisterHandlers(); // Register our handler functions void Handle_TextSubmitted(const CEGUI::EventArgs &e); // Handle when we press Enter after typing void Handle_SendButtonPressed(const CEGUI::EventArgs &e); // Handle when we press the Send button void ParseText(CEGUI::String inMsg); // Parse the text the user submitted. void OutputText(CEGUI::String inMsg); // Post the message to the ChatHistory listbox. CEGUI::Window *m_ConsoleWindow; // This will be a pointer to the ConsoleRoot window. CEGUI::String sNamePrefix; // This will be the prefix name we give the layout static int iInstanceNumber; // This will be the instance number for this class. };
Alright, so that will be our class. It might look daunting, but don't worry it will all make sense in the end.
Setting up the Window
Now we need to setup the window. We have done this before in the .layout tutorial so we will do it again here. However, we mentioned before that you could use a prefix when loading a .layout to avoid name conflicts if you load multiple layouts. So we need to account for that. Will we need multiple consoles? Who knows, might as well build it in while we're designing it right?
Lets write the CreateCEGUIWindow function:
void GameConsoleWindow::CreateCEGUIWindow() { // Get a local pointer to the CEGUI Window Manager, Purely for convenience to reduce typing CEGUI::WindowManager *pWindowManager = CEGUI::WindowManager::getSingletonPtr(); // Now before we load anything, lets increase our instance number to ensure no conflicts. // I like the format #_ConsoleRoot so thats how i'm gonna make the prefix. This simply // Increments the iInstanceNumber then puts it + a _ into the sNamePrefix string. sNamePrefix = ++iInstanceNumber + "_"; // Now that we can ensure that we have a safe prefix, and won't have any naming conflicts lets create the window // and assign it to our member window pointer m_ConsoleWindow m_ConsoleWindow = pWindowManager->loadWindowLayout(inLayoutName,sNamePrefix); // Being a good programmer, its a good idea to ensure that we got a valid window back. if (m_ConsoleWindow) { // Lets add our new window to the Root GUI Window CEGUI::System::getSingletonPtr()->getGUISheet()->addChildWindow(m_ConsoleWindow); // Now register the handlers for the events (Clicking, typing, etc) (this)->RegisterHandlers(); } else { // Something bad happened and we didn't successfully create the window lets output the information CEGUI::Logger::getSingletonPtr()->logEvent("Error: Unable to load the ConsoleWindow from .layout"); } }
Alright so that created the window. And after the window was created, we Registered its handlers. Lets look at how we go about registering those handlers. Below is the RegisterHandlers function, it will probably look familiar if you have been reading along:
void GameConsoleWindow::RegisterHandlers() { }