TrackViewSequenceManager.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 "TrackViewSequenceManager.h"
  10. // AzToolsFramework
  11. #include <AzToolsFramework/API/ComponentEntityObjectBus.h>
  12. #include <AzToolsFramework/API/EntityCompositionRequestBus.h>
  13. // CryCommon
  14. #include <CryCommon/Maestro/Bus/EditorSequenceComponentBus.h>
  15. #include <CryCommon/Maestro/Types/SequenceType.h>
  16. // AzCore
  17. #include <AzCore/std/sort.h>
  18. // Editor
  19. #include "AnimationContext.h"
  20. #include "GameEngine.h"
  21. ////////////////////////////////////////////////////////////////////////////
  22. CTrackViewSequenceManager::CTrackViewSequenceManager()
  23. {
  24. GetIEditor()->RegisterNotifyListener(this);
  25. AZ::EntitySystemBus::Handler::BusConnect();
  26. }
  27. ////////////////////////////////////////////////////////////////////////////
  28. CTrackViewSequenceManager::~CTrackViewSequenceManager()
  29. {
  30. AZ::EntitySystemBus::Handler::BusDisconnect();
  31. GetIEditor()->UnregisterNotifyListener(this);
  32. }
  33. ////////////////////////////////////////////////////////////////////////////
  34. void CTrackViewSequenceManager::OnEditorNotifyEvent(EEditorNotifyEvent event)
  35. {
  36. switch (event)
  37. {
  38. case eNotify_OnBeginGameMode:
  39. ResumeAllSequences();
  40. break;
  41. case eNotify_OnCloseScene:
  42. // Fall through
  43. case eNotify_OnBeginLoad:
  44. m_bUnloadingLevel = true;
  45. break;
  46. case eNotify_OnEndNewScene:
  47. // Fall through
  48. case eNotify_OnEndSceneOpen:
  49. // Fall through
  50. case eNotify_OnEndLoad:
  51. // Fall through
  52. case eNotify_OnLayerImportEnd:
  53. m_bUnloadingLevel = false;
  54. SortSequences();
  55. break;
  56. }
  57. }
  58. ////////////////////////////////////////////////////////////////////////////
  59. CTrackViewSequence* CTrackViewSequenceManager::GetSequenceByName(QString name) const
  60. {
  61. for (auto iter = m_sequences.begin(); iter != m_sequences.end(); ++iter)
  62. {
  63. CTrackViewSequence* sequence = (*iter).get();
  64. if (QString::fromUtf8(sequence->GetName().c_str()) == name)
  65. {
  66. return sequence;
  67. }
  68. }
  69. return nullptr;
  70. }
  71. ////////////////////////////////////////////////////////////////////////////
  72. CTrackViewSequence* CTrackViewSequenceManager::GetSequenceByEntityId(const AZ::EntityId& entityId) const
  73. {
  74. for (auto iter = m_sequences.begin(); iter != m_sequences.end(); ++iter)
  75. {
  76. CTrackViewSequence* sequence = (*iter).get();
  77. if (sequence->GetSequenceComponentEntityId() == entityId)
  78. {
  79. return sequence;
  80. }
  81. }
  82. return nullptr;
  83. }
  84. ////////////////////////////////////////////////////////////////////////////
  85. CTrackViewSequence* CTrackViewSequenceManager::GetSequenceByAnimSequence(IAnimSequence* pAnimSequence) const
  86. {
  87. for (auto iter = m_sequences.begin(); iter != m_sequences.end(); ++iter)
  88. {
  89. CTrackViewSequence* sequence = (*iter).get();
  90. if (sequence->m_pAnimSequence == pAnimSequence)
  91. {
  92. return sequence;
  93. }
  94. }
  95. return nullptr;
  96. }
  97. ////////////////////////////////////////////////////////////////////////////
  98. CTrackViewSequence* CTrackViewSequenceManager::GetSequenceByIndex(unsigned int index) const
  99. {
  100. if (index >= m_sequences.size())
  101. {
  102. return nullptr;
  103. }
  104. return m_sequences[index].get();
  105. }
  106. ////////////////////////////////////////////////////////////////////////////
  107. void CTrackViewSequenceManager::CreateSequence(QString name, [[maybe_unused]] SequenceType sequenceType)
  108. {
  109. CGameEngine* pGameEngine = GetIEditor()->GetGameEngine();
  110. if (!pGameEngine || !pGameEngine->IsLevelLoaded())
  111. {
  112. return;
  113. }
  114. CTrackViewSequence* pExistingSequence = GetSequenceByName(name);
  115. if (pExistingSequence)
  116. {
  117. return;
  118. }
  119. AzToolsFramework::ScopedUndoBatch undoBatch("Create TrackView Sequence");
  120. // create AZ::Entity at the current center of the viewport, but don't select it
  121. // Store the current selection for selection restore after the sequence component is created
  122. AzToolsFramework::EntityIdList selectedEntities;
  123. AzToolsFramework::ToolsApplicationRequests::Bus::BroadcastResult(selectedEntities, &AzToolsFramework::ToolsApplicationRequests::Bus::Events::GetSelectedEntities);
  124. AZ::EntityId newEntityId; // initialized with InvalidEntityId
  125. AzToolsFramework::EditorRequests::Bus::BroadcastResult(
  126. newEntityId, &AzToolsFramework::EditorRequests::Bus::Events::CreateNewEntity, AZ::EntityId());
  127. if (newEntityId.IsValid())
  128. {
  129. // set the entity name
  130. AZ::Entity* entity = nullptr;
  131. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationBus::Events::FindEntity, newEntityId);
  132. if (entity)
  133. {
  134. entity->SetName(static_cast<const char*>(name.toUtf8().data()));
  135. }
  136. // add the SequenceComponent. The SequenceComponent's Init() method will call OnCreateSequenceObject() which will actually create
  137. // the sequence and connect it
  138. // #TODO LY-21846: Use "SequenceService" to find component, rather than specific component-type.
  139. AzToolsFramework::EntityCompositionRequestBus::Broadcast(&AzToolsFramework::EntityCompositionRequests::AddComponentsToEntities, AzToolsFramework::EntityIdList{ newEntityId }, AZ::ComponentTypeList{ AZ::TypeId("{C02DC0E2-D0F3-488B-B9EE-98E28077EC56}") });
  140. // restore the Editor selection
  141. AzToolsFramework::ToolsApplicationRequests::Bus::Broadcast(&AzToolsFramework::ToolsApplicationRequests::Bus::Events::SetSelectedEntities, selectedEntities);
  142. undoBatch.MarkEntityDirty(newEntityId);
  143. }
  144. }
  145. ////////////////////////////////////////////////////////////////////////////
  146. IAnimSequence* CTrackViewSequenceManager::OnCreateSequenceObject(QString name, bool isLegacySequence, AZ::EntityId entityId)
  147. {
  148. IMovieSystem* movieSystem = AZ::Interface<IMovieSystem>::Get();
  149. // Drop legacy sequences on the floor, they are no longer supported.
  150. if (isLegacySequence && movieSystem)
  151. {
  152. movieSystem->LogUserNotificationMsg(AZStd::string::format("Legacy Sequences are no longer supported. Skipping '%s'.", name.toUtf8().data()));
  153. return nullptr;
  154. }
  155. if (movieSystem)
  156. {
  157. IAnimSequence* sequence = movieSystem->CreateSequence(name.toUtf8().data(), /*bload =*/ false, /*id =*/ 0U, SequenceType::SequenceComponent, entityId);
  158. AZ_Assert(sequence, "Failed to create sequence");
  159. AddTrackViewSequence(new CTrackViewSequence(sequence));
  160. return sequence;
  161. }
  162. else
  163. {
  164. return nullptr;
  165. }
  166. }
  167. ////////////////////////////////////////////////////////////////////////////
  168. void CTrackViewSequenceManager::OnSequenceActivated(const AZ::EntityId& entityId)
  169. {
  170. CAnimationContext* pAnimationContext = GetIEditor()->GetAnimation();
  171. if (pAnimationContext != nullptr)
  172. {
  173. pAnimationContext->OnSequenceActivated(entityId);
  174. }
  175. }
  176. void CTrackViewSequenceManager::OnSequenceDeactivated(const AZ::EntityId& entityId)
  177. {
  178. CAnimationContext* pAnimationContext = GetIEditor()->GetAnimation();
  179. if (pAnimationContext != nullptr)
  180. {
  181. pAnimationContext->OnSequenceDeactivated(entityId);
  182. }
  183. }
  184. ////////////////////////////////////////////////////////////////////////////
  185. void CTrackViewSequenceManager::OnCreateSequenceComponent(AZStd::intrusive_ptr<IAnimSequence>& sequence)
  186. {
  187. // Fix up the internal pointers in the sequence to match the deserialized structure
  188. sequence->InitPostLoad();
  189. IMovieSystem* movieSystem = AZ::Interface<IMovieSystem>::Get();
  190. // Add the sequence to the movie system
  191. if (movieSystem)
  192. {
  193. movieSystem->AddSequence(sequence.get());
  194. }
  195. // Create the TrackView Sequence
  196. CTrackViewSequence* newTrackViewSequence = new CTrackViewSequence(sequence);
  197. AddTrackViewSequence(newTrackViewSequence);
  198. }
  199. ////////////////////////////////////////////////////////////////////////////
  200. void CTrackViewSequenceManager::AddTrackViewSequence(CTrackViewSequence* sequenceToAdd)
  201. {
  202. m_sequences.push_back(std::unique_ptr<CTrackViewSequence>(sequenceToAdd));
  203. SortSequences();
  204. OnSequenceAdded(sequenceToAdd);
  205. }
  206. ////////////////////////////////////////////////////////////////////////////
  207. void CTrackViewSequenceManager::DeleteSequence(CTrackViewSequence* sequence)
  208. {
  209. const int numSequences = static_cast<int>(m_sequences.size());
  210. for (int sequenceIndex = 0; sequenceIndex < numSequences; ++sequenceIndex)
  211. {
  212. if (m_sequences[sequenceIndex].get() == sequence)
  213. {
  214. AzToolsFramework::ScopedUndoBatch undoBatch("Delete TrackView Sequence");
  215. // delete Sequence Component (and entity if there's no other components left on the entity except for the Transform Component)
  216. AZ::Entity* entity = nullptr;
  217. AZ::EntityId entityId = sequence->m_pAnimSequence->GetSequenceEntityId();
  218. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationBus::Events::FindEntity, entityId);
  219. if (entity)
  220. {
  221. const AZ::Uuid editorSequenceComponentTypeId(EditorSequenceComponentTypeId);
  222. AZ::Component* sequenceComponent = entity->FindComponent(editorSequenceComponentTypeId);
  223. if (sequenceComponent)
  224. {
  225. AZ::ComponentTypeList requiredComponents;
  226. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(requiredComponents, &AzToolsFramework::EditorEntityContextRequestBus::Events::GetRequiredComponentTypes);
  227. const int numComponentToDeleteEntity = static_cast<int>(requiredComponents.size() + 1);
  228. AZ::Entity::ComponentArrayType entityComponents = entity->GetComponents();
  229. if (entityComponents.size() == numComponentToDeleteEntity)
  230. {
  231. // if the entity only has required components + 1 (the found sequenceComponent), delete the Entity. No need to start undo here
  232. // AzToolsFramework::ToolsApplicationRequests::DeleteEntities will take care of that
  233. AzToolsFramework::EntityIdList entitiesToDelete;
  234. entitiesToDelete.push_back(entityId);
  235. AzToolsFramework::ToolsApplicationRequests::Bus::Broadcast(&AzToolsFramework::ToolsApplicationRequests::DeleteEntities, entitiesToDelete);
  236. }
  237. else
  238. {
  239. // just remove the sequence component from the entity
  240. CUndo undo("Delete TrackView Sequence");
  241. AzToolsFramework::EntityCompositionRequestBus::Broadcast(&AzToolsFramework::EntityCompositionRequests::RemoveComponents, AZ::Entity::ComponentArrayType{ sequenceComponent });
  242. }
  243. undoBatch.MarkEntityDirty(entityId);
  244. }
  245. }
  246. // sequence was deleted, we can stop searching
  247. break;
  248. }
  249. }
  250. }
  251. //////////////////////////////////////////////////////////////////////////
  252. void CTrackViewSequenceManager::RenameNode(CTrackViewAnimNode* animNode, const char* newName) const
  253. {
  254. AZ::EntityId entityId;
  255. CTrackViewSequence* sequence = animNode->GetSequence();
  256. AZ_Assert(sequence, "Nodes should never have a null sequence.");
  257. if (animNode->IsBoundToEditorObjects())
  258. {
  259. if (animNode->GetNodeType() == eTVNT_Sequence)
  260. {
  261. CTrackViewSequence* sequenceNode = static_cast<CTrackViewSequence*>(animNode);
  262. entityId = sequenceNode->GetSequenceComponentEntityId();
  263. }
  264. else if (animNode->GetNodeType() == eTVNT_AnimNode)
  265. {
  266. entityId = animNode->GetNodeEntityId();
  267. }
  268. }
  269. if (entityId.IsValid())
  270. {
  271. AzToolsFramework::ScopedUndoBatch undoBatch("ModifyEntityName");
  272. AZ::Entity* entity = nullptr;
  273. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationBus::Events::FindEntity, entityId);
  274. entity->SetName(newName);
  275. undoBatch.MarkEntityDirty(sequence->GetSequenceComponentEntityId());
  276. }
  277. else
  278. {
  279. AzToolsFramework::ScopedUndoBatch undoBatch("Rename TrackView Node");
  280. animNode->SetName(newName);
  281. undoBatch.MarkEntityDirty(sequence->GetSequenceComponentEntityId());
  282. }
  283. }
  284. void CTrackViewSequenceManager::RemoveSequenceInternal(CTrackViewSequence* sequence)
  285. {
  286. std::unique_ptr<CTrackViewSequence> storedTrackViewSequence;
  287. for (auto iter = m_sequences.begin(); iter != m_sequences.end(); ++iter)
  288. {
  289. std::unique_ptr<CTrackViewSequence>& currentSequence = *iter;
  290. if (currentSequence.get() == sequence)
  291. {
  292. // Hang onto this until we finish this function.
  293. currentSequence.swap(storedTrackViewSequence);
  294. // Remove from CryMovie and TrackView
  295. m_sequences.erase(iter);
  296. IMovieSystem* movieSystem = AZ::Interface<IMovieSystem>::Get();
  297. if (movieSystem)
  298. {
  299. movieSystem->RemoveSequence(sequence->m_pAnimSequence.get());
  300. }
  301. break;
  302. }
  303. }
  304. OnSequenceRemoved(sequence);
  305. }
  306. ////////////////////////////////////////////////////////////////////////////
  307. void CTrackViewSequenceManager::OnDeleteSequenceEntity(const AZ::EntityId& entityId)
  308. {
  309. CTrackViewSequence* sequence = GetSequenceByEntityId(entityId);
  310. assert(sequence);
  311. if (sequence)
  312. {
  313. const bool bUndoWasSuspended = GetIEditor()->IsUndoSuspended();
  314. bool isDuringUndo = false;
  315. AzToolsFramework::ToolsApplicationRequests::Bus::BroadcastResult(isDuringUndo, &AzToolsFramework::ToolsApplicationRequests::Bus::Events::IsDuringUndoRedo);
  316. if (bUndoWasSuspended)
  317. {
  318. GetIEditor()->ResumeUndo();
  319. }
  320. RemoveSequenceInternal(sequence);
  321. if (bUndoWasSuspended)
  322. {
  323. GetIEditor()->SuspendUndo();
  324. }
  325. }
  326. }
  327. ////////////////////////////////////////////////////////////////////////////
  328. void CTrackViewSequenceManager::SortSequences()
  329. {
  330. AZStd::stable_sort(m_sequences.begin(), m_sequences.end(),
  331. [](const std::unique_ptr<CTrackViewSequence>& a, const std::unique_ptr<CTrackViewSequence>& b) -> bool
  332. {
  333. QString aName = QString::fromUtf8(a.get()->GetName().c_str());
  334. QString bName = QString::fromUtf8(b.get()->GetName().c_str());
  335. return aName < bName;
  336. });
  337. }
  338. ////////////////////////////////////////////////////////////////////////////
  339. void CTrackViewSequenceManager::ResumeAllSequences()
  340. {
  341. for (auto iter = m_sequences.begin(); iter != m_sequences.end(); ++iter)
  342. {
  343. CTrackViewSequence* sequence = (*iter).get();
  344. if (sequence)
  345. {
  346. sequence->Resume();
  347. }
  348. }
  349. }
  350. ////////////////////////////////////////////////////////////////////////////
  351. void CTrackViewSequenceManager::OnSequenceAdded(CTrackViewSequence* sequence)
  352. {
  353. for (auto iter = m_listeners.begin(); iter != m_listeners.end(); ++iter)
  354. {
  355. (*iter)->OnSequenceAdded(sequence);
  356. }
  357. }
  358. ////////////////////////////////////////////////////////////////////////////
  359. void CTrackViewSequenceManager::OnSequenceRemoved(CTrackViewSequence* sequence)
  360. {
  361. for (auto iter = m_listeners.begin(); iter != m_listeners.end(); ++iter)
  362. {
  363. (*iter)->OnSequenceRemoved(sequence);
  364. }
  365. }
  366. ////////////////////////////////////////////////////////////////////////////
  367. CTrackViewAnimNodeBundle CTrackViewSequenceManager::GetAllRelatedAnimNodes(const AZ::EntityId entityId) const
  368. {
  369. CTrackViewAnimNodeBundle nodeBundle;
  370. const uint sequenceCount = GetCount();
  371. for (uint sequenceIndex = 0; sequenceIndex < sequenceCount; ++sequenceIndex)
  372. {
  373. CTrackViewSequence* sequence = GetSequenceByIndex(sequenceIndex);
  374. nodeBundle.AppendAnimNodeBundle(sequence->GetAllOwnedNodes(entityId));
  375. }
  376. return nodeBundle;
  377. }
  378. ////////////////////////////////////////////////////////////////////////////
  379. CTrackViewAnimNode* CTrackViewSequenceManager::GetActiveAnimNode(const AZ::EntityId entityId) const
  380. {
  381. CTrackViewAnimNodeBundle nodeBundle = GetAllRelatedAnimNodes(entityId);
  382. const uint nodeCount = nodeBundle.GetCount();
  383. for (uint nodeIndex = 0; nodeIndex < nodeCount; ++nodeIndex)
  384. {
  385. CTrackViewAnimNode* animNode = nodeBundle.GetNode(nodeIndex);
  386. if (animNode->IsActive())
  387. {
  388. return animNode;
  389. }
  390. }
  391. return nullptr;
  392. }
  393. void CTrackViewSequenceManager::OnEntityNameChanged(const AZ::EntityId& entityId, const AZStd::string& name)
  394. {
  395. CTrackViewAnimNodeBundle bundle;
  396. // entity or component entity sequence object
  397. bundle = GetAllRelatedAnimNodes(entityId);
  398. // GetAllRelatedAnimNodes only accounts for entities in the sequences, not the sequence entities themselves. We additionally check
  399. // for sequence entities that have object as their entity object for renaming
  400. const uint sequenceCount = GetCount();
  401. for (uint sequenceIndex = 0; sequenceIndex < sequenceCount; ++sequenceIndex)
  402. {
  403. CTrackViewSequence* sequence = GetSequenceByIndex(sequenceIndex);
  404. if (entityId == sequence->GetSequenceComponentEntityId())
  405. {
  406. bundle.AppendAnimNode(sequence);
  407. }
  408. }
  409. const uint numAffectedNodes = bundle.GetCount();
  410. for (uint i = 0; i < numAffectedNodes; ++i)
  411. {
  412. CTrackViewAnimNode* animNode = bundle.GetNode(i);
  413. animNode->SetName(name.c_str());
  414. }
  415. if (numAffectedNodes > 0)
  416. {
  417. GetIEditor()->Notify(eNotify_OnReloadTrackView);
  418. }
  419. }
  420. void CTrackViewSequenceManager::OnEntityDestruction(const AZ::EntityId& entityId)
  421. {
  422. // we handle pre-delete instead of delete because GetAllRelatedAnimNodes() uses the ObjectManager to find node owners
  423. CTrackViewAnimNodeBundle bundle = GetAllRelatedAnimNodes(entityId);
  424. const uint numAffectedAnimNodes = bundle.GetCount();
  425. for (uint i = 0; i < numAffectedAnimNodes; ++i)
  426. {
  427. CTrackViewAnimNode* animNode = bundle.GetNode(i);
  428. animNode->OnEntityRemoved();
  429. }
  430. if (numAffectedAnimNodes > 0)
  431. {
  432. // Only reload track view if the object being deleted has related anim nodes.
  433. GetIEditor()->Notify(eNotify_OnReloadTrackView);
  434. }
  435. }