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