[971] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1349] | 3 | * > www.orxonox.net < |
---|
[971] | 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 | |
---|
[971] | 29 | /** |
---|
[1755] | 30 | @file |
---|
| 31 | @brief |
---|
| 32 | Different definitions of input processing. |
---|
| 33 | */ |
---|
[973] | 34 | |
---|
[1413] | 35 | #ifndef _KeyBinder_H__ |
---|
| 36 | #define _KeyBinder_H__ |
---|
[973] | 37 | |
---|
[3327] | 38 | #include "InputPrereqs.h" |
---|
[1062] | 39 | |
---|
[3196] | 40 | #include <cassert> |
---|
| 41 | #include <string> |
---|
[1349] | 42 | #include <vector> |
---|
[2662] | 43 | |
---|
[3327] | 44 | #include "InputHandler.h" |
---|
[1520] | 45 | #include "Button.h" |
---|
| 46 | #include "HalfAxis.h" |
---|
[1887] | 47 | #include "InputCommands.h" |
---|
[3327] | 48 | #include "JoyStickQuantityListener.h" |
---|
[973] | 49 | |
---|
| 50 | namespace orxonox |
---|
| 51 | { |
---|
[1755] | 52 | /** |
---|
| 53 | @brief |
---|
| 54 | Handles mouse, keyboard and joy stick input while in the actual game mode. |
---|
| 55 | Manages the key bindings. |
---|
| 56 | */ |
---|
[3327] | 57 | class _CoreExport KeyBinder : public InputHandler, public JoyStickQuantityListener |
---|
[1755] | 58 | { |
---|
| 59 | public: |
---|
| 60 | KeyBinder (); |
---|
| 61 | virtual ~KeyBinder(); |
---|
[1022] | 62 | |
---|
[2710] | 63 | void loadBindings(const std::string& filename); |
---|
[1755] | 64 | void clearBindings(); |
---|
[1887] | 65 | bool setBinding(const std::string& binding, const std::string& name, bool bTemporary = false); |
---|
[1755] | 66 | void setConfigValues(); |
---|
| 67 | void resetJoyStickAxes(); |
---|
[1349] | 68 | |
---|
[1755] | 69 | protected: // functions |
---|
[3327] | 70 | void allDevicesUpdated(float dt); |
---|
| 71 | void mouseUpdated(float dt); |
---|
| 72 | void joyStickUpdated(unsigned int joyStick, float dt); |
---|
[1887] | 73 | // internal |
---|
[2087] | 74 | void tickHalfAxis(HalfAxis& halfAxis); |
---|
[1413] | 75 | |
---|
[1887] | 76 | void buttonThresholdChanged(); |
---|
[3327] | 77 | // from JoyStickQuantityListener interface |
---|
| 78 | virtual void JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList); |
---|
[1887] | 79 | void initialiseJoyStickBindings(); |
---|
| 80 | void compilePointerLists(); |
---|
[1219] | 81 | |
---|
[3327] | 82 | void buttonPressed (const KeyEvent& evt); |
---|
| 83 | void buttonReleased(const KeyEvent& evt); |
---|
| 84 | void buttonHeld (const KeyEvent& evt); |
---|
[1022] | 85 | |
---|
[3327] | 86 | void buttonPressed (MouseButtonCode::ByEnum button); |
---|
| 87 | void buttonReleased(MouseButtonCode::ByEnum button); |
---|
| 88 | void buttonHeld (MouseButtonCode::ByEnum button); |
---|
| 89 | void mouseMoved (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize); |
---|
| 90 | void mouseScrolled (int abs, int rel); |
---|
[1022] | 91 | |
---|
[3327] | 92 | void buttonPressed (unsigned int device, JoyStickButtonCode::ByEnum button); |
---|
| 93 | void buttonReleased(unsigned int device, JoyStickButtonCode::ByEnum button); |
---|
| 94 | void buttonHeld (unsigned int device, JoyStickButtonCode::ByEnum button); |
---|
| 95 | void axisMoved (unsigned int device, unsigned int axis, float value); |
---|
[1349] | 96 | |
---|
[1755] | 97 | protected: // variables |
---|
[1887] | 98 | //! Currently active joy sticks |
---|
[3327] | 99 | std::vector<JoyStick*> joySticks_; |
---|
[1022] | 100 | |
---|
[1887] | 101 | //! Actual key bindings for keys on the keyboard |
---|
| 102 | Button keys_ [KeyCode::numberOfKeys]; |
---|
| 103 | //! Number of mouse buttons in KeyBinder (+4) |
---|
[1888] | 104 | static const unsigned int numberOfMouseButtons_ = MouseButtonCode::numberOfButtons + 4; |
---|
[1887] | 105 | //! Actual key bindings for mouse buttons including the wheel(s) |
---|
| 106 | Button mouseButtons_ [numberOfMouseButtons_]; |
---|
| 107 | //! Actual key bindings for mouse axes |
---|
| 108 | HalfAxis mouseAxes_ [MouseAxisCode::numberOfAxes * 2]; |
---|
[1219] | 109 | |
---|
[1887] | 110 | //! Helper class to use something like std:vector<Button[64]> |
---|
| 111 | struct JoyStickButtonVector |
---|
| 112 | { |
---|
| 113 | Button& operator[](unsigned int index) { return buttons[index]; } |
---|
| 114 | Button buttons[JoyStickButtonCode::numberOfButtons]; |
---|
| 115 | }; |
---|
| 116 | //! Actual key bindings for joy stick buttons |
---|
| 117 | std::vector<JoyStickButtonVector> joyStickButtons_; |
---|
| 118 | //! Helper class to use something like std:vector<HalfAxis[48]> |
---|
| 119 | struct JoyStickAxisVector |
---|
| 120 | { |
---|
| 121 | HalfAxis& operator[](unsigned int index) { return halfAxes[index]; } |
---|
| 122 | HalfAxis halfAxes[JoyStickAxisCode::numberOfAxes * 2]; |
---|
| 123 | }; |
---|
| 124 | //! Actual key bindings for joy stick axes (and sliders) |
---|
| 125 | std::vector<JoyStickAxisVector> joyStickAxes_; |
---|
[1219] | 126 | |
---|
[1887] | 127 | //! Pointer map with all Buttons, including half axes |
---|
| 128 | std::map<std::string, Button*> allButtons_; |
---|
| 129 | //! Pointer list with all half axes |
---|
| 130 | std::vector<HalfAxis*> allHalfAxes_; |
---|
[1349] | 131 | |
---|
[1755] | 132 | /** |
---|
| 133 | @brief |
---|
| 134 | Commands that have additional parameters (axes) are executed at the end of |
---|
[2896] | 135 | update() so that all values can be buffered for single execution. |
---|
[1755] | 136 | */ |
---|
| 137 | std::vector<BufferedParamCommand*> paramCommandBuffer_; |
---|
[1349] | 138 | |
---|
[1755] | 139 | //! Keeps track of the absolute mouse value (incl. scroll wheel) |
---|
| 140 | int mousePosition_[2]; |
---|
| 141 | //! Used to derive mouse input if requested |
---|
| 142 | int mouseRelative_[2]; |
---|
| 143 | float deriveTime_; |
---|
[1349] | 144 | |
---|
[2103] | 145 | //! Config file used. ConfigFileType::NoType in case of KeyDetector. Also indicates whether we've already loaded. |
---|
| 146 | ConfigFileType configFile_; |
---|
[1755] | 147 | |
---|
[1887] | 148 | private: |
---|
[1755] | 149 | //##### ConfigValues ##### |
---|
[2087] | 150 | //! Whether to filter small value analog input |
---|
| 151 | bool bFilterAnalogNoise_; |
---|
[1755] | 152 | //! Threshold for analog triggers until which the state is 0. |
---|
| 153 | float analogThreshold_; |
---|
| 154 | //! Threshold for analog triggers until which the button is not pressed. |
---|
| 155 | float buttonThreshold_; |
---|
| 156 | //! Derive mouse input for absolute values? |
---|
| 157 | bool bDeriveMouseInput_; |
---|
| 158 | //! Accuracy of the mouse input deriver. The higher the more precise, but laggier. |
---|
| 159 | float derivePeriod_; |
---|
| 160 | //! mouse sensitivity |
---|
| 161 | float mouseSensitivity_; |
---|
| 162 | //! mouse sensitivity if mouse input is derived |
---|
| 163 | float mouseSensitivityDerived_; |
---|
[1887] | 164 | //! Equals one step of the mousewheel |
---|
[2087] | 165 | int mouseWheelStepSize_; |
---|
[1887] | 166 | |
---|
| 167 | //##### Constant config variables ##### |
---|
| 168 | // Use some value at about 1000. This can be configured with mouseSensitivity_ anyway. |
---|
| 169 | static const int mouseClippingSize_ = 1024; |
---|
[1755] | 170 | }; |
---|
[1887] | 171 | |
---|
[3327] | 172 | inline void KeyBinder::buttonPressed (const KeyEvent& evt) |
---|
| 173 | { assert(!keys_[evt.getKeyCode()].name_.empty()); keys_[evt.getKeyCode()].execute(KeybindMode::OnPress); } |
---|
[1887] | 174 | |
---|
[3327] | 175 | inline void KeyBinder::buttonReleased(const KeyEvent& evt) |
---|
| 176 | { assert(!keys_[evt.getKeyCode()].name_.empty()); keys_[evt.getKeyCode()].execute(KeybindMode::OnRelease); } |
---|
[1887] | 177 | |
---|
[3327] | 178 | inline void KeyBinder::buttonHeld (const KeyEvent& evt) |
---|
| 179 | { assert(!keys_[evt.getKeyCode()].name_.empty()); keys_[evt.getKeyCode()].execute(KeybindMode::OnHold); } |
---|
[1887] | 180 | |
---|
| 181 | |
---|
[3327] | 182 | inline void KeyBinder::buttonPressed (MouseButtonCode::ByEnum button) |
---|
| 183 | { mouseButtons_[button].execute(KeybindMode::OnPress); } |
---|
[1887] | 184 | |
---|
[3327] | 185 | inline void KeyBinder::buttonReleased(MouseButtonCode::ByEnum button) |
---|
| 186 | { mouseButtons_[button].execute(KeybindMode::OnRelease); } |
---|
[1887] | 187 | |
---|
[3327] | 188 | inline void KeyBinder::buttonHeld (MouseButtonCode::ByEnum button) |
---|
| 189 | { mouseButtons_[button].execute(KeybindMode::OnHold); } |
---|
[1887] | 190 | |
---|
| 191 | |
---|
[3327] | 192 | inline void KeyBinder::buttonPressed (unsigned int device, JoyStickButtonCode::ByEnum button) |
---|
| 193 | { joyStickButtons_[device][button].execute(KeybindMode::OnPress); } |
---|
[1887] | 194 | |
---|
[3327] | 195 | inline void KeyBinder::buttonReleased(unsigned int device, JoyStickButtonCode::ByEnum button) |
---|
| 196 | { joyStickButtons_[device][button].execute(KeybindMode::OnRelease); } |
---|
[1887] | 197 | |
---|
[3327] | 198 | inline void KeyBinder::buttonHeld (unsigned int device, JoyStickButtonCode::ByEnum button) |
---|
| 199 | { joyStickButtons_[device][button].execute(KeybindMode::OnHold); } |
---|
[1887] | 200 | |
---|
[3327] | 201 | inline void KeyBinder::allDevicesUpdated(float dt) |
---|
[1887] | 202 | { |
---|
| 203 | // execute all buffered bindings (additional parameter) |
---|
| 204 | for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++) |
---|
[2087] | 205 | { |
---|
| 206 | paramCommandBuffer_[i]->rel_ *= dt; |
---|
[1887] | 207 | paramCommandBuffer_[i]->execute(); |
---|
[2087] | 208 | } |
---|
[1887] | 209 | |
---|
| 210 | // always reset the relative movement of the mouse |
---|
| 211 | for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++) |
---|
| 212 | mouseAxes_[i].relVal_ = 0.0f; |
---|
| 213 | } |
---|
[1413] | 214 | } |
---|
[973] | 215 | |
---|
[1413] | 216 | #endif /* _KeyBinder_H__ */ |
---|