CompoundSplineTrack.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 "CompoundSplineTrack.h"
  9. #include "AnimSplineTrack.h"
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. UiCompoundSplineTrack::UiCompoundSplineTrack(int nDims, EUiAnimValue inValueType, CUiAnimParamType subTrackParamTypes[MAX_SUBTRACKS])
  12. : m_refCount(0)
  13. {
  14. assert(nDims > 0 && nDims <= MAX_SUBTRACKS);
  15. m_nDimensions = nDims;
  16. m_valueType = inValueType;
  17. m_nParamType = eUiAnimNodeType_Invalid;
  18. m_flags = 0;
  19. for (int i = 0; i < m_nDimensions; i++)
  20. {
  21. m_subTracks[i].reset(aznew C2DSplineTrack());
  22. m_subTracks[i]->SetParameterType(subTrackParamTypes[i]);
  23. if (inValueType == eUiAnimValue_RGB)
  24. {
  25. m_subTracks[i]->SetKeyValueRange(0.0f, 255.f);
  26. }
  27. }
  28. m_subTrackNames[0] = "X";
  29. m_subTrackNames[1] = "Y";
  30. m_subTrackNames[2] = "Z";
  31. m_subTrackNames[3] = "W";
  32. #ifdef UI_ANIMATION_SYSTEM_SUPPORT_EDITING
  33. m_bCustomColorSet = false;
  34. #endif
  35. }
  36. //////////////////////////////////////////////////////////////////////////
  37. // Need default constructor for AZ Serialization
  38. UiCompoundSplineTrack::UiCompoundSplineTrack()
  39. : m_refCount(0)
  40. , m_nDimensions(0)
  41. #ifdef UI_ANIMATION_SYSTEM_SUPPORT_EDITING
  42. , m_bCustomColorSet(false)
  43. #endif
  44. {
  45. }
  46. //////////////////////////////////////////////////////////////////////////
  47. void UiCompoundSplineTrack::SetTimeRange(const Range& timeRange)
  48. {
  49. for (int i = 0; i < m_nDimensions; i++)
  50. {
  51. m_subTracks[i]->SetTimeRange(timeRange);
  52. }
  53. }
  54. //////////////////////////////////////////////////////////////////////////
  55. void UiCompoundSplineTrack::PrepareNodeForSubTrackSerialization(XmlNodeRef& subTrackNode, XmlNodeRef& xmlNode, int i, bool bLoading)
  56. {
  57. assert(!bLoading || xmlNode->getChildCount() == m_nDimensions);
  58. if (bLoading)
  59. {
  60. subTrackNode = xmlNode->getChild(i);
  61. // First, check its version.
  62. if (strcmp(subTrackNode->getTag(), "SubTrack") == 0)
  63. // So, it's an old format.
  64. {
  65. #if 0
  66. CUiAnimParamType paramType = m_subTracks[i]->GetParameterType();
  67. // Recreate sub tracks as the old format.
  68. m_subTracks[i] = new CTcbFloatTrack;
  69. m_subTracks[i]->SetParameterType(paramType);
  70. #endif
  71. }
  72. }
  73. else
  74. {
  75. if (m_subTracks[i]->GetCurveType() == eUiAnimCurveType_BezierFloat)
  76. {
  77. // It's a new 2D Bezier curve.
  78. subTrackNode = xmlNode->newChild("NewSubTrack");
  79. }
  80. #if 0
  81. else
  82. // Old TCB spline
  83. {
  84. assert(m_subTracks[i]->GetCurveType() == eUiAnimCurveType_TCBFloat);
  85. subTrackNode = xmlNode->newChild("SubTrack");
  86. }
  87. #endif
  88. }
  89. }
  90. //////////////////////////////////////////////////////////////////////////
  91. bool UiCompoundSplineTrack::Serialize(IUiAnimationSystem* uiAnimationSystem, XmlNodeRef& xmlNode, bool bLoading, bool bLoadEmptyTracks /*=true */)
  92. {
  93. #ifdef UI_ANIMATION_SYSTEM_SUPPORT_EDITING
  94. if (bLoading)
  95. {
  96. int flags = m_flags;
  97. xmlNode->getAttr("Flags", flags);
  98. SetFlags(flags);
  99. xmlNode->getAttr("HasCustomColor", m_bCustomColorSet);
  100. if (m_bCustomColorSet)
  101. {
  102. unsigned int abgr;
  103. xmlNode->getAttr("CustomColor", abgr);
  104. m_customColor = ColorB(abgr);
  105. }
  106. }
  107. else
  108. {
  109. int flags = GetFlags();
  110. xmlNode->setAttr("Flags", flags);
  111. xmlNode->setAttr("HasCustomColor", m_bCustomColorSet);
  112. if (m_bCustomColorSet)
  113. {
  114. xmlNode->setAttr("CustomColor", m_customColor.pack_abgr8888());
  115. }
  116. }
  117. #endif
  118. for (int i = 0; i < m_nDimensions; i++)
  119. {
  120. XmlNodeRef subTrackNode;
  121. PrepareNodeForSubTrackSerialization(subTrackNode, xmlNode, i, bLoading);
  122. if (bLoading)
  123. {
  124. CUiAnimParamType paramType;
  125. paramType.Serialize(uiAnimationSystem, subTrackNode, bLoading);
  126. m_subTracks[i]->SetParameterType(paramType);
  127. UiAnimParamData paramData;
  128. paramData.Serialize(uiAnimationSystem, subTrackNode, bLoading);
  129. m_subTracks[i]->SetParamData(paramData);
  130. }
  131. else
  132. {
  133. CUiAnimParamType paramType = m_subTracks[i]->GetParameterType();
  134. paramType.Serialize(uiAnimationSystem, subTrackNode, bLoading);
  135. UiAnimParamData paramData = m_subTracks[i]->GetParamData();
  136. paramData.Serialize(uiAnimationSystem, subTrackNode, bLoading);
  137. }
  138. m_subTracks[i]->Serialize(uiAnimationSystem, subTrackNode, bLoading, bLoadEmptyTracks);
  139. }
  140. return true;
  141. }
  142. //////////////////////////////////////////////////////////////////////////
  143. bool UiCompoundSplineTrack::SerializeSelection(XmlNodeRef& xmlNode, bool bLoading, bool bCopySelected /*=false*/, float fTimeOffset /*=0*/)
  144. {
  145. for (int i = 0; i < m_nDimensions; i++)
  146. {
  147. XmlNodeRef subTrackNode;
  148. PrepareNodeForSubTrackSerialization(subTrackNode, xmlNode, i, bLoading);
  149. m_subTracks[i]->SerializeSelection(subTrackNode, bLoading, bCopySelected, fTimeOffset);
  150. }
  151. return true;
  152. }
  153. //////////////////////////////////////////////////////////////////////////
  154. void UiCompoundSplineTrack::GetValue(float time, float& value)
  155. {
  156. for (int i = 0; i < 1 && i < m_nDimensions; i++)
  157. {
  158. m_subTracks[i]->GetValue(time, value);
  159. }
  160. }
  161. //////////////////////////////////////////////////////////////////////////
  162. void UiCompoundSplineTrack::GetValue(float time, Vec3& value)
  163. {
  164. for (int i = 0; i < m_nDimensions; i++)
  165. {
  166. float v = value[i];
  167. m_subTracks[i]->GetValue(time, v);
  168. value[i] = v;
  169. }
  170. }
  171. //////////////////////////////////////////////////////////////////////////
  172. void UiCompoundSplineTrack::GetValue(float time, Vec4& value)
  173. {
  174. for (int i = 0; i < m_nDimensions; i++)
  175. {
  176. float v = value[i];
  177. m_subTracks[i]->GetValue(time, v);
  178. value[i] = v;
  179. }
  180. }
  181. //////////////////////////////////////////////////////////////////////////
  182. void UiCompoundSplineTrack::GetValue(float time, Quat& value)
  183. {
  184. if (m_nDimensions == 3)
  185. {
  186. // Assume Euler Angles XYZ
  187. float angles[3] = {0, 0, 0};
  188. for (int i = 0; i < m_nDimensions; i++)
  189. {
  190. m_subTracks[i]->GetValue(time, angles[i]);
  191. }
  192. value = Quat::CreateRotationXYZ(Ang3(DEG2RAD(angles[0]), DEG2RAD(angles[1]), DEG2RAD(angles[2])));
  193. }
  194. else
  195. {
  196. assert(0);
  197. value.SetIdentity();
  198. }
  199. }
  200. //////////////////////////////////////////////////////////////////////////
  201. void UiCompoundSplineTrack::GetValue(float time, AZ::Vector2& value)
  202. {
  203. for (int i = 0; i < m_nDimensions; i++)
  204. {
  205. float v = value.GetElement(i);
  206. m_subTracks[i]->GetValue(time, v);
  207. value.SetElement(i, v);
  208. }
  209. }
  210. //////////////////////////////////////////////////////////////////////////
  211. void UiCompoundSplineTrack::GetValue(float time, AZ::Vector3& value)
  212. {
  213. for (int i = 0; i < m_nDimensions; i++)
  214. {
  215. float v = value.GetElement(i);
  216. m_subTracks[i]->GetValue(time, v);
  217. value.SetElement(i, v);
  218. }
  219. }
  220. //////////////////////////////////////////////////////////////////////////
  221. void UiCompoundSplineTrack::GetValue(float time, AZ::Vector4& value)
  222. {
  223. for (int i = 0; i < m_nDimensions; i++)
  224. {
  225. float v = value.GetElement(i);
  226. m_subTracks[i]->GetValue(time, v);
  227. value.SetElement(i, v);
  228. }
  229. }
  230. //////////////////////////////////////////////////////////////////////////
  231. void UiCompoundSplineTrack::GetValue(float time, AZ::Color& value)
  232. {
  233. for (int i = 0; i < m_nDimensions; i++)
  234. {
  235. float v = value.GetElement(i);
  236. m_subTracks[i]->GetValue(time, v);
  237. value.SetElement(i, v);
  238. }
  239. }
  240. //////////////////////////////////////////////////////////////////////////
  241. void UiCompoundSplineTrack::SetValue(float time, const float& value, bool bDefault)
  242. {
  243. for (int i = 0; i < m_nDimensions; i++)
  244. {
  245. m_subTracks[i]->SetValue(time, value, bDefault);
  246. }
  247. }
  248. //////////////////////////////////////////////////////////////////////////
  249. void UiCompoundSplineTrack::SetValue(float time, const Vec3& value, bool bDefault)
  250. {
  251. for (int i = 0; i < m_nDimensions; i++)
  252. {
  253. m_subTracks[i]->SetValue(time, value[i], bDefault);
  254. }
  255. }
  256. //////////////////////////////////////////////////////////////////////////
  257. void UiCompoundSplineTrack::SetValue(float time, const Vec4& value, bool bDefault)
  258. {
  259. for (int i = 0; i < m_nDimensions; i++)
  260. {
  261. m_subTracks[i]->SetValue(time, value[i], bDefault);
  262. }
  263. }
  264. //////////////////////////////////////////////////////////////////////////
  265. void UiCompoundSplineTrack::SetValue(float time, const Quat& value, bool bDefault)
  266. {
  267. if (m_nDimensions == 3)
  268. {
  269. // Assume Euler Angles XYZ
  270. Ang3 angles = Ang3::GetAnglesXYZ(value);
  271. for (int i = 0; i < 3; i++)
  272. {
  273. float degree = RAD2DEG(angles[i]);
  274. if (false == bDefault)
  275. {
  276. // Try to prefer the shortest path of rotation.
  277. float degree0 = 0.0f;
  278. m_subTracks[i]->GetValue(time, degree0);
  279. degree = PreferShortestRotPath(degree, degree0);
  280. }
  281. m_subTracks[i]->SetValue(time, degree, bDefault);
  282. }
  283. }
  284. else
  285. {
  286. assert(0);
  287. }
  288. }
  289. //////////////////////////////////////////////////////////////////////////
  290. void UiCompoundSplineTrack::SetValue(float time, const AZ::Vector2& value, bool bDefault)
  291. {
  292. for (int i = 0; i < m_nDimensions; i++)
  293. {
  294. m_subTracks[i]->SetValue(time, value.GetElement(i), bDefault);
  295. }
  296. }
  297. //////////////////////////////////////////////////////////////////////////
  298. void UiCompoundSplineTrack::SetValue(float time, const AZ::Vector3& value, bool bDefault)
  299. {
  300. for (int i = 0; i < m_nDimensions; i++)
  301. {
  302. m_subTracks[i]->SetValue(time, value.GetElement(i), bDefault);
  303. }
  304. }
  305. //////////////////////////////////////////////////////////////////////////
  306. void UiCompoundSplineTrack::SetValue(float time, const AZ::Vector4& value, bool bDefault)
  307. {
  308. for (int i = 0; i < m_nDimensions; i++)
  309. {
  310. m_subTracks[i]->SetValue(time, value.GetElement(i), bDefault);
  311. }
  312. }
  313. //////////////////////////////////////////////////////////////////////////
  314. void UiCompoundSplineTrack::SetValue(float time, const AZ::Color& value, bool bDefault)
  315. {
  316. for (int i = 0; i < m_nDimensions; i++)
  317. {
  318. m_subTracks[i]->SetValue(time, value.GetElement(i), bDefault);
  319. }
  320. }
  321. //////////////////////////////////////////////////////////////////////////
  322. void UiCompoundSplineTrack::OffsetKeyPosition(const AZ::Vector3& offset)
  323. {
  324. AZ_Assert(m_nDimensions == 3, "expect 3 subtracks found %d", m_nDimensions);
  325. if (m_nDimensions == 3)
  326. {
  327. for (int i = 0; i < 3; i++)
  328. {
  329. IUiAnimTrack* pSubTrack = m_subTracks[i].get();
  330. // Iterate over all keys.
  331. for (int k = 0, num = pSubTrack->GetNumKeys(); k < num; k++)
  332. {
  333. // Offset each key.
  334. float time = pSubTrack->GetKeyTime(k);
  335. float value = 0;
  336. pSubTrack->GetValue(time, value);
  337. value = value + offset.GetElement(i);
  338. pSubTrack->SetValue(time, value);
  339. }
  340. }
  341. }
  342. }
  343. //////////////////////////////////////////////////////////////////////////
  344. IUiAnimTrack* UiCompoundSplineTrack::GetSubTrack(int nIndex) const
  345. {
  346. assert(nIndex >= 0 && nIndex < m_nDimensions);
  347. return m_subTracks[nIndex].get();
  348. }
  349. //////////////////////////////////////////////////////////////////////////
  350. AZStd::string UiCompoundSplineTrack::GetSubTrackName(int nIndex) const
  351. {
  352. assert(nIndex >= 0 && nIndex < m_nDimensions);
  353. return m_subTrackNames[nIndex];
  354. }
  355. //////////////////////////////////////////////////////////////////////////
  356. void UiCompoundSplineTrack::SetSubTrackName(int nIndex, const char* name)
  357. {
  358. assert(nIndex >= 0 && nIndex < m_nDimensions);
  359. assert(name);
  360. m_subTrackNames[nIndex] = name;
  361. }
  362. //////////////////////////////////////////////////////////////////////////
  363. int UiCompoundSplineTrack::GetNumKeys() const
  364. {
  365. int nKeys = 0;
  366. for (int i = 0; i < m_nDimensions; i++)
  367. {
  368. nKeys += m_subTracks[i]->GetNumKeys();
  369. }
  370. return nKeys;
  371. }
  372. //////////////////////////////////////////////////////////////////////////
  373. bool UiCompoundSplineTrack::HasKeys() const
  374. {
  375. for (int i = 0; i < m_nDimensions; i++)
  376. {
  377. if (m_subTracks[i]->GetNumKeys())
  378. {
  379. return true;
  380. }
  381. }
  382. return false;
  383. }
  384. float UiCompoundSplineTrack::PreferShortestRotPath(float degree, float degree0) const
  385. {
  386. // Assumes the degree is in (-PI, PI).
  387. assert(-181.0f < degree && degree < 181.0f);
  388. float degree00 = degree0;
  389. degree0 = fmod_tpl(degree0, 360.0f);
  390. float n = (degree00 - degree0) / 360.0f;
  391. float degreeAlt;
  392. if (degree >= 0)
  393. {
  394. degreeAlt = degree - 360.0f;
  395. }
  396. else
  397. {
  398. degreeAlt = degree + 360.0f;
  399. }
  400. if (fabs(degreeAlt - degree0) < fabs(degree - degree0))
  401. {
  402. return degreeAlt + n * 360.0f;
  403. }
  404. else
  405. {
  406. return degree + n * 360.0f;
  407. }
  408. }
  409. int UiCompoundSplineTrack::GetSubTrackIndex(int& key) const
  410. {
  411. assert(key >= 0 && key < GetNumKeys());
  412. int count = 0;
  413. for (int i = 0; i < m_nDimensions; i++)
  414. {
  415. if (key < count + m_subTracks[i]->GetNumKeys())
  416. {
  417. key = key - count;
  418. return i;
  419. }
  420. count += m_subTracks[i]->GetNumKeys();
  421. }
  422. return -1;
  423. }
  424. void UiCompoundSplineTrack::RemoveKey(int num)
  425. {
  426. assert(num >= 0 && num < GetNumKeys());
  427. int i = GetSubTrackIndex(num);
  428. assert(i >= 0);
  429. if (i < 0)
  430. {
  431. return;
  432. }
  433. m_subTracks[i]->RemoveKey(num);
  434. }
  435. void UiCompoundSplineTrack::GetKeyInfo(int key, const char*& description, float& duration)
  436. {
  437. static char str[64];
  438. duration = 0;
  439. description = str;
  440. const char* subDesc = NULL;
  441. float time = GetKeyTime(key);
  442. int m = 0;
  443. /// Using the time obtained, combine descriptions from keys of the same time
  444. /// in sub-tracks if any into one compound description.
  445. str[0] = 0;
  446. // A head case
  447. for (m = 0; m < m_subTracks[0]->GetNumKeys(); ++m)
  448. {
  449. if (m_subTracks[0]->GetKeyTime(m) == time)
  450. {
  451. float dummy;
  452. m_subTracks[0]->GetKeyInfo(m, subDesc, dummy);
  453. azstrcat(str, AZ_ARRAY_SIZE(str), subDesc);
  454. break;
  455. }
  456. }
  457. if (m == m_subTracks[0]->GetNumKeys())
  458. {
  459. azstrcat(str, AZ_ARRAY_SIZE(str), m_subTrackNames[0].c_str());
  460. }
  461. // Tail cases
  462. for (int i = 1; i < GetSubTrackCount(); ++i)
  463. {
  464. azstrcat(str, AZ_ARRAY_SIZE(str), ",");
  465. for (m = 0; m < m_subTracks[i]->GetNumKeys(); ++m)
  466. {
  467. if (m_subTracks[i]->GetKeyTime(m) == time)
  468. {
  469. float dummy;
  470. m_subTracks[i]->GetKeyInfo(m, subDesc, dummy);
  471. azstrcat(str, AZ_ARRAY_SIZE(str), subDesc);
  472. break;
  473. }
  474. }
  475. if (m == m_subTracks[i]->GetNumKeys())
  476. {
  477. azstrcat(str, AZ_ARRAY_SIZE(str), m_subTrackNames[i].c_str());
  478. }
  479. }
  480. }
  481. float UiCompoundSplineTrack::GetKeyTime(int index) const
  482. {
  483. assert(index >= 0 && index < GetNumKeys());
  484. int i = GetSubTrackIndex(index);
  485. assert(i >= 0);
  486. if (i < 0)
  487. {
  488. return 0;
  489. }
  490. return m_subTracks[i]->GetKeyTime(index);
  491. }
  492. void UiCompoundSplineTrack::SetKeyTime(int index, float time)
  493. {
  494. assert(index >= 0 && index < GetNumKeys());
  495. int i = GetSubTrackIndex(index);
  496. assert(i >= 0);
  497. if (i < 0)
  498. {
  499. return;
  500. }
  501. m_subTracks[i]->SetKeyTime(index, time);
  502. }
  503. bool UiCompoundSplineTrack::IsKeySelected(int key) const
  504. {
  505. assert(key >= 0 && key < GetNumKeys());
  506. int i = GetSubTrackIndex(key);
  507. assert(i >= 0);
  508. if (i < 0)
  509. {
  510. return false;
  511. }
  512. return m_subTracks[i]->IsKeySelected(key);
  513. }
  514. void UiCompoundSplineTrack::SelectKey(int key, bool select)
  515. {
  516. assert(key >= 0 && key < GetNumKeys());
  517. int i = GetSubTrackIndex(key);
  518. assert(i >= 0);
  519. if (i < 0)
  520. {
  521. return;
  522. }
  523. float keyTime = m_subTracks[i]->GetKeyTime(key);
  524. // In the case of compound tracks, animators want to
  525. // select all keys of the same time in the sub-tracks together.
  526. const float timeEpsilon = 0.001f;
  527. for (int k = 0; k < m_nDimensions; ++k)
  528. {
  529. for (int m = 0; m < m_subTracks[k]->GetNumKeys(); ++m)
  530. {
  531. if (fabs(m_subTracks[k]->GetKeyTime(m) - keyTime) < timeEpsilon)
  532. {
  533. m_subTracks[k]->SelectKey(m, select);
  534. break;
  535. }
  536. }
  537. }
  538. }
  539. int UiCompoundSplineTrack::NextKeyByTime(int key) const
  540. {
  541. assert(key >= 0 && key < GetNumKeys());
  542. float time = GetKeyTime(key);
  543. int count = 0, result = -1;
  544. float timeNext = FLT_MAX;
  545. for (int i = 0; i < GetSubTrackCount(); ++i)
  546. {
  547. for (int k = 0; k < m_subTracks[i]->GetNumKeys(); ++k)
  548. {
  549. float t = m_subTracks[i]->GetKeyTime(k);
  550. if (t > time)
  551. {
  552. if (t < timeNext)
  553. {
  554. timeNext = t;
  555. result = count + k;
  556. }
  557. break;
  558. }
  559. }
  560. count += m_subTracks[i]->GetNumKeys();
  561. }
  562. return result;
  563. }
  564. //////////////////////////////////////////////////////////////////////////
  565. void UiCompoundSplineTrack::Reflect(AZ::SerializeContext* serializeContext)
  566. {
  567. serializeContext->Class<UiCompoundSplineTrack>()
  568. ->Version(1)
  569. ->Field("Flags", &UiCompoundSplineTrack::m_flags)
  570. ->Field("ParamType", &UiCompoundSplineTrack::m_nParamType)
  571. ->Field("ParamData", &UiCompoundSplineTrack::m_componentParamData)
  572. ->Field("NumSubTracks", &UiCompoundSplineTrack::m_nDimensions)
  573. ->Field("SubTracks", &UiCompoundSplineTrack::m_subTracks)
  574. ->Field("SubTrackNames", &UiCompoundSplineTrack::m_subTrackNames);
  575. }