DirectorNodeAnimator.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include "EditorDefs.h"
  9. #include "DirectorNodeAnimator.h"
  10. // CryCommon
  11. #include <CryCommon/Maestro/Types/AnimParamType.h> // for AnimParamType
  12. // Editor
  13. #include "TrackView/TrackViewSequenceManager.h" // for CTrackViewSequence
  14. // AzCore
  15. #include <AzCore/std/algorithm.h>
  16. #include <AzCore/std/containers/vector.h>
  17. CDirectorNodeAnimator::CDirectorNodeAnimator([[maybe_unused]] CTrackViewAnimNode* pDirectorNode)
  18. {
  19. }
  20. void CDirectorNodeAnimator::Animate(CTrackViewAnimNode* pNode, const SAnimContext& ac)
  21. {
  22. if (!pNode->IsActiveDirector())
  23. {
  24. // Don't animate if it's not the sequence track of the active director
  25. return;
  26. }
  27. CTrackViewTrack* pSequenceTrack = pNode->GetTrackForParameter(AnimParamType::Sequence);
  28. if (pSequenceTrack && !pSequenceTrack->IsDisabled())
  29. {
  30. AZStd::vector<CTrackViewSequence*> inactiveSequences;
  31. AZStd::vector<CTrackViewSequence*> activeSequences;
  32. // Construct sets of sequences that need to be bound/unbound at this point
  33. const float time = ac.time;
  34. const unsigned int numKeys = pSequenceTrack->GetKeyCount();
  35. for (unsigned int i = 0; i < numKeys; ++i)
  36. {
  37. CTrackViewKeyHandle keyHandle = pSequenceTrack->GetKey(i);
  38. ISequenceKey sequenceKey;
  39. keyHandle.GetKey(&sequenceKey);
  40. CTrackViewSequence* pSequence = GetSequenceFromSequenceKey(sequenceKey);
  41. if (pSequence)
  42. {
  43. if (sequenceKey.time <= time)
  44. {
  45. stl::push_back_unique(activeSequences, pSequence);
  46. stl::find_and_erase(inactiveSequences, pSequence);
  47. }
  48. else
  49. {
  50. if (!stl::find(activeSequences, pSequence))
  51. {
  52. stl::push_back_unique(inactiveSequences, pSequence);
  53. }
  54. }
  55. }
  56. }
  57. // Unbind must occur before binding, because entities can be referenced in multiple sequences
  58. for (auto iter = inactiveSequences.begin(); iter != inactiveSequences.end(); ++iter)
  59. {
  60. CTrackViewSequence* pSequence = *iter;
  61. if (pSequence->IsBoundToEditorObjects())
  62. {
  63. // No notifications because unbinding would call ForceAnimation again
  64. CTrackViewSequenceNoNotificationContext context(pSequence);
  65. pSequence->UnBindFromEditorObjects();
  66. }
  67. }
  68. // Now bind sequences
  69. for (auto iter = activeSequences.begin(); iter != activeSequences.end(); ++iter)
  70. {
  71. CTrackViewSequence* pSequence = *iter;
  72. if (!pSequence->IsBoundToEditorObjects())
  73. {
  74. // No notifications because binding would call ForceAnimation again
  75. CTrackViewSequenceNoNotificationContext context(pSequence);
  76. pSequence->BindToEditorObjects();
  77. // Make sure the sequence is active, harmless to call if the sequences is already
  78. // active. The sequence may not be active in the Editor if this key was just created.
  79. pSequence->Activate();
  80. }
  81. }
  82. // Animate sub sequences
  83. ForEachActiveSequence(ac, pSequenceTrack, true,
  84. [&](CTrackViewSequence* pSequence, const SAnimContext& newAnimContext)
  85. {
  86. pSequence->Animate(newAnimContext);
  87. },
  88. [&](CTrackViewSequence* pSequence, [[maybe_unused]] const SAnimContext& newAnimContext)
  89. {
  90. pSequence->Reset(false);
  91. }
  92. );
  93. }
  94. }
  95. void CDirectorNodeAnimator::Render(CTrackViewAnimNode* pNode, const SAnimContext& ac)
  96. {
  97. if (!pNode->IsActiveDirector())
  98. {
  99. // Don't animate if it's not the sequence track of the active director
  100. return;
  101. }
  102. CTrackViewTrack* pSequenceTrack = pNode->GetTrackForParameter(AnimParamType::Sequence);
  103. if (pSequenceTrack && !pSequenceTrack->IsDisabled())
  104. {
  105. // Render sub sequences
  106. ForEachActiveSequence(ac, pSequenceTrack, false,
  107. [&](CTrackViewSequence* pSequence, [[maybe_unused]] const SAnimContext& newAnimContext)
  108. {
  109. pSequence->Render(newAnimContext);
  110. },
  111. [&]([[maybe_unused]] CTrackViewSequence* pSequence, [[maybe_unused]] const SAnimContext& newAnimContext) {}
  112. );
  113. }
  114. }
  115. void CDirectorNodeAnimator::ForEachActiveSequence(const SAnimContext& ac, CTrackViewTrack* pSequenceTrack,
  116. const bool bHandleOtherKeys, std::function<void(CTrackViewSequence*, const SAnimContext&)> animateFunction,
  117. std::function<void(CTrackViewSequence*, const SAnimContext&)> resetFunction)
  118. {
  119. const unsigned int numKeys = pSequenceTrack->GetKeyCount();
  120. if (bHandleOtherKeys)
  121. {
  122. // Reset all non-active sequences first
  123. for (unsigned int i = 0; i < numKeys; ++i)
  124. {
  125. CTrackViewKeyHandle keyHandle = pSequenceTrack->GetKey(i);
  126. ISequenceKey sequenceKey;
  127. keyHandle.GetKey(&sequenceKey);
  128. CTrackViewSequence* pSequence = GetSequenceFromSequenceKey(sequenceKey);
  129. if (pSequence)
  130. {
  131. SAnimContext newAnimContext = ac;
  132. const float duration = sequenceKey.fDuration;
  133. const float sequenceTime = ac.time - sequenceKey.time + sequenceKey.fStartTime;
  134. const float sequenceDuration = duration + sequenceKey.fStartTime;
  135. newAnimContext.time = AZStd::min(sequenceTime, sequenceDuration);
  136. const bool bInsideKeyRange = (sequenceTime >= 0.0f) && (sequenceTime <= sequenceDuration);
  137. if (!bInsideKeyRange)
  138. {
  139. if (ac.forcePlay && sequenceTime >= 0.0f && newAnimContext.time != pSequence->GetTime())
  140. {
  141. // If forcing animation force previous keys to their last playback position
  142. animateFunction(pSequence, newAnimContext);
  143. }
  144. resetFunction(pSequence, newAnimContext);
  145. }
  146. }
  147. }
  148. }
  149. for (unsigned int i = 0; i < numKeys; ++i)
  150. {
  151. CTrackViewKeyHandle keyHandle = pSequenceTrack->GetKey(i);
  152. ISequenceKey sequenceKey;
  153. keyHandle.GetKey(&sequenceKey);
  154. CTrackViewSequence* pSequence = GetSequenceFromSequenceKey(sequenceKey);
  155. if (pSequence)
  156. {
  157. SAnimContext newAnimContext = ac;
  158. const float duration = sequenceKey.fDuration;
  159. const float sequenceTime = ac.time - sequenceKey.time + sequenceKey.fStartTime;
  160. const float sequenceDuration = duration + sequenceKey.fStartTime;
  161. newAnimContext.time = AZStd::min(sequenceTime, sequenceDuration);
  162. const bool bInsideKeyRange = (sequenceTime >= 0.0f) && (sequenceTime <= sequenceDuration);
  163. if ((bInsideKeyRange && (newAnimContext.time != pSequence->GetTime() || ac.forcePlay)))
  164. {
  165. animateFunction(pSequence, newAnimContext);
  166. }
  167. }
  168. }
  169. }
  170. void CDirectorNodeAnimator::UnBind([[maybe_unused]] CTrackViewAnimNode* pNode)
  171. {
  172. const CTrackViewSequenceManager* pSequenceManager = GetIEditor()->GetSequenceManager();
  173. const unsigned int numSequences = pSequenceManager->GetCount();
  174. for (unsigned int sequenceIndex = 0; sequenceIndex < numSequences; ++sequenceIndex)
  175. {
  176. CTrackViewSequence* pSequence = pSequenceManager->GetSequenceByIndex(sequenceIndex);
  177. if (pSequence->IsActiveSequence())
  178. {
  179. // Don't care about the active sequence
  180. continue;
  181. }
  182. if (pSequence->IsBoundToEditorObjects())
  183. {
  184. pSequence->UnBindFromEditorObjects();
  185. }
  186. }
  187. }
  188. /*static*/ CTrackViewSequence* CDirectorNodeAnimator::GetSequenceFromSequenceKey(const ISequenceKey& sequenceKey)
  189. {
  190. CTrackViewSequence* retSequence = nullptr;
  191. const CTrackViewSequenceManager* sequenceManager = GetIEditor()->GetSequenceManager();
  192. if (sequenceManager)
  193. {
  194. if (sequenceKey.sequenceEntityId.IsValid())
  195. {
  196. retSequence = sequenceManager->GetSequenceByEntityId(sequenceKey.sequenceEntityId);
  197. AZ_Assert(retSequence, "Null sequence returned when a Sequence Component was expected.");
  198. }
  199. }
  200. return retSequence;
  201. }