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