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