AGX Dynamics  2.38.0.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 }
agxSensor::JoystickState::slidersX
agx::Int32Vector slidersX
Contains the values for all sliders X coordinate. Maximum of 4.
Definition: Joystick.h:180
agxSensor::JoystickListener::removeNotification
virtual void removeNotification()
Called when this listener has been removed from a JoystickManager.
Definition: Joystick.h:268
agxSensor::Joystick::State::Pov
Pov
Bitmasks indicating the state of the POV button.
Definition: Joystick.h:54
agxSensor::Joystick::State::slidersX
agx::Int32Vector slidersX
Contains the values for all sliders X coordinate. Maximum of 4.
Definition: Joystick.h:76
agxSensor::Joystick
Alternative Joystick interface for gamepads etc.
Definition: Joystick.h:40
agxSensor::JoystickListenerContainer
agx::HashVector< JoystickListener *, JoystickListenerRef > JoystickListenerContainer
Definition: Joystick.h:303
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:70
agxSensor::JoystickState::slidersY
agx::Int32Vector slidersY
Contains the values for all sliders Y coordinate. Maximum of 4.
Definition: Joystick.h:183
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:282
agxSensor::JoystickListener::getManager
const JoystickManager * getManager() const
Definition: Joystick.h:288
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::JoystickState::pov
agx::Int32Vector pov
Represents the value of POV button. Maximum of 4.
Definition: Joystick.h:177
agxSensor::Joystick::State::buttons
agx::BoolVector buttons
Contains the state (in/out) of all buttons.
Definition: Joystick.h:67
HashVector.h
DOXYGEN_START_INTERNAL_BLOCK
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
agxSensor::JoystickState
Store the current state of a Joystick/GamePad.
Definition: Joystick.h:151
agxSensor::JoystickState::buttons
agx::BoolVector buttons
Contains the state (in/out) of all buttons.
Definition: Joystick.h:171
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:263
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:174
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:287
agxSensor::JoystickListener::CallbackMask
CallbackMask
Definition: Joystick.h:201
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:158
export.h
AGXSENSOR_EXPORT
#define AGXSENSOR_EXPORT
agx::GraphNode::State
State
Definition: GraphNode.h:39
agxSensor::Joystick::State::slidersY
agx::Int32Vector slidersY
Contains the values for all sliders Y coordinate. Maximum of 4.
Definition: Joystick.h:79
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:119
agxSensor::JoystickListener::setMask
void setMask(int mask)
Assign new callback mask to this listener.
Definition: Joystick.h:274
agx::TimeStamp
agx::Real TimeStamp
Definition: TimeStamp.h:26
agxSensor::Joystick::State::pov
agx::Int32Vector pov
Represents the value of POV button. Maximum of 4.
Definition: Joystick.h:73
agxSensor::JoystickListener
Class that can be used to listen to events generated by a GamePad or Joystick.
Definition: Joystick.h:198
agxSensor::JoystickListenerRef
agx::ref_ptr< JoystickListener > JoystickListenerRef
Definition: Joystick.h:302
agxSensor::JoystickManager
A Joystick/GamePad manager that initializes the connected devices and allows for adding listeners.
Definition: Joystick.h:309
agx::init
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API.
agxSensor::JoystickListener::getMask
int getMask() const
Definition: Joystick.h:279
agxSensor::JoystickListener::JoystickListener
JoystickListener()
Default constructor for a joystick listener.
Definition: Joystick.h:216
agx::HashVector< JoystickListener *, JoystickListenerRef >
agxSensor::JoystickListener::JoystickListener
JoystickListener(int mask)
Create joystick listener given a callback mask (default ALL).
Definition: Joystick.h:223
agx_vector_types.h
agxSDK::EventListener::removeNotification
virtual void removeNotification()
Called when this listener is removed from the simulation.
Definition: EventListener.h:91