Orxonox  0.0.5 Codename: Arcturus
InputHandler.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 _InputHandler_H__
30 #define _InputHandler_H__
31 
32 #include "InputPrereqs.h"
33 
34 namespace orxonox
35 {
37  class IntVector2
38  {
39  public:
40  IntVector2() : x(0), y(0) { }
41  IntVector2(int _x, int _y) : x(_x), y(_y) { }
42  int x;
43  int y;
44  };
45 
46  namespace ButtonEvent
47  {
49  enum Value
50  {
54  };
55 
57  template <ButtonEvent::Value Event>
58  struct EnumToType { };
62  }
63 
64  namespace KeyboardModifier
65  {
67  enum Enum
68  {
69  Shift = 0x0000001,
70  Ctrl = 0x0000010,
71  Alt = 0x0000100
72  };
73  }
74 
77  {
78  public:
79  KeyEvent(const OIS::KeyEvent& evt)
80  : key_(static_cast<KeyCode::ByEnum>(evt.key))
81  , text_(evt.text)
82  , modifiers_(0)
83  { }
84  KeyEvent(KeyCode::ByEnum key, unsigned int text, int modifiers)
85  : key_(key)
86  , text_(text)
87  , modifiers_(modifiers)
88  { }
89  bool operator==(const KeyEvent& rhs) const
90  { return rhs.key_ == key_; }
91  bool operator!=(const KeyEvent& rhs) const
92  { return rhs.key_ != key_; }
93  void setModifiers(int modifiers)
94  { modifiers_ = modifiers; }
95 
97  { return static_cast<KeyboardModifier::Enum>(modifier & modifiers_); }
99  { return key_; }
100  unsigned int getText() const { return text_; }
101 
102  private:
104  unsigned int text_;
106  };
107 
119  {
120  public:
121  virtual ~InputHandler() = default;
122 
123  template<class T> void buttonEvent(unsigned int device, T button, ButtonEvent::TPress)
124  { this->buttonPressed(button); }
125  template<class T> void buttonEvent(unsigned int device, T button, ButtonEvent::TRelease)
126  { this->buttonReleased(button); }
127  template<class T> void buttonEvent(unsigned int device, T button, ButtonEvent::THold)
128  { this->buttonHeld(button); }
129 
130  virtual void buttonPressed (const KeyEvent& evt) { }
131  virtual void buttonReleased(const KeyEvent& evt) { }
132  virtual void buttonHeld (const KeyEvent& evt) { }
133 
134  virtual void buttonPressed (MouseButtonCode::ByEnum button) { }
135  virtual void buttonReleased(MouseButtonCode::ByEnum button) { }
136  virtual void buttonHeld (MouseButtonCode::ByEnum button) { }
137  virtual void mouseMoved (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) { }
138  virtual void mouseScrolled (int abs, int rel) { }
139 
140  virtual void buttonPressed (unsigned int joyStick, JoyStickButtonCode::ByEnum button) { }
141  virtual void buttonReleased(unsigned int joyStick, JoyStickButtonCode::ByEnum button) { }
142  virtual void buttonHeld (unsigned int joyStick, JoyStickButtonCode::ByEnum button) { }
143  virtual void axisMoved (unsigned int joyStick, unsigned int axis, float value){ }
144 
145  virtual void keyboardUpdated(float dt) { }
146  virtual void mouseUpdated (float dt) { }
147  virtual void joyStickUpdated(unsigned int joyStick, float dt) { }
148 
149  virtual void allDevicesUpdated(float dt) { }
150 
153  };
154 
155  template<> inline void InputHandler::buttonEvent<JoyStickButtonCode::ByEnum>(unsigned int device, JoyStickButtonCode::ByEnum button, ButtonEvent::TPress)
156  { this->buttonPressed(device - InputDeviceEnumerator::FirstJoyStick, button); }
157  template<> inline void InputHandler::buttonEvent<JoyStickButtonCode::ByEnum>(unsigned int device, JoyStickButtonCode::ByEnum button, ButtonEvent::TRelease)
158  { this->buttonReleased(device - InputDeviceEnumerator::FirstJoyStick, button); }
159  template<> inline void InputHandler::buttonEvent<JoyStickButtonCode::ByEnum>(unsigned int device, JoyStickButtonCode::ByEnum button, ButtonEvent::THold)
160  { this->buttonHeld(device - InputDeviceEnumerator::FirstJoyStick, button); }
161 }
162 
163 #endif /* _InputHandler_H__ */
virtual void buttonReleased(const KeyEvent &evt)
Definition: InputHandler.h:131
virtual void mouseUpdated(float dt)
Definition: InputHandler.h:146
Base class for all input handlers like KeyBinder, InputBuffer, etc.
Definition: InputHandler.h:118
virtual void buttonPressed(MouseButtonCode::ByEnum button)
Definition: InputHandler.h:134
KeyCode::ByEnum key_
Definition: InputHandler.h:103
void buttonEvent(unsigned int device, T button, ButtonEvent::TPress)
Definition: InputHandler.h:123
KeyEvent(KeyCode::ByEnum key, unsigned int text, int modifiers)
Definition: InputHandler.h:84
void setModifiers(int modifiers)
Definition: InputHandler.h:93
bool isModifierDown(KeyboardModifier::Enum modifier) const
Definition: InputHandler.h:96
IntVector2()
Definition: InputHandler.h:40
virtual void buttonPressed(const KeyEvent &evt)
Definition: InputHandler.h:130
void buttonEvent(unsigned int device, T button, ButtonEvent::TRelease)
Definition: InputHandler.h:125
virtual void buttonReleased(unsigned int joyStick, JoyStickButtonCode::ByEnum button)
Definition: InputHandler.h:141
void buttonEvent(unsigned int device, T button, ButtonEvent::THold)
Definition: InputHandler.h:127
Definition: InputHandler.h:71
ByEnum
Definition: InputPrereqs.h:362
virtual void buttonHeld(unsigned int joyStick, JoyStickButtonCode::ByEnum button)
Definition: InputHandler.h:142
KeyCode
Keyboard scan codes.
Definition: OISKeyboard.h:31
bool operator!=(const KeyEvent &rhs) const
Definition: InputHandler.h:91
virtual void buttonReleased(MouseButtonCode::ByEnum button)
Definition: InputHandler.h:135
virtual void keyboardUpdated(float dt)
Definition: InputHandler.h:145
virtual void axisMoved(unsigned int joyStick, unsigned int axis, float value)
Definition: InputHandler.h:143
bool operator==(const KeyEvent &rhs) const
Definition: InputHandler.h:89
Enables function overloading with integer values.
Definition: InputHandler.h:58
ByEnum
Mouse button codes as enumeration.
Definition: InputPrereqs.h:311
Declarations of all key/button/axis code enumeration and string literals and an input device enumerat...
virtual void mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
Definition: InputHandler.h:137
EnumToType< Press > TPress
Definition: InputHandler.h:59
Definition: InputHandler.h:69
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
unsigned int text_
Definition: InputHandler.h:104
int modifiers_
Definition: InputHandler.h:105
Definition: InputPrereqs.h:447
#define _CoreExport
Definition: CorePrereqs.h:61
virtual void joyStickUpdated(unsigned int joyStick, float dt)
Definition: InputHandler.h:147
virtual void mouseScrolled(int abs, int rel)
Definition: InputHandler.h:138
KeyCode::ByEnum getKeyCode() const
Definition: InputHandler.h:98
virtual void allDevicesUpdated(float dt)
Definition: InputHandler.h:149
Event argument for key events.
Definition: InputHandler.h:76
Definition: InputPrereqs.h:78
EnumToType< Release > TRelease
Definition: InputHandler.h:60
A Vector class containing two integers x and y.
Definition: InputHandler.h:37
KeyEvent(const OIS::KeyEvent &evt)
Definition: InputHandler.h:79
Definition: InputHandler.h:51
EnumToType< Hold > THold
Definition: InputHandler.h:61
int x
Definition: InputHandler.h:42
Definition: InputHandler.h:53
IntVector2(int _x, int _y)
Definition: InputHandler.h:41
Definition: InputHandler.h:52
int y
Definition: InputHandler.h:43
Definition: InputHandler.h:70
virtual void buttonHeld(MouseButtonCode::ByEnum button)
Definition: InputHandler.h:136
static InputHandler EMPTY
Use this input handler if you want to occupy a device in an input state.
Definition: InputHandler.h:152
ByEnum
Key codes as enumeration.
Definition: InputPrereqs.h:56
Value
Helper enum to deploy events with the help of templates.
Definition: InputHandler.h:49
unsigned int getText() const
Definition: InputHandler.h:100
Enum
Keyboard modifiers (shift, ctrl and alt)
Definition: InputHandler.h:67
Specialised for key events.
Definition: OISKeyboard.h:183
virtual void buttonPressed(unsigned int joyStick, JoyStickButtonCode::ByEnum button)
Definition: InputHandler.h:140
virtual void buttonHeld(const KeyEvent &evt)
Definition: InputHandler.h:132