ScreenFaderTrack.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "ScreenFaderTrack.h"
  10. #include <ITexture.h>
  11. //-----------------------------------------------------------------------------
  12. CScreenFaderTrack::CScreenFaderTrack()
  13. {
  14. m_lastTextureID = -1;
  15. SetScreenFaderTrackDefaults();
  16. }
  17. //-----------------------------------------------------------------------------
  18. CScreenFaderTrack::~CScreenFaderTrack()
  19. {
  20. ReleasePreloadedTextures();
  21. }
  22. //-----------------------------------------------------------------------------
  23. void CScreenFaderTrack::GetKeyInfo(int key, const char*& description, float& duration)
  24. {
  25. static char desc[32];
  26. assert(key >= 0 && key < (int)m_keys.size());
  27. CheckValid();
  28. description = 0;
  29. duration = m_keys[key].m_fadeTime;
  30. azstrcpy(desc, AZ_ARRAY_SIZE(desc), m_keys[key].m_fadeType == IScreenFaderKey::eFT_FadeIn ? "In" : "Out");
  31. description = desc;
  32. }
  33. //-----------------------------------------------------------------------------
  34. void CScreenFaderTrack::SerializeKey(IScreenFaderKey& key, XmlNodeRef& keyNode, bool bLoading)
  35. {
  36. if (bLoading)
  37. {
  38. keyNode->getAttr("fadeTime", key.m_fadeTime);
  39. Vec3 color(0, 0, 0);
  40. keyNode->getAttr("fadeColor", color);
  41. key.m_fadeColor = AZ::Color(color.x, color.y, color.z, 1.f);
  42. int fadeType = 0;
  43. keyNode->getAttr("fadeType", fadeType);
  44. key.m_fadeType = IScreenFaderKey::EFadeType(fadeType);
  45. int fadeChangeType(0);
  46. if (keyNode->getAttr("fadeChangeType", fadeChangeType))
  47. {
  48. key.m_fadeChangeType = IScreenFaderKey::EFadeChangeType(fadeChangeType);
  49. }
  50. else
  51. {
  52. key.m_fadeChangeType = IScreenFaderKey::eFCT_Linear;
  53. }
  54. const char* str;
  55. str = keyNode->getAttr("texture");
  56. key.m_strTexture = str;
  57. keyNode->getAttr("useCurColor", key.m_bUseCurColor);
  58. }
  59. else
  60. {
  61. keyNode->setAttr("fadeTime", key.m_fadeTime);
  62. Vec3 color(key.m_fadeColor.GetR(), key.m_fadeColor.GetG(), key.m_fadeColor.GetB());
  63. keyNode->setAttr("fadeColor", color);
  64. keyNode->setAttr("fadeType", (int)key.m_fadeType);
  65. keyNode->setAttr("fadeChangeType", (int)key.m_fadeChangeType);
  66. keyNode->setAttr("texture", key.m_strTexture.c_str());
  67. keyNode->setAttr("useCurColor", key.m_bUseCurColor);
  68. }
  69. }
  70. //-----------------------------------------------------------------------------
  71. void CScreenFaderTrack::SetFlags(int flags)
  72. {
  73. // call base class implementation. I'm avoiding the use of the Microsoft specific __super::SetFlags(flags) because it is not
  74. // platform agnostic
  75. TAnimTrack<IScreenFaderKey>::SetFlags(flags);
  76. if (flags & IAnimTrack::eAnimTrackFlags_Disabled)
  77. {
  78. // when we disable, 'clear' the screen fader effect to avoid the possibility of leaving the Editor in a faded state
  79. m_bTextureVisible = false;
  80. m_drawColor = Vec4(.0f, .0f, .0f, .0f);
  81. }
  82. }
  83. //-----------------------------------------------------------------------------
  84. void CScreenFaderTrack::PreloadTextures()
  85. {
  86. if (!m_preloadedTextures.empty())
  87. {
  88. ReleasePreloadedTextures();
  89. }
  90. const int nKeysCount = GetNumKeys();
  91. if (nKeysCount > 0)
  92. {
  93. m_preloadedTextures.reserve(nKeysCount);
  94. for (int nKeyIndex = 0; nKeyIndex < nKeysCount; ++nKeyIndex)
  95. {
  96. IScreenFaderKey key;
  97. GetKey(nKeyIndex, &key);
  98. if (!key.m_strTexture.empty())
  99. {
  100. }
  101. else
  102. {
  103. m_preloadedTextures.push_back(NULL);
  104. }
  105. }
  106. }
  107. }
  108. //-----------------------------------------------------------------------------
  109. ITexture* CScreenFaderTrack::GetActiveTexture() const
  110. {
  111. int index = GetLastTextureID();
  112. return (index >= 0 && (size_t) index < m_preloadedTextures.size()) ? m_preloadedTextures[index] : 0;
  113. }
  114. void CScreenFaderTrack::SetScreenFaderTrackDefaults()
  115. {
  116. m_bTextureVisible = false;
  117. m_drawColor = Vec4(1, 1, 1, 1);
  118. }
  119. //-----------------------------------------------------------------------------
  120. void CScreenFaderTrack::ReleasePreloadedTextures()
  121. {
  122. const size_t size = m_preloadedTextures.size();
  123. for (size_t i = 0; i < size; ++i)
  124. {
  125. SAFE_RELEASE(m_preloadedTextures[i]);
  126. }
  127. m_preloadedTextures.resize(0);
  128. }
  129. //-----------------------------------------------------------------------------
  130. bool CScreenFaderTrack::SetActiveTexture(int index)
  131. {
  132. ITexture* pTexture = GetActiveTexture();
  133. SetLastTextureID(index);
  134. // Check if textures should be reloaded.
  135. bool bNeedTexReload = pTexture == NULL; // Not yet loaded
  136. if (pTexture)
  137. {
  138. IScreenFaderKey key;
  139. GetKey(index, &key);
  140. if (strcmp(key.m_strTexture.c_str(), pTexture->GetName()) != 0)
  141. {
  142. bNeedTexReload = true; // Loaded, but a different texture
  143. }
  144. }
  145. if (bNeedTexReload)
  146. {
  147. // Ok, try to reload.
  148. PreloadTextures();
  149. pTexture = GetActiveTexture();
  150. }
  151. return pTexture != 0;
  152. }
  153. //////////////////////////////////////////////////////////////////////////
  154. static bool ScreenFaderTrackVersionConverter(
  155. AZ::SerializeContext& serializeContext,
  156. AZ::SerializeContext::DataElementNode& rootElement)
  157. {
  158. if (rootElement.GetVersion() < 3)
  159. {
  160. rootElement.AddElement(serializeContext, "BaseClass1", azrtti_typeid<IAnimTrack>());
  161. }
  162. return true;
  163. }
  164. template<>
  165. inline void TAnimTrack<IScreenFaderKey>::Reflect(AZ::ReflectContext* context)
  166. {
  167. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  168. {
  169. serializeContext->Class<TAnimTrack<IScreenFaderKey>, IAnimTrack>()
  170. ->Version(3, &ScreenFaderTrackVersionConverter)
  171. ->Field("Flags", &TAnimTrack<IScreenFaderKey>::m_flags)
  172. ->Field("Range", &TAnimTrack<IScreenFaderKey>::m_timeRange)
  173. ->Field("ParamType", &TAnimTrack<IScreenFaderKey>::m_nParamType)
  174. ->Field("Keys", &TAnimTrack<IScreenFaderKey>::m_keys)
  175. ->Field("Id", &TAnimTrack<IScreenFaderKey>::m_id);
  176. }
  177. }
  178. //////////////////////////////////////////////////////////////////////////
  179. void CScreenFaderTrack::Reflect(AZ::ReflectContext* context)
  180. {
  181. TAnimTrack<IScreenFaderKey>::Reflect(context);
  182. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  183. {
  184. serializeContext->Class<CScreenFaderTrack, TAnimTrack<IScreenFaderKey>>()
  185. ->Version(1);
  186. }
  187. }