3
0

CompoundSplineTrack.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 Vec3& offset)
  323. {
  324. if (m_nDimensions == 3)
  325. {
  326. for (int i = 0; i < 3; i++)
  327. {
  328. IUiAnimTrack* pSubTrack = m_subTracks[i].get();
  329. // Iterate over all keys.
  330. for (int k = 0, num = pSubTrack->GetNumKeys(); k < num; k++)
  331. {
  332. // Offset each key.
  333. float time = pSubTrack->GetKeyTime(k);
  334. float value = 0;
  335. pSubTrack->GetValue(time, value);
  336. value = value + offset[i];
  337. pSubTrack->SetValue(time, value);
  338. }
  339. }
  340. }
  341. else
  342. {
  343. assert(0);
  344. }
  345. }
  346. //////////////////////////////////////////////////////////////////////////
  347. IUiAnimTrack* UiCompoundSplineTrack::GetSubTrack(int nIndex) const
  348. {
  349. assert(nIndex >= 0 && nIndex < m_nDimensions);
  350. return m_subTracks[nIndex].get();
  351. }
  352. //////////////////////////////////////////////////////////////////////////
  353. AZStd::string UiCompoundSplineTrack::GetSubTrackName(int nIndex) const
  354. {
  355. assert(nIndex >= 0 && nIndex < m_nDimensions);
  356. return m_subTrackNames[nIndex];
  357. }
  358. //////////////////////////////////////////////////////////////////////////
  359. void UiCompoundSplineTrack::SetSubTrackName(int nIndex, const char* name)
  360. {
  361. assert(nIndex >= 0 && nIndex < m_nDimensions);
  362. assert(name);
  363. m_subTrackNames[nIndex] = name;
  364. }
  365. //////////////////////////////////////////////////////////////////////////
  366. int UiCompoundSplineTrack::GetNumKeys() const
  367. {
  368. int nKeys = 0;
  369. for (int i = 0; i < m_nDimensions; i++)
  370. {
  371. nKeys += m_subTracks[i]->GetNumKeys();
  372. }
  373. return nKeys;
  374. }
  375. //////////////////////////////////////////////////////////////////////////
  376. bool UiCompoundSplineTrack::HasKeys() const
  377. {
  378. for (int i = 0; i < m_nDimensions; i++)
  379. {
  380. if (m_subTracks[i]->GetNumKeys())
  381. {
  382. return true;
  383. }
  384. }
  385. return false;
  386. }
  387. float UiCompoundSplineTrack::PreferShortestRotPath(float degree, float degree0) const
  388. {
  389. // Assumes the degree is in (-PI, PI).
  390. assert(-181.0f < degree && degree < 181.0f);
  391. float degree00 = degree0;
  392. degree0 = fmod_tpl(degree0, 360.0f);
  393. float n = (degree00 - degree0) / 360.0f;
  394. float degreeAlt;
  395. if (degree >= 0)
  396. {
  397. degreeAlt = degree - 360.0f;
  398. }
  399. else
  400. {
  401. degreeAlt = degree + 360.0f;
  402. }
  403. if (fabs(degreeAlt - degree0) < fabs(degree - degree0))
  404. {
  405. return degreeAlt + n * 360.0f;
  406. }
  407. else
  408. {
  409. return degree + n * 360.0f;
  410. }
  411. }
  412. int UiCompoundSplineTrack::GetSubTrackIndex(int& key) const
  413. {
  414. assert(key >= 0 && key < GetNumKeys());
  415. int count = 0;
  416. for (int i = 0; i < m_nDimensions; i++)
  417. {
  418. if (key < count + m_subTracks[i]->GetNumKeys())
  419. {
  420. key = key - count;
  421. return i;
  422. }
  423. count += m_subTracks[i]->GetNumKeys();
  424. }
  425. return -1;
  426. }
  427. void UiCompoundSplineTrack::RemoveKey(int num)
  428. {
  429. assert(num >= 0 && num < GetNumKeys());
  430. int i = GetSubTrackIndex(num);
  431. assert(i >= 0);
  432. if (i < 0)
  433. {
  434. return;
  435. }
  436. m_subTracks[i]->RemoveKey(num);
  437. }
  438. void UiCompoundSplineTrack::GetKeyInfo(int key, const char*& description, float& duration)
  439. {
  440. static char str[64];
  441. duration = 0;
  442. description = str;
  443. const char* subDesc = NULL;
  444. float time = GetKeyTime(key);
  445. int m = 0;
  446. /// Using the time obtained, combine descriptions from keys of the same time
  447. /// in sub-tracks if any into one compound description.
  448. str[0] = 0;
  449. // A head case
  450. for (m = 0; m < m_subTracks[0]->GetNumKeys(); ++m)
  451. {
  452. if (m_subTracks[0]->GetKeyTime(m) == time)
  453. {
  454. float dummy;
  455. m_subTracks[0]->GetKeyInfo(m, subDesc, dummy);
  456. azstrcat(str, AZ_ARRAY_SIZE(str), subDesc);
  457. break;
  458. }
  459. }
  460. if (m == m_subTracks[0]->GetNumKeys())
  461. {
  462. azstrcat(str, AZ_ARRAY_SIZE(str), m_subTrackNames[0].c_str());
  463. }
  464. // Tail cases
  465. for (int i = 1; i < GetSubTrackCount(); ++i)
  466. {
  467. azstrcat(str, AZ_ARRAY_SIZE(str), ",");
  468. for (m = 0; m < m_subTracks[i]->GetNumKeys(); ++m)
  469. {
  470. if (m_subTracks[i]->GetKeyTime(m) == time)
  471. {
  472. float dummy;
  473. m_subTracks[i]->GetKeyInfo(m, subDesc, dummy);
  474. azstrcat(str, AZ_ARRAY_SIZE(str), subDesc);
  475. break;
  476. }
  477. }
  478. if (m == m_subTracks[i]->GetNumKeys())
  479. {
  480. azstrcat(str, AZ_ARRAY_SIZE(str), m_subTrackNames[i].c_str());
  481. }
  482. }
  483. }
  484. float UiCompoundSplineTrack::GetKeyTime(int index) const
  485. {
  486. assert(index >= 0 && index < GetNumKeys());
  487. int i = GetSubTrackIndex(index);
  488. assert(i >= 0);
  489. if (i < 0)
  490. {
  491. return 0;
  492. }
  493. return m_subTracks[i]->GetKeyTime(index);
  494. }
  495. void UiCompoundSplineTrack::SetKeyTime(int index, float time)
  496. {
  497. assert(index >= 0 && index < GetNumKeys());
  498. int i = GetSubTrackIndex(index);
  499. assert(i >= 0);
  500. if (i < 0)
  501. {
  502. return;
  503. }
  504. m_subTracks[i]->SetKeyTime(index, time);
  505. }
  506. bool UiCompoundSplineTrack::IsKeySelected(int key) const
  507. {
  508. assert(key >= 0 && key < GetNumKeys());
  509. int i = GetSubTrackIndex(key);
  510. assert(i >= 0);
  511. if (i < 0)
  512. {
  513. return false;
  514. }
  515. return m_subTracks[i]->IsKeySelected(key);
  516. }
  517. void UiCompoundSplineTrack::SelectKey(int key, bool select)
  518. {
  519. assert(key >= 0 && key < GetNumKeys());
  520. int i = GetSubTrackIndex(key);
  521. assert(i >= 0);
  522. if (i < 0)
  523. {
  524. return;
  525. }
  526. float keyTime = m_subTracks[i]->GetKeyTime(key);
  527. // In the case of compound tracks, animators want to
  528. // select all keys of the same time in the sub-tracks together.
  529. const float timeEpsilon = 0.001f;
  530. for (int k = 0; k < m_nDimensions; ++k)
  531. {
  532. for (int m = 0; m < m_subTracks[k]->GetNumKeys(); ++m)
  533. {
  534. if (fabs(m_subTracks[k]->GetKeyTime(m) - keyTime) < timeEpsilon)
  535. {
  536. m_subTracks[k]->SelectKey(m, select);
  537. break;
  538. }
  539. }
  540. }
  541. }
  542. int UiCompoundSplineTrack::NextKeyByTime(int key) const
  543. {
  544. assert(key >= 0 && key < GetNumKeys());
  545. float time = GetKeyTime(key);
  546. int count = 0, result = -1;
  547. float timeNext = FLT_MAX;
  548. for (int i = 0; i < GetSubTrackCount(); ++i)
  549. {
  550. for (int k = 0; k < m_subTracks[i]->GetNumKeys(); ++k)
  551. {
  552. float t = m_subTracks[i]->GetKeyTime(k);
  553. if (t > time)
  554. {
  555. if (t < timeNext)
  556. {
  557. timeNext = t;
  558. result = count + k;
  559. }
  560. break;
  561. }
  562. }
  563. count += m_subTracks[i]->GetNumKeys();
  564. }
  565. return result;
  566. }
  567. //////////////////////////////////////////////////////////////////////////
  568. void UiCompoundSplineTrack::Reflect(AZ::SerializeContext* serializeContext)
  569. {
  570. serializeContext->Class<UiCompoundSplineTrack>()
  571. ->Version(1)
  572. ->Field("Flags", &UiCompoundSplineTrack::m_flags)
  573. ->Field("ParamType", &UiCompoundSplineTrack::m_nParamType)
  574. ->Field("ParamData", &UiCompoundSplineTrack::m_componentParamData)
  575. ->Field("NumSubTracks", &UiCompoundSplineTrack::m_nDimensions)
  576. ->Field("SubTracks", &UiCompoundSplineTrack::m_subTracks)
  577. ->Field("SubTrackNames", &UiCompoundSplineTrack::m_subTrackNames);
  578. }