AnimationContext.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <AzToolsFramework/Prefab/PrefabPublicNotificationBus.h>
  15. #include <CryCommon/Maestro/Bus/SequenceComponentBus.h>
  16. struct IMovieSystem;
  17. class CTrackViewSequence;
  18. /** CAnimationContext listener interface
  19. */
  20. struct IAnimationContextListener
  21. {
  22. virtual void OnSequenceChanged([[maybe_unused]] CTrackViewSequence* pNewSequence) {}
  23. virtual void OnTimeChanged([[maybe_unused]] float newTime) {}
  24. };
  25. /** CAnimationContext stores information about current editable animation sequence.
  26. Stores information about whenever animation is being recorded know,
  27. current sequence, current time in sequence etc.
  28. */
  29. class CAnimationContext
  30. : public IEditorNotifyListener
  31. , public IUndoManagerListener
  32. , public ITrackViewSequenceManagerListener
  33. , public AzToolsFramework::Prefab::PrefabPublicNotificationBus::Handler
  34. , public Maestro::SequenceComponentNotificationBus::Handler
  35. {
  36. public:
  37. //////////////////////////////////////////////////////////////////////////
  38. // Constructors.
  39. //////////////////////////////////////////////////////////////////////////
  40. /** Constructor.
  41. */
  42. CAnimationContext();
  43. ~CAnimationContext();
  44. //////////////////////////////////////////////////////////////////////////
  45. // Accessors
  46. //////////////////////////////////////////////////////////////////////////
  47. void Init();
  48. // Listeners
  49. void AddListener(IAnimationContextListener* pListener);
  50. void RemoveListener(IAnimationContextListener* pListener);
  51. /** Return current animation time in active sequence.
  52. @return Current time.
  53. */
  54. float GetTime() const { return m_currTime; };
  55. float GetTimeScale() const { return m_fTimeScale; }
  56. void SetTimeScale(float fScale) { m_fTimeScale = fScale; }
  57. /** Set active editing sequence.
  58. @param sequence New active sequence.
  59. @param force Set to true to always run all of the new active sequence code including listeners even if the sequences is already selected.
  60. @param noNotify Set to true to skip over notifying listeners when a new sequences is selected.
  61. @param user Set to true if the new sequence is being selected by the user, false if set by internal system code.
  62. */
  63. void SetSequence(CTrackViewSequence* sequence, bool force, bool noNotify, bool user = false);
  64. /** Get currently edited sequence.
  65. */
  66. CTrackViewSequence* GetSequence() const { return m_pSequence; };
  67. /** Set time markers to play within.
  68. */
  69. void SetMarkers(Range Marker) { m_timeMarker = Marker; }
  70. /** Get time markers to play within.
  71. */
  72. Range GetMarkers() { return m_timeMarker; }
  73. /** Get time range of active animation sequence.
  74. */
  75. Range GetTimeRange() const { return m_timeRange; }
  76. /** Returns true if editor is recording animations now.
  77. */
  78. bool IsRecording() const { return m_recording && m_paused == 0; };
  79. /** Returns true if editor is playing animation now.
  80. */
  81. bool IsPlaying() const { return m_playing && m_paused == 0; };
  82. /** Returns true if currently playing or recording is paused.
  83. */
  84. bool IsPaused() const { return m_paused > 0; }
  85. /** Return if animation context is now in playing mode.
  86. In difference from IsPlaying function this function not affected by pause state.
  87. */
  88. bool IsPlayMode() const { return m_playing; };
  89. /** Return if animation context is now in recording mode.
  90. In difference from IsRecording function this function not affected by pause state.
  91. */
  92. bool IsRecordMode() const { return m_recording; };
  93. /** Returns true if currently looping as activated.
  94. */
  95. bool IsLoopMode() const { return m_bLooping; }
  96. /** Enable/Disable looping.
  97. */
  98. void SetLoopMode(bool bLooping) { m_bLooping = bLooping; }
  99. //////////////////////////////////////////////////////////////////////////
  100. // Operators
  101. //////////////////////////////////////////////////////////////////////////
  102. /** Set current animation time in active sequence.
  103. @param seq New active time.
  104. */
  105. void SetTime(float t);
  106. /** Set time in active sequence for reset animation.
  107. @param seq New active time.
  108. */
  109. void SetResetTime(float t) {m_resetTime = t; };
  110. /** Start animation recording.
  111. Automatically stop playing.
  112. @param recording True to start recording, false to stop.
  113. */
  114. void SetRecording(bool playing);
  115. /** Enables/Disables automatic recording, sets the time step for each recorded frame.
  116. */
  117. void SetAutoRecording(bool bEnable, float fTimeStep);
  118. //! Check if auto recording enabled.
  119. bool IsAutoRecording() const { return m_bAutoRecording; };
  120. /** Start/Stop animation playing.
  121. Automatically stop recording.
  122. @param playing True to start playing, false to stop.
  123. */
  124. void SetPlaying(bool playing);
  125. /** Pause animation playing/recording.
  126. */
  127. void Pause();
  128. /** Toggle playback
  129. */
  130. void TogglePlay();
  131. /** Resume animation playing/recording.
  132. */
  133. void Resume();
  134. /** Called every frame to update all animations if animation should be playing.
  135. */
  136. void Update();
  137. /** Force animation for current sequence.
  138. */
  139. void ForceAnimation();
  140. void OnPostRender();
  141. void UpdateTimeRange();
  142. /** Notify after a time change is complete and time control is released to 'playback' controls, for example after
  143. * a timeline drag
  144. */
  145. void TimeChanged(float newTime);
  146. /** Notify after a sequence has been activated, useful for Undo/Redo
  147. */
  148. void OnSequenceActivated(AZ::EntityId entityId);
  149. void OnSequenceDeactivated(AZ::EntityId entityId);
  150. /**
  151. * Returns true if Editor is in the Play Game mode.
  152. */
  153. bool IsInGameMode() const;
  154. /**
  155. * Returns true if Editor is in the Editing mode.
  156. */
  157. bool IsInEditingMode() const;
  158. /**
  159. * Returns true if a sequence is active and has "Autostart" (IAnimSequence::eSeqFlags_PlayOnReset) flag set.
  160. */
  161. bool IsSequenceAutostartFlagOn() const;
  162. /**
  163. * Switch camera, only in Editing mode, in the active Editor Viewport Widget to the newCameraEntityId
  164. * (including invalid Id, which corresponds to the default Editor camera).
  165. */
  166. void SwitchEditorViewportCamera(const AZ::EntityId& newCameraEntityId);
  167. private:
  168. static void GoToFrameCmd(IConsoleCmdArgs* pArgs);
  169. void NotifyTimeChangedListenersUsingCurrTime() const;
  170. //! IUndoManagerListener overrides
  171. void BeginUndoTransaction() override;
  172. void EndUndoTransaction() override;
  173. //! PrefabPublicNotificationBus override
  174. void OnPrefabInstancePropagationEnd() override;
  175. //! ITrackViewSequenceManagerListener override
  176. void OnSequenceRemoved(CTrackViewSequence* pSequence) override;
  177. //! IEditorNotifyListener override
  178. void OnEditorNotifyEvent(EEditorNotifyEvent event) override;
  179. /** SequenceComponentNotificationBus override:
  180. * Switches camera Id in the active editor viewport when a Sequence changes camera during playback in Track View,
  181. * only if in Editing mode and if the "Autostart" flag is set in the active sequence.
  182. */
  183. void OnCameraChanged([[maybe_unused]] const AZ::EntityId& oldCameraEntityId, const AZ::EntityId& newCameraEntityId) override;
  184. void AnimateActiveSequence();
  185. void SetRecordingInternal(bool enableRecording);
  186. /**
  187. * Store an active sequence when switching from Editing mode to Game mode or Saving mode.
  188. * @param isSwitchingToGameMode True if the function is called when switching from Editing mode to Game mode.
  189. */
  190. void StoreSequenceOnExitingEditMode(bool isSwitchingToGameMode);
  191. //! Restore a previously active sequence when switching back from Game mode or Saving mode to Editing mode.
  192. void RestoreSequenceOnEnteringEditMode();
  193. //! Current time within active animation sequence.
  194. float m_currTime;
  195. //! Used to stash the time we send out OnTimeChanged notifications
  196. mutable float m_lastTimeChangedNotificationTime;
  197. //! Force update in next frame
  198. bool m_bForceUpdateInNextFrame;
  199. //! Time within active animation sequence while reset animation.
  200. float m_resetTime;
  201. float m_fTimeScale;
  202. // Recording time step.
  203. float m_fRecordingTimeStep;
  204. float m_fRecordingCurrTime;
  205. bool m_bAutoRecording;
  206. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  207. //! Time range of active animation sequence.
  208. Range m_timeRange;
  209. Range m_timeMarker;
  210. /** An entity ID of a current camera in an active editor viewport, at the moment a sequence is activated,
  211. * or invalid if the default editor camera is used at the moment.
  212. * Used to restore the default editor viewport camera when clearing "Autostart" property or deselecting a sequence.
  213. */
  214. AZ::EntityId m_defaulViewCameraEntityId = AZ::EntityId();
  215. //! Id of the active viewport camera to restore when switching back from game mode and saving mode to Editing mode.
  216. AZ::EntityId m_viewCameraEntityIdToRestore = AZ::EntityId();
  217. /** Switched On with EEditorNotifyEvent::eNotify_OnBeginGameMode,
  218. * switched Off - after 2 frames in Update() since receiving EEditorNotifyEvent::eNotify_OnEndGameModein.
  219. */
  220. bool m_bIsInGameMode = false;
  221. /** Set to a positive value with EEditorNotifyEvent::eNotify_OnEndGameMode, in order to delay restoring a previously active
  222. * sequence and Editor Viewport camera, and then resetting m_bIsInGameMode. Decreased to 0 in Update().
  223. * Currently skipping 2 frames is needed, as Editor Viewport state goes from "Started" to "Stopping" and finally back to "Editor".
  224. */
  225. int m_countWaitingForExitingGameMode = 0;
  226. //! Currently active animation sequence.
  227. CTrackViewSequence* m_pSequence;
  228. /** Id of latest valid sequence that was selected. Useful for restoring the selected
  229. * sequence after undo has destroyed and recreated it.
  230. */
  231. AZ::EntityId m_mostRecentSequenceId;
  232. //! The current time of the most recent selected sequence. It's very useful to restore this after an undo.
  233. float m_mostRecentSequenceTime;
  234. //! Id of active sequence to restore (for switching back from game mode and saving)
  235. AZ::EntityId m_sequenceToRestore;
  236. //! Time of active sequence (for switching back from game mode and saving)
  237. float m_sequenceRestoreTime;
  238. bool m_bLooping;
  239. //! True if editor is recording animations now.
  240. bool m_recording;
  241. bool m_bSavedRecordingState;
  242. //! True if editor is playing animation now.
  243. bool m_playing;
  244. //! Stores how many times animation have been paused prior to calling resume.
  245. int m_paused;
  246. bool m_bSingleFrame;
  247. bool m_bPostRenderRegistered;
  248. bool m_bForcingAnimation;
  249. IMovieSystem* m_movieSystem;
  250. //! Listeners
  251. std::vector<IAnimationContextListener*> m_contextListeners;
  252. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  253. };
  254. #endif // CRYINCLUDE_EDITOR_ANIMATIONCONTEXT_H