Orxonox  0.0.5 Codename: Arcturus
KeyBinder.h
Go to the documentation of this file.
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 <memory>
39 
40 #include "InputHandler.h"
41 #include "Button.h"
42 #include "HalfAxis.h"
43 #include "InputCommands.h"
45 
46 // tolua_begin
47 namespace orxonox
48 {
49  // tolua_end
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(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
71  unsigned int getNumberOfBindings(const std::string& commandName); //tolua_export
72 
74  { return this->filename_; }
75  void setConfigValues();
76  void resetJoyStickAxes();
77  void resetMouseAxes();
78 
79  void changeMode(ConsoleCommand* command, KeybindMode::Value mode);
80 
81  protected: // functions
82  void loadBindings();
83  void buttonThresholdChanged();
84  void initialiseJoyStickBindings();
85  void compilePointerLists();
86  // from JoyStickQuantityListener interface
87  virtual void JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList) override;
88 
89  virtual void allDevicesUpdated(float dt) override;
90  virtual void mouseUpdated(float dt) override;
91  virtual void joyStickUpdated(unsigned int joyStick, float dt) override;
92  // internal
93  void tickHalfAxis(HalfAxis& halfAxis);
94 
95  virtual void buttonPressed (const KeyEvent& evt) override;
96  virtual void buttonReleased(const KeyEvent& evt) override;
97  virtual void buttonHeld (const KeyEvent& evt) override;
98 
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;
104 
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;
109 
110  protected: // variables
112  std::vector<JoyStick*> joySticks_;
113 
117  static const unsigned int numberOfMouseButtons_ = MouseButtonCode::numberOfButtons + 4;
119  Button mouseButtons_ [numberOfMouseButtons_];
122 
125  {
126  Button& operator[](unsigned int index) { return buttons[index]; }
128  };
130  std::vector<std::shared_ptr<JoyStickButtonVector>> joyStickButtons_;
133  {
134  HalfAxis& operator[](unsigned int index) { return halfAxes[index]; }
136  };
138  std::vector<std::shared_ptr<JoyStickAxisVector>> joyStickAxes_;
139 
141  std::map<std::string, Button*> allButtons_;
143  std::vector<HalfAxis*> allHalfAxes_;
145  std::map< std::string, std::vector<std::string>> allCommands_;
146 
152  std::vector<BufferedParamCommand*> paramCommandBuffer_;
153 
155  float mousePosition_[2];
157  int mouseRelative_[2];
158  float deriveTime_;
159 
166 
167  private:
168  void addButtonToCommand(const std::string& command, Button* button);
169 
170  //##### ConfigValues #####
187 
190 
191  //##### Constant config variables #####
192  // Use some value at about 1000. This can be configured with mouseSensitivity_ anyway.
193  static const int mouseClippingSize_ = 1024;
194  };// tolua_export
195 
196 
197  inline void KeyBinder::buttonPressed (const KeyEvent& evt)
198  { assert(!keys_[evt.getKeyCode()].name_.empty()); keys_[evt.getKeyCode()].execute(KeybindMode::OnPress, 1); }
199 
200  inline void KeyBinder::buttonReleased(const KeyEvent& evt)
201  { assert(!keys_[evt.getKeyCode()].name_.empty()); keys_[evt.getKeyCode()].execute(KeybindMode::OnRelease, 0); }
202 
203  inline void KeyBinder::buttonHeld (const KeyEvent& evt)
204  { assert(!keys_[evt.getKeyCode()].name_.empty()); keys_[evt.getKeyCode()].execute(KeybindMode::OnHold); }
205 
206 
208  { mouseButtons_[button].execute(KeybindMode::OnPress, 1); }
209 
211  { mouseButtons_[button].execute(KeybindMode::OnRelease, 0); }
212 
214  { mouseButtons_[button].execute(KeybindMode::OnHold); }
215 
216 
217  inline void KeyBinder::buttonPressed (unsigned int device, JoyStickButtonCode::ByEnum button)
218  { (*joyStickButtons_[device])[button].execute(KeybindMode::OnPress, 1); }
219 
220  inline void KeyBinder::buttonReleased(unsigned int device, JoyStickButtonCode::ByEnum button)
221  { (*joyStickButtons_[device])[button].execute(KeybindMode::OnRelease, 0); }
222 
223  inline void KeyBinder::buttonHeld (unsigned int device, JoyStickButtonCode::ByEnum button)
224  { (*joyStickButtons_[device])[button].execute(KeybindMode::OnHold); }
225 
226  inline void KeyBinder::allDevicesUpdated(float dt)
227  {
228  // execute all buffered bindings (additional parameter)
229  for (BufferedParamCommand* command : paramCommandBuffer_)
230  {
231  command->rel_ *= dt;
232  command->execute();
233  }
234 
235  // always reset the relative movement of the mouse
236  for (HalfAxis& axis : mouseAxes_)
237  axis.relVal_ = 0.0f;
238  }
239 }// tolua_export
240 
241 #endif /* _KeyBinder_H__ */
Base class for all input handlers like KeyBinder, InputBuffer, etc.
Definition: InputHandler.h:118
Different definitions of input processing.
Derive from this class to get informed when joy sticks get added/removed.
Definition: JoyStickQuantityListener.h:45
float deriveTime_
Definition: KeyBinder.h:158
const unsigned int numberOfButtons
Definition: InputPrereqs.h:359
The ConsoleCommand class stores all information about a console command which can be executed by Comm...
Definition: ConsoleCommand.h:88
Different definitions of input processing.
std::vector< std::shared_ptr< JoyStickAxisVector > > joyStickAxes_
Actual key bindings for joy stick axes (and sliders)
Definition: KeyBinder.h:138
std::map< std::string, std::vector< std::string > > allCommands_
Maps input commands to all Button names, including half axes.
Definition: KeyBinder.h:145
virtual void buttonPressed(const KeyEvent &evt) override
Definition: KeyBinder.h:197
Definition: CorePrereqs.h:126
::std::string string
Definition: gtest-port.h:756
std::vector< JoyStick * > joySticks_
Currently active joy sticks.
Definition: KeyBinder.h:112
int mouseWheelStepSize_
Equals one step of the mouse wheel.
Definition: KeyBinder.h:186
This class represents a config file, which is stored on the hard-disk and contains config values in d...
Definition: ConfigFile.h:51
ByEnum
Definition: InputPrereqs.h:362
const unsigned int numberOfKeys
Definition: InputPrereqs.h:53
float totalMouseSensitivity_
Multiplication of mouse sensitivity and clipping size.
Definition: KeyBinder.h:189
const unsigned int numberOfButtons
Definition: InputPrereqs.h:308
std::map< std::string, Button * > allButtons_
Pointer map with all Buttons, including half axes.
Definition: KeyBinder.h:141
std::vector< HalfAxis * > allHalfAxes_
Pointer list with all half axes.
Definition: KeyBinder.h:143
Button & operator[](unsigned int index)
Definition: KeyBinder.h:126
float mouseSensitivity_
mouse sensitivity
Definition: KeyBinder.h:182
bool bFilterAnalogNoise_
Whether to filter small value analog input.
Definition: KeyBinder.h:172
Definition: CorePrereqs.h:128
const unsigned int numberOfAxes
Definition: InputPrereqs.h:410
ByEnum
Mouse button codes as enumeration.
Definition: InputPrereqs.h:311
Definition: CorePrereqs.h:127
Declarations of all key/button/axis code enumeration and string literals and an input device enumerat...
HalfAxis & operator[](unsigned int index)
Definition: KeyBinder.h:134
virtual void buttonReleased(const KeyEvent &evt) override
Definition: KeyBinder.h:200
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION_MULTI() command(const std::string &fragment)
Returns a list of commands and groups and also supports auto-completion of the arguments of these com...
Definition: ArgumentCompletionFunctions.cc:178
Different definitions of input processing.
float buttonThreshold_
Threshold for analog triggers until which the button is not pressed.
Definition: KeyBinder.h:176
Value
Definition: CorePrereqs.h:124
Maps mouse, keyboard and joy stick input to command strings and executes them.
Definition: KeyBinder.h:59
#define _CoreExport
Definition: CorePrereqs.h:61
Definition: InputCommands.h:43
ConfigFile * fallbackConfigFile_
Config file from the data directory that only serves as fallback.
Definition: KeyBinder.h:165
KeyCode::ByEnum getKeyCode() const
Definition: InputHandler.h:98
const std::string & getBindingsFilename()
Definition: KeyBinder.h:73
Event argument for key events.
Definition: InputHandler.h:76
float mouseSensitivityDerived_
mouse sensitivity if mouse input is derived
Definition: KeyBinder.h:184
Helper class to use something like std:vector<Button[64]>
Definition: KeyBinder.h:124
A Vector class containing two integers x and y.
Definition: InputHandler.h:37
Helper class to use something like std:vector<HalfAxis[48]>
Definition: KeyBinder.h:132
const std::string filename_
Name of the file used in this KeyBinder (constant!)
Definition: KeyBinder.h:161
virtual void buttonHeld(const KeyEvent &evt) override
Definition: KeyBinder.h:203
bool bDeriveMouseInput_
Derive mouse input for absolute values?
Definition: KeyBinder.h:178
const unsigned int numberOfAxes
Definition: InputPrereqs.h:339
Definition: HalfAxis.h:45
virtual void allDevicesUpdated(float dt) override
Definition: KeyBinder.h:226
std::vector< BufferedParamCommand * > paramCommandBuffer_
Commands that have additional parameters (axes) are executed at the end of update() so that all value...
Definition: KeyBinder.h:152
float derivePeriod_
Accuracy of the mouse input deriver. The higher the more precise, but laggier.
Definition: KeyBinder.h:180
float analogThreshold_
Threshold for analog triggers until which the state is 0.
Definition: KeyBinder.h:174
Definition: Button.h:46
ConfigFile * configFile_
Config file used. nullptr in case of KeyDetector. Also indicates whether we&#39;ve already loaded...
Definition: KeyBinder.h:163
std::vector< std::shared_ptr< JoyStickButtonVector > > joyStickButtons_
Actual key bindings for joy stick buttons.
Definition: KeyBinder.h:130