TrackEventTrack.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "TrackEventTrack.h"
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. CAnimStringTable::CAnimStringTable()
  11. : m_refCount(0)
  12. {
  13. m_pLastPage = new Page;
  14. m_pLastPage->pPrev = NULL;
  15. m_pEnd = m_pLastPage->mem;
  16. }
  17. CAnimStringTable::~CAnimStringTable()
  18. {
  19. for (Page* p = m_pLastPage, * pp; p; p = pp)
  20. {
  21. pp = p->pPrev;
  22. delete p;
  23. }
  24. }
  25. //////////////////////////////////////////////////////////////////////////
  26. void CAnimStringTable::add_ref()
  27. {
  28. ++m_refCount;
  29. }
  30. //////////////////////////////////////////////////////////////////////////
  31. void CAnimStringTable::release()
  32. {
  33. if (--m_refCount <= 0)
  34. {
  35. delete this;
  36. }
  37. }
  38. const char* CAnimStringTable::Add(const char* p)
  39. {
  40. TableMap::iterator it = m_table.find(p);
  41. if (it != m_table.end())
  42. {
  43. return it->second;
  44. }
  45. char* pPageEnd = &m_pLastPage->mem[sizeof(m_pLastPage->mem)];
  46. size_t nAvailable = pPageEnd - m_pEnd;
  47. size_t nLen = strlen(p);
  48. if (nLen >= sizeof(m_pLastPage->mem))
  49. {
  50. CryFatalError("String table can't accomodate string");
  51. }
  52. if (nAvailable <= nLen)
  53. {
  54. // Not enough room. Allocate another page.
  55. Page* pPage = new Page;
  56. pPage->pPrev = m_pLastPage;
  57. m_pLastPage = pPage;
  58. m_pEnd = pPage->mem;
  59. pPageEnd = &m_pLastPage->mem[sizeof(m_pLastPage->mem)];
  60. nAvailable = pPageEnd - m_pEnd;
  61. }
  62. char* pRet = m_pEnd;
  63. m_pEnd += nLen + 1;
  64. azstrcpy(pRet, nAvailable, p);
  65. m_table.insert(std::make_pair(pRet, pRet));
  66. return pRet;
  67. }
  68. //////////////////////////////////////////////////////////////////////////
  69. void CTrackEventTrack::SerializeKey(IEventKey& key, XmlNodeRef& keyNode, bool bLoading)
  70. {
  71. if (bLoading)
  72. {
  73. const char* str;
  74. str = keyNode->getAttr("event");
  75. key.event = m_pStrings->Add(str);
  76. str = keyNode->getAttr("eventValue");
  77. key.eventValue = m_pStrings->Add(str);
  78. }
  79. else
  80. {
  81. if (!key.event.empty())
  82. {
  83. keyNode->setAttr("event", key.event.c_str());
  84. }
  85. if (!key.eventValue.empty())
  86. {
  87. keyNode->setAttr("eventValue", key.eventValue.c_str());
  88. }
  89. }
  90. }
  91. void CTrackEventTrack::SetKey(int index, IKey* key)
  92. {
  93. IEventKey* pEvKey = static_cast<IEventKey*>(key);
  94. // Intern string values
  95. pEvKey->event = m_pStrings->Add(pEvKey->event.c_str());
  96. pEvKey->eventValue = m_pStrings->Add(pEvKey->eventValue.c_str());
  97. pEvKey->animation = m_pStrings->Add(pEvKey->animation.c_str());
  98. TAnimTrack<IEventKey>::SetKey(index, pEvKey);
  99. }
  100. //////////////////////////////////////////////////////////////////////////
  101. void CTrackEventTrack::InitPostLoad(IAnimSequence* sequence)
  102. {
  103. m_pStrings = sequence->GetTrackEventStringTable();
  104. }
  105. CTrackEventTrack::CTrackEventTrack(IAnimStringTable* pStrings)
  106. : m_pStrings(pStrings)
  107. {
  108. }
  109. CTrackEventTrack::CTrackEventTrack()
  110. : CTrackEventTrack(nullptr)
  111. {
  112. }
  113. void CTrackEventTrack::GetKeyInfo(int key, const char*& description, float& duration)
  114. {
  115. static char desc[128];
  116. assert(key >= 0 && key < (int)m_keys.size());
  117. CheckValid();
  118. description = 0;
  119. duration = 0;
  120. azstrcpy(desc, AZ_ARRAY_SIZE(desc), m_keys[key].event.c_str());
  121. if (!m_keys[key].eventValue.empty())
  122. {
  123. azstrcat(desc, AZ_ARRAY_SIZE(desc), ", ");
  124. azstrcat(desc, AZ_ARRAY_SIZE(desc), m_keys[key].eventValue.c_str());
  125. }
  126. description = desc;
  127. }
  128. //////////////////////////////////////////////////////////////////////////
  129. static bool EventTrackVersionConverter(
  130. AZ::SerializeContext& serializeContext,
  131. AZ::SerializeContext::DataElementNode& rootElement)
  132. {
  133. if (rootElement.GetVersion() < 3)
  134. {
  135. rootElement.AddElement(serializeContext, "BaseClass1", azrtti_typeid<IAnimTrack>());
  136. }
  137. return true;
  138. }
  139. template<>
  140. inline void TAnimTrack<IEventKey>::Reflect(AZ::ReflectContext* context)
  141. {
  142. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  143. {
  144. serializeContext->Class<TAnimTrack<IEventKey>, IAnimTrack>()
  145. ->Version(3, &EventTrackVersionConverter)
  146. ->Field("Flags", &TAnimTrack<IEventKey>::m_flags)
  147. ->Field("Range", &TAnimTrack<IEventKey>::m_timeRange)
  148. ->Field("ParamType", &TAnimTrack<IEventKey>::m_nParamType)
  149. ->Field("Keys", &TAnimTrack<IEventKey>::m_keys)
  150. ->Field("Id", &TAnimTrack<IEventKey>::m_id);
  151. }
  152. }
  153. //////////////////////////////////////////////////////////////////////////
  154. void CTrackEventTrack::Reflect(AZ::ReflectContext* context)
  155. {
  156. TAnimTrack<IEventKey>::Reflect(context);
  157. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  158. {
  159. serializeContext->Class<CTrackEventTrack, TAnimTrack<IEventKey>>()
  160. ->Version(1);
  161. }
  162. }