Showing posts with label interface. Show all posts
Showing posts with label interface. Show all posts

Friday, August 10, 2007

Cegui and Ogre

Now that the project is starting to become more complex, I felt that the use of a GUI would help. In Ogre's Wiki it is recommended to use CEGUI. CEGUI is a free library usefull for creating windows and widgets such as buttons, listboxes or textboxes.

Has a guide I used this tutorial available in the wiki here.

First of all, before going to the code, CEGUI has several configuration files, you can see an example in your [OGRE_HOME]/media/gui directory. This is what they do:

.tga - Image File with all the textures of all widgets

.looknfeel - XML file that describes the look of all widgets and their behaviour.

.schema - XML file where all the widgets are listed and all configurations are linked.

.layout - XML file describing the windows that you want to put in one sheet in the Scene.


For now we'll be using the default look and feel, so the only thing that we have to create is a .layout file. Here an example that creates a window with four buttons:

<?xml version="1.0" ?>
<GUILayout>
<Window Type="DefaultWindow" Name="DemoLayout">
<Window Type="TaharezLook/FrameWindow" Name="Main">
<Property Name="UnifiedSize" Value="{{0.35,0},{0.6,0}}" />
<Property Name="UnifiedPosition" Value="{{0.05,0},{0.05,0}}" />

<!-- <Property Name="RelativeMinSize" Value="w:0.2 h:0.2" />
<Property Name="RelativeMaxSize" Value="w:1.0 h:1.0" /> -->

<Property Name="Text" Value="Sound and Gui Test" />
<Property Name="CloseButtonEnabled" Value="False" />

<Window Type="TaharezLook/Button" Name="cmdQuit">
<Property Name="Text" Value="Quit" />
<Property Name="UnifiedPosition" Value="{{0.2,0},{0.8,0}}" />
<Property Name="UnifiedSize" Value="{{0.6,0},{0.07,0}}" />
</Window>
<Window Type="TaharezLook/Button" Name="cmdSound">
<Property Name="UnifiedPosition" Value="{{0.2,0},{0.6,0}}" />
<Property Name="UnifiedSize" Value="{{0.6,0},{0.07,0}}" />
<Property Name="Text" Value="Sound" />
</Window>
<Window Type="TaharezLook/Button" Name="cmdInstantAction">
<Property Name="UnifiedPosition" Value="{{0.2,0},{0.5,0}}" />
<Property Name="UnifiedSize" Value="{{0.6,0},{0.07,0}}" />
<Property Name="Text" Value="InstantAction" />
</Window>
<Window Type="TaharezLook/Button" Name="cmdMusic">
<Property Name="UnifiedPosition" Value="{{0.2,0},{0.7,0}}" />
<Property Name="UnifiedSize" Value="{{0.6,0},{0.07,0}}" />
<Property Name="Text" Value="Music" />
</Window>
</Window>
</Window>
</GUILayout>
Now we have to load this layout to our application:

Initialization :


mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr);
mGUISystem = new CEGUI::System(mGUIRenderer);
CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Informative);
CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme");
mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");

mGUISystem->setDefaultFont((CEGUI::utf8*)"BlueHighway-12");

CEGUI::Window* sheet = CEGUI::WindowManager::getSingleton().loadWindowLayout("myLayout.layout");
mGUISystem->setGUISheet( sheet );

// set the mouse cursor initially in the middle of the screen
mGUISystem->injectMousePosition((float)mWindow->getWidth() / 2.0f, (float)mWindow->getHeight() / 2.0f);

Creating Handler's for the buttons:


CEGUI::Window *win;

CEGUI::Window *m_win = CEGUI::WindowManager::getSingleton().getWindow( "Main" );
win = m_win->getChild("cmdQuit");
win->subscribeEvent( CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber( &GuiScene::handleQuit, this ));
bool GuiScene::handleQuit(const CEGUI::EventArgs& e)
{
this->stop();

return true;
}
Taking care of the IO:

In MouseListener class:

bool GuiScene::mouseMoved(const OIS::MouseEvent &mouseEventRef)
{
mGUISystem->injectMouseMove(mouseEventRef.state.X.rel, mouseEventRef.state.Y.rel);

return true;
}

bool GuiScene::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
mGUISystem->injectMouseButtonDown(convertOISMouseButtonToCegui(id));

return true;
}

And that is it, this example is for Ogre 1.4.x and CEGUI 0.5.x for further explanations please look at the practical example. This is the final result:

fig.1 - CEGUI menu