[3274] | 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> |
---|
[3327] | 32 | #include "core/CoreIncludes.h" |
---|
[10624] | 33 | #include "core/command/ConsoleCommandIncludes.h" |
---|
[3274] | 34 | #include "InputState.h" |
---|
| 35 | |
---|
[3276] | 36 | #ifdef ORXONOX_PLATFORM_LINUX |
---|
[3327] | 37 | // include this as last, X11 seems to define some macros... |
---|
| 38 | #include <ois/linux/LinuxMouse.h> |
---|
[8729] | 39 | #undef Success |
---|
[3276] | 40 | #endif |
---|
| 41 | |
---|
[3274] | 42 | namespace orxonox |
---|
| 43 | { |
---|
[7284] | 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 | |
---|
[10624] | 53 | RegisterAbstractClass(Mouse).inheritsFrom<WindowEventListener>(); |
---|
| 54 | |
---|
[3327] | 55 | Mouse::Mouse(unsigned int id, OIS::InputManager* oisInputManager) |
---|
| 56 | : super(id, oisInputManager) |
---|
[3274] | 57 | { |
---|
[9667] | 58 | RegisterObject(Mouse); |
---|
[3327] | 59 | this->windowResized(this->getWindowWidth(), this->getWindowHeight()); |
---|
[3274] | 60 | |
---|
[3327] | 61 | #ifdef ORXONOX_PLATFORM_LINUX |
---|
[7284] | 62 | ModifyConsoleCommand(__CC_Mouse_name, __CC_grab_name).setObject(this); |
---|
| 63 | ModifyConsoleCommand(__CC_Mouse_name, __CC_ungrab_name).setObject(this); |
---|
[3327] | 64 | #endif |
---|
[3274] | 65 | } |
---|
| 66 | |
---|
[7284] | 67 | Mouse::~Mouse() |
---|
| 68 | { |
---|
| 69 | #ifdef ORXONOX_PLATFORM_LINUX |
---|
[11071] | 70 | ModifyConsoleCommand(__CC_Mouse_name, __CC_grab_name).setObject(nullptr); |
---|
| 71 | ModifyConsoleCommand(__CC_Mouse_name, __CC_ungrab_name).setObject(nullptr); |
---|
[7284] | 72 | #endif |
---|
| 73 | } |
---|
| 74 | |
---|
[3274] | 75 | //! OIS event handler |
---|
| 76 | bool Mouse::mouseMoved(const OIS::MouseEvent &e) |
---|
| 77 | { |
---|
| 78 | // check for actual moved event |
---|
| 79 | if (e.state.X.rel != 0 || e.state.Y.rel != 0) |
---|
| 80 | { |
---|
| 81 | IntVector2 abs(e.state.X.abs, e.state.Y.abs); |
---|
| 82 | IntVector2 rel(e.state.X.rel, e.state.Y.rel); |
---|
| 83 | IntVector2 clippingSize(e.state.width, e.state.height); |
---|
[11071] | 84 | for (InputState* state : inputStates_) |
---|
| 85 | state->mouseMoved(abs, rel, clippingSize); |
---|
[3274] | 86 | } |
---|
| 87 | |
---|
| 88 | // check for mouse scrolled event |
---|
| 89 | if (e.state.Z.rel != 0) |
---|
| 90 | { |
---|
[11071] | 91 | for (InputState* state : inputStates_) |
---|
| 92 | state->mouseScrolled(e.state.Z.abs, e.state.Z.rel); |
---|
[3274] | 93 | } |
---|
| 94 | |
---|
| 95 | return true; |
---|
| 96 | } |
---|
[3276] | 97 | |
---|
[3327] | 98 | void Mouse::windowResized(unsigned int newWidth, unsigned int newHeight) |
---|
| 99 | { |
---|
| 100 | oisDevice_->getMouseState().width = newWidth; |
---|
| 101 | oisDevice_->getMouseState().height = newHeight; |
---|
| 102 | } |
---|
[3276] | 103 | |
---|
| 104 | #ifdef ORXONOX_PLATFORM_LINUX |
---|
[3327] | 105 | void Mouse::grab() |
---|
[3276] | 106 | { |
---|
[3327] | 107 | OIS::LinuxMouse* linuxMouse = dynamic_cast<OIS::LinuxMouse*>(oisDevice_); |
---|
[3276] | 108 | assert(linuxMouse); |
---|
| 109 | linuxMouse->grab(true); |
---|
| 110 | } |
---|
| 111 | |
---|
[3327] | 112 | void Mouse::ungrab() |
---|
[3276] | 113 | { |
---|
[3327] | 114 | OIS::LinuxMouse* linuxMouse = dynamic_cast<OIS::LinuxMouse*>(oisDevice_); |
---|
[3276] | 115 | assert(linuxMouse); |
---|
| 116 | linuxMouse->grab(false); |
---|
| 117 | } |
---|
| 118 | #endif |
---|
[3274] | 119 | } |
---|