AGX Dynamics  2.37.3.2
Joystick.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. 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 #pragma once
18 
20 #include <agxSensor/export.h>
21 #include <agx/HashVector.h>
22 #include <agx/agx_vector_types.h>
23 
25 namespace OIS
26 {
27  class JoyStickState;
28  class JoyStick;
29 }
31 
32 
33 namespace agxSensor
34 {
35 
41  {
42  public:
43 
44  Joystick();
45 
47  class State
48  {
49  public:
50 
51  static const int NUM_AXES = 4;
52 
54  enum Pov {
55  CENTERED = 0x00000000,
56  NORTH = 0x00000001,
57  SOUTH = 0x00000010,
58  EAST = 0x00000100,
59  WEST = 0x00001000,
60  NORTHEAST = 0x00000101,
61  SOUTHEAST = 0x00000110,
62  NORTHWEST = 0x00001001,
63  SOUTHWEST = 0x00001010
64  };
65 
68 
71 
74 
77 
80 
82  State( const OIS::JoyStickState& oisState );
83 
84  protected:
85  State();
86  };
87 
94  virtual bool buttonChanged( const State &arg, int button, bool down ) =0;
95 
101  virtual bool axisMoved( const State &arg, int axis ) =0;
102 
108  virtual bool povMoved( const State &arg, int pov ) =0;
109 
115  virtual bool sliderMoved( const State &arg, int index) =0;
116 
121  bool init();
122 
128  agx::Real normalize( int axisValue ) const;
129 
130  State getState() const;
131 
132  protected:
133  virtual ~Joystick();
134  virtual void addNotification();
135  virtual void removeNotification();
136  virtual void pre(const agx::TimeStamp& );
137 
138  private:
139  OIS::JoyStick* m_oisJoystick;
140  void *m_listener;
141  bool m_initialized;
142  bool m_forceFeedbackFound;
143  };
144 
145 
146  class JoystickManager;
147 
152  {
153  public:
154 
155  static int const NUM_AXES = Joystick::State::NUM_AXES;
156 
158  enum Pov {
159  CENTERED = 0x00000000,
160  NORTH = 0x00000001,
161  SOUTH = 0x00000010,
162  EAST = 0x00000100,
163  WEST = 0x00001000,
164  NORTHEAST = 0x00000101,
165  SOUTHEAST = 0x00000110,
166  NORTHWEST = 0x00001001,
167  SOUTHWEST = 0x00001010
168  };
169 
172 
175 
178 
181 
184 
185 
187  JoystickState( const OIS::JoyStickState& oisState );
188 
189  protected:
190  JoystickState();
191  };
192 
193 
199  {
200  public:
202  BUTTON = (1<<0),
203  AXIS = (1<<1),
204  POV = (1<<2),
205  SLIDER = (1<<3),
206  ALL = (
207  BUTTON
208  | AXIS
209  | POV
210  | SLIDER
211  ) };
212 
217  : m_mask( ALL ), m_manager( nullptr ) {}
218 
223  JoystickListener( int mask )
224  : m_mask( mask ), m_manager( nullptr ) {}
225 
233  virtual bool buttonChanged( const agxSensor::JoystickState& state, int button, bool down);
234 
242  virtual bool axisUpdate( const agxSensor::JoystickState& state, int axis);
243 
250  virtual bool povMoved( const agxSensor::JoystickState& state, int pov );
251 
258  virtual bool sliderMoved( const agxSensor::JoystickState& state, int slider);
259 
263  virtual void addNotification() {}
264 
268  virtual void removeNotification() {}
269 
274  void setMask( int mask ) { m_mask = mask; }
275 
279  int getMask() const { return m_mask; }
280 
281  protected:
282  virtual ~JoystickListener() {}
283 
287  JoystickManager* getManager() { return m_manager; }
288  const JoystickManager* getManager() const { return m_manager; }
289 
290  private:
291  friend class JoystickManager;
292  void setManager( JoystickManager* manager )
293  {
294  m_manager = manager;
295  }
296 
297  private:
298  int m_mask;
299  JoystickManager* m_manager;
300  };
301 
304 
305 
310  {
311  public:
312  JoystickManager(intptr_t hwnd);
313 
319  bool add( JoystickListener* listener );
320 
326  bool remove( JoystickListener* listener );
327 
331  bool valid() const { return m_initialized; }
332 
336  bool inSimulation() const { return getSimulation() != nullptr; }
337 
341  JoystickState getState() const;
342 
348  agx::Real normalize( int axisValue ) const;
349 
354  virtual void buttonCallback( const agxSensor::JoystickState& state, int button, bool down );
355 
360  virtual void povCallback( const agxSensor::JoystickState& state, int pov );
361 
362  protected:
363  virtual ~JoystickManager();
364 
368  virtual void addNotification();
369 
373  virtual void removeNotification();
374 
378  virtual void pre( const agx::TimeStamp& );
379 
380  private:
381  JoystickListenerContainer m_listeners;
382  JoystickListenerContainer m_listenersToBeRemoved;
383  OIS::JoyStick* m_oisJoystick;
384  bool m_initialized;
385  intptr_t m_hwnd;
386  };
387 
389 }
bool inSimulation() const
Definition: Joystick.h:336
agx::ref_ptr< JoystickListener > JoystickListenerRef
Definition: Joystick.h:302
agx::Int32Vector axes
Contains the values for all axes.
Definition: Joystick.h:70
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API...
const JoystickManager * getManager() const
Definition: Joystick.h:288
JoystickListener()
Default constructor for a joystick listener.
Definition: Joystick.h:216
Derive from this class to implement a listener for simulation step events.
agx::Int32Vector slidersY
Contains the values for all sliders Y coordinate. Maximum of 4.
Definition: Joystick.h:183
Alternative Joystick interface for gamepads etc.
Definition: Joystick.h:40
#define AGXSENSOR_EXPORT
virtual void removeNotification()
Called when this listener has been removed from a JoystickManager.
Definition: Joystick.h:268
agx::HashVector< JoystickListener *, JoystickListenerRef > JoystickListenerContainer
Definition: Joystick.h:303
virtual void addNotification()
Called when this listener has been added to a JoystickManager.
Definition: Joystick.h:263
agx::Int32Vector slidersX
Contains the values for all sliders X coordinate. Maximum of 4.
Definition: Joystick.h:76
agx::ref_ptr< JoystickManager > JoystickManagerRef
Definition: Joystick.h:388
Class for storing a Joystick state.
Definition: Joystick.h:47
agx::Real TimeStamp
Definition: TimeStamp.h:26
Base class providing referencing counted objects.
Definition: Referenced.h:119
agx::Int32Vector slidersY
Contains the values for all sliders Y coordinate. Maximum of 4.
Definition: Joystick.h:79
agx::Int32Vector axes
Contains the values for all axes.
Definition: Joystick.h:174
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
void setMask(int mask)
Assign new callback mask to this listener.
Definition: Joystick.h:274
Pov
Bit masks indicating the state of the POV button.
Definition: Joystick.h:158
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:171
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:29
JoystickManager * getManager()
Definition: Joystick.h:287
agx::Int32Vector slidersX
Contains the values for all sliders X coordinate. Maximum of 4.
Definition: Joystick.h:180
agx::Int32Vector pov
Represents the value of POV button. Maximum of 4.
Definition: Joystick.h:73
Pov
Bitmasks indicating the state of the POV button.
Definition: Joystick.h:54
agx::Int32Vector pov
Represents the value of POV button. Maximum of 4.
Definition: Joystick.h:177
double Real
Definition: Real.h:42
agx::BoolVector buttons
Contains the state (in/out) of all buttons.
Definition: Joystick.h:67
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
Store the current state of a Joystick/GamePad.
Definition: Joystick.h:151
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97
Class that can be used to listen to events generated by a GamePad or Joystick.
Definition: Joystick.h:198
A Joystick/GamePad manager that initializes the connected devices and allows for adding listeners...
Definition: Joystick.h:309
JoystickListener(int mask)
Create joystick listener given a callback mask (default ALL).
Definition: Joystick.h:223