AnimationContext.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #ifndef CRYINCLUDE_EDITOR_ANIMATIONCONTEXT_H
  9. #define CRYINCLUDE_EDITOR_ANIMATIONCONTEXT_H
  10. #pragma once
  11. #include "Undo/Undo.h"
  12. #include "TrackView/TrackViewSequenceManager.h"
  13. #include <Range.h>
  14. #include <IMovieSystem.h>
  15. class CTrackViewSequence;
  16. /** CAnimationContext listener interface
  17. */
  18. struct IAnimationContextListener
  19. {
  20. virtual void OnSequenceChanged([[maybe_unused]] CTrackViewSequence* pNewSequence) {}
  21. virtual void OnTimeChanged([[maybe_unused]] float newTime) {}
  22. };
  23. AZ_PUSH_DISABLE_DLL_EXPORT_BASECLASS_WARNING
  24. /** CAnimationContext stores information about current editable animation sequence.
  25. Stores information about whenever animation is being recorded know,
  26. current sequence, current time in sequence etc.
  27. */
  28. class SANDBOX_API CAnimationContext
  29. : public IEditorNotifyListener
  30. , public IUndoManagerListener
  31. , public ITrackViewSequenceManagerListener
  32. {
  33. AZ_POP_DISABLE_DLL_EXPORT_BASECLASS_WARNING
  34. public:
  35. //////////////////////////////////////////////////////////////////////////
  36. // Constructors.
  37. //////////////////////////////////////////////////////////////////////////
  38. /** Constructor.
  39. */
  40. CAnimationContext();
  41. ~CAnimationContext();
  42. //////////////////////////////////////////////////////////////////////////
  43. // Accessors
  44. //////////////////////////////////////////////////////////////////////////
  45. void Init();
  46. // Listeners
  47. void AddListener(IAnimationContextListener* pListener);
  48. void RemoveListener(IAnimationContextListener* pListener);
  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 sequence New active sequence.
  57. @param force Set to true to always run all of the new active sequence code including listeners even if the sequences is already selected.
  58. @param noNotify Set to true to skip over notifying listeners when a new sequences is selected.
  59. @param user Set to true if the new sequence is being selected by the user, false if set by internal system code.
  60. */
  61. void SetSequence(CTrackViewSequence* sequence, bool force, bool noNotify, bool user = false);
  62. /** Get currently edited sequence.
  63. */
  64. CTrackViewSequence* GetSequence() const { return m_pSequence; };
  65. /** Set time markers to play within.
  66. */
  67. void SetMarkers(Range Marker) { m_timeMarker = Marker; }
  68. /** Get time markers to play within.
  69. */
  70. Range GetMarkers() { return m_timeMarker; }
  71. /** Get time range of active animation sequence.
  72. */
  73. Range GetTimeRange() const { return m_timeRange; }
  74. /** Returns true if editor is recording animations now.
  75. */
  76. bool IsRecording() const { return m_recording && m_paused == 0; };
  77. /** Returns true if editor is playing animation now.
  78. */
  79. bool IsPlaying() const { return m_playing && m_paused == 0; };
  80. /** Returns true if currently playing or recording is paused.
  81. */
  82. bool IsPaused() const { return m_paused > 0; }
  83. /** Return if animation context is now in playing mode.
  84. In difference from IsPlaying function this function not affected by pause state.
  85. */
  86. bool IsPlayMode() const { return m_playing; };
  87. /** Return if animation context is now in recording mode.
  88. In difference from IsRecording function this function not affected by pause state.
  89. */
  90. bool IsRecordMode() const { return m_recording; };
  91. /** Returns true if currently looping as activated.
  92. */
  93. bool IsLoopMode() const { return m_bLooping; }
  94. /** Enable/Disable looping.
  95. */
  96. void SetLoopMode(bool bLooping) { m_bLooping = bLooping; }
  97. //////////////////////////////////////////////////////////////////////////
  98. // Operators
  99. //////////////////////////////////////////////////////////////////////////
  100. /** Set current animation time in active sequence.
  101. @param seq New active time.
  102. */
  103. void SetTime(float t);
  104. /** Set time in active sequence for reset animation.
  105. @param seq New active time.
  106. */
  107. void SetResetTime(float t) {m_resetTime = t; };
  108. /** Start animation recorduing.
  109. Automatically stop playing.
  110. @param recording True to start recording, false to stop.
  111. */
  112. void SetRecording(bool playing);
  113. /** Enables/Disables automatic recording, sets the time step for each recorded frame.
  114. */
  115. void SetAutoRecording(bool bEnable, float fTimeStep);
  116. //! Check if auto recording enabled.
  117. bool IsAutoRecording() const { return m_bAutoRecording; };
  118. /** Start/Stop animation playing.
  119. Automatically stop recording.
  120. @param playing True to start playing, false to stop.
  121. */
  122. void SetPlaying(bool playing);
  123. /** Pause animation playing/recording.
  124. */
  125. void Pause();
  126. /** Toggle playback
  127. */
  128. void TogglePlay();
  129. /** Resume animation playing/recording.
  130. */
  131. void Resume();
  132. /** Called every frame to update all animations if animation should be playing.
  133. */
  134. void Update();
  135. /** Force animation for current sequence.
  136. */
  137. void ForceAnimation();
  138. void OnPostRender();
  139. void UpdateTimeRange();
  140. /** Notify after a time change is complete and time control is released to 'playback' controls, for example after
  141. * a timeline drag
  142. */
  143. void TimeChanged(float newTime);
  144. /** Notify after a sequence has been activated, useful for Undo/Redo
  145. */
  146. void OnSequenceActivated(AZ::EntityId entityId);
  147. private:
  148. static void GoToFrameCmd(IConsoleCmdArgs* pArgs);
  149. void NotifyTimeChangedListenersUsingCurrTime() const;
  150. virtual void BeginUndoTransaction() override;
  151. virtual void EndUndoTransaction() override;
  152. virtual void OnSequenceRemoved(CTrackViewSequence* pSequence) override;
  153. virtual void OnEditorNotifyEvent(EEditorNotifyEvent event) override;
  154. void AnimateActiveSequence();
  155. void SetRecordingInternal(bool enableRecording);
  156. //! Current time within active animation sequence.
  157. float m_currTime;
  158. //! Used to stash the time we send out OnTimeChanged notifications
  159. mutable float m_lastTimeChangedNotificationTime;
  160. //! Force update in next frame
  161. bool m_bForceUpdateInNextFrame;
  162. //! Time within active animation sequence while reset animation.
  163. float m_resetTime;
  164. float m_fTimeScale;
  165. // Recording time step.
  166. float m_fRecordingTimeStep;
  167. float m_fRecordingCurrTime;
  168. bool m_bAutoRecording;
  169. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  170. //! Time range of active animation sequence.
  171. Range m_timeRange;
  172. Range m_timeMarker;
  173. //! Currently active animation sequence.
  174. CTrackViewSequence* m_pSequence;
  175. //! Id of latest valid sequence that was selected. Useful for restoring the selected
  176. //! sequence after undo has destroyed and recreated it.
  177. AZ::EntityId m_mostRecentSequenceId;
  178. //! The current time of the most recent selected sequence. It's very useful to restore this after an undo.
  179. float m_mostRecentSequenceTime;
  180. //! Id of active sequence to restore (for switching back from game mode and saving)
  181. AZ::EntityId m_sequenceToRestore;
  182. //! Time of active sequence (for switching back from game mode and saving)
  183. float m_sequenceRestoreTime;
  184. bool m_bLooping;
  185. //! True if editor is recording animations now.
  186. bool m_recording;
  187. bool m_bSavedRecordingState;
  188. //! True if editor is playing animation now.
  189. bool m_playing;
  190. //! Stores how many times animation have been paused prior to calling resume.
  191. int m_paused;
  192. bool m_bSingleFrame;
  193. bool m_bPostRenderRegistered;
  194. bool m_bForcingAnimation;
  195. //! Listeners
  196. std::vector<IAnimationContextListener*> m_contextListeners;
  197. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  198. };
  199. #endif // CRYINCLUDE_EDITOR_ANIMATIONCONTEXT_H