AGXDynamics  2.35.0.3
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 
44  Joystick();
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 
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 }
390 
391 #endif
agxSensor::JoystickListener::removeNotification
virtual void removeNotification()
Called when this listener has been removed from a JoystickManager.
Definition: Joystick.h:270
agxSensor::Joystick::State::Pov
Pov
Bitmasks indicating the state of the POV button.
Definition: Joystick.h:52
agxSensor::Joystick
Joystick interface for gamepads etc.
Definition: Joystick.h:40
agxSensor::JoystickListenerContainer
agx::HashVector< JoystickListener *, JoystickListenerRef > JoystickListenerContainer
Definition: Joystick.h:305
agxSDK::StepEventListener::pre
virtual void pre(const agx::TimeStamp &time)
Called before a step is taken in the simulation Implement this method in the derived class to get cal...
Definition: StepEventListener.h:106
agx::Real
double Real
Definition: Real.h:42
agxSensor::JoystickManagerRef
agx::ref_ptr< JoystickManager > JoystickManagerRef
Definition: Joystick.h:388
agxSensor::Joystick::State::axes
agx::Int32Vector axes
Contains the values for all axes.
Definition: Joystick.h:68
agxSDK::StepEventListener
Derive from this class to implement a listener for simulation step events.
Definition: StepEventListener.h:37
agxSensor::JoystickListener::~JoystickListener
virtual ~JoystickListener()
Definition: Joystick.h:284
agxSensor::JoystickListener::getManager
const JoystickManager * getManager() const
Definition: Joystick.h:290
DOXYGEN_END_INTERNAL_BLOCK
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
agxSensor
The agxSensor namespace contains drivers for reading for devices such as Gamepad.
agxSensor::Joystick::State::buttons
agx::BoolVector buttons
Contains the state (in/out) of all buttons.
Definition: Joystick.h:65
HashVector.h
DOXYGEN_START_INTERNAL_BLOCK
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
agxSensor::JoystickState
Definition: Joystick.h:147
agxSensor::JoystickState::buttons
agx::BoolVector buttons
Contains the state (in/out) of all buttons.
Definition: Joystick.h:164
StepEventListener.h
agx::VectorPOD
Vector containing 'raw' data.
Definition: Vector.h:245
agxSensor::JoystickListener::addNotification
virtual void addNotification()
Called when this listener has been added to a JoystickManager.
Definition: Joystick.h:265
agx::ref_ptr
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:29
agxSDK::EventListener::getSimulation
Simulation * getSimulation()
agxSensor::JoystickState::axes
agx::Int32Vector axes
Contains the values for all axes.
Definition: Joystick.h:167
agxSensor::JoystickManager::inSimulation
bool inSimulation() const
Definition: Joystick.h:336
agxSensor::JoystickManager::valid
bool valid() const
Definition: Joystick.h:331
agxSensor::JoystickListener::getManager
JoystickManager * getManager()
Definition: Joystick.h:289
agxSensor::JoystickListener::CallbackMask
CallbackMask
Definition: Joystick.h:193
agxSensor::Joystick::State
Class for storing a Joystick state.
Definition: Joystick.h:47
agxSensor::JoystickState::Pov
Pov
Bit masks indicating the state of the POV button.
Definition: Joystick.h:151
agxSensor::Joystick::State::State
State()
Definition: Joystick.h:83
export.h
AGXSENSOR_EXPORT
#define AGXSENSOR_EXPORT
agx::GraphNode::State
State
Definition: GraphNode.h:39
agxSDK::EventListener::addNotification
virtual void addNotification()
Called when this listener is added to the simulation.
Definition: EventListener.h:86
agx::Referenced
Base class providing referencing counted objects.
Definition: Referenced.h:113
agxSensor::JoystickListener::setMask
void setMask(int mask)
Assign new callback mask to this listener.
Definition: Joystick.h:276
agx::TimeStamp
agx::Real TimeStamp
Definition: TimeStamp.h:26
agxSensor::JoystickListener
Definition: Joystick.h:190
agxSensor::JoystickListenerRef
agx::ref_ptr< JoystickListener > JoystickListenerRef
Definition: Joystick.h:304
agxSensor::JoystickManager
Definition: Joystick.h:309
agx::init
void AGXPHYSICS_EXPORT init()
Initialize agx.
agxSensor::JoystickListener::getMask
int getMask() const
Definition: Joystick.h:281
agxSensor::JoystickListener::JoystickListener
JoystickListener()
Default constructor for a joystick listener.
Definition: Joystick.h:210
agx::HashVector< JoystickListener *, JoystickListenerRef >
agxSensor::JoystickListener::JoystickListener
JoystickListener(int mask)
Create joystick listener given a callback mask (default ALL).
Definition: Joystick.h:217
agx_vector_types.h
agxSensor::JoystickState::JoystickState
JoystickState()
Definition: Joystick.h:185
agxSDK::EventListener::removeNotification
virtual void removeNotification()
Called when this listener is removed from the simulation.
Definition: EventListener.h:91