AGXDynamics  2.31.1.0
Joystick.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXSENSOR_JOYSTICK_H
18 #define AGXSENSOR_JOYSTICK_H
19 
21 #include <agxSensor/export.h>
22 #include <agx/HashVector.h>
23 #include <agx/agx_vector_types.h>
24 
26 namespace OIS
27 {
28  class JoyStickState;
29  class JoyStick;
30 }
32 
33 
34 namespace agxSensor
36 {
37 
42  {
43  public:
44 
45  Joystick();
46 
48  class State
49  {
50  public:
51 
53  enum Pov {
54  CENTERED = 0x00000000,
55  NORTH = 0x00000001,
56  SOUTH = 0x00000010,
57  EAST = 0x00000100,
58  WEST = 0x00001000,
59  NORTHEAST = 0x00000101,
60  SOUTHEAST = 0x00000110,
61  NORTHWEST = 0x00001001,
62  SOUTHWEST = 0x00001010
63  };
64 
67 
70 
72  int pov[4];
73 
75  int slidersX[4];
76 
78  int slidersY[4];
79 
81  State( const OIS::JoyStickState& oisState );
82 
83  protected:
84  State() {}
85  };
86 
93  virtual bool buttonChanged( const State &arg, int button, bool down ) =0;
94 
100  virtual bool axisMoved( const State &arg, int axis ) =0;
101 
107  virtual bool povMoved( const State &arg, int pov ) =0;
108 
114  virtual bool sliderMoved( const State &arg, int index) =0;
115 
120  bool init();
121 
127  agx::Real normalize( int axisValue ) const;
128 
129  State getState() const;
130 
131  protected:
132  virtual ~Joystick();
133  virtual void addNotification();
134  virtual void removeNotification();
135  virtual void pre(const agx::TimeStamp& );
136 
137  private:
138  OIS::JoyStick* m_oisJoystick;
139  void *m_listener;
140  bool m_initialized;
141  bool m_forceFeedbackFound;
142  };
143 
144 
146  class JoystickManager;
147 
149  {
150  public:
152  enum Pov {
153  CENTERED = 0x00000000,
154  NORTH = 0x00000001,
155  SOUTH = 0x00000010,
156  EAST = 0x00000100,
157  WEST = 0x00001000,
158  NORTHEAST = 0x00000101,
159  SOUTHEAST = 0x00000110,
160  NORTHWEST = 0x00001001,
161  SOUTHWEST = 0x00001010
162  };
163 
166 
169 
171  int pov[4];
172 
174  int slidersX[4];
175 
177  int slidersY[4];
178 
180  //agx::IntVector vector3s;
181 
183  JoystickState( const OIS::JoyStickState& oisState );
184 
185  protected:
187  };
188 
189 
192  {
193  public:
195  BUTTON = (1<<0),
196  AXIS = (1<<1),
197  POV = (1<<2),
198  SLIDER = (1<<3),
199 // VECTOR3 = (1<<4),
200  ALL = (
201  BUTTON
202  | AXIS
203  | POV
204  | SLIDER
205  //| VECTOR3
206  ) };
207 
212  : m_mask( ALL ), m_manager( nullptr ) {}
213 
218  JoystickListener( int mask )
219  : m_mask( mask ), m_manager( nullptr ) {}
220 
228  virtual bool buttonChanged( const agxSensor::JoystickState& state, int button, bool down);
229 
237  virtual bool axisUpdate( const agxSensor::JoystickState& state, int axis);
238 
245  virtual bool povMoved( const agxSensor::JoystickState& state, int pov );
246 
253  virtual bool sliderMoved( const agxSensor::JoystickState& state, int slider);
254 
255  /*
256  Called when vector3 \p vector3 has changed state.
257  \param state - current joystick state
258  \param vector3 - vector3 that has changed state (state.pov[pov])
259  \return true if this listener used this slider value (no other listeners will receive this call) - otherwise false
260  */
261  //virtual bool vector3( const agxSensor::JoystickState& /*state*/, int /*vector3*/ ) { return false; }
262 
266  virtual void addNotification() {}
267 
271  virtual void removeNotification() {}
272 
277  void setMask( int mask ) { m_mask = mask; }
278 
282  int getMask() const { return m_mask; }
283 
284  protected:
285  virtual ~JoystickListener() {}
286 
290  JoystickManager* getManager() { return m_manager; }
291  const JoystickManager* getManager() const { return m_manager; }
292 
293  private:
294  friend class JoystickManager;
295  void setManager( JoystickManager* manager )
296  {
297  m_manager = manager;
298  }
299 
300  private:
301  int m_mask;
302  JoystickManager* m_manager;
303  };
304 
307 
308 
311  {
312  public:
313  JoystickManager(intptr_t hwnd);
314 
320  bool add( JoystickListener* listener );
321 
327  bool remove( JoystickListener* listener );
328 
332  bool valid() const { return m_initialized; }
333 
337  bool inSimulation() const { return getSimulation() != nullptr; }
338 
342  JoystickState getState() const;
343 
349  agx::Real normalize( int axisValue ) const;
350 
355  virtual void buttonCallback( const agxSensor::JoystickState& state, int button, bool down );
356 
361  virtual void povCallback( const agxSensor::JoystickState& state, int pov );
362 
363  protected:
364  virtual ~JoystickManager();
365 
369  virtual void addNotification();
370 
374  virtual void removeNotification();
375 
379  virtual void pre( const agx::TimeStamp& );
380 
381  private:
382  JoystickListenerContainer m_listeners;
383  JoystickListenerContainer m_listenersToBeRemoved;
384  OIS::JoyStick* m_oisJoystick;
385  bool m_initialized;
386  intptr_t m_hwnd;
387  };
388 
390 }
391 
392 #endif
bool inSimulation() const
Definition: Joystick.h:337
agx::ref_ptr< JoystickListener > JoystickListenerRef
Definition: Joystick.h:305
agx::Int32Vector axes
Contains the values for all axes.
Definition: Joystick.h:69
void AGXPHYSICS_EXPORT init()
Initialize agx.
const JoystickManager * getManager() const
Definition: Joystick.h:291
JoystickListener()
Default constructor for a joystick listener.
Definition: Joystick.h:211
Derive from this class to implement a listener for simulation Step events.
Joystick interface for gamepads etc.
Definition: Joystick.h:41
#define AGXSENSOR_EXPORT
virtual void removeNotification()
Called when this listener has been removed from a JoystickManager.
Definition: Joystick.h:271
agx::HashVector< JoystickListener *, JoystickListenerRef > JoystickListenerContainer
Definition: Joystick.h:306
virtual void addNotification()
Called when this listener has been added to a JoystickManager.
Definition: Joystick.h:266
agx::ref_ptr< JoystickManager > JoystickManagerRef
Definition: Joystick.h:389
Class for storing a Joystick state.
Definition: Joystick.h:48
agx::Real TimeStamp
Definition: TimeStamp.h:26
Base class providing referencing counted objects.
Definition: Referenced.h:113
agx::Int32Vector axes
Contains the values for all axes.
Definition: Joystick.h:168
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
void setMask(int mask)
Assign new callback mask to this listener.
Definition: Joystick.h:277
Pov
Bit masks indicating the state of the POV button.
Definition: Joystick.h:152
The agxSensor namespace contains drivers for reading for devices such as Gamepad. ...
agx::BoolVector buttons
Contains the state (in/out) of all buttons.
Definition: Joystick.h:165
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:29
JoystickManager * getManager()
Definition: Joystick.h:290
Pov
Bitmasks indicating the state of the POV button.
Definition: Joystick.h:53
double Real
Definition: Real.h:42
agx::BoolVector buttons
Contains the state (in/out) of all buttons.
Definition: Joystick.h:66
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:102
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:101
JoystickListener(int mask)
Create joystick listener given a callback mask (default ALL).
Definition: Joystick.h:218