Presentation is loading. Please wait.

Presentation is loading. Please wait.

2012. 11. 8. GUI II. Ogre3D GUI with MyGUI.

Similar presentations

Presentation on theme: "2012. 11. 8. GUI II. Ogre3D GUI with MyGUI."— Presentation transcript:

1 GUI II. Ogre3D GUI with MyGUI

2 Project initialization
Project initialization Extract Run: OgreMyGui.sln Set include and library paths (if not correct) Set working directory (if not $(SolutionDir)/bin) Compile Run Play !!

3 Ghost Game with Overlays
Ghost Game with Overlays We continue to improve our Ghost Buster game from the previous session, where we added overlays to the game. We are going to use the MyGUI library, which targets Ogre3D as one of its supported rendering platforms. (OpenGL and Direct3D are also supported, so any game engine could use this GUI, but with Ogre3D, we do not have to go down to that level). 3

4 MyGUI free, open source library targeted at Ogre3D
relatively simple and flexible visual tools for GUI editing dismal documentation for coding

5 MyGUI compilation there is no downloadable SDK
MyGUI compilation there is no downloadable SDK but included in for you if code had to be compiled requires Ogre3D SDK requires FreeType library for Windows, install FreeType for Windows from requires CMake build tool In, you will find the compiled binaries of the MyGUI libraries. If, for a different project or different Ogre or MyGUI version, you need to build these yourself, you are in for a challenging task.

6 MyGUI configuration in CMake specify Ogre and FreeType directories as dependencies click “Generate” open generated MyGUI.sln in Visual Studio verify that boost version of the Ogre SDK is included (addition include dirs) compile MyGUIEngine and OgrePlatform

7 MyGUI includes include directories
..\..\..\MyGUI_3.2.0\MyGUIEngine\include ..\..\..\MyGUI_3.2.0\Platforms\Ogre\OgrePlatfo rm\include main.cpp #include "MyGUI.h" #include "MyGUI_OgrePlatform.h"

8 Channeling events to the GUI
Channeling events to the GUI class GuiInputHandler : public OIS::MouseListener , public OIS::KeyListener { public: bool mouseMoved( const OIS::MouseEvent &arg ) { return MyGUI::InputManager::getInstance().injectMouseMove( arg.state.X.abs, arg.state.Y.abs, arg.state.Z.abs); } bool mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id ){ return MyGUI::InputManager::getInstance().injectMousePress( arg.state.X.abs, arg.state.Y.abs, MyGUI::MouseButton::Enum(id)); } bool mouseReleased( const OIS::MouseEvent &arg,OIS::MouseButtonID id ) { return MyGUI::InputManager::getInstance().injectMouseRelease( arg.state.X.abs, arg.state.Y.abs, MyGUI::MouseButton::Enum(id)); } bool keyPressed( const OIS::KeyEvent &arg ) { return MyGUI::InputManager::getInstance().injectKeyPress( MyGUI::KeyCode::Enum(arg.key), arg.text); } bool keyReleased( const OIS::KeyEvent &arg ) { return MyGUI::InputManager::getInstance().injectKeyRelease( MyGUI::KeyCode::Enum(arg.key)); } }; We need a class GuiInputHandler that implements the OIS (Ogre’s namespace for input-handling-related classes) MouseListener and KeyListener interfaces. We will register an instance of this class to call the respective functions of MyGUI’s InputManager singleton.

9 InputManager modification (inputs.h)
InputManager modification (inputs.h) public: OIS::Keyboard* mKeyboard; OIS::Mouse* mMouse; mKeyboard = static_cast<OIS::Keyboard*>( OISInputManager->createInputObject( OIS::OISKeyboard, true )); mMouse = static_cast<OIS::Mouse*>( OISInputManager->createInputObject( OIS::OISMouse, true )); We need to make mKeyboard and mMouse accessible from outside. The easiest – if not very commendable – way is to make them public. In the constructor, where we create the mouse and keyboard objects, we need to make them buffered, otherwise they will not call the methods of the registered listener class.

10 Listener registration in main.cpp: setupListeners
Listener registration in main.cpp: setupListeners GuiInputHandler* guiInputHander = new GuiInputHandler(); inputManager->mMouse-> setEventCallback(guiInputHander); inputManager->mKeyboard-> We have to create an instance of GuiInputHandler, and set it as an event callback object for both the mouse and keyboard objects. This way all the events will reach the MyGUI library code.

11 GUI initialization main.cpp: setupScene
GUI initialization main.cpp: setupScene platform = new MyGUI::OgrePlatform(); platform->initialise(renderWindow, sceneManager); gui = new MyGUI::Gui(); gui->initialise(); // GUI element creation may commence here The GUI system needs to be initialized, and bound to the main Ogre objects it uses for display.

12 Let us create a progress bar!
//global MyGUI::ProgressPtr progressBar; // in setupScene() // GUI element creation may commence here progressBar = gui->createWidget<MyGUI::ProgressBar> ("ProgressBar",100,10,500,30,MyGUI::Align::Center,"Main"); // skin^ position^ progressBar->setEnabled(true); progressBar->setProgressRange(stopTimes[numStops-1]); progressBar->setProgressPosition(0);

13 Indicate progress in every frame!

14 Result This was pretty easy, but truth be told we could have achieved something similar with overlays, too. So let us make the next step a bit more interactive: a checkbox!

15 Add checkbox to toggle music on/off!
Add checkbox to toggle music on/off! MyGUI::ButtonPtr button = gui->createWidget<MyGUI::Button> ("CheckBox", 10, 40, 300, 26, MyGUI::Align::Default, "Main"); button->setCaption("Music"); There is no checkbox class in MyGUI… but there is a “CheckBox” skin for a Buttons. So we need to create a Button, with that skin, and manage the selection state ourselves.

16 Result

17 GameAudio.h: new method
GameAudio.h: new method void setMusicVolume(float musicVolume) { this->musicVolume = musicVolume; AL_SAFE_CALL( alSourcef (ambientSource, AL_GAIN, musicVolume), "unable to set ambient volume"); } We need to provide a way to change the volume of the music, so let us add a method to the GameAudio class which allows that. We need to pay attention to the type of the parameter to alSource: it is a float, so we must use alSourcef.

18 Global function void guiToggleMusic(MyGUI::Widget* _sender) {
Global function void guiToggleMusic(MyGUI::Widget* _sender) { MyGUI::ButtonPtr checkbox = _sender->castType<MyGUI::Button>(); if(checkbox->getStateSelected()) { checkbox->setStateSelected(false); gameAudio->setMusicVolume(0); } else { checkbox->setStateSelected(true); gameAudio->setMusicVolume(10); } This is the function that should get called when the “button” is clicked. We need to manage the selection state of the button. The “checkbox” skin displays a different texture (checked or empty) depending on the selection state. Also, we turn the music on and off by setting the volume.

19 Add event listener button->eventMouseButtonClick +=
Add event listener button->eventMouseButtonClick += MyGUI::newDelegate( guiToggleMusic); MyGUI uses delegates for handling events like the button-clicked-event. The += operator is overloaded to register such delegates. In our case, we register the global function guiToggleMusic, but MyGUI also allow the registration of static methods or even non-static methods (where the class instance also has to be given of course).

20 Result Clicking the checkbox should now mute or resume the music, while also toggling the tick mark.

21 The End Final project:

Download ppt "2012. 11. 8. GUI II. Ogre3D GUI with MyGUI."

Similar presentations

Ads by Google