AnimationContext.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include "Animation/UiAnimViewSequenceManager.h"
  10. #include <Range.h>
  11. #include <LyShine/Animation/IUiAnimation.h>
  12. #include "Undo/IUndoManagerListener.h"
  13. class CUiAnimViewSequence;
  14. /** CUiAnimationContext listener interface
  15. */
  16. struct IUiAnimationContextListener
  17. {
  18. virtual void OnSequenceChanged([[maybe_unused]] CUiAnimViewSequence* pNewSequence) {}
  19. virtual void OnTimeChanged([[maybe_unused]] float newTime) {}
  20. };
  21. /** CUiAnimationContext stores information about current editable animation sequence.
  22. Stores information about whenever animation is being recorded know,
  23. current sequence, current time in sequence etc.
  24. */
  25. class CUiAnimationContext
  26. : public IEditorNotifyListener
  27. , public IUndoManagerListener
  28. , public IUiAnimViewSequenceManagerListener
  29. {
  30. public:
  31. //////////////////////////////////////////////////////////////////////////
  32. // Constructors.
  33. //////////////////////////////////////////////////////////////////////////
  34. /** Constructor.
  35. */
  36. CUiAnimationContext();
  37. ~CUiAnimationContext();
  38. //////////////////////////////////////////////////////////////////////////
  39. // Accessors
  40. //////////////////////////////////////////////////////////////////////////
  41. void Init();
  42. // Listeners
  43. void AddListener(IUiAnimationContextListener* pListener);
  44. void RemoveListener(IUiAnimationContextListener* pListener);
  45. // Get the animation system for the active canvas
  46. IUiAnimationSystem* GetUiAnimationSystem() const;
  47. //! Called when the active canvas changes - possibly to no canvas
  48. void ActiveCanvasChanged();
  49. /** Return current animation time in active sequence.
  50. @return Current time.
  51. */
  52. float GetTime() const { return m_currTime; };
  53. float GetTimeScale() const { return m_fTimeScale; }
  54. void SetTimeScale(float fScale) { m_fTimeScale = fScale; }
  55. /** Set active editing sequence.
  56. @param seq New active sequence.
  57. */
  58. void SetSequence(CUiAnimViewSequence* pSequence, bool bForce, bool bNoNotify, bool recordUndo = false);
  59. /** Get currently edited sequence.
  60. */
  61. CUiAnimViewSequence* GetSequence() const { return m_pSequence; };
  62. /** Set time markers to play within.
  63. */
  64. void SetMarkers(Range Marker) { m_timeMarker = Marker; }
  65. /** Get time markers to play within.
  66. */
  67. Range GetMarkers() { return m_timeMarker; }
  68. /** Get time range of active animation sequence.
  69. */
  70. Range GetTimeRange() const { return m_timeRange; }
  71. /** Returns true if editor is recording animations now.
  72. */
  73. bool IsRecording() const { return m_recording && m_paused == 0; };
  74. /** Returns true if editor is playing animation now.
  75. */
  76. bool IsPlaying() const { return m_playing && m_paused == 0; };
  77. /** Returns true if currently playing or recording is paused.
  78. */
  79. bool IsPaused() const { return m_paused > 0; }
  80. /** Return if animation context is now in playing mode.
  81. In difference from IsPlaying function this function not affected by pause state.
  82. */
  83. bool IsPlayMode() const { return m_playing; };
  84. /** Return if animation context is now in recording mode.
  85. In difference from IsRecording function this function not affected by pause state.
  86. */
  87. bool IsRecordMode() const { return m_recording; };
  88. /** Returns true if currently looping as activated.
  89. */
  90. bool IsLoopMode() const { return m_bLooping; }
  91. /** Enable/Disable looping.
  92. */
  93. void SetLoopMode(bool bLooping) { m_bLooping = bLooping; }
  94. //////////////////////////////////////////////////////////////////////////
  95. // Operators
  96. //////////////////////////////////////////////////////////////////////////
  97. /** Set current animation time in active sequence.
  98. @param seq New active time.
  99. */
  100. void SetTime(float t);
  101. /** Set time in active sequence for reset animation.
  102. @param seq New active time.
  103. */
  104. void SetResetTime(float t) {m_resetTime = t; };
  105. /** Start animation recorduing.
  106. Automatically stop playing.
  107. @param recording True to start recording, false to stop.
  108. */
  109. void SetRecording(bool playing);
  110. /** Start/Stop animation playing.
  111. Automatically stop recording.
  112. @param playing True to start playing, false to stop.
  113. */
  114. void SetPlaying(bool playing);
  115. /** Pause animation playing/recording.
  116. */
  117. void Pause();
  118. /** Toggle playback
  119. */
  120. void TogglePlay();
  121. /** Resume animation playing/recording.
  122. */
  123. void Resume();
  124. /** Called every frame to update all animations if animation should be playing.
  125. */
  126. void Update();
  127. /** Force animation for current sequence.
  128. */
  129. void ForceAnimation();
  130. void OnPostRender();
  131. void UpdateTimeRange();
  132. private:
  133. void BeginUndoTransaction() override;
  134. void EndUndoTransaction() override;
  135. void OnSequenceRemoved(CUiAnimViewSequence* pSequence) override;
  136. void OnEditorNotifyEvent(EEditorNotifyEvent event) override;
  137. void AnimateActiveSequence();
  138. //! Current time within active animation sequence.
  139. float m_currTime;
  140. //! Force update in next frame
  141. bool m_bForceUpdateInNextFrame;
  142. //! Time within active animation sequence while reset animation.
  143. float m_resetTime;
  144. float m_fTimeScale;
  145. // Recording time step.
  146. float m_fRecordingCurrTime;
  147. //! Time range of active animation sequence.
  148. Range m_timeRange;
  149. Range m_timeMarker;
  150. //! Currently active animation sequence.
  151. CUiAnimViewSequence* m_pSequence;
  152. //! Name of active sequence (for switching back from game mode and saving)
  153. QString m_sequenceName;
  154. //! Time of active sequence (for switching back from game mode and saving)
  155. float m_sequenceTime;
  156. bool m_bLooping;
  157. //! True if editor is recording animations now.
  158. bool m_recording;
  159. bool m_bSavedRecordingState;
  160. //! True if editor is playing animation now.
  161. bool m_playing;
  162. //! Stores how many times animation have been paused prior to calling resume.
  163. int m_paused;
  164. bool m_bSingleFrame;
  165. bool m_bPostRenderRegistered;
  166. bool m_bForcingAnimation;
  167. //! Listeners
  168. std::vector<IUiAnimationContextListener*> m_contextListeners;
  169. };