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