AGX Dynamics  2.37.2.0
EventManager.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 #ifndef AGXSDK_EVENTMANAGER_H
17 #define AGXSDK_EVENTMANAGER_H
18 
19 #include <agxSDK/agxSDK.h>
20 
24 #include <agxSDK/GuiEvent.h>
25 
26 #include <map>
27 
28 #include <agxCollide/Space.h>
29 
31 
32 
33 namespace agxSDK
34 {
35 
36  AGX_DECLARE_POINTER_TYPES(EventManager);
37 
41  class AGXPHYSICS_EXPORT EventManager : public agx::Referenced
42  {
43  public:
45  enum ExecutePriority {
46  LOWEST_PRIORITY = 0,
47  DEFAULT_PRIORITY = 50,
48  HIGHEST_PRIORITY = 100
49  };
50 
55  enum CleanupSelectionMask {
56  STEP_LISTENERS = 0x1,
57  CONTACT_LISTENERS = 0x2,
58  GUI_LISTENERS = 0x4,
59  LISTENERS = STEP_LISTENERS | CONTACT_LISTENERS | GUI_LISTENERS,
60  CLEANUP_ALL = LISTENERS
61  };
62 
64  EventManager(Simulation* simulation);
65 
73  bool addStepEventListener(StepEventListener* listener, int priority = DEFAULT_PRIORITY );
74 
84  bool addContactEventListener(ContactEventListener* listener, int priority = DEFAULT_PRIORITY );
85 
91  bool addGuiEventListener(GuiEventListener* listener, int priority = DEFAULT_PRIORITY );
92 
96  bool addEventListener(EventListener* listener, int priority = DEFAULT_PRIORITY );
97 
101  void triggerContactEventListeners(const agx::TimeStamp& timeStamp,
102  const agxCollide::GeometryContactPtrVector& contacts,
103  agx::Bool isPost);
104 
108  void triggerSeparationContactEventListeners (const agx::TimeStamp& timeStamp,
109  const agxCollide::SeparationPairVector& separations);
110 
116  const agxSDK::EventListener* getEventListener(const agx::String& name) const;
117 
123  agxSDK::EventListener* getEventListener(const agx::String& name);
124 
130  const agxSDK::EventListener* getEventListener(const agx::Uuid& name) const;
131 
137  agxSDK::EventListener* getEventListener(const agx::Uuid& name);
138 
142  void triggerPreCollideStepEvent( const agx::TimeStamp& timeStamp );
143 
147  void triggerPreStepEvent( const agx::TimeStamp& timeStamp );
148 
149 
153  void triggerPostStepEvent( const agx::TimeStamp& timeStamp );
154 
158  void triggerLastStepEvent( const agx::TimeStamp& timeStamp );
159 
163  bool removeEventListener(EventListener* listener);
164 
169  bool triggerGuiEvent( const GuiEvent& guiEvent );
170 
175  void setEnableDetailedStatisticsReport( bool enable );
176 
180  bool getEnableDetailedStatisticsReport() const;
181 
182  template <typename T>
183  class PrioHolder
184  {
185  public:
186  PrioHolder( ): m_priority( 0 ) {}
187  PrioHolder( T obj, int priority ) : m_obj( obj ), m_priority( priority ) {}
188  inline int priority() const {
189  return m_priority;
190  }
191  inline operator T () {
192  return m_obj;
193  }
194  inline operator const T () const {
195  return m_obj;
196  }
197  inline operator int () const {
198  return m_priority;
199  }
200  inline T listener() {
201  return m_obj;
202  }
203  inline const T listener() const {
204  return m_obj;
205  }
206 
207  protected:
208  T m_obj;
209  int m_priority;
210  };
211 
215  void getEventListeners( EventListenerPtrVector& eventListeners ) const;
216 
217 
218 
219  protected:
220 
221 
222  void cleanup( CleanupSelectionMask selection );
223 
224  friend class Simulation;
225 
226  protected:
227 
228 
229  typedef std::map< uint32_t, PrioHolder<StepEventListenerRef> > StepEventListeners;
230  typedef std::map< uint32_t, PrioHolder<ContactEventListenerRef> > ContactEventListeners;
231  typedef std::map< uint32_t, PrioHolder<GuiEventListenerRef> > GuiEventListeners;
232 
233  private:
234 
235  bool checkPriorityForClass( EventListener* listener, int priority );
236  typedef agx::HashTable<agx::String, bool> SystemClassNames;
237  void initSystemClassNameTable();
238 
239  SystemClassNames m_systemClassNames;
240 
241  template <typename T1, typename DATA1>
242  class ListenerQueueCache
243  {
244  public:
245 
246  typedef agx::Vector<std::pair<DATA1, int> > SortableVectorType;
247  typedef agx::Vector<DATA1> QueueType;
248  typedef typename QueueType::iterator iterator;
249 
250  ListenerQueueCache() : m_dirty( true ), m_needRebuild( true ) {}
251 
252  void dirty( ) {
253  m_dirty = true;
254  }
255  void setNeedRebuild( ) {
256  m_needRebuild = true;
257  }
258 
259  void clear() {
260  m_queue.clear();
261  m_needRebuild = true;
262  m_dirty = true;
263  }
264 
265  bool isDirty() const {
266  return m_dirty;
267  }
268  bool needRebuild() const {
269  return m_needRebuild;
270  }
271 
272  QueueType& queue() {
273  return m_queue;
274  }
275 
276  inline iterator begin() {
277  return m_queue.begin();
278  }
279  inline iterator end() {
280  return m_queue.end();
281  }
282 
283 #ifdef __APPLE__
284  void build( T1& container) {
285  bool doSort = true;
286 #else
287  void build( T1& container, bool doSort = true ) {
288 #endif
289 
290  if ( needRebuild() ) {
291  clear();
292  SortableVectorType sortVector;
293  sortVector.reserve( container.size() );
294 
295  typename T1::iterator cit = container.begin();
296  for ( size_t i = 0; cit != container.end(); ++cit, ++i ) {
297  sortVector.push_back( std::make_pair(cit->second.listener().get(), cit->second.priority()) );
298  }
299 
300  if (doSort)
301  std::sort( sortVector.begin(), sortVector.end(), prioGreater<std::pair<DATA1, int> >() );
302 
303  m_queue.reserve( sortVector.size() );
304  for (size_t i = 0; i < sortVector.size(); i++) {
305  m_queue.push_back( sortVector[i].first );
306  }
307 
308  m_dirty = false;
309  m_needRebuild = false;
310  }
311 
312  }
313  private:
314 
315 #ifndef SWIG
316  // Class for sorting based on priority
317  template<class T>
318  struct prioGreater {
319  bool operator()(const T& _Left, const T& _Right) const {
320  return (_Left.second > _Right.second);
321  }
322  };
323 #endif
324 
325  QueueType m_queue;
326  bool m_dirty;
327  bool m_needRebuild;
328 
329  };
330 
331  Simulation* m_simulation;
332 
333  StepEventListeners m_step_listeners;
334  typedef ListenerQueueCache<StepEventListeners, StepEventListenerRef> StepEventListenerCache;
335  StepEventListenerCache m_step_listener_cache;
336 
337  ContactEventListeners m_contact_listeners;
338  typedef ListenerQueueCache<ContactEventListeners, ContactEventListenerRef> ContactListenerCache;
339  ContactListenerCache m_contact_listener_cache;
340 
341  GuiEventListeners m_gui_listeners;
342  typedef ListenerQueueCache<GuiEventListeners, GuiEventListenerRef > GuiEventListenerCache;
343  GuiEventListenerCache m_gui_listener_cache;
344 
345  agx::Timer m_eventTimer;
346  agx::Vector<bool> m_insideCallback;
347 
348  bool m_detailedStatisticsReport;
349 
350  protected:
351 
353  virtual ~EventManager();
354  };
355 }
357 
358 #endif
AGXTERRAIN_EXPORT void clear()
bool Bool
Definition: Integer.h:48
#define AGXPHYSICS_EXPORT
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
The Timer class permits timing execution speed with the same refinement as the built in hardware cloc...
Definition: Timer.h:74
agx::Real TimeStamp
Definition: TimeStamp.h:26
Base class providing referencing counted objects.
Definition: Referenced.h:119
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
LinearProbingHashSetImplementation< KeyT, HashT >::iterator begin(LinearProbingHashSetImplementation< KeyT, HashT > &set)
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Definition: Uuid.h:41
An EventListener can be associated to a Simulation and triggered upon various events.
Definition: EventListener.h:40
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
Inheritance with partial specialization due to bug with ref_ptr containers.
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
agx::Vector< EventListener * > EventListenerPtrVector
Definition: EventListener.h:32
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97