AGX Dynamics  2.35.0.1
Joystick.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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
35 {
36 
41  {
42  public:
43 
45 
47  class State
48  {
49  public:
50 
52  enum Pov {
53  CENTERED = 0x00000000,
54  NORTH = 0x00000001,
55  SOUTH = 0x00000010,
56  EAST = 0x00000100,
57  WEST = 0x00001000,
58  NORTHEAST = 0x00000101,
59  SOUTHEAST = 0x00000110,
60  NORTHWEST = 0x00001001,
61  SOUTHWEST = 0x00001010
62  };
63 
66 
69 
71  int pov[4];
72 
74  int slidersX[4];
75 
77  int slidersY[4];
78 
80  State( const OIS::JoyStickState& oisState );
81 
82  protected:
83  State() {}
84  };
85 
92  virtual bool buttonChanged( const State &arg, int button, bool down ) =0;
93 
99  virtual bool axisMoved( const State &arg, int axis ) =0;
100 
106  virtual bool povMoved( const State &arg, int pov ) =0;
107 
113  virtual bool sliderMoved( const State &arg, int index) =0;
114 
119  bool init();
120 
126  agx::Real normalize( int axisValue ) const;
127 
128  State getState() const;
129 
130  protected:
131  virtual ~Joystick();
132  virtual void addNotification();
133  virtual void removeNotification();
134  virtual void pre(const agx::TimeStamp& );
135 
136  private:
137  OIS::JoyStick* m_oisJoystick;
138  void *m_listener;
139  bool m_initialized;
140  bool m_forceFeedbackFound;
141  };
142 
143 
145  class JoystickManager;
146 
148  {
149  public:
151  enum Pov {
152  CENTERED = 0x00000000,
153  NORTH = 0x00000001,
154  SOUTH = 0x00000010,
155  EAST = 0x00000100,
156  WEST = 0x00001000,
157  NORTHEAST = 0x00000101,
158  SOUTHEAST = 0x00000110,
159  NORTHWEST = 0x00001001,
160  SOUTHWEST = 0x00001010
161  };
162 
165 
168 
170  int pov[4];
171 
173  int slidersX[4];
174 
176  int slidersY[4];
177 
179  //agx::IntVector vector3s;
180 
182  JoystickState( const OIS::JoyStickState& oisState );
183 
184  protected:
186  };
187 
188 
191  {
192  public:
194  BUTTON = (1<<0),
195  AXIS = (1<<1),
196  POV = (1<<2),
197  SLIDER = (1<<3),
198 // VECTOR3 = (1<<4),
199  ALL = (
200  BUTTON
201  | AXIS
202  | POV
203  | SLIDER
204  //| VECTOR3
205  ) };
206 
211  : m_mask( ALL ), m_manager( nullptr ) {}
212 
217  JoystickListener( int mask )
218  : m_mask( mask ), m_manager( nullptr ) {}
219 
227  virtual bool buttonChanged( const agxSensor::JoystickState& state, int button, bool down);
228 
236  virtual bool axisUpdate( const agxSensor::JoystickState& state, int axis);
237 
244  virtual bool povMoved( const agxSensor::JoystickState& state, int pov );
245 
252  virtual bool sliderMoved( const agxSensor::JoystickState& state, int slider);
253 
254  /*
255  Called when vector3 \p vector3 has changed state.
256  \param state - current joystick state
257  \param vector3 - vector3 that has changed state (state.pov[pov])
258  \return true if this listener used this slider value (no other listeners will receive this call) - otherwise false
259  */
260  //virtual bool vector3( const agxSensor::JoystickState& /*state*/, int /*vector3*/ ) { return false; }
261 
265  virtual void addNotification() {}
266 
270  virtual void removeNotification() {}
271 
276  void setMask( int mask ) { m_mask = mask; }
277 
281  int getMask() const { return m_mask; }
282 
283  protected:
284  virtual ~JoystickListener() {}
285 
289  JoystickManager* getManager() { return m_manager; }
290  const JoystickManager* getManager() const { return m_manager; }
291 
292  private:
293  friend class JoystickManager;
294  void setManager( JoystickManager* manager )
295  {
296  m_manager = manager;
297  }
298 
299  private:
300  int m_mask;
301  JoystickManager* m_manager;
302  };
303 
306 
307 
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 
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 }
390 
391 #endif
#define AGXSENSOR_EXPORT
Simulation * getSimulation()
Derive from this class to implement a listener for simulation step events.
JoystickListener()
Default constructor for a joystick listener.
Definition: Joystick.h:210
virtual bool axisUpdate(const agxSensor::JoystickState &state, int axis)
Called when the manager has a new joystick state.
virtual bool buttonChanged(const agxSensor::JoystickState &state, int button, bool down)
Called when button button changed state.
virtual void removeNotification()
Called when this listener has been removed from a JoystickManager.
Definition: Joystick.h:270
virtual bool sliderMoved(const agxSensor::JoystickState &state, int slider)
Called when slider slider has changed state.
virtual void addNotification()
Called when this listener has been added to a JoystickManager.
Definition: Joystick.h:265
void setMask(int mask)
Assign new callback mask to this listener.
Definition: Joystick.h:276
virtual bool povMoved(const agxSensor::JoystickState &state, int pov)
Called when POV button pov has changed state.
const JoystickManager * getManager() const
Definition: Joystick.h:290
JoystickListener(int mask)
Create joystick listener given a callback mask (default ALL).
Definition: Joystick.h:217
JoystickManager * getManager()
Definition: Joystick.h:289
virtual void pre(const agx::TimeStamp &)
Makes a capture of the joystick and fires callbacks.
virtual void addNotification()
Creates the device if not already created.
bool inSimulation() const
Definition: Joystick.h:336
virtual void povCallback(const agxSensor::JoystickState &state, int pov)
Internal method.
virtual void buttonCallback(const agxSensor::JoystickState &state, int button, bool down)
Internal method.
JoystickManager(intptr_t hwnd)
virtual void removeNotification()
Destroys the device and all listeners.
bool remove(JoystickListener *listener)
Remove joystick listener.
agx::Real normalize(int axisValue) const
Normalizes axis value [-1, 1].
JoystickState getState() const
bool add(JoystickListener *listener)
Add new joystick listener.
Pov
Bit masks indicating the state of the POV button.
Definition: Joystick.h:151
agx::Int32Vector axes
Contains the values for all axes.
Definition: Joystick.h:167
JoystickState(const OIS::JoyStickState &oisState)
Contains the values for all vector3s.
agx::BoolVector buttons
Contains the state (in/out) of all buttons.
Definition: Joystick.h:164
Class for storing a Joystick state.
Definition: Joystick.h:48
agx::BoolVector buttons
Contains the state (in/out) of all buttons.
Definition: Joystick.h:65
Pov
Bitmasks indicating the state of the POV button.
Definition: Joystick.h:52
agx::Int32Vector axes
Contains the values for all axes.
Definition: Joystick.h:68
State(const OIS::JoyStickState &oisState)
Copies state from device driver.
Joystick interface for gamepads etc.
Definition: Joystick.h:41
agx::Real normalize(int axisValue) const
Normalizes axis value [-1, 1].
virtual bool povMoved(const State &arg, int pov)=0
Called whenever an POV button is moved.
State getState() const
virtual bool sliderMoved(const State &arg, int index)=0
Called whenever an slider is moved.
virtual void pre(const agx::TimeStamp &)
Called before a step is taken in the simulation Implement this method in the derived class to get cal...
virtual bool axisMoved(const State &arg, int axis)=0
Called whenever an axis control is moved.
virtual void addNotification()
Called when this listener is added to the simulation.
bool init()
Initializes the joystick.
virtual void removeNotification()
Called when this listener is removed from the simulation.
virtual bool buttonChanged(const State &arg, int button, bool down)=0
Called whenever a button is pressed or released on a Joystick.
Base class providing referencing counted objects.
Definition: Referenced.h:114
Vector containing 'raw' data.
Definition: agx/Vector.h:246
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:30
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
The agxSensor namespace contains drivers for reading for devices such as Gamepad.
agx::HashVector< JoystickListener *, JoystickListenerRef > JoystickListenerContainer
Definition: Joystick.h:305
agx::ref_ptr< JoystickManager > JoystickManagerRef
Definition: Joystick.h:388
agx::ref_ptr< JoystickListener > JoystickListenerRef
Definition: Joystick.h:304
double Real
Definition: Real.h:42
agx::Real TimeStamp
Definition: TimeStamp.h:26