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