DelayedEventQueue.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifndef EVENT_DELAYEDEVENTQUEUE_H
  24. #define EVENT_DELAYEDEVENTQUEUE_H
  25. #include "Event.h"
  26. class EventListener;
  27. //! Delayed event
  28. struct DelayedEvent
  29. {
  30. //! Construct with no event data
  31. DelayedEvent() :
  32. mReceiver(0),
  33. mDelay(0.0f)
  34. {
  35. }
  36. //! Construct with event data but no receiver
  37. DelayedEvent(StringHash eventType, const VariantMap& eventData, float delay) :
  38. mReceiver(0),
  39. mEventType(eventType),
  40. mEventData(eventData),
  41. mDelay(delay)
  42. {
  43. }
  44. //! Construct with event data and receiver
  45. DelayedEvent(EventListener* receiver, StringHash eventType, const VariantMap& eventData, float delay) :
  46. mReceiver(receiver),
  47. mEventType(eventType),
  48. mEventData(eventData),
  49. mDelay(delay)
  50. {
  51. }
  52. //! Event receiver
  53. EventListener* mReceiver;
  54. //! Event type
  55. StringHash mEventType;
  56. //! Event parameters
  57. VariantMap mEventData;
  58. //! Delay until sending
  59. float mDelay;
  60. };
  61. //! Stores delayed events
  62. class DelayedEventQueue
  63. {
  64. public:
  65. //! Construct
  66. DelayedEventQueue();
  67. //! Destruct
  68. virtual ~DelayedEventQueue();
  69. //! Store a delayed event with no receiver specified
  70. void sendDelayedEvent(StringHash eventType, const VariantMap& eventData = VariantMap(), float delay = 0.0f);
  71. //! Store a delayed event with receiver specified
  72. void sendDelayedEvent(EventListener* receiver, StringHash eventType, const VariantMap& eventData = VariantMap(), float delay = 0.0f);
  73. //! Update delay timers and send events as necessary
  74. void processDelayedEvents(float timeStep);
  75. //! Clear all delayed events
  76. void clearDelayedEvents();
  77. //! Return all delayed events
  78. const std::vector<DelayedEvent>& getDelayedEvents() const { return mDelayedEvents; }
  79. private:
  80. //! Delayed events
  81. std::vector<DelayedEvent> mDelayedEvents;
  82. };
  83. #endif // EVENT_DELAYEDEVENTQUEUE_H