Orxonox  0.0.5 Codename: Arcturus
InputState.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 _InputState_H__
30 #define _InputState_H__
31 
32 #include "InputPrereqs.h"
33 
34 #include <cassert>
35 #include <string>
36 #include <vector>
37 #include <functional>
38 
39 #include "util/tribool.h"
40 #include "InputHandler.h"
41 #include "InputManager.h"
43 
44 #define INPUT_STATE_PUSH_CALL(deviceIndex, functionName, ...) \
45  InputManager::getInstance().pushCall(std::function<void ()>(std::bind(&InputHandler::functionName, handlers_[deviceIndex], __VA_ARGS__)))
46 
47 namespace orxonox
48 {
84  {
85  friend class InputManager;
86 
93 
94  public:
96  void setKeyHandler (InputHandler* handler)
97  { handlers_[keyboardIndex_s] = handler; bExpired_ = true; }
99  void setMouseHandler (InputHandler* handler)
100  { handlers_[mouseIndex_s] = handler; bExpired_ = true; }
107  bool setJoyStickHandler(InputHandler* handler, unsigned int joyStick);
109  void setJoyStickHandler(InputHandler* handler);
111  void setHandler (InputHandler* handler);
112 
113  void setMouseExclusive(tribool value) { exclusiveMouse_ = value; this->bExpired_ = true; }
114  tribool getMouseExclusive() const { return exclusiveMouse_; }
115 
117  const std::string& getName() const { return name_; }
119  int getPriority() const { return priority_; }
120 
122  bool isInputDeviceEnabled(unsigned int device);
123 
125  bool hasExpired() { return this->bExpired_; }
127  void resetExpiration() { bExpired_ = false; }
128 
130  void update(float dt, unsigned int device);
132  void update(float dt);
133 
135  template <typename EventType, class ButtonTypeParam>
136  void buttonEvent(unsigned int device, ButtonTypeParam button);
137 
139  void mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize);
141  void mouseScrolled(int abs, int rel);
143  void joyStickAxisMoved(unsigned int device, unsigned int axis, float value);
144 
145  // Functors
147  void entered();
149  void left();
151  void setEnterFunctor(const FunctorPtr& functor) { this->enterFunctor_ = functor; }
153  void setLeaveFunctor(const FunctorPtr& functor) { this->leaveFunctor_ = functor; }
154 
155  private:
156  InputState(const std::string& name, bool bAlwaysGetsInput, bool bTransparent, InputStatePriority priority);
157  ~InputState() = default;
158 
159  virtual void JoyStickQuantityChanged(const std::vector<JoyStick*>& joyStickList) override;
160 
162  void setPriority(int priority) { priority_ = priority; }
163 
165  const bool bAlwaysGetsInput_;
166  const bool bTransparent_;
168  int priority_;
169  bool bExpired_;
170  std::vector<InputHandler*> handlers_;
171  InputHandler* joyStickHandlerAll_;
175  };
176 
178  {
179  for (unsigned int i = 0; i < handlers_.size(); ++i)
180  if (handlers_[i] != nullptr)
181  INPUT_STATE_PUSH_CALL(i, allDevicesUpdated, dt);
182  }
183 
184  ORX_FORCEINLINE void InputState::update(float dt, unsigned int device)
185  {
186  switch (device)
187  {
189  if (handlers_[keyboardIndex_s] != nullptr)
190  INPUT_STATE_PUSH_CALL(keyboardIndex_s, keyboardUpdated, dt);
191  break;
192 
194  if (handlers_[mouseIndex_s] != nullptr)
195  INPUT_STATE_PUSH_CALL(mouseIndex_s, mouseUpdated, dt);
196  break;
197 
198  default: // joy sticks
199  if (handlers_[device] != nullptr)
200  INPUT_STATE_PUSH_CALL(device, joyStickUpdated, device - firstJoyStickIndex_s, dt);
201  break;
202  }
203  }
204 
205  template <typename EventType, class ButtonTypeParam>
206  ORX_FORCEINLINE void InputState::buttonEvent(unsigned int device, ButtonTypeParam button)
207  {
208  assert(device < handlers_.size());
209  if (handlers_[device] != nullptr)
210  {
211  // We have to store the function pointer to tell the compiler about its actual type because of overloading
212  void (InputHandler::*function)(unsigned int, ButtonTypeParam, EventType) = &InputHandler::buttonEvent<ButtonTypeParam>;
213  InputManager::getInstance().pushCall(std::function<void ()>(std::bind(function, handlers_[device], device, button, EventType())));
214  }
215  }
216 
218  {
219  if (handlers_[mouseIndex_s] != nullptr)
220  INPUT_STATE_PUSH_CALL(mouseIndex_s, mouseMoved, abs, rel, clippingSize);
221  }
222 
224  {
225  if (handlers_[mouseIndex_s] != nullptr)
226  INPUT_STATE_PUSH_CALL(mouseIndex_s, mouseScrolled, abs, rel);
227  }
228 
229  ORX_FORCEINLINE void InputState::joyStickAxisMoved(unsigned int device, unsigned int axis, float value)
230  {
231  assert(device < handlers_.size());
232  if (handlers_[device] != nullptr)
233  INPUT_STATE_PUSH_CALL(device, axisMoved, device - firstJoyStickIndex_s, axis, value);
234  }
235 }
236 
237 #endif /* _InputState_H__ */
void joyStickAxisMoved(unsigned int device, unsigned int axis, float value)
Event handler.
Definition: InputState.h:229
std::vector< InputHandler * > handlers_
Vector with all handlers where the index is the device ID.
Definition: InputState.h:170
Base class for all input handlers like KeyBinder, InputBuffer, etc.
Definition: InputHandler.h:118
std::shared_ptr< Functor > FunctorPtr
Definition: FunctorPtr.h:57
Derive from this class to get informed when joy sticks get added/removed.
Definition: JoyStickQuantityListener.h:45
int getPriority() const
Returns the priority of the state (which is unique if != 0)
Definition: InputState.h:119
const bool bTransparent_
See class declaration for explanation.
Definition: InputState.h:166
void mouseScrolled(int abs, int rel)
Event handler.
Definition: InputState.h:223
tribool exclusiveMouse_
See class declaration for explanation.
Definition: InputState.h:167
void buttonEvent(unsigned int device, ButtonTypeParam button)
Generic function that distributes all 9 button events.
int priority_
Current priority (might change)
Definition: InputState.h:168
::std::string string
Definition: gtest-port.h:756
void setMouseExclusive(tribool value)
Definition: InputState.h:113
void setEnterFunctor(const FunctorPtr &functor)
Sets a functor to be called upon activation of the state.
Definition: InputState.h:151
void setKeyHandler(InputHandler *handler)
Sets the keyboard event handler (overwrites if there already was one!)
Definition: InputState.h:96
void setMouseHandler(InputHandler *handler)
Sets the mouse event handler (overwrites if there already was one!)
Definition: InputState.h:99
FunctorPtr leaveFunctor_
Functor to be executed on leave.
Definition: InputState.h:174
typedef void(ENET_CALLBACK *ENetPacketFreeCallback)(struct _ENetPacket *)
void setLeaveFunctor(const FunctorPtr &functor)
Sets a functor to be called upon deactivation of the state.
Definition: InputState.h:153
const std::string & getName() const
Returns the name of the state (which is unique!)
Definition: InputState.h:117
void resetExpiration()
Call this if you have applied the changes resulting from changed handlers.
Definition: InputState.h:127
void pushCall(const std::function< void()> &function)
Definition: InputManager.h:188
Declarations of all key/button/axis code enumeration and string literals and an input device enumerat...
void setPriority(int priority)
Sets the priority (only to be used by the InputManager!)
Definition: InputState.h:162
Definition: InputPrereqs.h:445
Enumeration wrapper for input state priorities.
Definition: InputPrereqs.h:452
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
Definition: InputPrereqs.h:447
#define _CoreExport
Definition: CorePrereqs.h:61
FunctorPtr enterFunctor_
Functor to be executed on enter.
Definition: InputState.h:173
A 3-state boolean type.
Definition: tribool.h:38
const bool bAlwaysGetsInput_
See class declaration for explanation.
Definition: InputState.h:165
A Vector class containing two integers x and y.
Definition: InputHandler.h:37
void update(float dt, unsigned int device)
Updates one specific device handler with deviceUpdated.
Definition: InputState.h:184
static InputManager & getInstance()
Definition: InputManager.h:191
#define INPUT_STATE_PUSH_CALL(deviceIndex, functionName,...)
Definition: InputState.h:44
Value
Used to access the devices in an array.
Definition: InputPrereqs.h:443
InputStates allow you to customise the input event targets at runtime.
Definition: InputState.h:83
tribool getMouseExclusive() const
Definition: InputState.h:114
#define ORX_FORCEINLINE
Definition: OrxonoxConfig.h:95
Definition: InputPrereqs.h:446
internal::String name_
Definition: gtest.cc:2289
void mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
Event handler.
Definition: InputState.h:217
bool hasExpired()
Returns true if the handler situation has changed.
Definition: InputState.h:125
const std::string name_
Name of the state.
Definition: InputState.h:164
Manages the input devices (mouse, keyboard, joy sticks) and the input states.
Definition: InputManager.h:66
bool bExpired_
See hasExpired()
Definition: InputState.h:169