TimeRangesTrack.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "TimeRangesTrack.h"
  10. /// @deprecated Serialization for Sequence data in Component Entity Sequences now occurs through AZ::SerializeContext and the Sequence Component
  11. bool CTimeRangesTrack::Serialize(XmlNodeRef& xmlNode, bool bLoading, bool bLoadEmptyTracks)
  12. {
  13. return TAnimTrack<ITimeRangeKey>::Serialize(xmlNode, bLoading, bLoadEmptyTracks);
  14. }
  15. void CTimeRangesTrack::SerializeKey(ITimeRangeKey& key, XmlNodeRef& keyNode, bool bLoading)
  16. {
  17. if (bLoading)
  18. {
  19. key.m_duration = 0;
  20. key.m_endTime = 0;
  21. key.m_startTime = 0;
  22. key.m_speed = 1;
  23. keyNode->getAttr("length", key.m_duration);
  24. keyNode->getAttr("end", key.m_endTime);
  25. keyNode->getAttr("speed", key.m_speed);
  26. keyNode->getAttr("start", key.m_startTime);
  27. keyNode->getAttr("loop", key.m_bLoop);
  28. }
  29. else
  30. {
  31. if (key.m_duration > 0)
  32. {
  33. keyNode->setAttr("length", key.m_duration);
  34. }
  35. if (key.m_endTime > 0)
  36. {
  37. keyNode->setAttr("end", key.m_endTime);
  38. }
  39. if (key.m_speed != 1)
  40. {
  41. keyNode->setAttr("speed", key.m_speed);
  42. }
  43. if (key.m_startTime != 0)
  44. {
  45. keyNode->setAttr("start", key.m_startTime);
  46. }
  47. if (key.m_bLoop)
  48. {
  49. keyNode->setAttr("loop", key.m_bLoop);
  50. }
  51. }
  52. }
  53. float CTimeRangesTrack::GetKeyDuration(int key) const
  54. {
  55. assert(key >= 0 && key < (int)m_keys.size());
  56. return m_keys[key].GetActualDuration();
  57. }
  58. void CTimeRangesTrack::GetKeyInfo(int key, const char*& description, float& duration)
  59. {
  60. duration = GetKeyDuration(key);
  61. description = "";
  62. }
  63. int CTimeRangesTrack::GetActiveKeyIndexForTime(const float time)
  64. {
  65. const unsigned int numKeys = static_cast<unsigned int>(m_keys.size());
  66. if (numKeys == 0 || m_keys[0].time > time)
  67. {
  68. return -1;
  69. }
  70. int lastFound = 0;
  71. for (unsigned int i = 0; i < numKeys; ++i)
  72. {
  73. ITimeRangeKey& key = m_keys[i];
  74. if (key.time > time)
  75. {
  76. break;
  77. }
  78. else if (key.time <= time)
  79. {
  80. lastFound = i;
  81. }
  82. }
  83. return lastFound;
  84. }
  85. //////////////////////////////////////////////////////////////////////////
  86. static bool TimeRangesTrackVersionConverter(
  87. AZ::SerializeContext& serializeContext,
  88. AZ::SerializeContext::DataElementNode& rootElement)
  89. {
  90. if (rootElement.GetVersion() < 3)
  91. {
  92. rootElement.AddElement(serializeContext, "BaseClass1", azrtti_typeid<IAnimTrack>());
  93. }
  94. return true;
  95. }
  96. template<>
  97. inline void TAnimTrack<ITimeRangeKey>::Reflect(AZ::ReflectContext* context)
  98. {
  99. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  100. {
  101. serializeContext->Class<TAnimTrack<ITimeRangeKey>, IAnimTrack>()
  102. ->Version(3, &TimeRangesTrackVersionConverter)
  103. ->Field("Flags", &TAnimTrack<ITimeRangeKey>::m_flags)
  104. ->Field("Range", &TAnimTrack<ITimeRangeKey>::m_timeRange)
  105. ->Field("ParamType", &TAnimTrack<ITimeRangeKey>::m_nParamType)
  106. ->Field("Keys", &TAnimTrack<ITimeRangeKey>::m_keys)
  107. ->Field("Id", &TAnimTrack<ITimeRangeKey>::m_id);
  108. }
  109. }
  110. //////////////////////////////////////////////////////////////////////////
  111. void CTimeRangesTrack::Reflect(AZ::ReflectContext* context)
  112. {
  113. TAnimTrack<ITimeRangeKey>::Reflect(context);
  114. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  115. {
  116. serializeContext->Class<CTimeRangesTrack, TAnimTrack<ITimeRangeKey>>()
  117. ->Version(1);
  118. }
  119. }