3
0

CharacterTrack.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <AzCore/Serialization/SerializeContext.h>
  9. #include "CharacterTrack.h"
  10. #include "Maestro/Types/AnimValueType.h"
  11. #define LOOP_TRANSITION_TIME 1.0f
  12. //////////////////////////////////////////////////////////////////////////
  13. /// @deprecated Serialization for Sequence data in Component Entity Sequences now occurs through AZ::SerializeContext and the Sequence Component
  14. bool CCharacterTrack::Serialize(XmlNodeRef& xmlNode, bool bLoading, bool bLoadEmptyTracks)
  15. {
  16. if (bLoading)
  17. {
  18. xmlNode->getAttr("AnimationLayer", m_iAnimationLayer);
  19. }
  20. else
  21. {
  22. xmlNode->setAttr("AnimationLayer", m_iAnimationLayer);
  23. }
  24. return TAnimTrack<ICharacterKey>::Serialize(xmlNode, bLoading, bLoadEmptyTracks);
  25. }
  26. void CCharacterTrack::SerializeKey(ICharacterKey& key, XmlNodeRef& keyNode, bool bLoading)
  27. {
  28. if (bLoading)
  29. {
  30. const char* str;
  31. str = keyNode->getAttr("anim");
  32. key.m_animation = str;
  33. key.m_duration = 0;
  34. key.m_endTime = 0;
  35. key.m_startTime = 0;
  36. key.m_bLoop = false;
  37. key.m_bBlendGap = false;
  38. key.m_bInPlace = false;
  39. key.m_speed = 1;
  40. keyNode->getAttr("length", key.m_duration);
  41. keyNode->getAttr("end", key.m_endTime);
  42. keyNode->getAttr("speed", key.m_speed);
  43. keyNode->getAttr("loop", key.m_bLoop);
  44. keyNode->getAttr("blendGap", key.m_bBlendGap);
  45. keyNode->getAttr("inplace", key.m_bInPlace);
  46. keyNode->getAttr("start", key.m_startTime);
  47. }
  48. else
  49. {
  50. if (!key.m_animation.empty())
  51. {
  52. XmlString temp = key.m_animation.c_str();
  53. keyNode->setAttr("anim", temp);
  54. }
  55. if (key.m_duration > 0)
  56. {
  57. keyNode->setAttr("length", key.m_duration);
  58. }
  59. if (key.m_endTime > 0)
  60. {
  61. keyNode->setAttr("end", key.m_endTime);
  62. }
  63. if (key.m_speed != 1)
  64. {
  65. keyNode->setAttr("speed", key.m_speed);
  66. }
  67. if (key.m_bLoop)
  68. {
  69. keyNode->setAttr("loop", key.m_bLoop);
  70. }
  71. if (key.m_bBlendGap)
  72. {
  73. keyNode->setAttr("blendGap", key.m_bBlendGap);
  74. }
  75. if (key.m_bInPlace)
  76. {
  77. keyNode->setAttr("inplace", key.m_bInPlace);
  78. }
  79. if (key.m_startTime != 0)
  80. {
  81. keyNode->setAttr("start", key.m_startTime);
  82. }
  83. }
  84. }
  85. void CCharacterTrack::GetKeyInfo(int key, const char*& description, float& duration)
  86. {
  87. assert(key >= 0 && key < (int)m_keys.size());
  88. CheckValid();
  89. description = 0;
  90. duration = 0;
  91. if (!m_keys[key].m_animation.empty())
  92. {
  93. description = m_keys[key].m_animation.c_str();
  94. if (m_keys[key].m_bLoop)
  95. {
  96. float lastTime = m_timeRange.end;
  97. if (key + 1 < (int)m_keys.size())
  98. {
  99. lastTime = m_keys[key + 1].time;
  100. }
  101. // duration is unlimited but cannot last past end of track or time of next key on track.
  102. duration = lastTime - m_keys[key].time;
  103. }
  104. else
  105. {
  106. if (m_keys[key].m_speed == 0)
  107. {
  108. m_keys[key].m_speed = 1.0f;
  109. }
  110. duration = m_keys[key].GetActualDuration();
  111. }
  112. }
  113. }
  114. float CCharacterTrack::GetKeyDuration(int key) const
  115. {
  116. assert(key >= 0 && key < (int)m_keys.size());
  117. const float EPSILON = 0.001f;
  118. if (m_keys[key].m_bLoop)
  119. {
  120. float lastTime = m_timeRange.end;
  121. if (key + 1 < (int)m_keys.size())
  122. {
  123. // EPSILON is required to ensure the correct ordering when getting nearest keys.
  124. lastTime = m_keys[key + 1].time + std::min(LOOP_TRANSITION_TIME,
  125. GetKeyDuration(key + 1) - EPSILON);
  126. }
  127. // duration is unlimited but cannot last past end of track or time of next key on track.
  128. return std::max(lastTime - m_keys[key].time, 0.0f);
  129. }
  130. else
  131. {
  132. return m_keys[key].GetActualDuration();
  133. }
  134. }
  135. //////////////////////////////////////////////////////////////////////////
  136. static bool CharacterTrackVersionConverter(
  137. AZ::SerializeContext& serializeContext,
  138. AZ::SerializeContext::DataElementNode& rootElement)
  139. {
  140. if (rootElement.GetVersion() < 3)
  141. {
  142. rootElement.AddElement(serializeContext, "BaseClass1", azrtti_typeid<IAnimTrack>());
  143. }
  144. return true;
  145. }
  146. template<>
  147. inline void TAnimTrack<ICharacterKey>::Reflect(AZ::ReflectContext* context)
  148. {
  149. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  150. {
  151. serializeContext->Class<TAnimTrack<ICharacterKey>, IAnimTrack>()
  152. ->Version(3, &CharacterTrackVersionConverter)
  153. ->Field("Flags", &TAnimTrack<ICharacterKey>::m_flags)
  154. ->Field("Range", &TAnimTrack<ICharacterKey>::m_timeRange)
  155. ->Field("ParamType", &TAnimTrack<ICharacterKey>::m_nParamType)
  156. ->Field("Keys", &TAnimTrack<ICharacterKey>::m_keys)
  157. ->Field("Id", &TAnimTrack<ICharacterKey>::m_id);
  158. }
  159. }
  160. //////////////////////////////////////////////////////////////////////////
  161. AnimValueType CCharacterTrack::GetValueType()
  162. {
  163. return AnimValueType::CharacterAnim;
  164. }
  165. //////////////////////////////////////////////////////////////////////////
  166. void CCharacterTrack::Reflect(AZ::ReflectContext* context)
  167. {
  168. TAnimTrack<ICharacterKey>::Reflect(context);
  169. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  170. {
  171. serializeContext->Class<CCharacterTrack, TAnimTrack<ICharacterKey>>()
  172. ->Version(1)
  173. ->Field("AnimationLayer", &CCharacterTrack::m_iAnimationLayer);
  174. }
  175. }