TrackViewTrack.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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 "TrackViewTrack.h"
  10. // CryCommon
  11. #include <CryCommon/Maestro/Types/AnimParamType.h>
  12. // Editor
  13. #include "TrackViewSequence.h"
  14. #include "TrackViewNodeFactories.h"
  15. //////////////////////////////////////////////////////////////////////////
  16. void CTrackViewTrackBundle::AppendTrack(CTrackViewTrack* pTrack)
  17. {
  18. // Check if newly added key has different type than existing ones
  19. if (m_bAllOfSameType && m_tracks.size() > 0)
  20. {
  21. const CTrackViewTrack* pLastTrack = m_tracks.back();
  22. if (pTrack->GetParameterType() != pLastTrack->GetParameterType()
  23. || pTrack->GetCurveType() != pLastTrack->GetCurveType()
  24. || pTrack->GetValueType() != pLastTrack->GetValueType())
  25. {
  26. m_bAllOfSameType = false;
  27. }
  28. }
  29. stl::push_back_unique(m_tracks, pTrack);
  30. }
  31. //////////////////////////////////////////////////////////////////////////
  32. void CTrackViewTrackBundle::AppendTrackBundle(const CTrackViewTrackBundle& bundle)
  33. {
  34. for (auto iter = bundle.m_tracks.begin(); iter != bundle.m_tracks.end(); ++iter)
  35. {
  36. AppendTrack(*iter);
  37. }
  38. }
  39. bool CTrackViewTrackBundle::RemoveTrack(CTrackViewTrack* trackToRemove)
  40. {
  41. return stl::find_and_erase(m_tracks, trackToRemove);
  42. }
  43. //////////////////////////////////////////////////////////////////////////
  44. CTrackViewTrack::CTrackViewTrack(IAnimTrack* pTrack, CTrackViewAnimNode* pTrackAnimNode,
  45. CTrackViewNode* pParentNode, bool bIsSubTrack, unsigned int subTrackIndex)
  46. : CTrackViewNode(pParentNode)
  47. , m_pAnimTrack(pTrack)
  48. , m_pTrackAnimNode(pTrackAnimNode)
  49. , m_bIsSubTrack(bIsSubTrack)
  50. , m_subTrackIndex(subTrackIndex)
  51. {
  52. // Search for child tracks
  53. const unsigned int subTrackCount = m_pAnimTrack->GetSubTrackCount();
  54. for (unsigned int subTrackI = 0; subTrackI < subTrackCount; ++subTrackI)
  55. {
  56. IAnimTrack* pSubTrack = m_pAnimTrack->GetSubTrack(subTrackI);
  57. CTrackViewTrackFactory trackFactory;
  58. CTrackViewTrack* pNewTVTrack = trackFactory.BuildTrack(pSubTrack, pTrackAnimNode, this, true, subTrackI);
  59. m_childNodes.push_back(std::unique_ptr<CTrackViewNode>(pNewTVTrack));
  60. }
  61. m_bIsCompoundTrack = subTrackCount > 0;
  62. // Connect bus to listen for OnStart/StopPlayInEditor events
  63. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusConnect();
  64. }
  65. CTrackViewTrack::~CTrackViewTrack()
  66. {
  67. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusDisconnect();
  68. }
  69. //////////////////////////////////////////////////////////////////////////
  70. CTrackViewAnimNode* CTrackViewTrack::GetAnimNode() const
  71. {
  72. return m_pTrackAnimNode;
  73. }
  74. //////////////////////////////////////////////////////////////////////////
  75. bool CTrackViewTrack::SnapTimeToPrevKey(float& time) const
  76. {
  77. CTrackViewKeyHandle prevKey = const_cast<CTrackViewTrack*>(this)->GetPrevKey(time);
  78. if (prevKey.IsValid())
  79. {
  80. time = prevKey.GetTime();
  81. return true;
  82. }
  83. return false;
  84. }
  85. //////////////////////////////////////////////////////////////////////////
  86. bool CTrackViewTrack::SnapTimeToNextKey(float& time) const
  87. {
  88. CTrackViewKeyHandle prevKey = const_cast<CTrackViewTrack*>(this)->GetNextKey(time);
  89. if (prevKey.IsValid())
  90. {
  91. time = prevKey.GetTime();
  92. return true;
  93. }
  94. return false;
  95. }
  96. //////////////////////////////////////////////////////////////////////////
  97. void CTrackViewTrack::SetExpanded(bool expanded)
  98. {
  99. if (m_pAnimTrack)
  100. {
  101. CTrackViewSequence* sequence = GetSequence();
  102. if (nullptr != sequence)
  103. {
  104. if (GetExpanded() != expanded)
  105. {
  106. m_pAnimTrack->SetExpanded(expanded);
  107. if (expanded)
  108. {
  109. sequence->OnNodeChanged(this, ITrackViewSequenceListener::eNodeChangeType_Expanded);
  110. }
  111. else
  112. {
  113. sequence->OnNodeChanged(this, ITrackViewSequenceListener::eNodeChangeType_Collapsed);
  114. }
  115. }
  116. }
  117. }
  118. }
  119. //////////////////////////////////////////////////////////////////////////
  120. bool CTrackViewTrack::GetExpanded() const
  121. {
  122. return (m_pAnimTrack) ? m_pAnimTrack->GetExpanded() : false;
  123. }
  124. //////////////////////////////////////////////////////////////////////////
  125. CTrackViewKeyHandle CTrackViewTrack::GetPrevKey(const float time)
  126. {
  127. CTrackViewKeyHandle keyHandle;
  128. #ifdef max
  129. #undef max
  130. #endif
  131. const float startTime = time;
  132. float closestTime = -std::numeric_limits<float>::max();
  133. const int numKeys = m_pAnimTrack->GetNumKeys();
  134. for (int i = 0; i < numKeys; ++i)
  135. {
  136. const float keyTime = m_pAnimTrack->GetKeyTime(i);
  137. if (keyTime < startTime && keyTime > closestTime)
  138. {
  139. keyHandle = CTrackViewKeyHandle(this, i);
  140. closestTime = keyTime;
  141. }
  142. }
  143. return keyHandle;
  144. }
  145. //////////////////////////////////////////////////////////////////////////
  146. CTrackViewKeyHandle CTrackViewTrack::GetNextKey(const float time)
  147. {
  148. CTrackViewKeyHandle keyHandle;
  149. const float startTime = time;
  150. float closestTime = std::numeric_limits<float>::max();
  151. const int numKeys = m_pAnimTrack->GetNumKeys();
  152. for (int i = 0; i < numKeys; ++i)
  153. {
  154. const float keyTime = m_pAnimTrack->GetKeyTime(i);
  155. if (keyTime > startTime && keyTime < closestTime)
  156. {
  157. keyHandle = CTrackViewKeyHandle(this, i);
  158. closestTime = keyTime;
  159. }
  160. }
  161. return keyHandle;
  162. }
  163. //////////////////////////////////////////////////////////////////////////
  164. CTrackViewKeyBundle CTrackViewTrack::GetSelectedKeys()
  165. {
  166. CTrackViewKeyBundle bundle;
  167. if (m_bIsCompoundTrack)
  168. {
  169. for (auto iter = m_childNodes.begin(); iter != m_childNodes.end(); ++iter)
  170. {
  171. bundle.AppendKeyBundle((*iter)->GetSelectedKeys());
  172. }
  173. }
  174. else
  175. {
  176. bundle = GetKeys(true, -std::numeric_limits<float>::max(), std::numeric_limits<float>::max());
  177. }
  178. return bundle;
  179. }
  180. //////////////////////////////////////////////////////////////////////////
  181. CTrackViewKeyBundle CTrackViewTrack::GetAllKeys()
  182. {
  183. CTrackViewKeyBundle bundle;
  184. if (m_bIsCompoundTrack)
  185. {
  186. for (auto iter = m_childNodes.begin(); iter != m_childNodes.end(); ++iter)
  187. {
  188. bundle.AppendKeyBundle((*iter)->GetAllKeys());
  189. }
  190. }
  191. else
  192. {
  193. bundle = GetKeys(false, -std::numeric_limits<float>::max(), std::numeric_limits<float>::max());
  194. }
  195. return bundle;
  196. }
  197. //////////////////////////////////////////////////////////////////////////
  198. CTrackViewKeyBundle CTrackViewTrack::GetKeysInTimeRange(const float t0, const float t1)
  199. {
  200. CTrackViewKeyBundle bundle;
  201. if (m_bIsCompoundTrack)
  202. {
  203. for (auto iter = m_childNodes.begin(); iter != m_childNodes.end(); ++iter)
  204. {
  205. bundle.AppendKeyBundle((*iter)->GetKeysInTimeRange(t0, t1));
  206. }
  207. }
  208. else
  209. {
  210. bundle = GetKeys(false, t0, t1);
  211. }
  212. return bundle;
  213. }
  214. //////////////////////////////////////////////////////////////////////////
  215. CTrackViewKeyBundle CTrackViewTrack::GetKeys(const bool bOnlySelected, const float t0, const float t1)
  216. {
  217. CTrackViewKeyBundle bundle;
  218. const int keyCount = m_pAnimTrack->GetNumKeys();
  219. for (int keyIndex = 0; keyIndex < keyCount; ++keyIndex)
  220. {
  221. const float keyTime = m_pAnimTrack->GetKeyTime(keyIndex);
  222. const bool timeRangeOk = (t0 <= keyTime && keyTime <= t1);
  223. if (timeRangeOk && (!bOnlySelected || IsKeySelected(keyIndex)))
  224. {
  225. CTrackViewKeyHandle keyHandle(this, keyIndex);
  226. bundle.AppendKey(keyHandle);
  227. }
  228. }
  229. return bundle;
  230. }
  231. //////////////////////////////////////////////////////////////////////////
  232. CTrackViewKeyHandle CTrackViewTrack::CreateKey(const float time)
  233. {
  234. const int keyIndex = m_pAnimTrack->CreateKey(time);
  235. GetSequence()->OnKeysChanged();
  236. CTrackViewKeyHandle createdKeyHandle(this, keyIndex);
  237. GetSequence()->OnKeyAdded(createdKeyHandle);
  238. return createdKeyHandle;
  239. }
  240. //////////////////////////////////////////////////////////////////////////
  241. void CTrackViewTrack::SlideKeys(const float time0, const float timeOffset)
  242. {
  243. for (int i = 0; i < m_pAnimTrack->GetNumKeys(); ++i)
  244. {
  245. float keyTime = m_pAnimTrack->GetKeyTime(i);
  246. if (keyTime >= time0)
  247. {
  248. m_pAnimTrack->SetKeyTime(i, keyTime + timeOffset);
  249. }
  250. }
  251. }
  252. //////////////////////////////////////////////////////////////////////////
  253. void CTrackViewTrack::UpdateKeyDataAfterParentChanged(const AZ::Transform& oldParentWorldTM, const AZ::Transform& newParentWorldTM)
  254. {
  255. AZStd::unique_ptr<AzToolsFramework::ScopedUndoBatch> undoBatch;
  256. if (!AzToolsFramework::UndoRedoOperationInProgress())
  257. {
  258. undoBatch = AZStd::make_unique<AzToolsFramework::ScopedUndoBatch>("Update Key Data After Parent Changed");
  259. }
  260. m_pAnimTrack->UpdateKeyDataAfterParentChanged(oldParentWorldTM, newParentWorldTM);
  261. if (undoBatch.get())
  262. {
  263. undoBatch->MarkEntityDirty(GetSequence()->GetSequenceComponentEntityId());
  264. }
  265. }
  266. //////////////////////////////////////////////////////////////////////////
  267. CTrackViewKeyHandle CTrackViewTrack::GetKey(unsigned int index)
  268. {
  269. if (index < GetKeyCount())
  270. {
  271. return CTrackViewKeyHandle(this, index);
  272. }
  273. return CTrackViewKeyHandle();
  274. }
  275. //////////////////////////////////////////////////////////////////////////
  276. CTrackViewKeyConstHandle CTrackViewTrack::GetKey(unsigned int index) const
  277. {
  278. if (index < GetKeyCount())
  279. {
  280. return CTrackViewKeyConstHandle(this, index);
  281. }
  282. return CTrackViewKeyConstHandle();
  283. }
  284. //////////////////////////////////////////////////////////////////////////
  285. CTrackViewKeyHandle CTrackViewTrack::GetKeyByTime(const float time)
  286. {
  287. if (m_bIsCompoundTrack)
  288. {
  289. // Search key in sub tracks
  290. unsigned int currentIndex = 0;
  291. unsigned int childCount = GetChildCount();
  292. for (unsigned int i = 0; i < childCount; ++i)
  293. {
  294. CTrackViewTrack* pChildTrack = static_cast<CTrackViewTrack*>(GetChild(i));
  295. int keyIndex = pChildTrack->m_pAnimTrack->FindKey(time);
  296. if (keyIndex >= 0)
  297. {
  298. return CTrackViewKeyHandle(this, currentIndex + keyIndex);
  299. }
  300. currentIndex += pChildTrack->GetKeyCount();
  301. }
  302. }
  303. int keyIndex = m_pAnimTrack->FindKey(time);
  304. if (keyIndex < 0)
  305. {
  306. return CTrackViewKeyHandle();
  307. }
  308. return CTrackViewKeyHandle(this, keyIndex);
  309. }
  310. //////////////////////////////////////////////////////////////////////////
  311. CTrackViewKeyHandle CTrackViewTrack::GetNearestKeyByTime(const float time)
  312. {
  313. int minDelta = std::numeric_limits<int>::max();
  314. const unsigned int keyCount = GetKeyCount();
  315. for (unsigned int i = 0; i < keyCount; ++i)
  316. {
  317. CTrackViewKeyHandle keyHandle = GetKey(i);
  318. const int deltaT = abs((int)keyHandle.GetTime() - (int)time);
  319. // If deltaT got larger since last key, then the last key
  320. // was the key with minimum temporal distance to the given time
  321. if (deltaT > minDelta)
  322. {
  323. return CTrackViewKeyHandle(this, i - 1);
  324. }
  325. minDelta = std::min(minDelta, deltaT);
  326. }
  327. // If we didn't return above and there are keys, then the
  328. // last key needs to be the one with minimum distance
  329. if (keyCount > 0)
  330. {
  331. return CTrackViewKeyHandle(this, keyCount - 1);
  332. }
  333. // No keys
  334. return CTrackViewKeyHandle();
  335. }
  336. //////////////////////////////////////////////////////////////////////////
  337. void CTrackViewTrack::GetKeyValueRange(float& min, float& max) const
  338. {
  339. m_pAnimTrack->GetKeyValueRange(min, max);
  340. }
  341. //////////////////////////////////////////////////////////////////////////
  342. ColorB CTrackViewTrack::GetCustomColor() const
  343. {
  344. return m_pAnimTrack->GetCustomColor();
  345. }
  346. //////////////////////////////////////////////////////////////////////////
  347. void CTrackViewTrack::SetCustomColor(ColorB color)
  348. {
  349. m_pAnimTrack->SetCustomColor(color);
  350. }
  351. //////////////////////////////////////////////////////////////////////////
  352. bool CTrackViewTrack::HasCustomColor() const
  353. {
  354. return m_pAnimTrack->HasCustomColor();
  355. }
  356. //////////////////////////////////////////////////////////////////////////
  357. void CTrackViewTrack::ClearCustomColor()
  358. {
  359. m_pAnimTrack->ClearCustomColor();
  360. }
  361. //////////////////////////////////////////////////////////////////////////
  362. IAnimTrack::EAnimTrackFlags CTrackViewTrack::GetFlags() const
  363. {
  364. return (IAnimTrack::EAnimTrackFlags)m_pAnimTrack->GetFlags();
  365. }
  366. //////////////////////////////////////////////////////////////////////////
  367. CTrackViewTrackMemento CTrackViewTrack::GetMemento() const
  368. {
  369. CTrackViewTrackMemento memento;
  370. memento.m_serializedTrackState = XmlHelpers::CreateXmlNode("TrackState");
  371. m_pAnimTrack->Serialize(memento.m_serializedTrackState, false);
  372. return memento;
  373. }
  374. //////////////////////////////////////////////////////////////////////////
  375. void CTrackViewTrack::RestoreFromMemento(const CTrackViewTrackMemento& memento)
  376. {
  377. // We're going to de-serialize, so this is const safe
  378. XmlNodeRef& xmlNode = const_cast<XmlNodeRef&>(memento.m_serializedTrackState);
  379. m_pAnimTrack->Serialize(xmlNode, true);
  380. }
  381. //////////////////////////////////////////////////////////////////////////
  382. AZStd::string CTrackViewTrack::GetName() const
  383. {
  384. CTrackViewNode* pParentNode = GetParentNode();
  385. if (pParentNode->GetNodeType() == eTVNT_Track)
  386. {
  387. CTrackViewTrack* pParentTrack = static_cast<CTrackViewTrack*>(pParentNode);
  388. return pParentTrack->m_pAnimTrack->GetSubTrackName(m_subTrackIndex);
  389. }
  390. return GetAnimNode()->GetParamName(GetParameterType());
  391. }
  392. //////////////////////////////////////////////////////////////////////////
  393. void CTrackViewTrack::SetDisabled(bool bDisabled)
  394. {
  395. if (bDisabled)
  396. {
  397. m_pAnimTrack->SetFlags(m_pAnimTrack->GetFlags() | IAnimTrack::eAnimTrackFlags_Disabled);
  398. GetSequence()->OnNodeChanged(this, ITrackViewSequenceListener::eNodeChangeType_Disabled);
  399. }
  400. else
  401. {
  402. m_pAnimTrack->SetFlags(m_pAnimTrack->GetFlags() & ~IAnimTrack::eAnimTrackFlags_Disabled);
  403. GetSequence()->OnNodeChanged(this, ITrackViewSequenceListener::eNodeChangeType_Enabled);
  404. }
  405. }
  406. //////////////////////////////////////////////////////////////////////////
  407. bool CTrackViewTrack::IsDisabled() const
  408. {
  409. return m_pAnimTrack->GetFlags() & IAnimTrack::eAnimTrackFlags_Disabled;
  410. }
  411. //////////////////////////////////////////////////////////////////////////
  412. void CTrackViewTrack::SetMuted(bool bMuted)
  413. {
  414. if (UsesMute())
  415. {
  416. if (bMuted)
  417. {
  418. m_pAnimTrack->SetFlags(m_pAnimTrack->GetFlags() | IAnimTrack::eAnimTrackFlags_Muted);
  419. GetSequence()->OnNodeChanged(this, ITrackViewSequenceListener::eNodeChangeType_Muted);
  420. }
  421. else
  422. {
  423. m_pAnimTrack->SetFlags(m_pAnimTrack->GetFlags() & ~IAnimTrack::eAnimTrackFlags_Muted);
  424. GetSequence()->OnNodeChanged(this, ITrackViewSequenceListener::eNodeChangeType_Unmuted);
  425. }
  426. }
  427. }
  428. //////////////////////////////////////////////////////////////////////////
  429. // Returns whether the track is muted, or false if the track does not use muting
  430. bool CTrackViewTrack::IsMuted() const
  431. {
  432. return m_pAnimTrack->UsesMute() ? (m_pAnimTrack->GetFlags() & IAnimTrack::eAnimTrackFlags_Muted) : false;
  433. }
  434. //////////////////////////////////////////////////////////////////////////
  435. void CTrackViewTrack::SetKey(unsigned int keyIndex, IKey* pKey)
  436. {
  437. m_pAnimTrack->SetKey(keyIndex, pKey);
  438. m_pTrackAnimNode->GetSequence()->OnKeysChanged();
  439. }
  440. //////////////////////////////////////////////////////////////////////////
  441. void CTrackViewTrack::GetKey(unsigned int keyIndex, IKey* pKey) const
  442. {
  443. m_pAnimTrack->GetKey(keyIndex, pKey);
  444. }
  445. //////////////////////////////////////////////////////////////////////////
  446. void CTrackViewTrack::SelectKey(unsigned int keyIndex, bool bSelect)
  447. {
  448. const bool bWasSelected = m_pAnimTrack->IsKeySelected(keyIndex);
  449. m_pAnimTrack->SelectKey(keyIndex, bSelect);
  450. if (bSelect != bWasSelected)
  451. {
  452. m_pTrackAnimNode->GetSequence()->OnKeySelectionChanged();
  453. }
  454. }
  455. //////////////////////////////////////////////////////////////////////////
  456. void CTrackViewTrack::SetKeyTime(const int index, const float time, bool notifyListeners)
  457. {
  458. const float bOldTime = m_pAnimTrack->GetKeyTime(index);
  459. m_pAnimTrack->SetKeyTime(index, time);
  460. if (notifyListeners && (bOldTime != time))
  461. {
  462. // The keys were just make invalid by the above SetKeyTime(), so sort them now
  463. // to make sure they are ready to be used. Only do this when notifyListeners
  464. // is set so client callers can batch up a bunch of SetKeyTime calls if desired.
  465. m_pAnimTrack->SortKeys();
  466. m_pTrackAnimNode->GetSequence()->OnKeysChanged();
  467. }
  468. }
  469. //////////////////////////////////////////////////////////////////////////
  470. float CTrackViewTrack::GetKeyTime(const int index) const
  471. {
  472. return m_pAnimTrack->GetKeyTime(index);
  473. }
  474. //////////////////////////////////////////////////////////////////////////
  475. void CTrackViewTrack::RemoveKey(const int index)
  476. {
  477. m_pAnimTrack->RemoveKey(index);
  478. m_pTrackAnimNode->GetSequence()->OnKeysChanged();
  479. }
  480. //////////////////////////////////////////////////////////////////////////
  481. int CTrackViewTrack::CloneKey(const int index)
  482. {
  483. int newIndex = m_pAnimTrack->CloneKey(index);
  484. m_pTrackAnimNode->GetSequence()->OnKeysChanged();
  485. return newIndex;
  486. }
  487. //////////////////////////////////////////////////////////////////////////
  488. void CTrackViewTrack::SelectKeys(const bool bSelected)
  489. {
  490. m_pTrackAnimNode->GetSequence()->QueueNotifications();
  491. if (!m_bIsCompoundTrack)
  492. {
  493. unsigned int keyCount = GetKeyCount();
  494. for (unsigned int i = 0; i < keyCount; ++i)
  495. {
  496. m_pAnimTrack->SelectKey(i, bSelected);
  497. m_pTrackAnimNode->GetSequence()->OnKeySelectionChanged();
  498. }
  499. }
  500. else
  501. {
  502. // Affect sub tracks
  503. unsigned int childCount = GetChildCount();
  504. for (unsigned int childIndex = 0; childIndex < childCount; ++childIndex)
  505. {
  506. CTrackViewTrack* pChildTrack = static_cast<CTrackViewTrack*>(GetChild(childIndex));
  507. pChildTrack->SelectKeys(bSelected);
  508. m_pTrackAnimNode->GetSequence()->OnKeySelectionChanged();
  509. }
  510. }
  511. m_pTrackAnimNode->GetSequence()->SubmitPendingNotifications();
  512. }
  513. //////////////////////////////////////////////////////////////////////////
  514. bool CTrackViewTrack::IsKeySelected(unsigned int keyIndex) const
  515. {
  516. if (m_pAnimTrack)
  517. {
  518. return m_pAnimTrack->IsKeySelected(keyIndex);
  519. }
  520. return false;
  521. }
  522. //////////////////////////////////////////////////////////////////////////
  523. void CTrackViewTrack::SetSortMarkerKey(unsigned int keyIndex, bool enabled)
  524. {
  525. if (m_pAnimTrack)
  526. {
  527. return m_pAnimTrack->SetSortMarkerKey(keyIndex, enabled);
  528. }
  529. }
  530. //////////////////////////////////////////////////////////////////////////
  531. bool CTrackViewTrack::IsSortMarkerKey(unsigned int keyIndex) const
  532. {
  533. if (m_pAnimTrack)
  534. {
  535. return m_pAnimTrack->IsSortMarkerKey(keyIndex);
  536. }
  537. return false;
  538. }
  539. //////////////////////////////////////////////////////////////////////////
  540. CTrackViewKeyHandle CTrackViewTrack::GetSubTrackKeyHandle(unsigned int index) const
  541. {
  542. // Return handle to sub track key
  543. unsigned int childCount = GetChildCount();
  544. for (unsigned int childIndex = 0; childIndex < childCount; ++childIndex)
  545. {
  546. CTrackViewTrack* pChildTrack = static_cast<CTrackViewTrack*>(GetChild(childIndex));
  547. const unsigned int childKeyCount = pChildTrack->GetKeyCount();
  548. if (index < childKeyCount)
  549. {
  550. return pChildTrack->GetKey(index);
  551. }
  552. index -= childKeyCount;
  553. }
  554. return CTrackViewKeyHandle();
  555. }
  556. //////////////////////////////////////////////////////////////////////////
  557. void CTrackViewTrack::SetAnimationLayerIndex(const int index)
  558. {
  559. if (m_pAnimTrack)
  560. {
  561. m_pAnimTrack->SetAnimationLayerIndex(index);
  562. }
  563. }
  564. //////////////////////////////////////////////////////////////////////////
  565. int CTrackViewTrack::GetAnimationLayerIndex() const
  566. {
  567. return m_pAnimTrack->GetAnimationLayerIndex();
  568. }
  569. //////////////////////////////////////////////////////////////////////////
  570. void CTrackViewTrack::OnStartPlayInEditor()
  571. {
  572. // remap any AZ::EntityId's used in tracks
  573. if (m_pAnimTrack)
  574. {
  575. // OnStopPlayInEditor clears this as well, but we clear it here in case OnStartPlayInEditor() is called multiple times before OnStopPlayInEditor()
  576. m_paramTypeToStashedEntityIdMap.clear();
  577. CAnimParamType trackParamType = m_pAnimTrack->GetParameterType();
  578. const AnimParamType paramType = trackParamType.GetType();
  579. if (paramType == AnimParamType::Camera || paramType == AnimParamType::Sequence)
  580. {
  581. ISelectKey selectKey;
  582. ISequenceKey sequenceKey;
  583. IKey* key = nullptr;
  584. for (int i = 0; i < m_pAnimTrack->GetNumKeys(); i++)
  585. {
  586. AZ::EntityId entityIdToRemap;
  587. if (paramType == AnimParamType::Camera)
  588. {
  589. m_pAnimTrack->GetKey(i, &selectKey);
  590. entityIdToRemap = selectKey.cameraAzEntityId;
  591. key = &selectKey;
  592. }
  593. else if (paramType == AnimParamType::Sequence)
  594. {
  595. m_pAnimTrack->GetKey(i, &sequenceKey);
  596. entityIdToRemap = sequenceKey.sequenceEntityId;
  597. key = &sequenceKey;
  598. }
  599. // stash the entity Id for restore in OnStopPlayInEditor
  600. m_paramTypeToStashedEntityIdMap[trackParamType].push_back(entityIdToRemap);
  601. if (entityIdToRemap.IsValid())
  602. {
  603. AZ::EntityId remappedId;
  604. AzToolsFramework::EditorEntityContextRequestBus::Broadcast(&AzToolsFramework::EditorEntityContextRequestBus::Events::MapEditorIdToRuntimeId, entityIdToRemap, remappedId);
  605. // remap
  606. if (paramType == AnimParamType::Camera)
  607. {
  608. selectKey.cameraAzEntityId = remappedId;
  609. }
  610. else if (paramType == AnimParamType::Sequence)
  611. {
  612. sequenceKey.sequenceEntityId = remappedId;
  613. }
  614. m_pAnimTrack->SetKey(i, key);
  615. }
  616. }
  617. }
  618. }
  619. }
  620. //////////////////////////////////////////////////////////////////////////
  621. void CTrackViewTrack::OnStopPlayInEditor()
  622. {
  623. // restore any AZ::EntityId's remapped in OnStartPlayInEditor
  624. if (m_pAnimTrack && m_paramTypeToStashedEntityIdMap.size())
  625. {
  626. CAnimParamType trackParamType = m_pAnimTrack->GetParameterType();
  627. const AnimParamType paramType = trackParamType.GetType();
  628. if (paramType == AnimParamType::Camera || paramType == AnimParamType::Sequence)
  629. {
  630. for (int i = 0; i < m_pAnimTrack->GetNumKeys(); i++)
  631. {
  632. ISelectKey selectKey;
  633. ISequenceKey sequenceKey;
  634. IKey* key = nullptr;
  635. // restore entityIds
  636. if (paramType == AnimParamType::Camera)
  637. {
  638. m_pAnimTrack->GetKey(i, &selectKey);
  639. selectKey.cameraAzEntityId = m_paramTypeToStashedEntityIdMap[trackParamType][i];
  640. key = &selectKey;
  641. }
  642. else if (paramType == AnimParamType::Sequence)
  643. {
  644. m_pAnimTrack->GetKey(i, &sequenceKey);
  645. sequenceKey.sequenceEntityId = m_paramTypeToStashedEntityIdMap[trackParamType][i];
  646. key = &sequenceKey;
  647. }
  648. m_pAnimTrack->SetKey(i, key);
  649. }
  650. }
  651. // clear the StashedEntityIdMap now that we've consumed it
  652. m_paramTypeToStashedEntityIdMap.clear();
  653. }
  654. }
  655. //////////////////////////////////////////////////////////////////////////
  656. void CTrackViewTrack::CopyKeysToClipboard(XmlNodeRef& xmlNode, const bool bOnlySelectedKeys, const bool bOnlyFromSelectedTracks)
  657. {
  658. if (bOnlyFromSelectedTracks && !IsSelected())
  659. {
  660. return;
  661. }
  662. if (GetKeyCount() == 0)
  663. {
  664. return;
  665. }
  666. if (bOnlySelectedKeys)
  667. {
  668. CTrackViewKeyBundle keyBundle = GetSelectedKeys();
  669. if (keyBundle.GetKeyCount() == 0)
  670. {
  671. return;
  672. }
  673. }
  674. XmlNodeRef childNode = xmlNode->newChild("Track");
  675. childNode->setAttr("name", GetName().c_str());
  676. GetParameterType().SaveToXml(childNode);
  677. childNode->setAttr("valueType", static_cast<int>(GetValueType()));
  678. m_pAnimTrack->SerializeSelection(childNode, false, bOnlySelectedKeys);
  679. }
  680. //////////////////////////////////////////////////////////////////////////
  681. void CTrackViewTrack::PasteKeys(XmlNodeRef xmlNode, const float timeOffset)
  682. {
  683. CTrackViewSequence* sequence = GetSequence();
  684. AZ_Assert(sequence, "Expected sequence not to be null.");
  685. AzToolsFramework::ScopedUndoBatch undoBatch("Paste Keys");
  686. m_pAnimTrack->SerializeSelection(xmlNode, true, true, timeOffset);
  687. undoBatch.MarkEntityDirty(sequence->GetSequenceComponentEntityId());
  688. }