| 1 | /* | 
|---|
| 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 |  *                    > www.orxonox.net < | 
|---|
| 4 |  * | 
|---|
| 5 |  * | 
|---|
| 6 |  *   License notice: | 
|---|
| 7 |  * | 
|---|
| 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
| 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
| 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 |  *   of the License, or (at your option) any later version. | 
|---|
| 12 |  * | 
|---|
| 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 |  *   GNU General Public License for more details. | 
|---|
| 17 |  * | 
|---|
| 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
| 19 |  *   along with this program; if not, write to the Free Software | 
|---|
| 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 |  * | 
|---|
| 22 |  *   Author: | 
|---|
| 23 |  *      Reto Grieder | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "Mouse.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include <ois/OISMouse.h> | 
|---|
| 32 | #include "core/CoreIncludes.h" | 
|---|
| 33 | #include "core/command/ConsoleCommand.h" | 
|---|
| 34 | #include "InputState.h" | 
|---|
| 35 |  | 
|---|
| 36 | #ifdef ORXONOX_PLATFORM_LINUX | 
|---|
| 37 | // include this as last, X11 seems to define some macros... | 
|---|
| 38 | #include <ois/linux/LinuxMouse.h> | 
|---|
| 39 | #undef Success | 
|---|
| 40 | #endif | 
|---|
| 41 |  | 
|---|
| 42 | namespace orxonox | 
|---|
| 43 | { | 
|---|
| 44 | #ifdef ORXONOX_PLATFORM_LINUX | 
|---|
| 45 |     static const std::string __CC_Mouse_name = "Mouse"; | 
|---|
| 46 |     static const std::string __CC_grab_name = "grab"; | 
|---|
| 47 |     static const std::string __CC_ungrab_name = "ungrab"; | 
|---|
| 48 |  | 
|---|
| 49 |     SetConsoleCommand(__CC_Mouse_name, __CC_grab_name,   &Mouse::grab); | 
|---|
| 50 |     SetConsoleCommand(__CC_Mouse_name, __CC_ungrab_name, &Mouse::ungrab); | 
|---|
| 51 | #endif | 
|---|
| 52 |  | 
|---|
| 53 |     Mouse::Mouse(unsigned int id, OIS::InputManager* oisInputManager) | 
|---|
| 54 |         : super(id, oisInputManager) | 
|---|
| 55 |     { | 
|---|
| 56 |         RegisterObject(Mouse); | 
|---|
| 57 |         this->windowResized(this->getWindowWidth(), this->getWindowHeight()); | 
|---|
| 58 |  | 
|---|
| 59 | #ifdef ORXONOX_PLATFORM_LINUX | 
|---|
| 60 |         ModifyConsoleCommand(__CC_Mouse_name, __CC_grab_name).setObject(this); | 
|---|
| 61 |         ModifyConsoleCommand(__CC_Mouse_name, __CC_ungrab_name).setObject(this); | 
|---|
| 62 | #endif | 
|---|
| 63 |     } | 
|---|
| 64 |  | 
|---|
| 65 |     Mouse::~Mouse() | 
|---|
| 66 |     { | 
|---|
| 67 | #ifdef ORXONOX_PLATFORM_LINUX | 
|---|
| 68 |         ModifyConsoleCommand(__CC_Mouse_name, __CC_grab_name).setObject(0); | 
|---|
| 69 |         ModifyConsoleCommand(__CC_Mouse_name, __CC_ungrab_name).setObject(0); | 
|---|
| 70 | #endif | 
|---|
| 71 |     } | 
|---|
| 72 |  | 
|---|
| 73 |     //! OIS event handler | 
|---|
| 74 |     bool Mouse::mouseMoved(const OIS::MouseEvent &e) | 
|---|
| 75 |     { | 
|---|
| 76 |         // check for actual moved event | 
|---|
| 77 |         if (e.state.X.rel != 0 || e.state.Y.rel != 0) | 
|---|
| 78 |         { | 
|---|
| 79 |             IntVector2 abs(e.state.X.abs, e.state.Y.abs); | 
|---|
| 80 |             IntVector2 rel(e.state.X.rel, e.state.Y.rel); | 
|---|
| 81 |             IntVector2 clippingSize(e.state.width, e.state.height); | 
|---|
| 82 |             for (unsigned int i = 0; i < inputStates_.size(); ++i) | 
|---|
| 83 |                 inputStates_[i]->mouseMoved(abs, rel, clippingSize); | 
|---|
| 84 |         } | 
|---|
| 85 |  | 
|---|
| 86 |         // check for mouse scrolled event | 
|---|
| 87 |         if (e.state.Z.rel != 0) | 
|---|
| 88 |         { | 
|---|
| 89 |             for (unsigned int i = 0; i < inputStates_.size(); ++i) | 
|---|
| 90 |                 inputStates_[i]->mouseScrolled(e.state.Z.abs, e.state.Z.rel); | 
|---|
| 91 |         } | 
|---|
| 92 |  | 
|---|
| 93 |         return true; | 
|---|
| 94 |     } | 
|---|
| 95 |  | 
|---|
| 96 |     void Mouse::windowResized(unsigned int newWidth, unsigned int newHeight) | 
|---|
| 97 |     { | 
|---|
| 98 |         oisDevice_->getMouseState().width  = newWidth; | 
|---|
| 99 |         oisDevice_->getMouseState().height = newHeight; | 
|---|
| 100 |     } | 
|---|
| 101 |  | 
|---|
| 102 | #ifdef ORXONOX_PLATFORM_LINUX | 
|---|
| 103 |     void Mouse::grab() | 
|---|
| 104 |     { | 
|---|
| 105 |         OIS::LinuxMouse* linuxMouse = dynamic_cast<OIS::LinuxMouse*>(oisDevice_); | 
|---|
| 106 |         assert(linuxMouse); | 
|---|
| 107 |         linuxMouse->grab(true); | 
|---|
| 108 |     } | 
|---|
| 109 |  | 
|---|
| 110 |     void Mouse::ungrab() | 
|---|
| 111 |     { | 
|---|
| 112 |         OIS::LinuxMouse* linuxMouse = dynamic_cast<OIS::LinuxMouse*>(oisDevice_); | 
|---|
| 113 |         assert(linuxMouse); | 
|---|
| 114 |         linuxMouse->grab(false); | 
|---|
| 115 |     } | 
|---|
| 116 | #endif | 
|---|
| 117 | } | 
|---|