Full change for foolish misunderstanding.
[psg.git] / src / ui.cpp
blobccb09d65beec1f7e0b2f622fa29337d9b410a7d0
1 /*
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/>.
20 #include "ui.h"
21 #include <assert.h>
22 #include <exception>
23 #include <string>
24 #include <iostream>
25 #include <sstream>
27 //forward declaration
29 /**
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,
36 bool *cont,
37 PoolGame *game) :
38 mRoot(root),
39 mRenderer(renderer),
40 mSystem(system),
41 mWindowManager(wmgr),
42 mContinue(cont),
43 mGame(game)
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");
48 mPowerShoot = 0.5f;
53 /**
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()
59 return mPowerShoot;
62 /**
63 * This method switch current menu
65 bool UI::setScene (Scene sc) {
66 CEGUI::Window *tmp_window = NULL;
67 switch (sc)
69 case M_MAIN_MENU:
70 mSystem->setGUISheet(main_sheet);
71 break;
72 case M_DISABLED:
73 setScene(M_HUD_MENU);
74 break;
75 case M_IN_GAME_MENU:
76 mSystem->setGUISheet(in_game_sheet);
77 break;
78 case M_HUD_MENU:
79 mSystem->setGUISheet(hud_sheet);
81 //tmp_window = mWindowManager->getWindow("HUD/PowerSlider");
82 //tmp_window->deactivate();
83 break;
84 case M_SET_SHOT_POWER:
85 //THIS STATE IS UNUSED.
86 //tmp_window = mWindowManager->getWindow("HUD/PowerSlider");
87 //tmp_window->deactivate();
88 break;
89 default:
90 //throw Exception("Unknown scene at UI::setScene method");
91 break;
94 setEventHandlers(sc);
95 this->mCurrentScene = sc;
96 return true;
99 /**
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);
112 return true;
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)
121 mGame->restart();
122 setScene(M_MAIN_MENU);
123 return true;
127 * This is handler of click event. It simply quits our game
129 bool UI::quitButtonHandler(const CEGUI::EventArgs &event)
131 *mContinue = false;
132 return true;
136 * This is handler of click event. It's switch from main menu
137 * to game
139 bool UI::startButtonHandler(const CEGUI::EventArgs &event)
141 setScene(M_DISABLED);
142 return true;
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();
154 return true;
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)
210 switch (sc)
212 case M_MAIN_MENU:
213 add_quit_event_handler ("MainMenu/QuitButton");
214 add_start_event_handler ("MainMenu/StartButton");
215 break;
216 case M_IN_GAME_MENU:
217 add_quit_event_handler ("GameMenu/QuitButton");
218 add_restart_event_handler("GameMenu/RestartButton");
219 add_continue_event_handler("GameMenu/ContinueButton");
220 case M_DISABLED:
221 break;
222 case M_HUD_MENU:
223 add_slider_event_hander("HUD/PowerSlider");
225 break;
226 case M_SET_SHOT_POWER:
227 break;
228 default:
229 break;
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,
252 OIS::Mouse *mouse,
253 Ogre::SceneManager *mgr,
254 CEGUI::System *system,
255 bool *cont,
256 PoolGame *game) :
257 mMouse(mouse),
258 mKeyboard(keyboard),
259 mSystem(system),
260 mContinue(cont),
261 mGame(game)
263 assert(mouse != NULL);
264 assert(game != NULL);
267 mouse->setEventCallback(this);
268 last_player = -2;
269 last_color = -2;
273 * This is implementation of FrameListener::frameStarted method.
274 * It's check state of game from mGame member and set correct messages to
275 * 2d HUD
277 bool MenuFrameListener::frameStarted(const Ogre::FrameEvent &evt)
279 std::ostringstream osbuf (std::ostringstream::out);
280 bool change = false;
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);
289 if (last_color != 2)
291 osbuf << " " << "CURRENT BALL: " << (last_color == 1 ? "STRIPES" : "SOLID");
293 change = true;
296 if (mGame->gameEnded() == -1 && mGame->isWhiteBallMovable())
298 osbuf << " FAUL. PLEASE PLACE WHITE BALL.";
299 change = true;
302 if (mGame->gameEnded() != -1)
304 osbuf << "PLAYER " << mGame->gameEnded() + 1 << " WON";
305 change = true;
309 if (change)
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();
317 return *mContinue;
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);
327 return true;
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)
337 switch (buttonID)
339 case OIS::MB_Left:
340 return CEGUI::LeftButton;
342 case OIS::MB_Right:
343 return CEGUI::RightButton;
345 case OIS::MB_Middle:
346 return CEGUI::MiddleButton;
348 default:
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));
362 return true;
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));
373 return true;