AnimKey.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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.
  135. AZ::EntityId cameraAzEntityId; // will be Invalid for legacy Cameras
  136. float fDuration;
  137. float fBlendTime;
  138. ISelectKey()
  139. {
  140. fDuration = 0;
  141. fBlendTime = 0;
  142. }
  143. };
  144. /** ISequenceKey used in sequence track.
  145. */
  146. struct ISequenceKey
  147. : public IKey
  148. {
  149. AZStd::string szSelection; //!@deprecated : use sequenceEntityId to identify sequences
  150. AZ::EntityId sequenceEntityId;
  151. float fDuration;
  152. float fStartTime;
  153. float fEndTime;
  154. bool bOverrideTimes;
  155. bool bDoNotStop;
  156. ISequenceKey()
  157. {
  158. fDuration = 0;
  159. fStartTime = 0;
  160. fEndTime = 0;
  161. bOverrideTimes = false;
  162. bDoNotStop = false; // default crysis behaviour
  163. }
  164. };
  165. /** ISoundKey used in sound track.
  166. */
  167. struct ISoundKey
  168. : public IKey
  169. {
  170. ISoundKey()
  171. : fDuration(0.0f)
  172. {
  173. customColor.x = Col_TrackviewDefault.r;
  174. customColor.y = Col_TrackviewDefault.g;
  175. customColor.z = Col_TrackviewDefault.b;
  176. }
  177. AZStd::string sStartTrigger;
  178. AZStd::string sStopTrigger;
  179. float fDuration;
  180. Vec3 customColor;
  181. };
  182. /** ITimeRangeKey used in time ranges animation track.
  183. */
  184. #define ANIMKEY_TIME_RANGE_END_TIME_UNSET .0f
  185. struct ITimeRangeKey
  186. : public IKey
  187. {
  188. float m_duration; //!< Duration in seconds of this animation.
  189. float m_startTime; //!< Start time of this animation (Offset from beginning of animation).
  190. float m_endTime; //!< End time of this animation (can be smaller than the duration).
  191. float m_speed; //!< Speed multiplier for this key.
  192. bool m_bLoop; //!< True if time is looping
  193. ITimeRangeKey()
  194. {
  195. m_duration = 0.0f;
  196. m_endTime = ANIMKEY_TIME_RANGE_END_TIME_UNSET;
  197. m_startTime = 0.0f;
  198. m_speed = 1.0f;
  199. m_bLoop = false;
  200. }
  201. float GetValidEndTime() const
  202. {
  203. float endTime = m_endTime;
  204. if (endTime == ANIMKEY_TIME_RANGE_END_TIME_UNSET || (!m_bLoop && endTime > m_duration))
  205. {
  206. endTime = m_duration;
  207. }
  208. return endTime;
  209. }
  210. float GetValidSpeed() const
  211. {
  212. float speed = m_speed;
  213. if (speed <= 0.0f)
  214. {
  215. speed = 1.0f;
  216. }
  217. return speed;
  218. }
  219. float GetActualDuration() const
  220. {
  221. return (GetValidEndTime() - m_startTime) / GetValidSpeed();
  222. }
  223. // Return true if the input time falls in range of the start/end time for this key.
  224. bool IsInRange(float sequenceTime) const
  225. {
  226. return sequenceTime >= time && sequenceTime <= (time + GetActualDuration());
  227. }
  228. };
  229. /** ICharacterKey used in Character animation track.
  230. */
  231. struct ICharacterKey
  232. : public ITimeRangeKey
  233. {
  234. AZStd::string m_animation; //!< Name of character animation.
  235. bool m_bBlendGap; //!< True if gap to next animation should be blended
  236. bool m_bInPlace; // Play animation in place (Do not move root).
  237. ICharacterKey()
  238. : ITimeRangeKey()
  239. {
  240. m_bLoop = false;
  241. m_bBlendGap = false;
  242. m_bInPlace = false;
  243. }
  244. };
  245. /** IExprKey used in expression animation track.
  246. */
  247. struct IExprKey
  248. : public IKey
  249. {
  250. IExprKey()
  251. {
  252. pszName[0] = 0;
  253. fAmp = 1.0f;
  254. fBlendIn = 0.5f;
  255. fHold = 1.0f;
  256. fBlendOut = 0.5f;
  257. }
  258. char pszName[128]; //!< Name of morph-target
  259. float fAmp;
  260. float fBlendIn;
  261. float fHold;
  262. float fBlendOut;
  263. };
  264. /** IConsoleKey used in Console track, triggers console commands and variables.
  265. */
  266. struct IConsoleKey
  267. : public IKey
  268. {
  269. AZStd::string command;
  270. };
  271. struct ILookAtKey
  272. : public IKey
  273. {
  274. AZStd::string szSelection; //!< Node name.
  275. float fDuration;
  276. AZStd::string lookPose;
  277. float smoothTime;
  278. ILookAtKey()
  279. {
  280. fDuration = 0;
  281. smoothTime = 0.2f;
  282. }
  283. };
  284. //! Discrete (non-interpolated) float key.
  285. struct IDiscreteFloatKey
  286. : public IKey
  287. {
  288. float m_fValue;
  289. void SetValue(float fValue)
  290. {
  291. m_fValue = fValue;
  292. }
  293. IDiscreteFloatKey()
  294. {
  295. m_fValue = -1.0f;
  296. }
  297. };
  298. //! A key for the capture track.
  299. struct ICaptureKey
  300. : public IKey
  301. {
  302. friend class AnimSerializer;
  303. AZStd::string folder;
  304. AZStd::string prefix;
  305. float duration;
  306. float timeStep;
  307. bool once;
  308. ICaptureKey()
  309. : IKey()
  310. , duration(0.0f)
  311. , timeStep(0.033f)
  312. , once(false)
  313. {
  314. ICVar* pCaptureFolderCVar = gEnv->pConsole->GetCVar("capture_folder");
  315. if (pCaptureFolderCVar != NULL && pCaptureFolderCVar->GetString())
  316. {
  317. folder = pCaptureFolderCVar->GetString();
  318. }
  319. ICVar* pCaptureFilePrefixCVar = gEnv->pConsole->GetCVar("capture_file_prefix");
  320. if (pCaptureFilePrefixCVar != NULL && pCaptureFilePrefixCVar->GetString())
  321. {
  322. prefix = pCaptureFilePrefixCVar->GetString();
  323. }
  324. }
  325. ICaptureKey(const ICaptureKey& other)
  326. : IKey(other)
  327. , folder(other.folder)
  328. , prefix(other.prefix)
  329. , duration(other.duration)
  330. , timeStep(other.timeStep)
  331. , once(other.once)
  332. {
  333. }
  334. };
  335. //! Boolean key.
  336. struct IBoolKey
  337. : public IKey
  338. {
  339. IBoolKey() {};
  340. };
  341. //! Comment Key.
  342. struct ICommentKey
  343. : public IKey
  344. {
  345. enum ETextAlign : int
  346. {
  347. eTA_Left = 0,
  348. eTA_Center = BIT(1),
  349. eTA_Right = BIT(2)
  350. };
  351. //-----------------------------------------------------------------------------
  352. //!
  353. ICommentKey()
  354. : m_duration(1.f)
  355. , m_size(1.f)
  356. , m_align(eTA_Left)
  357. , m_strFont("default")
  358. , m_color(1.f, 1.f, 1.f, 1.f)
  359. {
  360. }
  361. //-----------------------------------------------------------------------------
  362. //!
  363. ICommentKey(const ICommentKey& other)
  364. : IKey(other)
  365. , m_strComment(other.m_strComment)
  366. , m_strFont(other.m_strFont)
  367. {
  368. m_duration = other.m_duration;
  369. m_color = other.m_color;
  370. m_size = other.m_size;
  371. m_align = other.m_align;
  372. }
  373. AZStd::string m_strComment;
  374. float m_duration;
  375. AZStd::string m_strFont;
  376. AZ::Color m_color;
  377. float m_size;
  378. ETextAlign m_align;
  379. };
  380. //-----------------------------------------------------------------------------
  381. //!
  382. struct IScreenFaderKey
  383. : public IKey
  384. {
  385. //-----------------------------------------------------------------------------
  386. //!
  387. enum EFadeType : int
  388. {
  389. eFT_FadeIn = 0, eFT_FadeOut = 1
  390. };
  391. enum EFadeChangeType : int
  392. {
  393. eFCT_Linear = 0, eFCT_Square = 1, eFCT_CubicSquare = 2, eFCT_SquareRoot = 3, eFCT_Sin = 4
  394. };
  395. //-----------------------------------------------------------------------------
  396. //!
  397. IScreenFaderKey()
  398. : IKey()
  399. , m_fadeTime(2.f)
  400. , m_bUseCurColor(true)
  401. , m_fadeType(eFT_FadeOut)
  402. , m_fadeChangeType(eFCT_Linear)
  403. {
  404. m_fadeColor = AZ::Color(.0f, .0f, .0f, 1.0f);
  405. }
  406. //-----------------------------------------------------------------------------
  407. //!
  408. IScreenFaderKey(const IScreenFaderKey& other)
  409. : IKey(other)
  410. , m_fadeTime(other.m_fadeTime)
  411. , m_bUseCurColor(other.m_bUseCurColor)
  412. , m_fadeType(other.m_fadeType)
  413. , m_fadeChangeType(other.m_fadeChangeType)
  414. {
  415. m_fadeColor = other.m_fadeColor;
  416. m_strTexture = other.m_strTexture;
  417. }
  418. //-----------------------------------------------------------------------------
  419. //!
  420. float m_fadeTime;
  421. AZ::Color m_fadeColor;
  422. AZStd::string m_strTexture;
  423. bool m_bUseCurColor;
  424. EFadeType m_fadeType;
  425. EFadeChangeType m_fadeChangeType;
  426. };
  427. namespace AZ
  428. {
  429. AZ_TYPE_INFO_SPECIALIZE(IKey, "{680BD51E-C106-4BBF-9A6F-CD551E00519F}");
  430. AZ_TYPE_INFO_SPECIALIZE(IBoolKey, "{DBF8044F-6E64-403D-807D-F3152F640703}");
  431. AZ_TYPE_INFO_SPECIALIZE(ICaptureKey, "{93AA8D63-6B1E-4D33-8CC3-C82147BB95CB}");
  432. AZ_TYPE_INFO_SPECIALIZE(ICharacterKey, "{6D1FB9E2-128C-4B33-84FF-4F696C1F7D53}");
  433. AZ_TYPE_INFO_SPECIALIZE(ICommentKey, "{99C2234E-A4DD-45D1-90C3-D5AFC54FA47F}");
  434. AZ_TYPE_INFO_SPECIALIZE(IConsoleKey, "{8C0DCB9B-297D-4AF4-A0D1-F5160E6900E8}");
  435. AZ_TYPE_INFO_SPECIALIZE(IDiscreteFloatKey, "{469A2B90-E019-4147-A53F-2EB42E179596}");
  436. AZ_TYPE_INFO_SPECIALIZE(IEventKey, "{F09533AA-9780-494D-9E5C-8CB98266AC5E}");
  437. AZ_TYPE_INFO_SPECIALIZE(ILookAtKey, "{6F4CED0E-D83A-40E2-B7BF-038D82BC0374}");
  438. AZ_TYPE_INFO_SPECIALIZE(IScreenFaderKey, "{FA15E27D-603F-4829-925A-E36D75C93964}");
  439. AZ_TYPE_INFO_SPECIALIZE(ISelectKey, "{FCEADCF5-042E-473B-845F-0778F087B6DC}");
  440. AZ_TYPE_INFO_SPECIALIZE(ISequenceKey, "{B55294AD-F14E-43AC-B6B5-AC27B377FE00}");
  441. AZ_TYPE_INFO_SPECIALIZE(ISoundKey, "{452E50CF-B7D0-42D5-A86A-B295682674BB}");
  442. AZ_TYPE_INFO_SPECIALIZE(ITimeRangeKey, "{17807C95-C7A1-481B-AD94-C54D83928D0B}");
  443. }
  444. #endif // CRYINCLUDE_CRYCOMMON_ANIMKEY_H