SpineEventMonitor.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //////////////////////////////////////////////////////////////////////
  2. // filename: SpineEventMonitor.h
  3. //
  4. // purpose: Monitor spAnimationState Events
  5. /////////////////////////////////////////////////////////////////////
  6. #pragma once
  7. #include <vector>
  8. #include <string>
  9. // forward declarations
  10. typedef struct spAnimationState spAnimationState;
  11. typedef struct spTrackEntry spTrackEntry;
  12. typedef struct spEvent spEvent;
  13. //////////////////////////////////////////////////////////////////////
  14. // class: SpineEventMonitor
  15. //
  16. // purpose: Monitor spAnimationState Events and report when there
  17. // are no more spTrackEntry(s) waiting to play on track 0;
  18. //
  19. // Also allows for debug printing of Events to console.
  20. /////////////////////////////////////////////////////////////////////
  21. class SpineEventMonitor
  22. {
  23. public:
  24. SpineEventMonitor(spAnimationState* _pAnimationState = nullptr);
  25. virtual ~SpineEventMonitor();
  26. void RegisterListener(spAnimationState* _pAnimationState);
  27. void SetDebugLogging(bool val) { bLogging = val; }
  28. bool GetDebugLogging() { return bLogging; }
  29. virtual bool isAnimationPlaying();
  30. protected:
  31. static void spineAnimStateHandler(spAnimationState* state, int type, spTrackEntry* entry, spEvent* event);
  32. virtual void OnSpineAnimationStateEvent(spAnimationState* state, int type, spTrackEntry* trackEntry, spEvent* event);
  33. protected:
  34. spAnimationState *pAnimState;
  35. bool bLogging;
  36. };
  37. //////////////////////////////////////////////////////////////////////
  38. // class: InterruptMonitor
  39. //
  40. // purpose: Allows a programmer to interrupt/stop the updating
  41. // of an animation based on a specific sequence of
  42. // events generated by the animation.
  43. /////////////////////////////////////////////////////////////////////
  44. class InterruptMonitor : public SpineEventMonitor
  45. {
  46. private:
  47. struct InterruptEvent
  48. {
  49. InterruptEvent() {
  50. mEventType = -1; // invalid
  51. mTrackEntry = nullptr;
  52. }
  53. bool matches(spAnimationState* state, int type, spTrackEntry* trackEntry, spEvent* event);
  54. std::string mAnimName;
  55. int mEventType;
  56. spTrackEntry* mTrackEntry;
  57. std::string mEventName;
  58. };
  59. typedef std::vector<InterruptEvent> InterruptEventStack;
  60. public:
  61. InterruptMonitor(spAnimationState* _pAnimationState = nullptr);
  62. ~InterruptMonitor() {}
  63. virtual bool isAnimationPlaying() override;
  64. public:
  65. InterruptMonitor& AddInterruptEvent(int theEventType);
  66. InterruptMonitor& AddInterruptEvent(int theEventType, const std::string& theAnimationName);
  67. InterruptMonitor& AddInterruptEvent(int theEventType, spTrackEntry* theTrackEntry);
  68. InterruptMonitor& AddInterruptEventTrigger(const std::string& theEventTriggerName);
  69. protected:
  70. virtual void OnSpineAnimationStateEvent(spAnimationState* state, int type, spTrackEntry* trackEntry, spEvent* event) override;
  71. virtual void OnMatchingComplete() {}
  72. protected:
  73. bool bForceInterrupt;
  74. InterruptEventStack mEventStack; // must match these events in this order
  75. size_t mEventStackCursor;
  76. };
  77. /*
  78. EXAMPLE
  79. =======
  80. SpineEventMonitor eventMonitor(state);
  81. eventMonitor.SetDebugLogging(true);
  82. while(eventMonitor.isAnimationPlaying()){
  83. // update...
  84. }
  85. EXAMPLE
  86. =======
  87. InterruptMonitor eventMonitor(state);
  88. eventMonitor.SetDebugLogging(true);
  89. // Interrupt the animation on this specific sequence of spEventType(s)
  90. eventMonitor
  91. .AddInterruptEvent(SP_ANIMATION_INTERRUPT, "jump") // First, wait for INTERRUPT signal on the 'jump' animation spTrackEntry
  92. .AddInterruptEvent(SP_ANIMATION_START); // Then, stop on any following START signal
  93. */