UiAnimViewSequence.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  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 "UiEditorAnimationBus.h"
  9. #include "UiAnimViewSequence.h"
  10. #include "UiAnimViewSequenceManager.h"
  11. #include "UiAnimViewAnimNode.h"
  12. #include "UiAnimViewUndo.h"
  13. #include "UiAnimViewTrack.h"
  14. #include "UiAnimViewNodeFactories.h"
  15. #include "UiAnimViewSequence.h"
  16. #include "AnimationContext.h"
  17. #if UI_ANIMATION_REMOVED
  18. #include "Objects/EntityObject.h"
  19. #endif
  20. #include "Clipboard.h"
  21. #include "UiEditorAnimationBus.h"
  22. #include <Util/EditorUtils.h>
  23. #include <QApplication>
  24. //////////////////////////////////////////////////////////////////////////
  25. CUiAnimViewSequence::CUiAnimViewSequence(IUiAnimSequence* pSequence)
  26. : CUiAnimViewAnimNode(pSequence, nullptr, nullptr)
  27. , m_pAnimSequence(pSequence)
  28. , m_bBoundToEditorObjects(false)
  29. , m_selectionRecursionLevel(0)
  30. , m_bQueueNotifications(false)
  31. , m_bKeySelectionChanged(false)
  32. , m_bKeysChanged(false)
  33. , m_bForceUiAnimation(false)
  34. , m_bNodeSelectionChanged(false)
  35. , m_time(0.0f)
  36. , m_bNoNotifications(false)
  37. {
  38. assert(m_pAnimSequence);
  39. SetExpanded(true);
  40. }
  41. //////////////////////////////////////////////////////////////////////////
  42. CUiAnimViewSequence::~CUiAnimViewSequence()
  43. {
  44. // For safety. Should be done by sequence manager
  45. UiAnimUndoManager::Get()->RemoveListener(this);
  46. }
  47. //////////////////////////////////////////////////////////////////////////
  48. void CUiAnimViewSequence::Load()
  49. {
  50. const int nodeCount = m_pAnimSequence->GetNodeCount();
  51. for (int i = 0; i < nodeCount; ++i)
  52. {
  53. IUiAnimNode* pNode = m_pAnimSequence->GetNode(i);
  54. // Only add top level nodes to sequence
  55. if (!pNode->GetParent())
  56. {
  57. CUiAnimViewAnimNodeFactory animNodeFactory;
  58. CUiAnimViewAnimNode* pNewTVAnimNode = animNodeFactory.BuildAnimNode(m_pAnimSequence.get(), pNode, this);
  59. m_childNodes.push_back(std::unique_ptr<CUiAnimViewNode>(pNewTVAnimNode));
  60. }
  61. }
  62. SortNodes();
  63. }
  64. //////////////////////////////////////////////////////////////////////////
  65. void CUiAnimViewSequence::BindToEditorObjects()
  66. {
  67. m_bBoundToEditorObjects = true;
  68. CUiAnimViewAnimNode::BindToEditorObjects();
  69. }
  70. //////////////////////////////////////////////////////////////////////////
  71. void CUiAnimViewSequence::UnBindFromEditorObjects()
  72. {
  73. m_bBoundToEditorObjects = false;
  74. CUiAnimViewAnimNode::UnBindFromEditorObjects();
  75. }
  76. //////////////////////////////////////////////////////////////////////////
  77. bool CUiAnimViewSequence::IsBoundToEditorObjects() const
  78. {
  79. return m_bBoundToEditorObjects;
  80. }
  81. //////////////////////////////////////////////////////////////////////////
  82. CUiAnimViewKeyHandle CUiAnimViewSequence::FindSingleSelectedKey()
  83. {
  84. CUiAnimViewSequence* pSequence = nullptr;
  85. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  86. if (!pSequence)
  87. {
  88. return CUiAnimViewKeyHandle();
  89. }
  90. CUiAnimViewKeyBundle selectedKeys = pSequence->GetSelectedKeys();
  91. if (selectedKeys.GetKeyCount() != 1)
  92. {
  93. return CUiAnimViewKeyHandle();
  94. }
  95. return selectedKeys.GetKey(0);
  96. }
  97. //////////////////////////////////////////////////////////////////////////
  98. bool CUiAnimViewSequence::IsAncestorOf(CUiAnimViewSequence* pSequence) const
  99. {
  100. return m_pAnimSequence->IsAncestorOf(pSequence->m_pAnimSequence.get());
  101. }
  102. //////////////////////////////////////////////////////////////////////////
  103. void CUiAnimViewSequence::BeginCutScene([[maybe_unused]] const bool bResetFx) const
  104. {
  105. }
  106. //////////////////////////////////////////////////////////////////////////
  107. void CUiAnimViewSequence::EndCutScene() const
  108. {
  109. }
  110. //////////////////////////////////////////////////////////////////////////
  111. void CUiAnimViewSequence::Render(const SUiAnimContext& animContext)
  112. {
  113. for (auto iter = m_childNodes.begin(); iter != m_childNodes.end(); ++iter)
  114. {
  115. CUiAnimViewNode* pChildNode = (*iter).get();
  116. if (pChildNode->GetNodeType() == eUiAVNT_AnimNode)
  117. {
  118. CUiAnimViewAnimNode* pChildAnimNode = (CUiAnimViewAnimNode*)pChildNode;
  119. pChildAnimNode->Render(animContext);
  120. }
  121. }
  122. m_pAnimSequence->Render();
  123. }
  124. //////////////////////////////////////////////////////////////////////////
  125. void CUiAnimViewSequence::Animate(const SUiAnimContext& animContext)
  126. {
  127. if (!m_pAnimSequence->IsActivated())
  128. {
  129. return;
  130. }
  131. m_time = animContext.time;
  132. m_pAnimSequence->Animate(animContext);
  133. CUiAnimViewSequenceNoNotificationContext context(this);
  134. for (auto iter = m_childNodes.begin(); iter != m_childNodes.end(); ++iter)
  135. {
  136. CUiAnimViewNode* pChildNode = (*iter).get();
  137. if (pChildNode->GetNodeType() == eUiAVNT_AnimNode)
  138. {
  139. CUiAnimViewAnimNode* pChildAnimNode = (CUiAnimViewAnimNode*)pChildNode;
  140. pChildAnimNode->Animate(animContext);
  141. }
  142. }
  143. }
  144. //////////////////////////////////////////////////////////////////////////
  145. void CUiAnimViewSequence::AddListener(IUiAnimViewSequenceListener* pListener)
  146. {
  147. stl::push_back_unique(m_sequenceListeners, pListener);
  148. }
  149. //////////////////////////////////////////////////////////////////////////
  150. void CUiAnimViewSequence::RemoveListener(IUiAnimViewSequenceListener* pListener)
  151. {
  152. stl::find_and_erase(m_sequenceListeners, pListener);
  153. }
  154. //////////////////////////////////////////////////////////////////////////
  155. void CUiAnimViewSequence::OnNodeSelectionChanged()
  156. {
  157. if (m_bNoNotifications)
  158. {
  159. return;
  160. }
  161. if (m_bQueueNotifications)
  162. {
  163. m_bNodeSelectionChanged = true;
  164. }
  165. else
  166. {
  167. CUiAnimViewSequenceNoNotificationContext context(this);
  168. for (auto iter = m_sequenceListeners.begin(); iter != m_sequenceListeners.end(); ++iter)
  169. {
  170. (*iter)->OnNodeSelectionChanged(this);
  171. }
  172. }
  173. }
  174. //////////////////////////////////////////////////////////////////////////
  175. void CUiAnimViewSequence::ForceAnimation()
  176. {
  177. if (m_bNoNotifications)
  178. {
  179. return;
  180. }
  181. if (m_bQueueNotifications)
  182. {
  183. m_bForceUiAnimation = true;
  184. }
  185. else
  186. {
  187. if (IsActive())
  188. {
  189. CUiAnimationContext* pAnimationContext = nullptr;
  190. UiEditorAnimationBus::BroadcastResult(pAnimationContext, &UiEditorAnimationBus::Events::GetAnimationContext);
  191. pAnimationContext->ForceAnimation();
  192. }
  193. }
  194. }
  195. //////////////////////////////////////////////////////////////////////////
  196. void CUiAnimViewSequence::OnKeySelectionChanged()
  197. {
  198. if (m_bNoNotifications)
  199. {
  200. return;
  201. }
  202. if (m_bQueueNotifications)
  203. {
  204. m_bKeySelectionChanged = true;
  205. }
  206. else
  207. {
  208. CUiAnimViewSequenceNoNotificationContext context(this);
  209. for (auto iter = m_sequenceListeners.begin(); iter != m_sequenceListeners.end(); ++iter)
  210. {
  211. (*iter)->OnKeySelectionChanged(this);
  212. }
  213. }
  214. }
  215. //////////////////////////////////////////////////////////////////////////
  216. void CUiAnimViewSequence::OnKeysChanged()
  217. {
  218. if (m_bNoNotifications)
  219. {
  220. return;
  221. }
  222. if (m_bQueueNotifications)
  223. {
  224. m_bKeysChanged = true;
  225. }
  226. else
  227. {
  228. CUiAnimViewSequenceNoNotificationContext context(this);
  229. for (auto iter = m_sequenceListeners.begin(); iter != m_sequenceListeners.end(); ++iter)
  230. {
  231. (*iter)->OnKeysChanged(this);
  232. }
  233. if (IsActive())
  234. {
  235. CUiAnimationContext* pAnimationContext = nullptr;
  236. UiEditorAnimationBus::BroadcastResult(pAnimationContext, &UiEditorAnimationBus::Events::GetAnimationContext);
  237. pAnimationContext->ForceAnimation();
  238. }
  239. }
  240. }
  241. //////////////////////////////////////////////////////////////////////////
  242. void CUiAnimViewSequence::OnNodeChanged(CUiAnimViewNode* pNode, IUiAnimViewSequenceListener::ENodeChangeType type)
  243. {
  244. #if UI_ANIMATION_REMOVED // UI_ANIMATION_REVISIT do we need any of this?
  245. if (pNode && pNode->GetNodeType() == eUiAVNT_AnimNode)
  246. {
  247. CUiAnimViewAnimNode* pAnimNode = static_cast<CUiAnimViewAnimNode*>(pNode);
  248. CEntityObject* pNodeEntity = pAnimNode->GetNodeEntity();
  249. if (pAnimNode->IsActive() && pNodeEntity)
  250. {
  251. switch (type)
  252. {
  253. case IUiAnimViewSequenceListener::eNodeChangeType_Added:
  254. pNodeEntity->SetTransformDelegate(pAnimNode);
  255. pNodeEntity->RegisterListener(pAnimNode);
  256. ForceAnimation();
  257. break;
  258. case IUiAnimViewSequenceListener::eNodeChangeType_Removed:
  259. pNodeEntity->SetTransformDelegate(nullptr);
  260. pNodeEntity->UnregisterListener(pAnimNode);
  261. ForceAnimation();
  262. break;
  263. }
  264. }
  265. switch (type)
  266. {
  267. case IUiAnimViewSequenceListener::eNodeChangeType_Enabled:
  268. // Fall through
  269. case IUiAnimViewSequenceListener::eNodeChangeType_Hidden:
  270. // Fall through
  271. case IUiAnimViewSequenceListener::eNodeChangeType_SetAsActiveDirector:
  272. // Fall through
  273. case IUiAnimViewSequenceListener::eNodeChangeType_NodeOwnerChanged:
  274. ForceAnimation();
  275. break;
  276. }
  277. }
  278. #endif
  279. if (m_bNoNotifications)
  280. {
  281. return;
  282. }
  283. CUiAnimViewSequenceNoNotificationContext context(this);
  284. for (auto iter = m_sequenceListeners.begin(); iter != m_sequenceListeners.end(); ++iter)
  285. {
  286. (*iter)->OnNodeChanged(pNode, type);
  287. }
  288. }
  289. //////////////////////////////////////////////////////////////////////////
  290. void CUiAnimViewSequence::OnNodeRenamed(CUiAnimViewNode* pNode, const char* pOldName)
  291. {
  292. bool bLightAnimationSetActive = GetFlags() & IUiAnimSequence::eSeqFlags_LightAnimationSet;
  293. if (bLightAnimationSetActive)
  294. {
  295. UpdateLightAnimationRefs(pOldName, pNode->GetName().c_str());
  296. }
  297. if (m_bNoNotifications)
  298. {
  299. return;
  300. }
  301. CUiAnimViewSequenceNoNotificationContext context(this);
  302. for (auto iter = m_sequenceListeners.begin(); iter != m_sequenceListeners.end(); ++iter)
  303. {
  304. (*iter)->OnNodeRenamed(pNode, pOldName);
  305. }
  306. }
  307. //////////////////////////////////////////////////////////////////////////
  308. void CUiAnimViewSequence::OnSequenceSettingsChanged()
  309. {
  310. if (m_bNoNotifications)
  311. {
  312. return;
  313. }
  314. CUiAnimViewSequenceNoNotificationContext context(this);
  315. for (auto iter = m_sequenceListeners.begin(); iter != m_sequenceListeners.end(); ++iter)
  316. {
  317. (*iter)->OnSequenceSettingsChanged(this);
  318. }
  319. }
  320. //////////////////////////////////////////////////////////////////////////
  321. void CUiAnimViewSequence::QueueNotifications()
  322. {
  323. m_bQueueNotifications = true;
  324. ++m_selectionRecursionLevel;
  325. }
  326. //////////////////////////////////////////////////////////////////////////
  327. void CUiAnimViewSequence::SubmitPendingNotifcations()
  328. {
  329. assert(m_selectionRecursionLevel > 0);
  330. if (m_selectionRecursionLevel > 0)
  331. {
  332. --m_selectionRecursionLevel;
  333. }
  334. if (m_selectionRecursionLevel == 0)
  335. {
  336. m_bQueueNotifications = false;
  337. if (m_bNodeSelectionChanged)
  338. {
  339. OnNodeSelectionChanged();
  340. }
  341. if (m_bKeysChanged)
  342. {
  343. OnKeysChanged();
  344. }
  345. if (m_bKeySelectionChanged)
  346. {
  347. OnKeySelectionChanged();
  348. }
  349. if (m_bForceUiAnimation)
  350. {
  351. ForceAnimation();
  352. }
  353. m_bForceUiAnimation = false;
  354. m_bKeysChanged = false;
  355. m_bNodeSelectionChanged = false;
  356. m_bKeySelectionChanged = false;
  357. }
  358. }
  359. //////////////////////////////////////////////////////////////////////////
  360. void CUiAnimViewSequence::DeleteSelectedNodes()
  361. {
  362. assert(UiAnimUndo::IsRecording());
  363. CUiAnimViewSequenceNotificationContext context(this);
  364. if (IsSelected())
  365. {
  366. CUiAnimViewSequenceManager::GetSequenceManager()->DeleteSequence(this);
  367. return;
  368. }
  369. CUiAnimViewAnimNodeBundle selectedNodes = GetSelectedAnimNodes();
  370. const unsigned int numSelectedNodes = selectedNodes.GetCount();
  371. #if UI_ANIMATION_REMOVED // lights
  372. // Check if any reference to the light animation to be deleted exists, and abort the removal, if any.
  373. const bool bLightAnimationSetActive = GetFlags() & IUiAnimSequence::eSeqFlags_LightAnimationSet;
  374. if (bLightAnimationSetActive)
  375. {
  376. QStringList lightNodes;
  377. // Construct set of selected light nodes
  378. for (unsigned int i = 0; i < numSelectedNodes; ++i)
  379. {
  380. CUiAnimViewAnimNode* pCurrentNode = selectedNodes.GetNode(i);
  381. if (pCurrentNode->GetType() == eUiAnimNodeType_Light)
  382. {
  383. stl::push_back_unique(lightNodes, pCurrentNode->GetName());
  384. }
  385. }
  386. // Check all entities if any is referencing any selected light node
  387. std::vector<CBaseObject*> entityObjects;
  388. GetIEditor()->GetObjectManager()->FindObjectsOfType(&CEntityObject::staticMetaObject, entityObjects);
  389. for (size_t i = 0; i < entityObjects.size(); ++i)
  390. {
  391. QString lightAnimationName = static_cast<CEntityObject*>(entityObjects[i])->GetLightAnimation();
  392. if (stl::find(lightNodes, lightAnimationName))
  393. {
  394. QMessageBox::critical(QApplication::activeWindow(), QString(), QObject::tr("The node '%1' cannot be removed since there is a light entity still using it.").arg(lightAnimationName));
  395. return;
  396. }
  397. }
  398. }
  399. #endif
  400. CUiAnimViewTrackBundle selectedTracks = GetSelectedTracks();
  401. const unsigned int numSelectedTracks = selectedTracks.GetCount();
  402. for (unsigned int i = 0; i < numSelectedTracks; ++i)
  403. {
  404. CUiAnimViewTrack* pTrack = selectedTracks.GetTrack(i);
  405. // Ignore sub tracks
  406. if (!pTrack->IsSubTrack())
  407. {
  408. pTrack->GetAnimNode()->RemoveTrack(pTrack);
  409. }
  410. }
  411. for (unsigned int i = 0; i < numSelectedNodes; ++i)
  412. {
  413. CUiAnimViewAnimNode* pNode = selectedNodes.GetNode(i);
  414. CUiAnimViewAnimNode* pParentNode = static_cast<CUiAnimViewAnimNode*>(pNode->GetParentNode());
  415. pParentNode->RemoveSubNode(pNode);
  416. }
  417. }
  418. //////////////////////////////////////////////////////////////////////////
  419. void CUiAnimViewSequence::SelectSelectedNodesInViewport()
  420. {
  421. assert(UiAnimUndo::IsRecording());
  422. CUiAnimViewAnimNodeBundle selectedNodes = GetSelectedAnimNodes();
  423. std::vector<CBaseObject*> entitiesToBeSelected;
  424. #if UI_ANIMATION_REMOVED // lights
  425. const unsigned int numSelectedNodes = selectedNodes.GetCount();
  426. // Also select objects that refer to light animation
  427. const bool bLightAnimationSetActive = GetFlags() & IUiAnimSequence::eSeqFlags_LightAnimationSet;
  428. if (bLightAnimationSetActive)
  429. {
  430. QStringList lightNodes;
  431. // Construct set of selected light nodes
  432. for (unsigned int i = 0; i < numSelectedNodes; ++i)
  433. {
  434. CUiAnimViewAnimNode* pCurrentNode = selectedNodes.GetNode(i);
  435. if (pCurrentNode->GetType() == eUiAnimNodeType_Light)
  436. {
  437. stl::push_back_unique(lightNodes, pCurrentNode->GetName());
  438. }
  439. }
  440. // Check all entities if any is referencing any selected light node
  441. std::vector<CBaseObject*> entityObjects;
  442. GetIEditor()->GetObjectManager()->FindObjectsOfType(&CEntityObject::staticMetaObject, entityObjects);
  443. for (size_t i = 0; i < entityObjects.size(); ++i)
  444. {
  445. QString lightAnimationName = static_cast<CEntityObject*>(entityObjects[i])->GetLightAnimation();
  446. if (stl::find(lightNodes, lightAnimationName))
  447. {
  448. stl::push_back_unique(entitiesToBeSelected, entityObjects[i]);
  449. }
  450. }
  451. }
  452. else
  453. {
  454. for (unsigned int i = 0; i < numSelectedNodes; ++i)
  455. {
  456. CUiAnimViewAnimNode* pNode = selectedNodes.GetNode(i);
  457. CEntityObject* pEntity = pNode->GetNodeEntity();
  458. if (pEntity)
  459. {
  460. stl::push_back_unique(entitiesToBeSelected, pEntity);
  461. }
  462. }
  463. }
  464. #endif
  465. }
  466. //////////////////////////////////////////////////////////////////////////
  467. void CUiAnimViewSequence::SyncSelectedTracksToBase()
  468. {
  469. CUiAnimViewAnimNodeBundle selectedNodes = GetSelectedAnimNodes();
  470. bool bNothingWasSynced = true;
  471. const unsigned int numSelectedNodes = selectedNodes.GetCount();
  472. if (numSelectedNodes > 0)
  473. {
  474. UiAnimUndo undo("Sync selected tracks to base");
  475. for (unsigned int i = 0; i < numSelectedNodes; ++i)
  476. {
  477. #if UI_ANIMATION_REMOVED // don't support CEntityObject
  478. CUiAnimViewAnimNode* pAnimNode = selectedNodes.GetNode(i);
  479. CEntityObject* pEntityObject = pAnimNode->GetNodeEntity();
  480. if (pEntityObject)
  481. {
  482. CUiAnimViewAnimNode* pAnimNode = CUiAnimViewSequenceManager::GetSequenceManager()->GetActiveAnimNode(pEntityObject);
  483. if (pAnimNode)
  484. {
  485. ITransformDelegate* pDelegate = pEntityObject->GetTransformDelegate();
  486. pEntityObject->SetTransformDelegate(nullptr);
  487. const Vec3 position = pAnimNode->GetPos();
  488. pEntityObject->SetPos(position);
  489. const Quat rotation = pAnimNode->GetRotation();
  490. pEntityObject->SetRotation(rotation);
  491. const Vec3 scale = pAnimNode->GetScale();
  492. pEntityObject->SetScale(scale);
  493. pEntityObject->SetTransformDelegate(pDelegate);
  494. bNothingWasSynced = false;
  495. }
  496. }
  497. #endif
  498. }
  499. if (bNothingWasSynced)
  500. {
  501. undo.Cancel();
  502. }
  503. }
  504. }
  505. //////////////////////////////////////////////////////////////////////////
  506. void CUiAnimViewSequence::SyncSelectedTracksFromBase()
  507. {
  508. CUiAnimViewAnimNodeBundle selectedNodes = GetSelectedAnimNodes();
  509. bool bNothingWasSynced = true;
  510. const unsigned int numSelectedNodes = selectedNodes.GetCount();
  511. if (numSelectedNodes > 0)
  512. {
  513. UiAnimUndo undo("Sync selected tracks to base");
  514. #if UI_ANIMATION_REMOVED // don't support CEntityObject
  515. for (unsigned int i = 0; i < numSelectedNodes; ++i)
  516. {
  517. CUiAnimViewAnimNode* pAnimNode = selectedNodes.GetNode(i);
  518. CEntityObject* pEntityObject = pAnimNode->GetNodeEntity();
  519. if (pEntityObject)
  520. {
  521. CUiAnimViewAnimNode* pAnimNode = CUiAnimViewSequenceManager::GetSequenceManager()->GetActiveAnimNode(pEntityObject);
  522. if (pAnimNode)
  523. {
  524. ITransformDelegate* pDelegate = pEntityObject->GetTransformDelegate();
  525. pEntityObject->SetTransformDelegate(nullptr);
  526. const Vec3 position = pEntityObject->GetPos();
  527. pAnimNode->SetPos(position);
  528. const Quat rotation = pEntityObject->GetRotation();
  529. pAnimNode->SetRotation(rotation);
  530. const Vec3 scale = pEntityObject->GetScale();
  531. pEntityObject->SetScale(scale);
  532. pEntityObject->SetTransformDelegate(pDelegate);
  533. bNothingWasSynced = false;
  534. }
  535. }
  536. }
  537. #endif
  538. if (bNothingWasSynced)
  539. {
  540. undo.Cancel();
  541. }
  542. }
  543. if (IsActive())
  544. {
  545. CUiAnimationContext* pAnimationContext = nullptr;
  546. UiEditorAnimationBus::BroadcastResult(pAnimationContext, &UiEditorAnimationBus::Events::GetAnimationContext);
  547. pAnimationContext->ForceAnimation();
  548. }
  549. }
  550. //////////////////////////////////////////////////////////////////////////
  551. void CUiAnimViewSequence::UpdateLightAnimationRefs([[maybe_unused]] const char* pOldName, [[maybe_unused]] const char* pNewName)
  552. {
  553. #if UI_ANIMATION_REMOVED // lights
  554. std::vector<CBaseObject*> entityObjects;
  555. GetIEditor()->GetObjectManager()->FindObjectsOfType(&CEntityObject::staticMetaObject, entityObjects);
  556. std::for_each(std::begin(entityObjects), std::end(entityObjects),
  557. [&pOldName, &pNewName](CBaseObject* pBaseObject)
  558. {
  559. CEntityObject* pEntityObject = static_cast<CEntityObject*>(pBaseObject);
  560. bool bLight = pEntityObject && pEntityObject->GetEntityClass().Compare("Light") == 0;
  561. if (bLight)
  562. {
  563. string lightAnimation = pEntityObject->GetEntityPropertyString("lightanimation_LightAnimation");
  564. if (lightAnimation == pOldName)
  565. {
  566. pEntityObject->SetEntityPropertyString("lightanimation_LightAnimation", pNewName);
  567. }
  568. }
  569. });
  570. #endif
  571. }
  572. //////////////////////////////////////////////////////////////////////////
  573. bool CUiAnimViewSequence::SetName(const char* pName)
  574. {
  575. // Check if there is already a sequence with that name
  576. const CUiAnimViewSequenceManager* pSequenceManager = CUiAnimViewSequenceManager::GetSequenceManager();
  577. if (pSequenceManager->GetSequenceByName(pName))
  578. {
  579. return false;
  580. }
  581. AZStd::string oldName = GetName();
  582. m_pAnimSequence->SetName(pName);
  583. if (UiAnimUndo::IsRecording())
  584. {
  585. UiAnimUndo::Record(new CUndoAnimNodeRename(this, oldName));
  586. }
  587. GetSequence()->OnNodeRenamed(this, oldName.c_str());
  588. return true;
  589. }
  590. //////////////////////////////////////////////////////////////////////////
  591. void CUiAnimViewSequence::DeleteSelectedKeys()
  592. {
  593. assert(UiAnimUndo::IsRecording());
  594. StoreUndoForTracksWithSelectedKeys();
  595. CUiAnimViewSequenceNotificationContext context(this);
  596. CUiAnimViewKeyBundle selectedKeys = GetSelectedKeys();
  597. for (int k = (int)selectedKeys.GetKeyCount() - 1; k >= 0; --k)
  598. {
  599. CUiAnimViewKeyHandle skey = selectedKeys.GetKey(k);
  600. skey.Delete();
  601. }
  602. // The selected keys are deleted, so notify the selection was just changed.
  603. OnKeySelectionChanged();
  604. }
  605. //////////////////////////////////////////////////////////////////////////
  606. void CUiAnimViewSequence::StoreUndoForTracksWithSelectedKeys()
  607. {
  608. assert(UiAnimUndo::IsRecording());
  609. CUiAnimViewKeyBundle selectedKeys = GetSelectedKeys();
  610. // Construct the set of tracks that have selected keys
  611. std::set<CUiAnimViewTrack*> tracks;
  612. for (int k = 0; k < (int)selectedKeys.GetKeyCount(); ++k)
  613. {
  614. CUiAnimViewKeyHandle skey = selectedKeys.GetKey(k);
  615. tracks.insert(skey.GetTrack());
  616. }
  617. // Store one key selection undo before...
  618. UiAnimUndo::Record(new CUndoAnimKeySelection(this));
  619. // For each of those tracks store an undo object
  620. for (auto iter = tracks.begin(); iter != tracks.end(); ++iter)
  621. {
  622. UiAnimUndo::Record(new CUndoTrackObject(*iter, false));
  623. }
  624. // ... and one after key changes
  625. UiAnimUndo::Record(new CUndoAnimKeySelection(this));
  626. }
  627. //////////////////////////////////////////////////////////////////////////
  628. void CUiAnimViewSequence::CopyKeysToClipboard(const bool bOnlySelectedKeys, const bool bOnlyFromSelectedTracks)
  629. {
  630. XmlNodeRef copyNode = XmlHelpers::CreateXmlNode("CopyKeysNode");
  631. CopyKeysToClipboard(copyNode, bOnlySelectedKeys, bOnlyFromSelectedTracks);
  632. CClipboard clip(nullptr);
  633. clip.Put(copyNode, "Track view keys");
  634. }
  635. //////////////////////////////////////////////////////////////////////////
  636. void CUiAnimViewSequence::CopyKeysToClipboard(XmlNodeRef& xmlNode, const bool bOnlySelectedKeys, const bool bOnlyFromSelectedTracks)
  637. {
  638. for (auto iter = m_childNodes.begin(); iter != m_childNodes.end(); ++iter)
  639. {
  640. CUiAnimViewNode* pChildNode = (*iter).get();
  641. pChildNode->CopyKeysToClipboard(xmlNode, bOnlySelectedKeys, bOnlyFromSelectedTracks);
  642. }
  643. }
  644. //////////////////////////////////////////////////////////////////////////
  645. void CUiAnimViewSequence::PasteKeysFromClipboard(CUiAnimViewAnimNode* pTargetNode, CUiAnimViewTrack* pTargetTrack, const float timeOffset)
  646. {
  647. assert(UiAnimUndo::IsRecording());
  648. CClipboard clipboard(nullptr);
  649. XmlNodeRef clipboardContent = clipboard.Get();
  650. if (clipboardContent)
  651. {
  652. std::vector<TMatchedTrackLocation> matchedLocations = GetMatchedPasteLocations(clipboardContent, pTargetNode, pTargetTrack);
  653. for (auto iter = matchedLocations.begin(); iter != matchedLocations.end(); ++iter)
  654. {
  655. const TMatchedTrackLocation& location = *iter;
  656. CUiAnimViewTrack* pTrack = location.first;
  657. const XmlNodeRef& trackNode = location.second;
  658. pTrack->PasteKeys(trackNode, timeOffset);
  659. }
  660. OnKeysChanged();
  661. }
  662. }
  663. //////////////////////////////////////////////////////////////////////////
  664. std::vector<CUiAnimViewSequence::TMatchedTrackLocation>
  665. CUiAnimViewSequence::GetMatchedPasteLocations(XmlNodeRef clipboardContent, CUiAnimViewAnimNode* pTargetNode, CUiAnimViewTrack* pTargetTrack)
  666. {
  667. std::vector<TMatchedTrackLocation> matchedLocations;
  668. bool bPastingSingleNode = false;
  669. XmlNodeRef singleNode;
  670. bool bPastingSingleTrack = false;
  671. XmlNodeRef singleTrack;
  672. // Check if the XML tree only contains one node and if so if that node only contains one track
  673. for (XmlNodeRef currentNode = clipboardContent; currentNode->getChildCount() > 0; currentNode = currentNode->getChild(0))
  674. {
  675. bool bAllChildsAreTracks = true;
  676. const unsigned int numChilds = currentNode->getChildCount();
  677. for (unsigned int i = 0; i < numChilds; ++i)
  678. {
  679. XmlNodeRef childNode = currentNode->getChild(i);
  680. if (strcmp(currentNode->getChild(0)->getTag(), "Track") != 0)
  681. {
  682. bAllChildsAreTracks = false;
  683. break;
  684. }
  685. }
  686. if (bAllChildsAreTracks)
  687. {
  688. bPastingSingleNode = true;
  689. singleNode = currentNode;
  690. if (currentNode->getChildCount() == 1)
  691. {
  692. bPastingSingleTrack = true;
  693. singleTrack = currentNode->getChild(0);
  694. }
  695. }
  696. else if (currentNode->getChildCount() != 1)
  697. {
  698. break;
  699. }
  700. }
  701. if (bPastingSingleTrack && pTargetNode && pTargetTrack)
  702. {
  703. // We have a target node & track, so try to match the value type
  704. unsigned int valueType = 0;
  705. if (singleTrack->getAttr("valueType", valueType))
  706. {
  707. if (pTargetTrack->GetValueType() == valueType)
  708. {
  709. matchedLocations.push_back(TMatchedTrackLocation(pTargetTrack, singleTrack));
  710. return matchedLocations;
  711. }
  712. }
  713. }
  714. if (bPastingSingleNode && pTargetNode)
  715. {
  716. // Set of tracks that were already matched
  717. std::vector<CUiAnimViewTrack*> matchedTracks;
  718. // We have a single node to paste and have been given a target node
  719. // so try to match the tracks by param type
  720. const unsigned int numTracks = singleNode->getChildCount();
  721. for (unsigned int i = 0; i < numTracks; ++i)
  722. {
  723. XmlNodeRef trackNode = singleNode->getChild(i);
  724. // Try to match the track
  725. auto matchingTracks = GetMatchingTracks(pTargetNode, trackNode);
  726. for (auto iter = matchingTracks.begin(); iter != matchingTracks.end(); ++iter)
  727. {
  728. CUiAnimViewTrack* pMatchedTrack = *iter;
  729. // Pick the first track that was matched *and* was not already matched
  730. if (!stl::find(matchedTracks, pMatchedTrack))
  731. {
  732. stl::push_back_unique(matchedTracks, pMatchedTrack);
  733. matchedLocations.push_back(TMatchedTrackLocation(pMatchedTrack, trackNode));
  734. break;
  735. }
  736. }
  737. }
  738. // Return if matching succeeded
  739. if (matchedLocations.size() > 0)
  740. {
  741. return matchedLocations;
  742. }
  743. }
  744. if (!bPastingSingleNode)
  745. {
  746. // Ok, we're pasting keys from multiple nodes, haven't been given any target
  747. // or matching the targets failed. Ignore given target pointers and start
  748. // a recursive match at the sequence root.
  749. GetMatchedPasteLocationsRec(matchedLocations, this, clipboardContent);
  750. }
  751. return matchedLocations;
  752. }
  753. //////////////////////////////////////////////////////////////////////////
  754. std::deque<CUiAnimViewTrack*> CUiAnimViewSequence::GetMatchingTracks(CUiAnimViewAnimNode* pAnimNode, XmlNodeRef trackNode)
  755. {
  756. std::deque<CUiAnimViewTrack*> matchingTracks;
  757. const AZStd::string trackName = trackNode->getAttr("name");
  758. IUiAnimationSystem* animationSystem = nullptr;
  759. UiEditorAnimationBus::BroadcastResult(animationSystem, &UiEditorAnimationBus::Events::GetAnimationSystem);
  760. CUiAnimParamType animParamType;
  761. animParamType.Serialize(animationSystem, trackNode, true);
  762. unsigned int valueType;
  763. if (!trackNode->getAttr("valueType", valueType))
  764. {
  765. return matchingTracks;
  766. }
  767. CUiAnimViewTrackBundle tracks = pAnimNode->GetTracksByParam(animParamType);
  768. const unsigned int trackCount = tracks.GetCount();
  769. if (trackCount > 0)
  770. {
  771. // Search for a track with the given name and value type
  772. for (unsigned int i = 0; i < trackCount; ++i)
  773. {
  774. CUiAnimViewTrack* pTrack = tracks.GetTrack(i);
  775. if (pTrack->GetValueType() == valueType)
  776. {
  777. if (pTrack->GetName() == trackName)
  778. {
  779. matchingTracks.push_back(pTrack);
  780. }
  781. }
  782. }
  783. // Then with lower precedence add the tracks that only match the value
  784. for (unsigned int i = 0; i < trackCount; ++i)
  785. {
  786. CUiAnimViewTrack* pTrack = tracks.GetTrack(i);
  787. if (pTrack->GetValueType() == valueType)
  788. {
  789. stl::push_back_unique(matchingTracks, pTrack);
  790. }
  791. }
  792. }
  793. return matchingTracks;
  794. }
  795. //////////////////////////////////////////////////////////////////////////
  796. void CUiAnimViewSequence::GetMatchedPasteLocationsRec(std::vector<TMatchedTrackLocation>& locations, CUiAnimViewNode* pCurrentNode, XmlNodeRef clipboardNode)
  797. {
  798. if (pCurrentNode->GetNodeType() == eUiAVNT_Sequence)
  799. {
  800. if (strcmp(clipboardNode->getTag(), "CopyKeysNode") != 0)
  801. {
  802. return;
  803. }
  804. }
  805. const unsigned int numChildNodes = clipboardNode->getChildCount();
  806. for (unsigned int nodeIndex = 0; nodeIndex < numChildNodes; ++nodeIndex)
  807. {
  808. XmlNodeRef xmlChildNode = clipboardNode->getChild(nodeIndex);
  809. const AZStd::string tagName = xmlChildNode->getTag();
  810. if (tagName == "Node")
  811. {
  812. const AZStd::string nodeName = xmlChildNode->getAttr("name");
  813. int nodeType = eUiAnimNodeType_Invalid;
  814. xmlChildNode->getAttr("type", nodeType);
  815. const unsigned int childCount = pCurrentNode->GetChildCount();
  816. for (unsigned int i = 0; i < childCount; ++i)
  817. {
  818. CUiAnimViewNode* pChildNode = pCurrentNode->GetChild(i);
  819. if (pChildNode->GetNodeType() == eUiAVNT_AnimNode)
  820. {
  821. CUiAnimViewAnimNode* pAnimNode = static_cast<CUiAnimViewAnimNode*>(pChildNode);
  822. if (pAnimNode->GetName() == nodeName && pAnimNode->GetType() == nodeType)
  823. {
  824. GetMatchedPasteLocationsRec(locations, pChildNode, xmlChildNode);
  825. }
  826. }
  827. }
  828. }
  829. else if (tagName == "Track")
  830. {
  831. const AZStd::string trackName = xmlChildNode->getAttr("name");
  832. IUiAnimationSystem* animationSystem = nullptr;
  833. UiEditorAnimationBus::BroadcastResult(animationSystem, &UiEditorAnimationBus::Events::GetAnimationSystem);
  834. CUiAnimParamType trackParamType;
  835. trackParamType.Serialize(animationSystem, xmlChildNode, true);
  836. int trackParamValue = eUiAnimValue_Unknown;
  837. xmlChildNode->getAttr("valueType", trackParamValue);
  838. const unsigned int childCount = pCurrentNode->GetChildCount();
  839. for (unsigned int i = 0; i < childCount; ++i)
  840. {
  841. CUiAnimViewNode* pNode = pCurrentNode->GetChild(i);
  842. if (pNode->GetNodeType() == eUiAVNT_Track)
  843. {
  844. CUiAnimViewTrack* pTrack = static_cast<CUiAnimViewTrack*>(pNode);
  845. if (pTrack->GetName() == trackName && pTrack->GetParameterType() == trackParamType)
  846. {
  847. locations.push_back(TMatchedTrackLocation(pTrack, xmlChildNode));
  848. }
  849. }
  850. }
  851. }
  852. }
  853. }
  854. //////////////////////////////////////////////////////////////////////////
  855. void CUiAnimViewSequence::AdjustKeysToTimeRange(Range newTimeRange)
  856. {
  857. assert (UiAnimUndo::IsRecording());
  858. // Store one key selection undo before...
  859. UiAnimUndo::Record(new CUndoAnimKeySelection(this));
  860. // Store key undo for each track
  861. CUiAnimViewTrackBundle tracks = GetAllTracks();
  862. const unsigned int numTracks = tracks.GetCount();
  863. for (unsigned int i = 0; i < numTracks; ++i)
  864. {
  865. CUiAnimViewTrack* pTrack = tracks.GetTrack(i);
  866. UiAnimUndo::Record(new CUndoTrackObject(pTrack, false));
  867. }
  868. // ... and one after key changes
  869. UiAnimUndo::Record(new CUndoAnimKeySelection(this));
  870. // Set new time range
  871. Range oldTimeRange = GetTimeRange();
  872. float offset = newTimeRange.start - oldTimeRange.start;
  873. // Calculate scale ratio.
  874. float scale = newTimeRange.Length() / oldTimeRange.Length();
  875. SetTimeRange(newTimeRange);
  876. CUiAnimViewKeyBundle keyBundle = GetAllKeys();
  877. const unsigned int numKeys = keyBundle.GetKeyCount();
  878. for (unsigned int i = 0; i < numKeys; ++i)
  879. {
  880. CUiAnimViewKeyHandle keyHandle = keyBundle.GetKey(i);
  881. keyHandle.SetTime(offset + keyHandle.GetTime() * scale);
  882. }
  883. }
  884. //////////////////////////////////////////////////////////////////////////
  885. void CUiAnimViewSequence::SetTimeRange(Range timeRange)
  886. {
  887. if (UiAnimUndo::IsRecording())
  888. {
  889. // Store old sequence settings
  890. UiAnimUndo::Record(new CUndoSequenceSettings(this));
  891. }
  892. m_pAnimSequence->SetTimeRange(timeRange);
  893. OnSequenceSettingsChanged();
  894. }
  895. //////////////////////////////////////////////////////////////////////////
  896. Range CUiAnimViewSequence::GetTimeRange() const
  897. {
  898. return m_pAnimSequence->GetTimeRange();
  899. }
  900. //////////////////////////////////////////////////////////////////////////
  901. void CUiAnimViewSequence::SetFlags(IUiAnimSequence::EUiAnimSequenceFlags flags)
  902. {
  903. if (UiAnimUndo::IsRecording())
  904. {
  905. // Store old sequence settings
  906. UiAnimUndo::Record(new CUndoSequenceSettings(this));
  907. }
  908. m_pAnimSequence->SetFlags(flags);
  909. OnSequenceSettingsChanged();
  910. }
  911. //////////////////////////////////////////////////////////////////////////
  912. IUiAnimSequence::EUiAnimSequenceFlags CUiAnimViewSequence::GetFlags() const
  913. {
  914. return (IUiAnimSequence::EUiAnimSequenceFlags)m_pAnimSequence->GetFlags();
  915. }
  916. //////////////////////////////////////////////////////////////////////////
  917. void CUiAnimViewSequence::DeselectAllKeys()
  918. {
  919. assert(UiAnimUndo::IsRecording());
  920. CUiAnimViewSequenceNotificationContext context(this);
  921. CUiAnimViewKeyBundle selectedKeys = GetSelectedKeys();
  922. for (unsigned int i = 0; i < selectedKeys.GetKeyCount(); ++i)
  923. {
  924. CUiAnimViewKeyHandle keyHandle = selectedKeys.GetKey(i);
  925. keyHandle.Select(false);
  926. }
  927. }
  928. //////////////////////////////////////////////////////////////////////////
  929. void CUiAnimViewSequence::OffsetSelectedKeys(const float timeOffset)
  930. {
  931. assert(UiAnimUndo::IsRecording());
  932. CUiAnimViewSequenceNotificationContext context(this);
  933. CUiAnimViewKeyBundle selectedKeys = GetSelectedKeys();
  934. for (int k = 0; k < (int)selectedKeys.GetKeyCount(); ++k)
  935. {
  936. CUiAnimViewKeyHandle skey = selectedKeys.GetKey(k);
  937. skey.Offset(timeOffset);
  938. }
  939. }
  940. //////////////////////////////////////////////////////////////////////////
  941. float CUiAnimViewSequence::ClipTimeOffsetForOffsetting(const float timeOffset)
  942. {
  943. CUiAnimViewKeyBundle selectedKeys = GetSelectedKeys();
  944. float newTimeOffset = timeOffset;
  945. for (int k = 0; k < (int)selectedKeys.GetKeyCount(); ++k)
  946. {
  947. CUiAnimViewKeyHandle skey = selectedKeys.GetKey(k);
  948. const float keyTime = skey.GetTime();
  949. float newKeyTime = keyTime + timeOffset;
  950. Range extendedTimeRange(0.0f, GetTimeRange().end);
  951. extendedTimeRange.ClipValue(newKeyTime);
  952. float offset = newKeyTime - keyTime;
  953. if (fabs(offset) < fabs(newTimeOffset))
  954. {
  955. newTimeOffset = offset;
  956. }
  957. }
  958. return newTimeOffset;
  959. }
  960. //////////////////////////////////////////////////////////////////////////
  961. float CUiAnimViewSequence::ClipTimeOffsetForScaling(float timeOffset)
  962. {
  963. if (timeOffset <= 0)
  964. {
  965. return timeOffset;
  966. }
  967. CUiAnimViewKeyBundle selectedKeys = GetSelectedKeys();
  968. float newTimeOffset = timeOffset;
  969. for (int k = 0; k < (int)selectedKeys.GetKeyCount(); ++k)
  970. {
  971. CUiAnimViewKeyHandle skey = selectedKeys.GetKey(k);
  972. float keyTime = skey.GetTime();
  973. float newKeyTime = keyTime * timeOffset;
  974. GetTimeRange().ClipValue(newKeyTime);
  975. float offset = newKeyTime / keyTime;
  976. if (offset < newTimeOffset)
  977. {
  978. newTimeOffset = offset;
  979. }
  980. }
  981. return newTimeOffset;
  982. }
  983. //////////////////////////////////////////////////////////////////////////
  984. void CUiAnimViewSequence::ScaleSelectedKeys(const float timeOffset)
  985. {
  986. assert(UiAnimUndo::IsRecording());
  987. CUiAnimViewSequenceNotificationContext context(this);
  988. if (timeOffset <= 0)
  989. {
  990. return;
  991. }
  992. CUiAnimViewKeyBundle selectedKeys = GetSelectedKeys();
  993. const CUiAnimViewTrack* pTrack = nullptr;
  994. for (int k = 0; k < (int)selectedKeys.GetKeyCount(); ++k)
  995. {
  996. CUiAnimViewKeyHandle skey = selectedKeys.GetKey(k);
  997. if (pTrack != skey.GetTrack())
  998. {
  999. pTrack = skey.GetTrack();
  1000. }
  1001. float keyt = skey.GetTime() * timeOffset;
  1002. skey.SetTime(keyt);
  1003. }
  1004. }
  1005. //////////////////////////////////////////////////////////////////////////
  1006. float CUiAnimViewSequence::ClipTimeOffsetForSliding(const float timeOffset)
  1007. {
  1008. CUiAnimViewKeyBundle keys = GetSelectedKeys();
  1009. std::set<CUiAnimViewTrack*> tracks;
  1010. std::set<CUiAnimViewTrack*>::const_iterator pTrackIter;
  1011. Range timeRange = GetTimeRange();
  1012. // Get the first key in the timeline among selected and
  1013. // also gather tracks.
  1014. float time0 = timeRange.end;
  1015. for (int k = 0; k < (int)keys.GetKeyCount(); ++k)
  1016. {
  1017. CUiAnimViewKeyHandle skey = keys.GetKey(k);
  1018. tracks.insert(skey.GetTrack());
  1019. float keyTime = skey.GetTime();
  1020. if (keyTime < time0)
  1021. {
  1022. time0 = keyTime;
  1023. }
  1024. }
  1025. // If 'bAll' is true, slide all tracks.
  1026. // (Otherwise, slide only selected tracks.)
  1027. bool bAll = Qt::AltModifier & QApplication::queryKeyboardModifiers();
  1028. if (bAll)
  1029. {
  1030. keys = GetKeysInTimeRange(time0, timeRange.end);
  1031. // Gather tracks again.
  1032. tracks.clear();
  1033. for (int k = 0; k < (int)keys.GetKeyCount(); ++k)
  1034. {
  1035. CUiAnimViewKeyHandle skey = keys.GetKey(k);
  1036. tracks.insert(skey.GetTrack());
  1037. }
  1038. }
  1039. float newTimeOffset = timeOffset;
  1040. for (pTrackIter = tracks.begin(); pTrackIter != tracks.end(); ++pTrackIter)
  1041. {
  1042. CUiAnimViewTrack* pTrack = *pTrackIter;
  1043. for (unsigned int i = 0; i < pTrack->GetKeyCount(); ++i)
  1044. {
  1045. CUiAnimViewKeyHandle keyHandle = pTrack->GetKey(i);
  1046. const float keyTime = keyHandle.GetTime();
  1047. if (keyTime >= time0)
  1048. {
  1049. float newKeyTime = keyTime + timeOffset;
  1050. timeRange.ClipValue(newKeyTime);
  1051. float offset = newKeyTime - keyTime;
  1052. if (fabs(offset) < fabs(newTimeOffset))
  1053. {
  1054. newTimeOffset = offset;
  1055. }
  1056. }
  1057. }
  1058. }
  1059. return newTimeOffset;
  1060. }
  1061. //////////////////////////////////////////////////////////////////////////
  1062. void CUiAnimViewSequence::SlideKeys(float timeOffset)
  1063. {
  1064. assert(UiAnimUndo::IsRecording());
  1065. CUiAnimViewSequenceNotificationContext context(this);
  1066. CUiAnimViewKeyBundle keys = GetSelectedKeys();
  1067. std::set<CUiAnimViewTrack*> tracks;
  1068. std::set<CUiAnimViewTrack*>::const_iterator pTrackIter;
  1069. Range timeRange = GetTimeRange();
  1070. // Get the first key in the timeline among selected and
  1071. // also gather tracks.
  1072. float time0 = timeRange.end;
  1073. for (int k = 0; k < (int)keys.GetKeyCount(); ++k)
  1074. {
  1075. CUiAnimViewKeyHandle skey = keys.GetKey(k);
  1076. tracks.insert(skey.GetTrack());
  1077. float keyTime = skey.GetTime();
  1078. if (keyTime < time0)
  1079. {
  1080. time0 = keyTime;
  1081. }
  1082. }
  1083. // If 'bAll' is true, slide all tracks.
  1084. // (Otherwise, slide only selected tracks.)
  1085. bool bAll = Qt::AltModifier & QApplication::queryKeyboardModifiers();
  1086. if (bAll)
  1087. {
  1088. keys = GetKeysInTimeRange(time0, timeRange.end);
  1089. // Gather tracks again.
  1090. tracks.clear();
  1091. for (int k = 0; k < (int)keys.GetKeyCount(); ++k)
  1092. {
  1093. CUiAnimViewKeyHandle skey = keys.GetKey(k);
  1094. tracks.insert(skey.GetTrack());
  1095. }
  1096. }
  1097. for (pTrackIter = tracks.begin(); pTrackIter != tracks.end(); ++pTrackIter)
  1098. {
  1099. CUiAnimViewTrack* pTrack = *pTrackIter;
  1100. pTrack->SlideKeys(time0, timeOffset);
  1101. }
  1102. }
  1103. //////////////////////////////////////////////////////////////////////////
  1104. void CUiAnimViewSequence::CloneSelectedKeys()
  1105. {
  1106. assert(UiAnimUndo::IsRecording());
  1107. CUiAnimViewSequenceNotificationContext context(this);
  1108. CUiAnimViewKeyBundle selectedKeys = GetSelectedKeys();
  1109. const CUiAnimViewTrack* pTrack = nullptr;
  1110. // In case of multiple cloning, indices cannot be used as a solid pointer to the original.
  1111. // So use the time of keys as an identifier, instead.
  1112. std::vector<float> selectedKeyTimes;
  1113. for (size_t k = 0; k < selectedKeys.GetKeyCount(); ++k)
  1114. {
  1115. CUiAnimViewKeyHandle skey = selectedKeys.GetKey(static_cast<unsigned int>(k));
  1116. if (pTrack != skey.GetTrack())
  1117. {
  1118. pTrack = skey.GetTrack();
  1119. }
  1120. selectedKeyTimes.push_back(skey.GetTime());
  1121. }
  1122. // Now, do the actual cloning.
  1123. for (size_t k = 0; k < selectedKeyTimes.size(); ++k)
  1124. {
  1125. CUiAnimViewKeyHandle skey = selectedKeys.GetKey(static_cast<unsigned int>(k));
  1126. skey = skey.GetTrack()->GetKeyByTime(selectedKeyTimes[k]);
  1127. assert(skey.IsValid());
  1128. if (!skey.IsValid())
  1129. {
  1130. continue;
  1131. }
  1132. CUiAnimViewKeyHandle newKey = skey.Clone();
  1133. // Select new key.
  1134. newKey.Select(true);
  1135. // Deselect cloned key.
  1136. skey.Select(false);
  1137. }
  1138. }
  1139. //////////////////////////////////////////////////////////////////////////
  1140. void CUiAnimViewSequence::BeginUndoTransaction()
  1141. {
  1142. QueueNotifications();
  1143. }
  1144. //////////////////////////////////////////////////////////////////////////
  1145. void CUiAnimViewSequence::EndUndoTransaction()
  1146. {
  1147. SubmitPendingNotifcations();
  1148. }
  1149. //////////////////////////////////////////////////////////////////////////
  1150. void CUiAnimViewSequence::BeginRestoreTransaction()
  1151. {
  1152. QueueNotifications();
  1153. }
  1154. //////////////////////////////////////////////////////////////////////////
  1155. void CUiAnimViewSequence::EndRestoreTransaction()
  1156. {
  1157. SubmitPendingNotifcations();
  1158. }
  1159. //////////////////////////////////////////////////////////////////////////
  1160. bool CUiAnimViewSequence::IsActiveSequence() const
  1161. {
  1162. CUiAnimViewSequence* pSequence = nullptr;
  1163. UiEditorAnimationBus::BroadcastResult(pSequence, &UiEditorAnimationBus::Events::GetCurrentSequence);
  1164. return pSequence == this;
  1165. }
  1166. //////////////////////////////////////////////////////////////////////////
  1167. const float CUiAnimViewSequence::GetTime() const
  1168. {
  1169. return m_time;
  1170. }