TrackViewSequenceManager.cpp 17 KB

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