TrackEventTrack.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. CUiAnimStringTable::CUiAnimStringTable()
  11. : m_refCount(0)
  12. {
  13. m_pLastPage = new Page;
  14. m_pLastPage->pPrev = NULL;
  15. m_pEnd = m_pLastPage->mem;
  16. }
  17. CUiAnimStringTable::~CUiAnimStringTable()
  18. {
  19. for (Page* p = m_pLastPage, * pp; p; p = pp)
  20. {
  21. pp = p->pPrev;
  22. delete p;
  23. }
  24. }
  25. //////////////////////////////////////////////////////////////////////////
  26. void CUiAnimStringTable::add_ref()
  27. {
  28. ++m_refCount;
  29. }
  30. //////////////////////////////////////////////////////////////////////////
  31. void CUiAnimStringTable::release()
  32. {
  33. if (--m_refCount <= 0)
  34. {
  35. delete this;
  36. }
  37. }
  38. const char* CUiAnimStringTable::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 CUiTrackEventTrack::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 CUiTrackEventTrack::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. TUiAnimTrack<IEventKey>::SetKey(index, pEvKey);
  99. }
  100. //////////////////////////////////////////////////////////////////////////
  101. void CUiTrackEventTrack::InitPostLoad(IUiAnimSequence* sequence)
  102. {
  103. m_pStrings = sequence->GetTrackEventStringTable();
  104. }
  105. CUiTrackEventTrack::CUiTrackEventTrack(IUiAnimStringTable* pStrings)
  106. : m_pStrings(pStrings)
  107. {
  108. }
  109. CUiTrackEventTrack::CUiTrackEventTrack()
  110. : CUiTrackEventTrack(nullptr)
  111. {
  112. }
  113. void CUiTrackEventTrack::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. template<>
  130. inline void TUiAnimTrack<IEventKey>::Reflect(AZ::SerializeContext* serializeContext)
  131. {
  132. serializeContext->Class<TUiAnimTrack<IEventKey> >()
  133. ->Version(2)
  134. ->Field("Flags", &TUiAnimTrack<IEventKey>::m_flags)
  135. ->Field("Range", &TUiAnimTrack<IEventKey>::m_timeRange)
  136. ->Field("ParamType", &TUiAnimTrack<IEventKey>::m_nParamType)
  137. ->Field("Keys", &TUiAnimTrack<IEventKey>::m_keys);
  138. }
  139. //////////////////////////////////////////////////////////////////////////
  140. void CUiTrackEventTrack::Reflect(AZ::SerializeContext* serializeContext)
  141. {
  142. TUiAnimTrack<IEventKey>::Reflect(serializeContext);
  143. serializeContext->Class<CUiTrackEventTrack, TUiAnimTrack<IEventKey> >()
  144. ->Version(1);
  145. }