[918] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1056] | 3 | * > www.orxonox.net < |
---|
[918] | 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 | */ |
---|
[973] | 28 | |
---|
[918] | 29 | /** |
---|
| 30 | @file |
---|
| 31 | @brief Implementation of a little Input handler that distributes everything |
---|
| 32 | coming from OIS. |
---|
| 33 | */ |
---|
[973] | 34 | |
---|
| 35 | #ifndef _InputManager_H__ |
---|
| 36 | #define _InputManager_H__ |
---|
| 37 | |
---|
[1062] | 38 | #include "CorePrereqs.h" |
---|
| 39 | |
---|
[1219] | 40 | #include <map> |
---|
[1293] | 41 | #include <vector> |
---|
[973] | 42 | |
---|
[1219] | 43 | #include "ois/OIS.h" |
---|
[1349] | 44 | #include "util/Math.h" |
---|
[1062] | 45 | #include "Tickable.h" |
---|
[1293] | 46 | #include "InputInterfaces.h" |
---|
[973] | 47 | |
---|
| 48 | namespace orxonox |
---|
| 49 | { |
---|
| 50 | /** |
---|
[1349] | 51 | * Helper class to realise a vector<int[4]> |
---|
| 52 | */ |
---|
| 53 | class POVStates |
---|
| 54 | { |
---|
| 55 | public: |
---|
| 56 | int operator[](unsigned int index) { return povStates[index]; } |
---|
| 57 | int povStates[4]; |
---|
| 58 | }; |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Helper class to realise a vector< {int[4], int[4]} > |
---|
| 62 | */ |
---|
| 63 | class SliderStates |
---|
| 64 | { |
---|
| 65 | public: |
---|
| 66 | IntVector2 sliderStates[4]; |
---|
| 67 | }; |
---|
| 68 | |
---|
| 69 | /** |
---|
[973] | 70 | @brief Captures and distributes mouse and keyboard input. |
---|
| 71 | */ |
---|
| 72 | class _CoreExport InputManager |
---|
[1293] | 73 | : public TickableReal, |
---|
[1219] | 74 | public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener |
---|
[973] | 75 | { |
---|
[1219] | 76 | public: // enumerations |
---|
| 77 | /** |
---|
| 78 | @brief Designates the way input is handled and redirected. |
---|
| 79 | */ |
---|
| 80 | enum InputState |
---|
| 81 | { |
---|
| 82 | IS_UNINIT, //!< InputManager has not yet been initialised. |
---|
| 83 | IS_NONE, //!< Input is discarded. |
---|
| 84 | IS_NORMAL, //!< Normal play state. Key and button bindings are active. |
---|
| 85 | IS_GUI, //!< All OIS input events are passed to CEGUI. |
---|
| 86 | IS_CONSOLE, //!< Keyboard input is redirected to the InputBuffer. |
---|
| 87 | IS_CUSTOM //!< Any possible configuration. |
---|
| 88 | }; |
---|
[973] | 89 | |
---|
[1219] | 90 | public: // static functions |
---|
[1293] | 91 | static bool initialise(const size_t windowHnd, int windowWidth, int windowHeight, |
---|
| 92 | bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false); |
---|
[1219] | 93 | static bool initialiseKeyboard(); |
---|
| 94 | static bool initialiseMouse(); |
---|
| 95 | static bool initialiseJoySticks(); |
---|
| 96 | static int numberOfKeyboards(); |
---|
| 97 | static int numberOfMice(); |
---|
| 98 | static int numberOfJoySticks(); |
---|
[1066] | 99 | |
---|
[1219] | 100 | static void destroy(); |
---|
| 101 | static void destroyKeyboard(); |
---|
| 102 | static void destroyMouse(); |
---|
| 103 | static void destroyJoySticks(); |
---|
| 104 | |
---|
[1293] | 105 | static bool isModifierDown(KeyboardModifier::Enum modifier); |
---|
| 106 | static bool isKeyDown(KeyCode::Enum key); |
---|
[1349] | 107 | //static const MouseState getMouseState(); |
---|
| 108 | //static const JoyStickState getJoyStickState(unsigned int ID); |
---|
[1293] | 109 | |
---|
[1219] | 110 | static void setWindowExtents(const int width, const int height); |
---|
| 111 | |
---|
| 112 | static void setInputState(const InputState state); |
---|
| 113 | static InputState getInputState(); |
---|
| 114 | |
---|
| 115 | static bool addKeyHandler (KeyHandler* handler, const std::string& name); |
---|
| 116 | static bool removeKeyHandler (const std::string& name); |
---|
| 117 | static KeyHandler* getKeyHandler (const std::string& name); |
---|
| 118 | static bool enableKeyHandler (const std::string& name); |
---|
| 119 | static bool disableKeyHandler (const std::string& name); |
---|
| 120 | static bool isKeyHandlerActive (const std::string& name); |
---|
| 121 | |
---|
| 122 | static bool addMouseHandler (MouseHandler* handler, const std::string& name); |
---|
| 123 | static bool removeMouseHandler (const std::string& name); |
---|
| 124 | static MouseHandler* getMouseHandler (const std::string& name); |
---|
| 125 | static bool enableMouseHandler (const std::string& name); |
---|
| 126 | static bool disableMouseHandler (const std::string& name); |
---|
| 127 | static bool isMouseHandlerActive (const std::string& name); |
---|
| 128 | |
---|
| 129 | static bool addJoyStickHandler (JoyStickHandler* handler, const std::string& name); |
---|
| 130 | static bool removeJoyStickHandler (const std::string& name); |
---|
| 131 | static JoyStickHandler* getJoyStickHandler(const std::string& name); |
---|
[1293] | 132 | static bool enableJoyStickHandler (const std::string& name, unsigned int id); |
---|
| 133 | static bool disableJoyStickHandler (const std::string& name, unsigned int id); |
---|
| 134 | static bool isJoyStickHandlerActive (const std::string& name, unsigned int id); |
---|
[1219] | 135 | |
---|
| 136 | private: // functions |
---|
[973] | 137 | // don't mess with a Singleton |
---|
| 138 | InputManager (); |
---|
| 139 | InputManager (const InputManager&); |
---|
| 140 | ~InputManager(); |
---|
| 141 | |
---|
[1219] | 142 | // Intenal methods |
---|
[1293] | 143 | bool _initialise(const size_t, int, int, bool, bool, bool); |
---|
[1219] | 144 | bool _initialiseKeyboard(); |
---|
| 145 | bool _initialiseMouse(); |
---|
| 146 | bool _initialiseJoySticks(); |
---|
[973] | 147 | |
---|
[1219] | 148 | void _destroy(); |
---|
| 149 | void _destroyKeyboard(); |
---|
| 150 | void _destroyMouse(); |
---|
| 151 | void _destroyJoySticks(); |
---|
[973] | 152 | |
---|
[1349] | 153 | void _updateTickables(); |
---|
| 154 | |
---|
[1219] | 155 | void tick(float dt); |
---|
| 156 | |
---|
| 157 | // input events |
---|
[1293] | 158 | bool mousePressed (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
| 159 | bool mouseReleased (const OIS::MouseEvent &arg, OIS::MouseButtonID id); |
---|
[1219] | 160 | bool mouseMoved (const OIS::MouseEvent &arg); |
---|
[1293] | 161 | bool keyPressed (const OIS::KeyEvent &arg); |
---|
| 162 | bool keyReleased (const OIS::KeyEvent &arg); |
---|
| 163 | bool buttonPressed (const OIS::JoyStickEvent &arg, int button); |
---|
| 164 | bool buttonReleased(const OIS::JoyStickEvent &arg, int button); |
---|
| 165 | bool axisMoved (const OIS::JoyStickEvent &arg, int axis); |
---|
| 166 | bool sliderMoved (const OIS::JoyStickEvent &arg, int id); |
---|
| 167 | bool povMoved (const OIS::JoyStickEvent &arg, int id); |
---|
[1349] | 168 | //bool vector3Moved (const OIS::JoyStickEvent &arg, int id); |
---|
[1219] | 169 | |
---|
| 170 | static InputManager& _getSingleton(); |
---|
| 171 | static InputManager* _getSingletonPtr() { return &_getSingleton(); } |
---|
| 172 | |
---|
| 173 | private: // variables |
---|
| 174 | OIS::InputManager* inputSystem_; //!< OIS input manager |
---|
| 175 | OIS::Keyboard* keyboard_; //!< OIS mouse |
---|
| 176 | OIS::Mouse* mouse_; //!< OIS keyboard |
---|
| 177 | std::vector<OIS::JoyStick*> joySticks_; //!< OIS joy sticks |
---|
[1293] | 178 | unsigned int joySticksSize_; |
---|
[1219] | 179 | |
---|
| 180 | InputState state_; |
---|
| 181 | InputState stateRequest_; |
---|
[1293] | 182 | unsigned int keyboardModifiers_; |
---|
[1219] | 183 | |
---|
[1349] | 184 | //! Keeps track of the joy stick POV states |
---|
| 185 | std::vector<POVStates> povStates_; |
---|
| 186 | //! Keeps track of the possibly two slider axes |
---|
| 187 | std::vector<SliderStates> sliderStates_; |
---|
| 188 | |
---|
[1219] | 189 | std::map<std::string, KeyHandler*> keyHandlers_; |
---|
| 190 | std::map<std::string, MouseHandler*> mouseHandlers_; |
---|
| 191 | std::map<std::string, JoyStickHandler*> joyStickHandlers_; |
---|
| 192 | |
---|
| 193 | std::vector<KeyHandler*> activeKeyHandlers_; |
---|
| 194 | std::vector<MouseHandler*> activeMouseHandlers_; |
---|
[1293] | 195 | std::vector<std::vector<JoyStickHandler*> > activeJoyStickHandlers_; |
---|
[1349] | 196 | std::vector<InputTickable*> activeHandlers_; |
---|
[1219] | 197 | |
---|
[1293] | 198 | std::vector<Key> keysDown_; |
---|
| 199 | std::vector<MouseButton::Enum> mouseButtonsDown_; |
---|
| 200 | std::vector<std::vector<int> > joyStickButtonsDown_; |
---|
[1219] | 201 | |
---|
[973] | 202 | }; |
---|
[1293] | 203 | |
---|
[973] | 204 | } |
---|
| 205 | |
---|
| 206 | #endif /* _InputManager_H__ */ |
---|