O3dInputBridge.cpp

Go to the documentation of this file.
00001 /*
00002 SagaEngine library
00003 Copyright (c) 2002-2006 Skalden Studio AS
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to distribute the library under the terms of the 
00010 Q Public License version 1.0. Be sure to read and understand the license
00011 before using the library. It should be included here, or you may read it
00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl
00013 
00014 The original version of this library can be located at:
00015 http://www.sagaengine.com/
00016 
00017 Rune Myrland
00018 rune@skalden.com
00019 */
00020 
00021 
00022 #include "O3dPre.hpp"
00023 #include "O3dInputBridge.hpp"
00024 #include "o3d/area/O3dManager.hpp"
00025 #include "o3d/config/o3d_config.hpp"
00026 #include "o3d/input/InputHandler.hpp"
00027 #include "o3d/input/InputManager.hpp"
00028 #include "o3d/input/Console.hpp"
00029 #include "o3d/schema/O3dSchema.hpp"
00030 #include "sim/SimEngine.hpp"
00031 #include <OgreRenderWindow.h>
00032 #include <OgreCamera.h>
00033 
00034 using namespace se_core;
00035 
00036 
00037 namespace se_ogre {
00038 
00039     O3dInputBridge
00040     ::O3dInputBridge(Ogre::RenderWindow* win)
00041             : oldJoyButtons_(0), keyState_(0), isLeftShiftDown_(false), isRightShiftDown_(false) {
00042 
00043         // BEGIN INPUT INITIALIZATION
00044         bool bufferedKeys = true;
00045         bool bufferedMouse = true;
00046         bool bufferedJoy = true;
00047 
00048         size_t windowHnd = 0;
00049         /*
00050 #if defined OIS_WIN32_PLATFORM
00051         win->getCustomAttribute("HWND", &windowHnd);
00052 #elif defined OIS_LINUX_PLATFORM
00053         win->getCustomAttribute("GLXWINDOW", &windowHnd);
00054 #endif
00055         */
00056         win->getCustomAttribute("WINDOW", &windowHnd);
00057         //Both x11 and Win32 use handle of some sort..
00058         std::ostringstream windowHndStr;
00059         windowHndStr << windowHnd;
00060 
00061         OIS::ParamList pl;
00062         pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
00063 
00064         inputManager_ = OIS::InputManager::createInputSystem( pl );
00065         keyboard_ = static_cast<OIS::Keyboard*>(inputManager_->createInputObject( OIS::OISKeyboard, bufferedKeys ));
00066         keyboard_->setEventCallback(this);
00067         mouse_ = static_cast<OIS::Mouse*>(inputManager_->createInputObject( OIS::OISMouse, bufferedMouse ));
00068         mouse_->setEventCallback(this);
00069         try
00070         {
00071             joy_ = static_cast<OIS::JoyStick*>(inputManager_->createInputObject( OIS::OISJoyStick, bufferedJoy ));
00072             joy_->setBuffered(false);
00073             joy_->setEventCallback(this);
00074         }
00075         catch(OIS::Exception& e)
00076         {
00077             LogWarning(e.eText);
00078             joy_ = 0;
00079         }
00080     }
00081 
00082 
00083     O3dInputBridge
00084     ::~O3dInputBridge() {
00085         inputManager_->destroyInputObject( joy_ );
00086         //delete eventProcessor_;
00087     }
00088 
00089 
00090     void O3dInputBridge
00091     ::step() {
00092         if(joy_) {
00093             joy_->capture();
00094 
00095             int buttonCount = joy_->buttons();
00096             int buttons = 0;
00097             for(int i = 0; i < buttonCount; ++i) {
00098                 int mask = (1L << i);
00099                 if(joy_->getJoyStickState().buttonDown(i)) {
00100                     buttons |= mask;
00101                 }
00102                 if((buttons & mask) != (oldJoyButtons_ & mask)) {
00103                     if((buttons & mask) == 0) {
00104                         O3dSchema::inputManager().active()->joyButtonReleased(i);
00105                     }
00106                     else {
00107                         O3dSchema::inputManager().active()->joyButtonPressed(i);
00108                     }
00109                 }
00110 
00111             }
00112             oldJoyButtons_ = buttons;
00113 
00114             //Check to see if any of the axes values have changed.. if so send events
00115             for( int i = 0; i < 8; ++i )
00116             {
00117                 OIS::JoyStickEvent temp(joy_, joy_->getJoyStickState());
00118                 // TODO: Check - did move?
00119                 O3dSchema::inputManager().active()->axisMoved( temp, i );
00120                 // TODO: Check - did move?
00121                 O3dSchema::inputManager().active()->sliderMoved( temp, i );
00122             }
00123 
00124         }
00125         if(keyboard_) {
00126             keyboard_->capture();
00127         }
00128         if(mouse_) {
00129             mouse_->capture();
00130         }
00131     }
00132 
00133 
00134     bool O3dInputBridge
00135     ::keyPressed(const OIS::KeyEvent& e) {
00136         // Tab flips focus on console
00137         LogDetail("Key: " << e.key);
00138         switch(e.key) {
00139         case OIS::KC_HOME:
00140             if(O3dSchema::console) {
00141                 O3dSchema::console->flipFocus();
00142             }
00143             break;
00144         case OIS::KC_ESCAPE:
00145             SimSchema::simEngine.setGameOver(true);
00146             break;
00147         case OIS::KC_SCROLL:
00148             O3dSchema::worldManager->flipDebugOverlay();
00149             break;
00150         case OIS::KC_PAUSE:
00151             {
00152                 Ogre::PolygonMode mode = O3dSchema::playerCamera->getPolygonMode();
00153                 switch(mode) {
00154                 case Ogre::PM_SOLID:
00155                     mode = Ogre::PM_WIREFRAME;
00156                     break;
00157                 case Ogre::PM_WIREFRAME:
00158                     mode = Ogre::PM_SOLID;
00159                     break;
00160                 }
00161                 O3dSchema::playerCamera->setPolygonMode(mode);
00162             }
00163             break;
00164 
00165         case OIS::KC_LSHIFT:
00166             O3dSchema::inputManager().setModifier(InputManager::LSHIFT, true);
00167             break;
00168 
00169         case OIS::KC_RSHIFT:
00170             O3dSchema::inputManager().setModifier(InputManager::RSHIFT, true);
00171             break;
00172 
00173         case OIS::KC_RCONTROL:
00174             O3dSchema::inputManager().setModifier(InputManager::RCTRL, true);
00175             break;
00176 
00177         case OIS::KC_LCONTROL:
00178             O3dSchema::inputManager().setModifier(InputManager::LCTRL, true);
00179             break;
00180 
00181         case 56: // Alt
00182             O3dSchema::inputManager().setModifier(InputManager::ALT, true);
00183             break;
00184 
00185         case OIS::KC_LWIN:
00186             O3dSchema::inputManager().setModifier(InputManager::WIN, true);
00187             break;
00188         }
00189 
00190         // If console is focused, send input there
00191         bool isProcessed = false;
00192         if(O3dSchema::console && O3dSchema::console->isFocused()) {
00193             O3dSchema::console->keyPressed(&e);
00194             isProcessed = true;
00195         }
00196         // If not, send to game input handler
00197         else if(O3dSchema::inputManager().active()) {
00198             O3dSchema::inputManager().active()->keyPressed(&e);
00199             isProcessed = true;
00200         }
00201 
00202 
00203         return true;
00204     }
00205 
00206 
00207     bool O3dInputBridge
00208     ::keyReleased(const OIS::KeyEvent& e) {
00209         switch(e.key) {
00210         case OIS::KC_LSHIFT:
00211             O3dSchema::inputManager().setModifier(InputManager::LSHIFT, false);
00212             break;
00213 
00214         case OIS::KC_RSHIFT:
00215             O3dSchema::inputManager().setModifier(InputManager::RSHIFT, false);
00216 
00217         case OIS::KC_RCONTROL:
00218             O3dSchema::inputManager().setModifier(InputManager::RCTRL, false);
00219             break;
00220 
00221         case OIS::KC_LCONTROL:
00222             O3dSchema::inputManager().setModifier(InputManager::LCTRL, false);
00223             break;
00224 
00225         case 56: // Alt
00226             O3dSchema::inputManager().setModifier(InputManager::ALT, false);
00227             break;
00228 
00229         case OIS::KC_LWIN:
00230             O3dSchema::inputManager().setModifier(InputManager::WIN, false);
00231             break;
00232         }
00233 
00234         if(O3dSchema::inputManager().active()) {
00235             O3dSchema::inputManager().active()->keyReleased(&e);
00236         }
00237         return true;
00238     }
00239 
00240 
00241     bool O3dInputBridge
00242     ::mousePressed(const OIS::MouseEvent& e, OIS::MouseButtonID id) {
00243         // If console is focused, send input there
00244         if(O3dSchema::console && O3dSchema::console->isFocused()) {
00245             O3dSchema::console->mousePressed(&e, id);
00246         }
00247         else if(O3dSchema::inputManager().active()) {
00248             O3dSchema::inputManager().active()->mousePressed(&e, id);
00249         }
00250         return true;
00251     }
00252 
00253 
00254     bool O3dInputBridge
00255     ::mouseReleased(const OIS::MouseEvent& e, OIS::MouseButtonID id) {
00256         // If console is focused, send input there
00257         if(O3dSchema::console && O3dSchema::console->isFocused()) {
00258             O3dSchema::console->mouseReleased(&e, id);
00259         }
00260         else if(O3dSchema::inputManager().active()) {
00261             O3dSchema::inputManager().active()->mouseReleased(&e, id);
00262         }
00263         return true;
00264     }
00265 
00266     bool O3dInputBridge
00267     ::mouseMoved(const OIS::MouseEvent& e) {
00268         // If console is focused, send input there
00269         if(O3dSchema::console && O3dSchema::console->isFocused()) {
00270             O3dSchema::console->mouseMoved(&e);
00271         }
00272         else if(O3dSchema::inputManager().active()) {
00273             O3dSchema::inputManager().active()->mouseMoved(&e);
00274         }
00275         return true;
00276     }
00277 
00278 
00279     /*
00280     void O3dInputBridge
00281     ::mouseDragged(const OIS::MouseEvent* e) {
00282         // If console is focused, send input there
00283         if(O3dSchema::console && O3dSchema::console->isFocused()) {
00284             O3dSchema::console->mouseDragged(e);
00285         }
00286         else if(O3dSchema::inputManager().active()) {
00287             O3dSchema::inputManager().active()->mouseDragged(e);
00288         }
00289     }
00290     */
00291 
00292 
00293     bool O3dInputBridge
00294     ::buttonPressed (const OIS::JoyStickEvent &arg, int button) {
00295         O3dSchema::inputManager().active()->buttonPressed(arg, button);
00296         return true;
00297     }
00298 
00299     bool O3dInputBridge
00300     ::buttonReleased (const OIS::JoyStickEvent &arg, int button) {
00301         O3dSchema::inputManager().active()->buttonReleased(arg, button);
00302         return true;
00303     }
00304 
00305     bool O3dInputBridge
00306     ::axisMoved (const OIS::JoyStickEvent &arg, int axis) {
00307         O3dSchema::inputManager().active()->axisMoved(arg, axis);
00308         return true;
00309     }
00310 
00311     bool O3dInputBridge
00312     ::sliderMoved (const OIS::JoyStickEvent &arg, int axis) {
00313         O3dSchema::inputManager().active()->sliderMoved(arg, axis);
00314         return true;
00315     }
00316 
00317 
00318     bool O3dInputBridge
00319     ::povMoved (const OIS::JoyStickEvent &arg, int axis) {
00320         O3dSchema::inputManager().active()->sliderMoved(arg, axis);
00321         return true;
00322     }
00323 }

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:05 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo