AnimSequence.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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. // Description : Implementation of IAnimSequence interface.
  9. #include "AnimSequence.h"
  10. #include "StlUtils.h"
  11. #include "EventNode.h"
  12. #include "AzEntityNode.h"
  13. #include "UiAnimSerialize.h"
  14. //////////////////////////////////////////////////////////////////////////
  15. CUiAnimSequence::CUiAnimSequence()
  16. : CUiAnimSequence(nullptr, 0)
  17. {
  18. }
  19. //////////////////////////////////////////////////////////////////////////
  20. CUiAnimSequence::CUiAnimSequence(IUiAnimationSystem* pUiAnimationSystem, uint32 id)
  21. : m_refCount(0)
  22. {
  23. m_nextGenId = 1;
  24. m_pUiAnimationSystem = pUiAnimationSystem;
  25. m_flags = 0;
  26. m_pParentSequence = NULL;
  27. m_timeRange.Set(0, 10);
  28. m_bPaused = false;
  29. m_bActive = false;
  30. m_pOwner = NULL;
  31. m_pActiveDirector = NULL;
  32. m_precached = false;
  33. m_bResetting = false;
  34. m_id = id;
  35. m_time = -FLT_MAX;
  36. m_pEventStrings = aznew CUiAnimStringTable;
  37. }
  38. //////////////////////////////////////////////////////////////////////////
  39. CUiAnimSequence::~CUiAnimSequence()
  40. {
  41. // clear reference to me from all my nodes
  42. for (int i = static_cast<int>(m_nodes.size()); --i >= 0;)
  43. {
  44. if (m_nodes[i])
  45. {
  46. m_nodes[i]->SetSequence(nullptr);
  47. }
  48. }
  49. }
  50. //////////////////////////////////////////////////////////////////////////
  51. void CUiAnimSequence::add_ref()
  52. {
  53. ++m_refCount;
  54. }
  55. //////////////////////////////////////////////////////////////////////////
  56. void CUiAnimSequence::release()
  57. {
  58. if (--m_refCount <= 0)
  59. {
  60. delete this;
  61. }
  62. }
  63. //////////////////////////////////////////////////////////////////////////
  64. void CUiAnimSequence::SetName(const char* name)
  65. {
  66. if (!m_pUiAnimationSystem)
  67. {
  68. return; // should never happen, null pointer guard
  69. }
  70. AZStd::string originalName = GetName();
  71. m_name = name;
  72. m_pUiAnimationSystem->OnSequenceRenamed(originalName.c_str(), m_name.c_str());
  73. if (GetOwner())
  74. {
  75. GetOwner()->OnModified();
  76. }
  77. }
  78. //////////////////////////////////////////////////////////////////////////
  79. const char* CUiAnimSequence::GetName() const
  80. {
  81. return m_name.c_str();
  82. }
  83. //////////////////////////////////////////////////////////////////////////
  84. void CUiAnimSequence::SetFlags(int flags)
  85. {
  86. m_flags = flags;
  87. }
  88. //////////////////////////////////////////////////////////////////////////
  89. int CUiAnimSequence::GetFlags() const
  90. {
  91. return m_flags;
  92. }
  93. //////////////////////////////////////////////////////////////////////////
  94. int CUiAnimSequence::GetCutSceneFlags(const bool localFlags) const
  95. {
  96. int currentFlags = m_flags & (eSeqFlags_NoHUD | eSeqFlags_NoPlayer | eSeqFlags_16To9 | eSeqFlags_NoGameSounds | eSeqFlags_NoAbort);
  97. if (m_pParentSequence != NULL)
  98. {
  99. if (localFlags == true)
  100. {
  101. currentFlags &= ~m_pParentSequence->GetCutSceneFlags();
  102. }
  103. else
  104. {
  105. currentFlags |= m_pParentSequence->GetCutSceneFlags();
  106. }
  107. }
  108. return currentFlags;
  109. }
  110. //////////////////////////////////////////////////////////////////////////
  111. void CUiAnimSequence::SetParentSequence(IUiAnimSequence* pParentSequence)
  112. {
  113. m_pParentSequence = pParentSequence;
  114. }
  115. //////////////////////////////////////////////////////////////////////////
  116. const IUiAnimSequence* CUiAnimSequence::GetParentSequence() const
  117. {
  118. return m_pParentSequence;
  119. }
  120. //////////////////////////////////////////////////////////////////////////
  121. int CUiAnimSequence::GetNodeCount() const
  122. {
  123. return static_cast<int>(m_nodes.size());
  124. }
  125. //////////////////////////////////////////////////////////////////////////
  126. IUiAnimNode* CUiAnimSequence::GetNode(int index) const
  127. {
  128. assert(index >= 0 && index < (int)m_nodes.size());
  129. return m_nodes[index].get();
  130. }
  131. //////////////////////////////////////////////////////////////////////////
  132. bool CUiAnimSequence::AddNode(IUiAnimNode* pAnimNode)
  133. {
  134. AZ_Assert(pAnimNode, "Expected valid animNode");
  135. if(!pAnimNode)
  136. {
  137. return false;
  138. }
  139. pAnimNode->SetSequence(this);
  140. pAnimNode->SetTimeRange(m_timeRange);
  141. // Check if this node already in sequence.
  142. bool found = false;
  143. for (int i = 0; i < (int)m_nodes.size(); i++)
  144. {
  145. if (pAnimNode == m_nodes[i].get())
  146. {
  147. found = true;
  148. break;
  149. }
  150. }
  151. if (!found)
  152. {
  153. m_nodes.push_back(AZStd::intrusive_ptr<IUiAnimNode>(pAnimNode));
  154. }
  155. const int nodeId = static_cast<CUiAnimNode*>(pAnimNode)->GetId();
  156. if (nodeId >= static_cast<int>(m_nextGenId))
  157. {
  158. m_nextGenId = nodeId + 1;
  159. }
  160. if (pAnimNode->NeedToRender())
  161. {
  162. AddNodeNeedToRender(pAnimNode);
  163. }
  164. bool bNewDirectorNode = m_pActiveDirector == NULL && pAnimNode->GetType() == eUiAnimNodeType_Director;
  165. if (bNewDirectorNode)
  166. {
  167. m_pActiveDirector = pAnimNode;
  168. }
  169. return true;
  170. }
  171. //////////////////////////////////////////////////////////////////////////
  172. IUiAnimNode* CUiAnimSequence::CreateNodeInternal(EUiAnimNodeType nodeType, uint32 nNodeId)
  173. {
  174. CUiAnimNode* pAnimNode = NULL;
  175. if (nNodeId == -1)
  176. {
  177. nNodeId = m_nextGenId;
  178. }
  179. switch (nodeType)
  180. {
  181. case eUiAnimNodeType_AzEntity:
  182. pAnimNode = aznew CUiAnimAzEntityNode(nNodeId);
  183. break;
  184. case eUiAnimNodeType_Event:
  185. pAnimNode = aznew CUiAnimEventNode(nNodeId);
  186. break;
  187. }
  188. if (pAnimNode)
  189. {
  190. AddNode(pAnimNode);
  191. }
  192. return pAnimNode;
  193. }
  194. //////////////////////////////////////////////////////////////////////////
  195. IUiAnimNode* CUiAnimSequence::CreateNode(EUiAnimNodeType nodeType)
  196. {
  197. return CreateNodeInternal(nodeType);
  198. }
  199. //////////////////////////////////////////////////////////////////////////
  200. IUiAnimNode* CUiAnimSequence::CreateNode(XmlNodeRef node)
  201. {
  202. if (!GetUiAnimationSystem())
  203. {
  204. return 0; // should never happen, null pointer guard
  205. }
  206. EUiAnimNodeType type;
  207. static_cast<UiAnimationSystem*>(GetUiAnimationSystem())->SerializeNodeType(type, node, true, IUiAnimSequence::kSequenceVersion, 0);
  208. XmlString name;
  209. if (!node->getAttr("Name", name))
  210. {
  211. return 0;
  212. }
  213. IUiAnimNode* pNewNode = CreateNode(type);
  214. if (!pNewNode)
  215. {
  216. return 0;
  217. }
  218. pNewNode->SetName(name);
  219. pNewNode->Serialize(node, true, true);
  220. CUiAnimNode* newAnimNode = static_cast<CUiAnimNode*>(pNewNode);
  221. // Make sure de-serializing this node didn't just create an id conflict. This can happen sometimes
  222. // when copy/pasting nodes from a different sequence to this one.
  223. for (auto curNode : m_nodes)
  224. {
  225. CUiAnimNode* animNode = static_cast<CUiAnimNode*>(curNode.get());
  226. if (animNode->GetId() == newAnimNode->GetId() && animNode != newAnimNode)
  227. {
  228. // Conflict detected, resolve it by assigning a new id to the new node.
  229. newAnimNode->SetId(m_nextGenId++);
  230. }
  231. }
  232. return pNewNode;
  233. }
  234. //////////////////////////////////////////////////////////////////////////
  235. // Only called from undo/redo
  236. void CUiAnimSequence::RemoveNode(IUiAnimNode* node)
  237. {
  238. assert(node != 0);
  239. static_cast<CUiAnimNode*>(node)->Activate(false);
  240. static_cast<CUiAnimNode*>(node)->OnReset();
  241. for (int i = 0; i < (int)m_nodes.size(); )
  242. {
  243. if (node == m_nodes[i].get())
  244. {
  245. m_nodes.erase(m_nodes.begin() + i);
  246. if (node->NeedToRender())
  247. {
  248. RemoveNodeNeedToRender(node);
  249. }
  250. continue;
  251. }
  252. if (m_nodes[i]->GetParent() == node)
  253. {
  254. m_nodes[i]->SetParent(0);
  255. }
  256. i++;
  257. }
  258. // The removed one was the active director node.
  259. if (m_pActiveDirector == node)
  260. {
  261. // Clear the active one.
  262. m_pActiveDirector = NULL;
  263. // If there is another director node, set it as active.
  264. for (AnimNodes::const_iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  265. {
  266. IUiAnimNode* pNode = it->get();
  267. if (pNode->GetType() == eUiAnimNodeType_Director)
  268. {
  269. SetActiveDirector(pNode);
  270. break;
  271. }
  272. }
  273. }
  274. }
  275. //////////////////////////////////////////////////////////////////////////
  276. void CUiAnimSequence::RemoveAll()
  277. {
  278. stl::free_container(m_nodes);
  279. stl::free_container(m_events);
  280. stl::free_container(m_nodesNeedToRender);
  281. m_pActiveDirector = NULL;
  282. }
  283. //////////////////////////////////////////////////////////////////////////
  284. void CUiAnimSequence::Reset(bool bSeekToStart)
  285. {
  286. if (GetFlags() & eSeqFlags_LightAnimationSet)
  287. {
  288. return;
  289. }
  290. m_precached = false;
  291. m_bResetting = true;
  292. if (!bSeekToStart)
  293. {
  294. for (AnimNodes::iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  295. {
  296. IUiAnimNode* pAnimNode = it->get();
  297. static_cast<CUiAnimNode*>(pAnimNode)->OnReset();
  298. }
  299. m_bResetting = false;
  300. return;
  301. }
  302. bool bWasActive = m_bActive;
  303. if (!bWasActive)
  304. {
  305. Activate();
  306. }
  307. SUiAnimContext ec;
  308. ec.bSingleFrame = true;
  309. ec.bResetting = true;
  310. ec.pSequence = this;
  311. ec.time = m_timeRange.start;
  312. Animate(ec);
  313. if (!bWasActive)
  314. {
  315. Deactivate();
  316. }
  317. else
  318. {
  319. for (AnimNodes::iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  320. {
  321. IUiAnimNode* pAnimNode = it->get();
  322. static_cast<CUiAnimNode*>(pAnimNode)->OnReset();
  323. }
  324. }
  325. m_bResetting = false;
  326. }
  327. //////////////////////////////////////////////////////////////////////////
  328. void CUiAnimSequence::ResetHard()
  329. {
  330. if (GetFlags() & eSeqFlags_LightAnimationSet)
  331. {
  332. return;
  333. }
  334. m_bResetting = true;
  335. bool bWasActive = m_bActive;
  336. if (!bWasActive)
  337. {
  338. Activate();
  339. }
  340. SUiAnimContext ec;
  341. ec.bSingleFrame = true;
  342. ec.bResetting = true;
  343. ec.pSequence = this;
  344. ec.time = m_timeRange.start;
  345. Animate(ec);
  346. if (!bWasActive)
  347. {
  348. Deactivate();
  349. }
  350. else
  351. {
  352. for (AnimNodes::iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  353. {
  354. IUiAnimNode* pAnimNode = it->get();
  355. static_cast<CUiAnimNode*>(pAnimNode)->OnResetHard();
  356. }
  357. }
  358. m_bResetting = false;
  359. }
  360. //////////////////////////////////////////////////////////////////////////
  361. void CUiAnimSequence::Pause()
  362. {
  363. if (GetFlags() & eSeqFlags_LightAnimationSet || m_bPaused)
  364. {
  365. return;
  366. }
  367. m_bPaused = true;
  368. // Detach animation block from all nodes in this sequence.
  369. for (AnimNodes::iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  370. {
  371. IUiAnimNode* pAnimNode = it->get();
  372. static_cast<CUiAnimNode*>(pAnimNode)->OnPause();
  373. }
  374. }
  375. //////////////////////////////////////////////////////////////////////////
  376. void CUiAnimSequence::Resume()
  377. {
  378. if (GetFlags() & eSeqFlags_LightAnimationSet)
  379. {
  380. return;
  381. }
  382. if (m_bPaused)
  383. {
  384. m_bPaused = false;
  385. for (AnimNodes::iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  386. {
  387. IUiAnimNode* pAnimNode = it->get();
  388. static_cast<CUiAnimNode*>(pAnimNode)->OnResume();
  389. }
  390. }
  391. }
  392. //////////////////////////////////////////////////////////////////////////
  393. void CUiAnimSequence::OnLoop()
  394. {
  395. for (AnimNodes::iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  396. {
  397. IUiAnimNode* pAnimNode = it->get();
  398. static_cast<CUiAnimNode*>(pAnimNode)->OnLoop();
  399. }
  400. }
  401. //////////////////////////////////////////////////////////////////////////
  402. bool CUiAnimSequence::IsPaused() const
  403. {
  404. return m_bPaused;
  405. }
  406. //////////////////////////////////////////////////////////////////////////
  407. void CUiAnimSequence::OnStart()
  408. {
  409. for (AnimNodes::iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  410. {
  411. IUiAnimNode* pAnimNode = it->get();
  412. static_cast<CUiAnimNode*>(pAnimNode)->OnStart();
  413. }
  414. }
  415. //////////////////////////////////////////////////////////////////////////
  416. void CUiAnimSequence::OnStop()
  417. {
  418. for (AnimNodes::iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  419. {
  420. IUiAnimNode* pAnimNode = it->get();
  421. static_cast<CUiAnimNode*>(pAnimNode)->OnStop();
  422. }
  423. }
  424. //////////////////////////////////////////////////////////////////////////
  425. void CUiAnimSequence::StillUpdate()
  426. {
  427. if (GetFlags() & eSeqFlags_LightAnimationSet)
  428. {
  429. return;
  430. }
  431. for (AnimNodes::iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  432. {
  433. IUiAnimNode* pAnimNode = it->get();
  434. pAnimNode->StillUpdate();
  435. }
  436. }
  437. //////////////////////////////////////////////////////////////////////////
  438. void CUiAnimSequence::Animate(const SUiAnimContext& ec)
  439. {
  440. assert(m_bActive);
  441. if (GetFlags() & eSeqFlags_LightAnimationSet)
  442. {
  443. return;
  444. }
  445. SUiAnimContext animContext = ec;
  446. animContext.pSequence = this;
  447. m_time = animContext.time;
  448. // Evaluate all animation nodes in sequence.
  449. // The director first.
  450. if (m_pActiveDirector)
  451. {
  452. m_pActiveDirector->Animate(animContext);
  453. }
  454. for (AnimNodes::iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  455. {
  456. // Make sure correct animation block is binded to node.
  457. IUiAnimNode* pAnimNode = it->get();
  458. // All other (inactive) director nodes are skipped.
  459. if (pAnimNode->GetType() == eUiAnimNodeType_Director)
  460. {
  461. continue;
  462. }
  463. // If this is a descendant of a director node and that director is currently not active, skip this one.
  464. IUiAnimNode* pParentDirector = pAnimNode->HasDirectorAsParent();
  465. if (pParentDirector && pParentDirector != m_pActiveDirector)
  466. {
  467. continue;
  468. }
  469. if (pAnimNode->GetFlags() & eUiAnimNodeFlags_Disabled)
  470. {
  471. continue;
  472. }
  473. // Animate node.
  474. pAnimNode->Animate(animContext);
  475. }
  476. }
  477. //////////////////////////////////////////////////////////////////////////
  478. void CUiAnimSequence::Render()
  479. {
  480. for (AnimNodes::iterator it = m_nodesNeedToRender.begin(); it != m_nodesNeedToRender.end(); ++it)
  481. {
  482. IUiAnimNode* pAnimNode = it->get();
  483. pAnimNode->Render();
  484. }
  485. }
  486. //////////////////////////////////////////////////////////////////////////
  487. void CUiAnimSequence::Activate()
  488. {
  489. if (m_bActive)
  490. {
  491. return;
  492. }
  493. m_bActive = true;
  494. // Assign animation block to all nodes in this sequence.
  495. for (AnimNodes::iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  496. {
  497. CUiAnimNode* pAnimNode = static_cast<CUiAnimNode*>(it->get());
  498. pAnimNode->OnReset();
  499. pAnimNode->Activate(true);
  500. }
  501. }
  502. typedef AZStd::fixed_string<512> stack_string;
  503. //////////////////////////////////////////////////////////////////////////
  504. void CUiAnimSequence::Deactivate()
  505. {
  506. if (!m_bActive)
  507. {
  508. return;
  509. }
  510. // Detach animation block from all nodes in this sequence.
  511. for (AnimNodes::iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  512. {
  513. CUiAnimNode* pAnimNode = static_cast<CUiAnimNode*>(it->get());
  514. pAnimNode->Activate(false);
  515. pAnimNode->OnReset();
  516. }
  517. // Remove a possibly cached game hint associated with this anim sequence.
  518. stack_string sTemp("anim_sequence_");
  519. sTemp += m_name.c_str();
  520. // Audio: Release precached sound
  521. m_bActive = false;
  522. m_precached = false;
  523. }
  524. //////////////////////////////////////////////////////////////////////////
  525. void CUiAnimSequence::PrecacheData(float startTime)
  526. {
  527. PrecacheStatic(startTime);
  528. }
  529. //////////////////////////////////////////////////////////////////////////
  530. void CUiAnimSequence::PrecacheStatic(const float startTime)
  531. {
  532. // pre-cache animation keys
  533. for (AnimNodes::const_iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  534. {
  535. IUiAnimNode* pAnimNode = it->get();
  536. static_cast<CUiAnimNode*>(pAnimNode)->PrecacheStatic(startTime);
  537. }
  538. PrecacheDynamic(startTime);
  539. if (m_precached)
  540. {
  541. return;
  542. }
  543. // Try to cache this sequence's game hint if one exists.
  544. stack_string sTemp("anim_sequence_");
  545. sTemp += m_name.c_str();
  546. gEnv->pLog->Log("=== Precaching render data for Ui animation: %s ===", GetName());
  547. m_precached = true;
  548. }
  549. //////////////////////////////////////////////////////////////////////////
  550. void CUiAnimSequence::PrecacheDynamic(float time)
  551. {
  552. // pre-cache animation keys
  553. for (AnimNodes::const_iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  554. {
  555. IUiAnimNode* pAnimNode = it->get();
  556. static_cast<CUiAnimNode*>(pAnimNode)->PrecacheDynamic(time);
  557. }
  558. }
  559. //////////////////////////////////////////////////////////////////////////
  560. void CUiAnimSequence::Reflect(AZ::SerializeContext* serializeContext)
  561. {
  562. serializeContext->Class<CUiAnimSequence>()
  563. ->Version(2)
  564. ->Field("Name", &CUiAnimSequence::m_name)
  565. ->Field("Flags", &CUiAnimSequence::m_flags)
  566. ->Field("TimeRange", &CUiAnimSequence::m_timeRange)
  567. ->Field("ID", &CUiAnimSequence::m_id)
  568. ->Field("Nodes", &CUiAnimSequence::m_nodes)
  569. ->Field("Events", &CUiAnimSequence::m_events);
  570. }
  571. //////////////////////////////////////////////////////////////////////////
  572. void CUiAnimSequence::Serialize(XmlNodeRef& xmlNode, bool bLoading, bool bLoadEmptyTracks, uint32 overrideId, [[maybe_unused]] bool bResetLightAnimSet)
  573. {
  574. if (bLoading)
  575. {
  576. // Load.
  577. RemoveAll();
  578. int sequenceVersion = 0;
  579. xmlNode->getAttr("SequenceVersion", sequenceVersion);
  580. Range timeRange;
  581. m_name = xmlNode->getAttr("Name");
  582. xmlNode->getAttr("Flags", m_flags);
  583. xmlNode->getAttr("StartTime", timeRange.start);
  584. xmlNode->getAttr("EndTime", timeRange.end);
  585. xmlNode->getAttr("ID", m_id);
  586. if (overrideId != 0)
  587. {
  588. m_id = overrideId;
  589. }
  590. INDENT_LOG_DURING_SCOPE(true, "Loading sequence '%s' (start time = %.2f, end time = %.2f) %s ID #%u",
  591. m_name.c_str(), timeRange.start, timeRange.end, overrideId ? "override" : "default", m_id);
  592. // Loading.
  593. XmlNodeRef nodes = xmlNode->findChild("Nodes");
  594. if (nodes)
  595. {
  596. uint32 id;
  597. EUiAnimNodeType nodeType;
  598. for (int i = 0; i < nodes->getChildCount(); ++i)
  599. {
  600. XmlNodeRef childNode = nodes->getChild(i);
  601. childNode->getAttr("Id", id);
  602. static_cast<UiAnimationSystem*>(GetUiAnimationSystem())->SerializeNodeType(nodeType, childNode, bLoading, sequenceVersion, m_flags);
  603. if (nodeType == eUiAnimNodeType_Invalid)
  604. {
  605. continue;
  606. }
  607. IUiAnimNode* pAnimNode = CreateNodeInternal((EUiAnimNodeType)nodeType, id);
  608. if (!pAnimNode)
  609. {
  610. continue;
  611. }
  612. pAnimNode->Serialize(childNode, bLoading, bLoadEmptyTracks);
  613. }
  614. // When all nodes loaded restore group hierarchy
  615. for (AnimNodes::const_iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  616. {
  617. CUiAnimNode* pAnimNode = static_cast<CUiAnimNode*>((*it).get());
  618. pAnimNode->PostLoad();
  619. // And properly adjust the 'm_lastGenId' to prevent the id clash.
  620. if (pAnimNode->GetId() >= (int)m_nextGenId)
  621. {
  622. m_nextGenId = pAnimNode->GetId() + 1;
  623. }
  624. }
  625. }
  626. // Setting the time range must be done after the loading of all nodes
  627. // since it sets the time range of tracks, also.
  628. SetTimeRange(timeRange);
  629. Deactivate();
  630. //ComputeTimeRange();
  631. if (GetOwner())
  632. {
  633. GetOwner()->OnModified();
  634. }
  635. }
  636. else
  637. {
  638. xmlNode->setAttr("SequenceVersion", IUiAnimSequence::kSequenceVersion);
  639. const char* fullname = GetName();
  640. xmlNode->setAttr("Name", fullname); // Save the full path as a name.
  641. xmlNode->setAttr("Flags", m_flags);
  642. xmlNode->setAttr("StartTime", m_timeRange.start);
  643. xmlNode->setAttr("EndTime", m_timeRange.end);
  644. xmlNode->setAttr("ID", m_id);
  645. // Save.
  646. XmlNodeRef nodes = xmlNode->newChild("Nodes");
  647. int num = GetNodeCount();
  648. for (int i = 0; i < num; i++)
  649. {
  650. IUiAnimNode* pAnimNode = GetNode(i);
  651. if (pAnimNode)
  652. {
  653. XmlNodeRef xn = nodes->newChild("Node");
  654. pAnimNode->Serialize(xn, bLoading, true);
  655. }
  656. }
  657. }
  658. }
  659. //////////////////////////////////////////////////////////////////////////
  660. void CUiAnimSequence::InitPostLoad(IUiAnimationSystem* pUiAnimationSystem, bool remapIds, LyShine::EntityIdMap* entityIdMap)
  661. {
  662. m_pUiAnimationSystem = pUiAnimationSystem;
  663. int nodeCount = GetNodeCount();
  664. for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
  665. {
  666. IUiAnimNode* animNode = GetNode(nodeIndex);
  667. if (animNode)
  668. {
  669. animNode->InitPostLoad(this, remapIds, entityIdMap);
  670. }
  671. }
  672. }
  673. //////////////////////////////////////////////////////////////////////////
  674. void CUiAnimSequence::SetTimeRange(Range timeRange)
  675. {
  676. m_timeRange = timeRange;
  677. // Set this time range for every track in animation.
  678. // Set time range to be in range of largest animation track.
  679. for (AnimNodes::iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  680. {
  681. IUiAnimNode* anode = it->get();
  682. anode->SetTimeRange(timeRange);
  683. }
  684. }
  685. //////////////////////////////////////////////////////////////////////////
  686. void CUiAnimSequence::AdjustKeysToTimeRange(const Range& timeRange)
  687. {
  688. float offset = timeRange.start - m_timeRange.start;
  689. // Calculate scale ratio.
  690. float scale = timeRange.Length() / m_timeRange.Length();
  691. m_timeRange = timeRange;
  692. // Set time range to be in range of largest animation track.
  693. for (AnimNodes::iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  694. {
  695. IUiAnimNode* pAnimNode = it->get();
  696. int trackCount = pAnimNode->GetTrackCount();
  697. for (int paramIndex = 0; paramIndex < trackCount; paramIndex++)
  698. {
  699. IUiAnimTrack* track = pAnimNode->GetTrackByIndex(paramIndex);
  700. int nkey = track->GetNumKeys();
  701. for (int k = 0; k < nkey; k++)
  702. {
  703. float keytime = track->GetKeyTime(k);
  704. keytime = offset + keytime * scale;
  705. track->SetKeyTime(k, keytime);
  706. }
  707. }
  708. }
  709. }
  710. //////////////////////////////////////////////////////////////////////////
  711. void CUiAnimSequence::ComputeTimeRange()
  712. {
  713. Range timeRange = m_timeRange;
  714. // Set time range to be in range of largest animation track.
  715. for (AnimNodes::iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  716. {
  717. IUiAnimNode* pAnimNode = it->get();
  718. int trackCount = pAnimNode->GetTrackCount();
  719. for (int paramIndex = 0; paramIndex < trackCount; paramIndex++)
  720. {
  721. IUiAnimTrack* track = pAnimNode->GetTrackByIndex(paramIndex);
  722. int nkey = track->GetNumKeys();
  723. if (nkey > 0)
  724. {
  725. timeRange.start = std::min(timeRange.start, track->GetKeyTime(0));
  726. timeRange.end = std::max(timeRange.end, track->GetKeyTime(nkey - 1));
  727. }
  728. }
  729. }
  730. if (timeRange.start > 0)
  731. {
  732. timeRange.start = 0;
  733. }
  734. m_timeRange = timeRange;
  735. }
  736. //////////////////////////////////////////////////////////////////////////
  737. bool CUiAnimSequence::AddTrackEvent(const char* szEvent)
  738. {
  739. AZ_Assert(szEvent && szEvent[0], "Track Event is nullptr.");
  740. if (stl::push_back_unique(m_events, szEvent))
  741. {
  742. NotifyTrackEvent(IUiTrackEventListener::eTrackEventReason_Added, szEvent);
  743. return true;
  744. }
  745. return false;
  746. }
  747. //////////////////////////////////////////////////////////////////////////
  748. bool CUiAnimSequence::RemoveTrackEvent(const char* szEvent)
  749. {
  750. AZ_Assert(szEvent && szEvent[0], "Track Event is nullptr.");
  751. if (stl::find_and_erase(m_events, szEvent))
  752. {
  753. NotifyTrackEvent(IUiTrackEventListener::eTrackEventReason_Removed, szEvent);
  754. return true;
  755. }
  756. return false;
  757. }
  758. //////////////////////////////////////////////////////////////////////////
  759. bool CUiAnimSequence::RenameTrackEvent(const char* szEvent, const char* szNewEvent)
  760. {
  761. AZ_Assert(szEvent && szEvent[0], "Track Event is nullptr.");
  762. AZ_Assert(szNewEvent && szNewEvent[0], "New Track Event is nullptr.");
  763. for (size_t i = 0; i < m_events.size(); ++i)
  764. {
  765. if (m_events[i] == szEvent)
  766. {
  767. m_events[i] = szNewEvent;
  768. NotifyTrackEvent(IUiTrackEventListener::eTrackEventReason_Renamed, szEvent, szNewEvent);
  769. return true;
  770. }
  771. }
  772. return false;
  773. }
  774. //////////////////////////////////////////////////////////////////////////
  775. bool CUiAnimSequence::MoveUpTrackEvent(const char* szEvent)
  776. {
  777. AZ_Assert(szEvent && szEvent[0], "Track Event is nullptr.");
  778. for (size_t i = 0; i < m_events.size(); ++i)
  779. {
  780. if (m_events[i] == szEvent)
  781. {
  782. AZ_Assert(i > 0, "Event already first in Track.");
  783. if (i > 0)
  784. {
  785. std::swap(m_events[i - 1], m_events[i]);
  786. NotifyTrackEvent(IUiTrackEventListener::eTrackEventReason_MovedUp, szEvent);
  787. }
  788. return true;
  789. }
  790. }
  791. return false;
  792. }
  793. //////////////////////////////////////////////////////////////////////////
  794. bool CUiAnimSequence::MoveDownTrackEvent(const char* szEvent)
  795. {
  796. AZ_Assert(szEvent && szEvent[0], "Track Event is nullptr.");
  797. for (size_t i = 0; i < m_events.size(); ++i)
  798. {
  799. if (m_events[i] == szEvent)
  800. {
  801. AZ_Assert(i < m_events.size() - 1, "Event already last in Track.");
  802. if (i < m_events.size() - 1)
  803. {
  804. std::swap(m_events[i], m_events[i + 1]);
  805. NotifyTrackEvent(IUiTrackEventListener::eTrackEventReason_MovedDown, szEvent);
  806. }
  807. return true;
  808. }
  809. }
  810. return false;
  811. }
  812. //////////////////////////////////////////////////////////////////////////
  813. void CUiAnimSequence::ClearTrackEvents()
  814. {
  815. m_events.clear();
  816. }
  817. //////////////////////////////////////////////////////////////////////////
  818. int CUiAnimSequence::GetTrackEventsCount() const
  819. {
  820. return static_cast<int>(m_events.size());
  821. }
  822. //////////////////////////////////////////////////////////////////////////
  823. char const* CUiAnimSequence::GetTrackEvent(int iIndex) const
  824. {
  825. char const* szResult = NULL;
  826. const bool bValid = (iIndex >= 0 && iIndex < GetTrackEventsCount());
  827. AZ_Assert(bValid, "Track Event index out of range.");
  828. if (bValid)
  829. {
  830. szResult = m_events[iIndex].c_str();
  831. }
  832. return szResult;
  833. }
  834. //////////////////////////////////////////////////////////////////////////
  835. void CUiAnimSequence::NotifyTrackEvent(IUiTrackEventListener::ETrackEventReason reason,
  836. const char* event, const char* param)
  837. {
  838. // Notify listeners
  839. for (TUiTrackEventListeners::iterator j = m_listeners.begin(); j != m_listeners.end(); ++j)
  840. {
  841. (*j)->OnTrackEvent(this, reason, event, (void*)param);
  842. }
  843. // Pass to Animation System to notify via EBus
  844. GetUiAnimationSystem()->NotifyTrackEventListeners(event, param, this);
  845. }
  846. //////////////////////////////////////////////////////////////////////////
  847. void CUiAnimSequence::TriggerTrackEvent(const char* event, const char* param)
  848. {
  849. NotifyTrackEvent(IUiTrackEventListener::eTrackEventReason_Triggered, event, param);
  850. }
  851. //////////////////////////////////////////////////////////////////////////
  852. void CUiAnimSequence::AddTrackEventListener(IUiTrackEventListener* pListener)
  853. {
  854. if (AZStd::find(m_listeners.begin(), m_listeners.end(), pListener) == m_listeners.end())
  855. {
  856. m_listeners.push_back(pListener);
  857. }
  858. }
  859. //////////////////////////////////////////////////////////////////////////
  860. void CUiAnimSequence::RemoveTrackEventListener(IUiTrackEventListener* pListener)
  861. {
  862. TUiTrackEventListeners::iterator it = AZStd::find(m_listeners.begin(), m_listeners.end(), pListener);
  863. if (it != m_listeners.end())
  864. {
  865. m_listeners.erase(it);
  866. }
  867. }
  868. //////////////////////////////////////////////////////////////////////////
  869. IUiAnimNode* CUiAnimSequence::FindNodeById(int nNodeId)
  870. {
  871. for (AnimNodes::const_iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  872. {
  873. IUiAnimNode* pAnimNode = it->get();
  874. if (static_cast<CUiAnimNode*>(pAnimNode)->GetId() == nNodeId)
  875. {
  876. return pAnimNode;
  877. }
  878. }
  879. return 0;
  880. }
  881. //////////////////////////////////////////////////////////////////////////
  882. IUiAnimNode* CUiAnimSequence::FindNodeByName(const char* sNodeName, const IUiAnimNode* pParentDirector)
  883. {
  884. for (AnimNodes::const_iterator it = m_nodes.begin(); it != m_nodes.end(); ++it)
  885. {
  886. IUiAnimNode* pAnimNode = it->get();
  887. // Case insensitive name comparison.
  888. if (_stricmp(((CUiAnimNode*)pAnimNode)->GetNameFast(), sNodeName) == 0)
  889. {
  890. bool bParentDirectorCheck = pAnimNode->HasDirectorAsParent() == pParentDirector;
  891. if (bParentDirectorCheck)
  892. {
  893. return pAnimNode;
  894. }
  895. }
  896. }
  897. return 0;
  898. }
  899. //////////////////////////////////////////////////////////////////////////
  900. void CUiAnimSequence::ReorderNode(IUiAnimNode* pNode, IUiAnimNode* pPivotNode, bool bNext)
  901. {
  902. if (pNode == pPivotNode || !pNode)
  903. {
  904. return;
  905. }
  906. AZStd::intrusive_ptr<IUiAnimNode> pTempHolder(pNode); // Keep reference to node so it is not deleted by erasing from list.
  907. stl::find_and_erase_if(m_nodes, [pNode](const AZStd::intrusive_ptr<IUiAnimNode>& sp) { return sp.get() == pNode; });
  908. AnimNodes::iterator it;
  909. for (it = m_nodes.begin(); it != m_nodes.end(); ++it)
  910. {
  911. IUiAnimNode* pAnimNode = it->get();
  912. if (pAnimNode == pPivotNode)
  913. {
  914. if (bNext)
  915. {
  916. m_nodes.insert(it + 1, AZStd::intrusive_ptr<IUiAnimNode>(pNode));
  917. }
  918. else
  919. {
  920. m_nodes.insert(it, AZStd::intrusive_ptr<IUiAnimNode>(pNode));
  921. }
  922. break;
  923. }
  924. }
  925. if (it == m_nodes.end())
  926. {
  927. m_nodes.insert(m_nodes.begin(), AZStd::intrusive_ptr<IUiAnimNode>(pNode));
  928. }
  929. }
  930. //////////////////////////////////////////////////////////////////////////
  931. void CUiAnimSequence::CopyNodeChildren(XmlNodeRef& xmlNode, IUiAnimNode* pAnimNode)
  932. {
  933. for (int k = 0; k < GetNodeCount(); ++k)
  934. {
  935. if (GetNode(k)->GetParent() == pAnimNode)
  936. {
  937. XmlNodeRef childNode = xmlNode->newChild("Node");
  938. GetNode(k)->Serialize(childNode, false, true);
  939. if (GetNode(k)->GetType() == eUiAnimNodeType_Group
  940. || pAnimNode->GetType() == eUiAnimNodeType_Director)
  941. {
  942. CopyNodeChildren(xmlNode, GetNode(k));
  943. }
  944. }
  945. }
  946. }
  947. //////////////////////////////////////////////////////////////////////////
  948. void CUiAnimSequence::CopyNodes(XmlNodeRef& xmlNode, IUiAnimNode** pSelectedNodes, uint32 count)
  949. {
  950. for (uint32 i = 0; i < count; ++i)
  951. {
  952. IUiAnimNode* pAnimNode = pSelectedNodes[i];
  953. if (pAnimNode)
  954. {
  955. XmlNodeRef xn = xmlNode->newChild("Node");
  956. pAnimNode->Serialize(xn, false, true);
  957. // If it is a group node, copy its children also.
  958. if (pAnimNode->GetType() == eUiAnimNodeType_Group || pAnimNode->GetType() == eUiAnimNodeType_Director)
  959. {
  960. CopyNodeChildren(xmlNode, pAnimNode);
  961. }
  962. }
  963. }
  964. }
  965. //////////////////////////////////////////////////////////////////////////
  966. void CUiAnimSequence::PasteNodes(const XmlNodeRef& xmlNode, IUiAnimNode* pParent)
  967. {
  968. int type, id;
  969. std::map<int, IUiAnimNode*> idToNode;
  970. for (int i = 0; i < xmlNode->getChildCount(); i++)
  971. {
  972. XmlNodeRef xn = xmlNode->getChild(i);
  973. if (!xn->getAttr("Type", type))
  974. {
  975. continue;
  976. }
  977. xn->getAttr("Id", id);
  978. IUiAnimNode* node = CreateNode((EUiAnimNodeType)type);
  979. if (!node)
  980. {
  981. continue;
  982. }
  983. idToNode[id] = node;
  984. xn->setAttr("Id", static_cast<CUiAnimNode*>(node)->GetId());
  985. node->Serialize(xn, true, true);
  986. int parentId = 0;
  987. if (xn->getAttr("ParentNode", parentId))
  988. {
  989. node->SetParent(idToNode[parentId]);
  990. }
  991. else
  992. // This means a top-level node.
  993. {
  994. if (pParent)
  995. {
  996. node->SetParent(pParent);
  997. }
  998. }
  999. }
  1000. }
  1001. //////////////////////////////////////////////////////////////////////////
  1002. bool CUiAnimSequence::AddNodeNeedToRender(IUiAnimNode* pNode)
  1003. {
  1004. assert(pNode != 0);
  1005. return stl::push_back_unique(m_nodesNeedToRender, AZStd::intrusive_ptr<IUiAnimNode>(pNode));
  1006. }
  1007. //////////////////////////////////////////////////////////////////////////
  1008. void CUiAnimSequence::RemoveNodeNeedToRender(IUiAnimNode* pNode)
  1009. {
  1010. assert(pNode != 0);
  1011. stl::find_and_erase_if(m_nodesNeedToRender, [pNode](const AZStd::intrusive_ptr<IUiAnimNode>& sp) { return sp.get() == pNode; });
  1012. }
  1013. //////////////////////////////////////////////////////////////////////////
  1014. void CUiAnimSequence::SetActiveDirector(IUiAnimNode* pDirectorNode)
  1015. {
  1016. if (!pDirectorNode)
  1017. {
  1018. return;
  1019. }
  1020. AZ_Assert(pDirectorNode->GetType() == eUiAnimNodeType_Director, "New Director Node is not Director Type.");
  1021. if (pDirectorNode->GetType() != eUiAnimNodeType_Director)
  1022. {
  1023. return; // It's not a director node.
  1024. }
  1025. if (pDirectorNode->GetSequence() != this)
  1026. {
  1027. return; // It's not a node belong to this sequence.
  1028. }
  1029. m_pActiveDirector = pDirectorNode;
  1030. }
  1031. //////////////////////////////////////////////////////////////////////////
  1032. IUiAnimNode* CUiAnimSequence::GetActiveDirector() const
  1033. {
  1034. return m_pActiveDirector;
  1035. }
  1036. //////////////////////////////////////////////////////////////////////////
  1037. bool CUiAnimSequence::IsAncestorOf(const IUiAnimSequence* pSequence) const
  1038. {
  1039. AZ_Assert(this != pSequence, "Checked if UiAnimSequence was ancestor of itself.");
  1040. if (this == pSequence)
  1041. {
  1042. return true;
  1043. }
  1044. // UI_ANIMATION_REVISIT: was only doing anything for sequence tracks
  1045. return false;
  1046. }