2 * Copyright 2008 Jacek Caban
3 * Copyright 2008 Piotr Caban
4 * Copyright 2008 Jaroslaw Sobiecki
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 * This is constructor of UI class
32 UI::UI(Ogre::Root
*&root
,
33 CEGUI::OgreCEGUIRenderer
*renderer
,
34 CEGUI::System
*system
,
35 CEGUI::WindowManager
*wmgr
,
45 main_sheet
= mWindowManager
->loadWindowLayout((CEGUI::utf8
*) "main_menu.layout");
46 hud_sheet
= mWindowManager
->loadWindowLayout((CEGUI::utf8
*) "game_hud.layout");
47 in_game_sheet
= mWindowManager
->loadWindowLayout((CEGUI::utf8
*) "game_menu.layout");
54 * This method returns current value of slider which represents power of
55 * shoot. It's real value betwen 0 and 1.
57 Ogre::Real
UI::getPowerShot()
63 * This method switch current menu
65 bool UI::setScene (Scene sc
) {
66 CEGUI::Window
*tmp_window
= NULL
;
70 mSystem
->setGUISheet(main_sheet
);
76 mSystem
->setGUISheet(in_game_sheet
);
79 mSystem
->setGUISheet(hud_sheet
);
81 //tmp_window = mWindowManager->getWindow("HUD/PowerSlider");
82 //tmp_window->deactivate();
84 case M_SET_SHOT_POWER
:
85 //THIS STATE IS UNUSED.
86 //tmp_window = mWindowManager->getWindow("HUD/PowerSlider");
87 //tmp_window->deactivate();
90 //throw Exception("Unknown scene at UI::setScene method");
95 this->mCurrentScene
= sc
;
100 * This method returns currently choosen menu
102 Scene
UI::getScene() {
103 return mCurrentScene
;
107 * This is handler of click event. It simply switch from menu to game
109 bool UI::continueButtonHandler(const CEGUI::EventArgs
&event
)
111 setScene(M_DISABLED
);
116 * This is handler of click event. It resets state of game in order
117 * to restart our gameplay.
119 bool UI::restartButtonHandler(const CEGUI::EventArgs
&event
)
122 setScene(M_MAIN_MENU
);
127 * This is handler of click event. It simply quits our game
129 bool UI::quitButtonHandler(const CEGUI::EventArgs
&event
)
136 * This is handler of click event. It's switch from main menu
139 bool UI::startButtonHandler(const CEGUI::EventArgs
&event
)
141 setScene(M_DISABLED
);
146 * This is handler of slider value change. It assign current
147 * value of slider to member mPowerShoot
149 bool UI::sliderButtonHandler(const CEGUI::EventArgs
&event
)
151 const CEGUI::WindowEventArgs
*arg
= dynamic_cast<const CEGUI::WindowEventArgs
*> (&event
);
152 CEGUI::Slider
*slider
= (CEGUI::Slider
*) arg
->window
;
153 mPowerShoot
= slider
->getCurrentValue();
159 * This method assign event handler to CEGUI event, connected with
160 * widget (window) named window_name. This method
161 * assign event of value changing to sliderButtonHandler handler
163 void UI::add_slider_event_hander(const CEGUI::String window_name
)
165 mWindowManager
->getWindow(window_name
)->subscribeEvent(CEGUI::Slider::EventValueChanged
,
166 CEGUI::Event::Subscriber(&UI::sliderButtonHandler
, this));
170 * This method assign event handler to CEGUI event, connected with
171 * widget (window) named window_name. This method
172 * assign event of button click to startButtonHandler handler
174 void UI::add_start_event_handler(const CEGUI::String window_name
)
176 mWindowManager
->getWindow(window_name
)->subscribeEvent(CEGUI::Window::EventMouseClick
,
177 CEGUI::Event::Subscriber(&UI::startButtonHandler
, this));
182 * This method assign event handler to CEGUI event, connected with
183 * widget (window) named window_name. This method
184 * assign event of button click to restartButtonHandler handler
186 void UI::add_restart_event_handler(const CEGUI::String window_name
)
188 mWindowManager
->getWindow(window_name
)->subscribeEvent(CEGUI::Window::EventMouseClick
,
189 CEGUI::Event::Subscriber(&UI::restartButtonHandler
, this));
193 * This method assign event handler to CEGUI event, connected with
194 * widget (window) named window_name. This method
195 * assign event of button click to continueButtonHandler handler
197 void UI::add_continue_event_handler(const CEGUI::String window_name
)
199 mWindowManager
->getWindow(window_name
)->subscribeEvent(CEGUI::Window::EventMouseClick
,
200 CEGUI::Event::Subscriber(&UI::continueButtonHandler
, this));
205 * This method assign various event handlers to
206 * various windows. It's based on currently chossen scene
208 void UI::setEventHandlers (Scene sc
)
213 add_quit_event_handler ("MainMenu/QuitButton");
214 add_start_event_handler ("MainMenu/StartButton");
217 add_quit_event_handler ("GameMenu/QuitButton");
218 add_restart_event_handler("GameMenu/RestartButton");
219 add_continue_event_handler("GameMenu/ContinueButton");
223 add_slider_event_hander("HUD/PowerSlider");
226 case M_SET_SHOT_POWER
:
236 * This method assign event handler to CEGUI event, connected with
237 * widget (window) named window_name. This method
238 * assign event of button click to quitButtonHandler handler
240 void UI::add_quit_event_handler (const CEGUI::String window_name
)
242 mWindowManager
->getWindow(window_name
)->subscribeEvent(CEGUI::Window::EventMouseClick
,
243 CEGUI::Event::Subscriber(&UI::quitButtonHandler
, this));
249 * This is MenuFrameListener constructor
251 MenuFrameListener::MenuFrameListener(OIS::Keyboard
*keyboard
,
253 Ogre::SceneManager
*mgr
,
254 CEGUI::System
*system
,
263 assert(mouse
!= NULL
);
264 assert(game
!= NULL
);
267 mouse
->setEventCallback(this);
273 * This is implementation of FrameListener::frameStarted method.
274 * It's check state of game from mGame member and set correct messages to
277 bool MenuFrameListener::frameStarted(const Ogre::FrameEvent
&evt
)
279 std::ostringstream
osbuf (std::ostringstream::out
);
282 last_player
= mGame
->currentPlayer();
283 last_color
= mGame
->currentColor();
285 if (last_player
!= -1 && mGame
->gameEnded() == -1)
287 osbuf
<< "CURRENT PLAYER: " << (last_player
+ 1);
291 osbuf
<< " " << "CURRENT BALL: " << (last_color
== 1 ? "STRIPES" : "SOLID");
296 if (mGame
->gameEnded() == -1 && mGame
->isWhiteBallMovable())
298 osbuf
<< " FAUL. PLEASE PLACE WHITE BALL.";
302 if (mGame
->gameEnded() != -1)
304 osbuf
<< "PLAYER " << mGame
->gameEnded() + 1 << " WON";
311 CEGUI::Window
*window
= CEGUI::WindowManager::getSingletonPtr()->getWindow("HUD/CurrentPlayerBallLabel");
312 window
->setProperty("Text", osbuf
.str());
315 if (mMouse
) mMouse
->capture();
316 if (mKeyboard
) mKeyboard
->capture();
321 * This is simple implementation of MouseListener::mouseMoved method. It's
322 * translates OIS mouse move event to CEGUI mouse move event
324 bool MenuFrameListener::mouseMoved(const OIS::MouseEvent
&arg
)
326 CEGUI::System::getSingleton().injectMousePosition(arg
.state
.X
.abs
*2, arg
.state
.Y
.abs
*2);
331 * This method translate OIS mousebutton id to CEGUI mousebutton id
332 * This code was taken from
333 * http://www.ogre3d.org/wiki/index.php/Basic_Tutorial_7#Converting_and_Injecting_Mouse_Events
335 CEGUI::MouseButton
MenuFrameListener::convertButton(OIS::MouseButtonID buttonID
)
340 return CEGUI::LeftButton
;
343 return CEGUI::RightButton
;
346 return CEGUI::MiddleButton
;
349 return CEGUI::LeftButton
;
355 * This method is implementation of MouseListener::mousePressed
356 * It's translates mousePressed OIS event to CEGUI event
358 bool MenuFrameListener::mousePressed(const OIS::MouseEvent
&arg
,
359 OIS::MouseButtonID id
)
361 CEGUI::System::getSingleton().injectMouseButtonDown(convertButton(id
));
366 * This method is implementation of MouseListener::mousePressed
367 * It's translates mouseRelased OIS event to CEGUI event
369 bool MenuFrameListener::mouseReleased(const OIS::MouseEvent
&arg
,
370 OIS::MouseButtonID id
)
372 CEGUI::System::getSingleton().injectMouseButtonUp(convertButton(id
));