[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 | |
---|
[1413] | 29 | #ifndef _KeyBinder_H__ |
---|
| 30 | #define _KeyBinder_H__ |
---|
[973] | 31 | |
---|
[3327] | 32 | #include "InputPrereqs.h" |
---|
[1062] | 33 | |
---|
[3196] | 34 | #include <cassert> |
---|
| 35 | #include <string> |
---|
[1349] | 36 | #include <vector> |
---|
[6417] | 37 | #include <map> |
---|
[11071] | 38 | #include <memory> |
---|
[2662] | 39 | |
---|
[3327] | 40 | #include "InputHandler.h" |
---|
[1520] | 41 | #include "Button.h" |
---|
| 42 | #include "HalfAxis.h" |
---|
[1887] | 43 | #include "InputCommands.h" |
---|
[3327] | 44 | #include "JoyStickQuantityListener.h" |
---|
[973] | 45 | |
---|
[6417] | 46 | // tolua_begin |
---|
[973] | 47 | namespace orxonox |
---|
| 48 | { |
---|
[6417] | 49 | // tolua_end |
---|
[1755] | 50 | /** |
---|
| 51 | @brief |
---|
[5929] | 52 | Maps mouse, keyboard and joy stick input to command strings and executes them. |
---|
| 53 | |
---|
| 54 | The bindings are stored in ini-files (like the one for configValues) in the config Path. |
---|
| 55 | @remarks |
---|
| 56 | You cannot change the filename because the KeyBinderManager maps these filenames to the |
---|
| 57 | KeyBinders. If you need to load other bindings, just create a new one. |
---|
[1755] | 58 | */ |
---|
[6417] | 59 | class _CoreExport KeyBinder // tolua_export |
---|
| 60 | : public InputHandler, public JoyStickQuantityListener |
---|
| 61 | { // tolua_export |
---|
[1755] | 62 | public: |
---|
[5929] | 63 | KeyBinder (const std::string& filename); |
---|
[1755] | 64 | virtual ~KeyBinder(); |
---|
[1022] | 65 | |
---|
[1755] | 66 | void clearBindings(); |
---|
[1887] | 67 | bool setBinding(const std::string& binding, const std::string& name, bool bTemporary = false); |
---|
[11106] | 68 | std::string getBinding(const std::string& commandName); //tolua_export |
---|
| 69 | std::string getBinding(const std::string& commandName, unsigned int index); //tolua_export |
---|
| 70 | std::string getBindingReadable(const std::string& commandName); //tolua_export |
---|
[6417] | 71 | unsigned int getNumberOfBindings(const std::string& commandName); //tolua_export |
---|
| 72 | |
---|
[5929] | 73 | const std::string& getBindingsFilename() |
---|
| 74 | { return this->filename_; } |
---|
[1755] | 75 | void setConfigValues(); |
---|
| 76 | void resetJoyStickAxes(); |
---|
[7859] | 77 | void resetMouseAxes(); |
---|
[1349] | 78 | |
---|
[7861] | 79 | void changeMode(ConsoleCommand* command, KeybindMode::Value mode); |
---|
| 80 | |
---|
[1755] | 81 | protected: // functions |
---|
[5929] | 82 | void loadBindings(); |
---|
| 83 | void buttonThresholdChanged(); |
---|
| 84 | void initialiseJoyStickBindings(); |
---|
| 85 | void compilePointerLists(); |
---|
| 86 | // from JoyStickQuantityListener interface |
---|
[11071] | 87 | virtual void JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList) override; |
---|
[5929] | 88 | |
---|
[11071] | 89 | virtual void allDevicesUpdated(float dt) override; |
---|
| 90 | virtual void mouseUpdated(float dt) override; |
---|
| 91 | virtual void joyStickUpdated(unsigned int joyStick, float dt) override; |
---|
[1887] | 92 | // internal |
---|
[2087] | 93 | void tickHalfAxis(HalfAxis& halfAxis); |
---|
[1413] | 94 | |
---|
[11071] | 95 | virtual void buttonPressed (const KeyEvent& evt) override; |
---|
| 96 | virtual void buttonReleased(const KeyEvent& evt) override; |
---|
| 97 | virtual void buttonHeld (const KeyEvent& evt) override; |
---|
[1022] | 98 | |
---|
[11071] | 99 | virtual void buttonPressed (MouseButtonCode::ByEnum button) override; |
---|
| 100 | virtual void buttonReleased(MouseButtonCode::ByEnum button) override; |
---|
| 101 | virtual void buttonHeld (MouseButtonCode::ByEnum button) override; |
---|
| 102 | virtual void mouseMoved (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) override; |
---|
| 103 | virtual void mouseScrolled (int abs, int rel) override; |
---|
[1022] | 104 | |
---|
[11071] | 105 | virtual void buttonPressed (unsigned int device, JoyStickButtonCode::ByEnum button) override; |
---|
| 106 | virtual void buttonReleased(unsigned int device, JoyStickButtonCode::ByEnum button) override; |
---|
| 107 | virtual void buttonHeld (unsigned int device, JoyStickButtonCode::ByEnum button) override; |
---|
| 108 | virtual void axisMoved (unsigned int device, unsigned int axis, float value) override; |
---|
[1349] | 109 | |
---|
[1755] | 110 | protected: // variables |
---|
[1887] | 111 | //! Currently active joy sticks |
---|
[3327] | 112 | std::vector<JoyStick*> joySticks_; |
---|
[1022] | 113 | |
---|
[1887] | 114 | //! Actual key bindings for keys on the keyboard |
---|
| 115 | Button keys_ [KeyCode::numberOfKeys]; |
---|
| 116 | //! Number of mouse buttons in KeyBinder (+4) |
---|
[1888] | 117 | static const unsigned int numberOfMouseButtons_ = MouseButtonCode::numberOfButtons + 4; |
---|
[1887] | 118 | //! Actual key bindings for mouse buttons including the wheel(s) |
---|
| 119 | Button mouseButtons_ [numberOfMouseButtons_]; |
---|
| 120 | //! Actual key bindings for mouse axes |
---|
| 121 | HalfAxis mouseAxes_ [MouseAxisCode::numberOfAxes * 2]; |
---|
[1219] | 122 | |
---|
[1887] | 123 | //! Helper class to use something like std:vector<Button[64]> |
---|
| 124 | struct JoyStickButtonVector |
---|
| 125 | { |
---|
| 126 | Button& operator[](unsigned int index) { return buttons[index]; } |
---|
| 127 | Button buttons[JoyStickButtonCode::numberOfButtons]; |
---|
| 128 | }; |
---|
| 129 | //! Actual key bindings for joy stick buttons |
---|
[11071] | 130 | std::vector<std::shared_ptr<JoyStickButtonVector>> joyStickButtons_; |
---|
[1887] | 131 | //! Helper class to use something like std:vector<HalfAxis[48]> |
---|
| 132 | struct JoyStickAxisVector |
---|
| 133 | { |
---|
| 134 | HalfAxis& operator[](unsigned int index) { return halfAxes[index]; } |
---|
| 135 | HalfAxis halfAxes[JoyStickAxisCode::numberOfAxes * 2]; |
---|
| 136 | }; |
---|
| 137 | //! Actual key bindings for joy stick axes (and sliders) |
---|
[11071] | 138 | std::vector<std::shared_ptr<JoyStickAxisVector>> joyStickAxes_; |
---|
[1219] | 139 | |
---|
[1887] | 140 | //! Pointer map with all Buttons, including half axes |
---|
| 141 | std::map<std::string, Button*> allButtons_; |
---|
| 142 | //! Pointer list with all half axes |
---|
| 143 | std::vector<HalfAxis*> allHalfAxes_; |
---|
[6417] | 144 | //! Maps input commands to all Button names, including half axes |
---|
[11071] | 145 | std::map< std::string, std::vector<std::string>> allCommands_; |
---|
[1349] | 146 | |
---|
[1755] | 147 | /** |
---|
| 148 | @brief |
---|
| 149 | Commands that have additional parameters (axes) are executed at the end of |
---|
[2896] | 150 | update() so that all values can be buffered for single execution. |
---|
[1755] | 151 | */ |
---|
| 152 | std::vector<BufferedParamCommand*> paramCommandBuffer_; |
---|
[1349] | 153 | |
---|
[6417] | 154 | //! Keeps track of the absolute mouse value |
---|
| 155 | float mousePosition_[2]; |
---|
[1755] | 156 | //! Used to derive mouse input if requested |
---|
| 157 | int mouseRelative_[2]; |
---|
| 158 | float deriveTime_; |
---|
[1349] | 159 | |
---|
[5929] | 160 | //! Name of the file used in this KeyBinder (constant!) |
---|
| 161 | const std::string filename_; |
---|
[11071] | 162 | //! Config file used. nullptr in case of KeyDetector. Also indicates whether we've already loaded. |
---|
[6536] | 163 | ConfigFile* configFile_; |
---|
| 164 | //! Config file from the data directory that only serves as fallback |
---|
| 165 | ConfigFile* fallbackConfigFile_; |
---|
[1755] | 166 | |
---|
[1887] | 167 | private: |
---|
[6417] | 168 | void addButtonToCommand(const std::string& command, Button* button); |
---|
| 169 | |
---|
[1755] | 170 | //##### ConfigValues ##### |
---|
[2087] | 171 | //! Whether to filter small value analog input |
---|
| 172 | bool bFilterAnalogNoise_; |
---|
[1755] | 173 | //! Threshold for analog triggers until which the state is 0. |
---|
| 174 | float analogThreshold_; |
---|
| 175 | //! Threshold for analog triggers until which the button is not pressed. |
---|
| 176 | float buttonThreshold_; |
---|
| 177 | //! Derive mouse input for absolute values? |
---|
| 178 | bool bDeriveMouseInput_; |
---|
| 179 | //! Accuracy of the mouse input deriver. The higher the more precise, but laggier. |
---|
| 180 | float derivePeriod_; |
---|
| 181 | //! mouse sensitivity |
---|
| 182 | float mouseSensitivity_; |
---|
| 183 | //! mouse sensitivity if mouse input is derived |
---|
| 184 | float mouseSensitivityDerived_; |
---|
[6417] | 185 | //! Equals one step of the mouse wheel |
---|
[2087] | 186 | int mouseWheelStepSize_; |
---|
[1887] | 187 | |
---|
[6417] | 188 | //! Multiplication of mouse sensitivity and clipping size |
---|
| 189 | float totalMouseSensitivity_; |
---|
| 190 | |
---|
[1887] | 191 | //##### Constant config variables ##### |
---|
| 192 | // Use some value at about 1000. This can be configured with mouseSensitivity_ anyway. |
---|
| 193 | static const int mouseClippingSize_ = 1024; |
---|
[6417] | 194 | };// tolua_export |
---|
[1887] | 195 | |
---|
[5929] | 196 | |
---|
[3327] | 197 | inline void KeyBinder::buttonPressed (const KeyEvent& evt) |
---|
[9978] | 198 | { assert(!keys_[evt.getKeyCode()].name_.empty()); keys_[evt.getKeyCode()].execute(KeybindMode::OnPress, 1); } |
---|
[1887] | 199 | |
---|
[3327] | 200 | inline void KeyBinder::buttonReleased(const KeyEvent& evt) |
---|
[9978] | 201 | { assert(!keys_[evt.getKeyCode()].name_.empty()); keys_[evt.getKeyCode()].execute(KeybindMode::OnRelease, 0); } |
---|
[1887] | 202 | |
---|
[3327] | 203 | inline void KeyBinder::buttonHeld (const KeyEvent& evt) |
---|
| 204 | { assert(!keys_[evt.getKeyCode()].name_.empty()); keys_[evt.getKeyCode()].execute(KeybindMode::OnHold); } |
---|
[1887] | 205 | |
---|
| 206 | |
---|
[3327] | 207 | inline void KeyBinder::buttonPressed (MouseButtonCode::ByEnum button) |
---|
[9978] | 208 | { mouseButtons_[button].execute(KeybindMode::OnPress, 1); } |
---|
[1887] | 209 | |
---|
[3327] | 210 | inline void KeyBinder::buttonReleased(MouseButtonCode::ByEnum button) |
---|
[9978] | 211 | { mouseButtons_[button].execute(KeybindMode::OnRelease, 0); } |
---|
[1887] | 212 | |
---|
[3327] | 213 | inline void KeyBinder::buttonHeld (MouseButtonCode::ByEnum button) |
---|
| 214 | { mouseButtons_[button].execute(KeybindMode::OnHold); } |
---|
[1887] | 215 | |
---|
| 216 | |
---|
[3327] | 217 | inline void KeyBinder::buttonPressed (unsigned int device, JoyStickButtonCode::ByEnum button) |
---|
[9978] | 218 | { (*joyStickButtons_[device])[button].execute(KeybindMode::OnPress, 1); } |
---|
[1887] | 219 | |
---|
[3327] | 220 | inline void KeyBinder::buttonReleased(unsigned int device, JoyStickButtonCode::ByEnum button) |
---|
[9978] | 221 | { (*joyStickButtons_[device])[button].execute(KeybindMode::OnRelease, 0); } |
---|
[1887] | 222 | |
---|
[3327] | 223 | inline void KeyBinder::buttonHeld (unsigned int device, JoyStickButtonCode::ByEnum button) |
---|
[5695] | 224 | { (*joyStickButtons_[device])[button].execute(KeybindMode::OnHold); } |
---|
[1887] | 225 | |
---|
[3327] | 226 | inline void KeyBinder::allDevicesUpdated(float dt) |
---|
[1887] | 227 | { |
---|
| 228 | // execute all buffered bindings (additional parameter) |
---|
[11071] | 229 | for (BufferedParamCommand* command : paramCommandBuffer_) |
---|
[2087] | 230 | { |
---|
[11071] | 231 | command->rel_ *= dt; |
---|
| 232 | command->execute(); |
---|
[2087] | 233 | } |
---|
[1887] | 234 | |
---|
| 235 | // always reset the relative movement of the mouse |
---|
[11071] | 236 | for (HalfAxis& axis : mouseAxes_) |
---|
| 237 | axis.relVal_ = 0.0f; |
---|
[1887] | 238 | } |
---|
[6417] | 239 | }// tolua_export |
---|
[973] | 240 | |
---|
[1413] | 241 | #endif /* _KeyBinder_H__ */ |
---|