AnimKey.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. #ifndef CRYINCLUDE_CRYCOMMON_ANIMKEY_H
  9. #define CRYINCLUDE_CRYCOMMON_ANIMKEY_H
  10. #pragma once
  11. #include <IConsole.h> // <> required for Interfuscator
  12. #include <ISystem.h>
  13. #include <Cry_Color.h>
  14. #include <AzCore/Math/Color.h>
  15. #include <AzCore/Component/EntityId.h>
  16. enum EAnimKeyFlags
  17. {
  18. AKEY_SELECTED = 0x01, //! This key is selected in track view.
  19. AKEY_SORT_MARKER = 0x02 //! Internal use to locate a key after a sort.
  20. };
  21. //! Interface to animation key.
  22. //! Not real interface though...
  23. //! No virtuals for optimization reason.
  24. struct IKey
  25. {
  26. float time;
  27. int flags;
  28. // compare keys.
  29. bool operator<(const IKey& key) const { return time < key.time; }
  30. bool operator==(const IKey& key) const { return time == key.time; }
  31. bool operator>(const IKey& key) const { return time > key.time; }
  32. bool operator<=(const IKey& key) const { return time <= key.time; }
  33. bool operator>=(const IKey& key) const { return time >= key.time; }
  34. bool operator!=(const IKey& key) const { return time != key.time; }
  35. IKey()
  36. : time(0)
  37. , flags(0) {};
  38. virtual ~IKey() = default;
  39. };
  40. /** I2DBezierKey used in float tracks.
  41. Its x component actually represents kinda time-warping curve.
  42. */
  43. struct I2DBezierKey
  44. : public IKey
  45. {
  46. Vec2 value;
  47. };
  48. /** ITcbKey used in all TCB tracks.
  49. */
  50. struct ITcbKey
  51. : public IKey
  52. {
  53. // Values.
  54. float fval[4];
  55. // Key controls.
  56. float tens; //!< Key tension value.
  57. float cont; //!< Key continuity value.
  58. float bias; //!< Key bias value.
  59. float easeto; //!< Key ease to value.
  60. float easefrom; //!< Key ease from value.
  61. //! Protect from direct instantiation of this class.
  62. //! Only derived classes can be created,
  63. ITcbKey()
  64. {
  65. fval[0] = 0;
  66. fval[1] = 0;
  67. fval[2] = 0;
  68. fval[3] = 0;
  69. tens = 0, cont = 0, bias = 0, easeto = 0, easefrom = 0;
  70. };
  71. void SetFloat(float val) { fval[0] = val; };
  72. void SetVec3(const Vec3& val)
  73. {
  74. fval[0] = val.x;
  75. fval[1] = val.y;
  76. fval[2] = val.z;
  77. };
  78. void SetQuat(const Quat& val)
  79. {
  80. fval[0] = val.v.x;
  81. fval[1] = val.v.y;
  82. fval[2] = val.v.z;
  83. fval[3] = val.w;
  84. };
  85. ILINE void SetValue(float val) { SetFloat(val); }
  86. ILINE void SetValue(const Vec3& val) { SetVec3(val); }
  87. ILINE void SetValue(const Quat& val) { SetQuat(val); }
  88. float GetFloat() const { return *((float*)fval); };
  89. Vec3 GetVec3() const
  90. {
  91. Vec3 vec;
  92. vec.x = fval[0];
  93. vec.y = fval[1];
  94. vec.z = fval[2];
  95. return vec;
  96. };
  97. Quat GetQuat() const
  98. {
  99. Quat quat;
  100. quat.v.x = fval[0];
  101. quat.v.y = fval[1];
  102. quat.v.z = fval[2];
  103. quat.w = fval[3];
  104. return quat;
  105. };
  106. ILINE void GetValue(float& val) { val = GetFloat(); };
  107. ILINE void GetValue(Vec3& val) { val = GetVec3(); };
  108. ILINE void GetValue(Quat& val) { val = GetQuat(); };
  109. };
  110. struct IEventKey
  111. : public IKey
  112. {
  113. AZStd::string event;
  114. AZStd::string eventValue;
  115. AZStd::string animation;
  116. AZStd::string target;
  117. union
  118. {
  119. float value;
  120. float duration;
  121. };
  122. bool bNoTriggerInScrubbing;
  123. IEventKey()
  124. {
  125. duration = 0;
  126. bNoTriggerInScrubbing = false;
  127. }
  128. };
  129. /** ISelectKey used in Camera selection track or Scene node.
  130. */
  131. struct ISelectKey
  132. : public IKey
  133. {
  134. AZStd::string szSelection; //!< Node name (an existing camera entity name), or empty.
  135. AZ::EntityId cameraAzEntityId; //!< Valid EntityId for an existing camera, or invalid.
  136. float fDuration; //!< Duration of camera activity in CSelectTrack, not user-defined, calculated for compatibility and UI sliders ranges.
  137. float fBlendTime; //!< Time in seconds to a next key where camera parameters are interpolated.
  138. //!< Initial properties of an existing camera, used to interpolate between camera keys, stored when CSelectTrack is activated.
  139. float m_FoV; //!< Initial FoV of an existing camera; positive value indicates that the key was initialized.
  140. float m_nearZ; //!< Initial near clipping distance of an existing camera, if initialized.
  141. AZ::Vector3 m_position; //!< Initial world position of an existing camera, if initialized.
  142. AZ::Quaternion m_rotation; //!< Initial world rotation of an existing camera, if initialized.
  143. ISelectKey()
  144. {
  145. Reset();
  146. }
  147. //!< @returns True if a valid camera controller EntityId and Name are set, otherwise returns false.
  148. bool IsValid() const { return cameraAzEntityId.IsValid() && !szSelection.empty(); }
  149. //!< @returns True if a valid camera controller EntityId is set and camera properties are stored, otherwise returns false.
  150. bool IsInitialized() const { return IsValid() && m_FoV > 0.0f; }
  151. //!< @returns True if a valid camera controller EntityId is set, otherwise returns false and invalidates camera properties.
  152. bool CheckValid()
  153. {
  154. if (IsValid())
  155. {
  156. return true;
  157. }
  158. Reset();
  159. return false;
  160. }
  161. //!< Invalidates all key camera data.
  162. void Reset()
  163. {
  164. szSelection.clear();
  165. cameraAzEntityId = AZ::EntityId();
  166. fDuration = 0;
  167. fBlendTime = 0;
  168. ResetCameraProperies();
  169. }
  170. //!< Invalidates key camera properties.
  171. void ResetCameraProperies()
  172. {
  173. m_FoV = -1.0f;
  174. m_nearZ = 0;
  175. m_position = AZ::Vector3::CreateZero();
  176. m_rotation = AZ::Quaternion::CreateIdentity();
  177. }
  178. //!< Copies stored key camera properties.
  179. void CopyCameraProperies(const ISelectKey& rhs)
  180. {
  181. AZ_Assert(cameraAzEntityId == rhs.cameraAzEntityId, "Invalid camera data.")
  182. m_FoV = rhs.m_FoV;
  183. m_nearZ = rhs.m_nearZ;
  184. m_position = rhs.m_position;
  185. m_rotation = rhs.m_rotation;
  186. }
  187. };
  188. /** ISequenceKey used in sequence track.
  189. */
  190. struct ISequenceKey
  191. : public IKey
  192. {
  193. AZStd::string szSelection; //!@deprecated : use sequenceEntityId to identify sequences
  194. AZ::EntityId sequenceEntityId;
  195. float fDuration;
  196. float fStartTime;
  197. float fEndTime;
  198. bool bOverrideTimes;
  199. bool bDoNotStop;
  200. ISequenceKey()
  201. {
  202. fDuration = 0;
  203. fStartTime = 0;
  204. fEndTime = 0;
  205. bOverrideTimes = false;
  206. bDoNotStop = false;
  207. }
  208. };
  209. /** ISoundKey used in sound track.
  210. */
  211. struct ISoundKey
  212. : public IKey
  213. {
  214. ISoundKey()
  215. : fDuration(0.0f)
  216. {
  217. customColor.Set(Col_TrackviewDefault.r, Col_TrackviewDefault.g, Col_TrackviewDefault.b);
  218. }
  219. AZStd::string sStartTrigger;
  220. AZStd::string sStopTrigger;
  221. float fDuration;
  222. Vec3 customColor;
  223. };
  224. /** ITimeRangeKey used in time ranges animation track.
  225. */
  226. #define ANIMKEY_TIME_RANGE_END_TIME_UNSET .0f
  227. struct ITimeRangeKey
  228. : public IKey
  229. {
  230. float m_duration; //!< Duration in seconds of this animation.
  231. float m_startTime; //!< Start time of this animation (Offset from beginning of animation).
  232. float m_endTime; //!< End time of this animation (can be smaller than the duration).
  233. float m_speed; //!< Speed multiplier for this key.
  234. bool m_bLoop; //!< True if time is looping
  235. ITimeRangeKey()
  236. {
  237. m_duration = 0.0f;
  238. m_endTime = ANIMKEY_TIME_RANGE_END_TIME_UNSET;
  239. m_startTime = 0.0f;
  240. m_speed = 1.0f;
  241. m_bLoop = false;
  242. }
  243. float GetValidEndTime() const
  244. {
  245. float endTime = m_endTime;
  246. if (endTime == ANIMKEY_TIME_RANGE_END_TIME_UNSET || (!m_bLoop && endTime > m_duration))
  247. {
  248. endTime = m_duration;
  249. }
  250. return endTime;
  251. }
  252. float GetValidSpeed() const
  253. {
  254. float speed = m_speed;
  255. if (speed <= 0.0f)
  256. {
  257. speed = 1.0f;
  258. }
  259. return speed;
  260. }
  261. float GetActualDuration() const
  262. {
  263. return (GetValidEndTime() - m_startTime) / GetValidSpeed();
  264. }
  265. // Return true if the input time falls in range of the start/end time for this key.
  266. bool IsInRange(float sequenceTime) const
  267. {
  268. return sequenceTime >= time && sequenceTime <= (time + GetActualDuration());
  269. }
  270. };
  271. /** ICharacterKey used in Character animation track.
  272. */
  273. struct ICharacterKey
  274. : public ITimeRangeKey
  275. {
  276. AZStd::string m_animation; //!< Name of character animation.
  277. bool m_bBlendGap; //!< True if gap to next animation should be blended
  278. bool m_bInPlace; // Play animation in place (Do not move root).
  279. ICharacterKey()
  280. : ITimeRangeKey()
  281. {
  282. m_bLoop = false;
  283. m_bBlendGap = false;
  284. m_bInPlace = false;
  285. }
  286. };
  287. /** IExprKey used in expression animation track.
  288. */
  289. struct IExprKey
  290. : public IKey
  291. {
  292. IExprKey()
  293. {
  294. pszName[0] = 0;
  295. fAmp = 1.0f;
  296. fBlendIn = 0.5f;
  297. fHold = 1.0f;
  298. fBlendOut = 0.5f;
  299. }
  300. char pszName[128]; //!< Name of morph-target
  301. float fAmp;
  302. float fBlendIn;
  303. float fHold;
  304. float fBlendOut;
  305. };
  306. /** IConsoleKey used in Console track, triggers console commands and variables.
  307. */
  308. struct IConsoleKey
  309. : public IKey
  310. {
  311. AZStd::string command;
  312. };
  313. struct ILookAtKey
  314. : public IKey
  315. {
  316. AZStd::string szSelection; //!< Node name.
  317. float fDuration;
  318. AZStd::string lookPose;
  319. float smoothTime;
  320. ILookAtKey()
  321. {
  322. fDuration = 0;
  323. smoothTime = 0.2f;
  324. }
  325. };
  326. //! Discrete (non-interpolated) float key.
  327. struct IDiscreteFloatKey
  328. : public IKey
  329. {
  330. float m_fValue;
  331. void SetValue(float fValue)
  332. {
  333. m_fValue = fValue;
  334. }
  335. IDiscreteFloatKey()
  336. {
  337. m_fValue = -1.0f;
  338. }
  339. };
  340. //! A key for the capture track.
  341. struct ICaptureKey
  342. : public IKey
  343. {
  344. friend class AnimSerializer;
  345. AZStd::string folder;
  346. AZStd::string prefix;
  347. float duration;
  348. float timeStep;
  349. bool once;
  350. ICaptureKey()
  351. : IKey()
  352. , duration(0.0f)
  353. , timeStep(0.033f)
  354. , once(false)
  355. {
  356. ICVar* pCaptureFolderCVar = gEnv->pConsole->GetCVar("capture_folder");
  357. if (pCaptureFolderCVar != NULL && pCaptureFolderCVar->GetString())
  358. {
  359. folder = pCaptureFolderCVar->GetString();
  360. }
  361. ICVar* pCaptureFilePrefixCVar = gEnv->pConsole->GetCVar("capture_file_prefix");
  362. if (pCaptureFilePrefixCVar != NULL && pCaptureFilePrefixCVar->GetString())
  363. {
  364. prefix = pCaptureFilePrefixCVar->GetString();
  365. }
  366. }
  367. ICaptureKey(const ICaptureKey& other)
  368. : IKey(other)
  369. , folder(other.folder)
  370. , prefix(other.prefix)
  371. , duration(other.duration)
  372. , timeStep(other.timeStep)
  373. , once(other.once)
  374. {
  375. }
  376. };
  377. //! Boolean key.
  378. struct IBoolKey
  379. : public IKey
  380. {
  381. IBoolKey() {};
  382. };
  383. //! Comment Key.
  384. struct ICommentKey
  385. : public IKey
  386. {
  387. enum ETextAlign : int
  388. {
  389. eTA_Left = 0,
  390. eTA_Center = BIT(1),
  391. eTA_Right = BIT(2)
  392. };
  393. //-----------------------------------------------------------------------------
  394. //!
  395. ICommentKey()
  396. : m_duration(1.f)
  397. , m_size(1.f)
  398. , m_align(eTA_Left)
  399. , m_strFont("default")
  400. , m_color(1.f, 1.f, 1.f, 1.f)
  401. {
  402. }
  403. //-----------------------------------------------------------------------------
  404. //!
  405. ICommentKey(const ICommentKey& other)
  406. : IKey(other)
  407. , m_strComment(other.m_strComment)
  408. , m_strFont(other.m_strFont)
  409. {
  410. m_duration = other.m_duration;
  411. m_color = other.m_color;
  412. m_size = other.m_size;
  413. m_align = other.m_align;
  414. }
  415. AZStd::string m_strComment;
  416. float m_duration;
  417. AZStd::string m_strFont;
  418. AZ::Color m_color;
  419. float m_size;
  420. ETextAlign m_align;
  421. };
  422. //-----------------------------------------------------------------------------
  423. //!
  424. struct IScreenFaderKey
  425. : public IKey
  426. {
  427. //-----------------------------------------------------------------------------
  428. //!
  429. enum EFadeType : int
  430. {
  431. eFT_FadeIn = 0, eFT_FadeOut = 1
  432. };
  433. enum EFadeChangeType : int
  434. {
  435. eFCT_Linear = 0, eFCT_Square = 1, eFCT_CubicSquare = 2, eFCT_SquareRoot = 3, eFCT_Sin = 4
  436. };
  437. //-----------------------------------------------------------------------------
  438. //!
  439. IScreenFaderKey()
  440. : IKey()
  441. , m_fadeTime(2.f)
  442. , m_bUseCurColor(true)
  443. , m_fadeType(eFT_FadeOut)
  444. , m_fadeChangeType(eFCT_Linear)
  445. {
  446. m_fadeColor = AZ::Color(.0f, .0f, .0f, 1.0f);
  447. }
  448. //-----------------------------------------------------------------------------
  449. //!
  450. IScreenFaderKey(const IScreenFaderKey& other)
  451. : IKey(other)
  452. , m_fadeTime(other.m_fadeTime)
  453. , m_bUseCurColor(other.m_bUseCurColor)
  454. , m_fadeType(other.m_fadeType)
  455. , m_fadeChangeType(other.m_fadeChangeType)
  456. {
  457. m_fadeColor = other.m_fadeColor;
  458. m_strTexture = other.m_strTexture;
  459. }
  460. //-----------------------------------------------------------------------------
  461. //!
  462. float m_fadeTime;
  463. AZ::Color m_fadeColor;
  464. AZStd::string m_strTexture;
  465. bool m_bUseCurColor;
  466. EFadeType m_fadeType;
  467. EFadeChangeType m_fadeChangeType;
  468. };
  469. /** IStringKey used in string tracks.
  470. */
  471. struct IStringKey
  472. : public IKey
  473. {
  474. AZStd::string m_strValue;
  475. IStringKey() = default;
  476. IStringKey(const AZStd::string value)
  477. : m_strValue(value)
  478. {
  479. }
  480. };
  481. namespace AZ
  482. {
  483. AZ_TYPE_INFO_SPECIALIZE(IKey, "{680BD51E-C106-4BBF-9A6F-CD551E00519F}");
  484. AZ_TYPE_INFO_SPECIALIZE(IBoolKey, "{DBF8044F-6E64-403D-807D-F3152F640703}");
  485. AZ_TYPE_INFO_SPECIALIZE(ICaptureKey, "{93AA8D63-6B1E-4D33-8CC3-C82147BB95CB}");
  486. AZ_TYPE_INFO_SPECIALIZE(ICharacterKey, "{6D1FB9E2-128C-4B33-84FF-4F696C1F7D53}");
  487. AZ_TYPE_INFO_SPECIALIZE(ICommentKey, "{99C2234E-A4DD-45D1-90C3-D5AFC54FA47F}");
  488. AZ_TYPE_INFO_SPECIALIZE(IConsoleKey, "{8C0DCB9B-297D-4AF4-A0D1-F5160E6900E8}");
  489. AZ_TYPE_INFO_SPECIALIZE(IDiscreteFloatKey, "{469A2B90-E019-4147-A53F-2EB42E179596}");
  490. AZ_TYPE_INFO_SPECIALIZE(IEventKey, "{F09533AA-9780-494D-9E5C-8CB98266AC5E}");
  491. AZ_TYPE_INFO_SPECIALIZE(ILookAtKey, "{6F4CED0E-D83A-40E2-B7BF-038D82BC0374}");
  492. AZ_TYPE_INFO_SPECIALIZE(IScreenFaderKey, "{FA15E27D-603F-4829-925A-E36D75C93964}");
  493. AZ_TYPE_INFO_SPECIALIZE(ISelectKey, "{FCEADCF5-042E-473B-845F-0778F087B6DC}");
  494. AZ_TYPE_INFO_SPECIALIZE(ISequenceKey, "{B55294AD-F14E-43AC-B6B5-AC27B377FE00}");
  495. AZ_TYPE_INFO_SPECIALIZE(ISoundKey, "{452E50CF-B7D0-42D5-A86A-B295682674BB}");
  496. AZ_TYPE_INFO_SPECIALIZE(ITimeRangeKey, "{17807C95-C7A1-481B-AD94-C54D83928D0B}");
  497. AZ_TYPE_INFO_SPECIALIZE(IStringKey, "{A35D94C2-776B-4BA7-BBBC-1A1FD4402023}");
  498. }
  499. #endif // CRYINCLUDE_CRYCOMMON_ANIMKEY_H