CommentNodeAnimator.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 "EditorDefs.h"
  9. #include "CommentNodeAnimator.h"
  10. // CryCommon
  11. #include <CryCommon/Maestro/Types/AnimParamType.h>
  12. #include <CryCommon/IFont.h>
  13. // Editor
  14. #include "Settings.h"
  15. CCommentNodeAnimator::CCommentNodeAnimator(CTrackViewAnimNode* pCommentNode)
  16. {
  17. assert(pCommentNode);
  18. m_pCommentNode = pCommentNode;
  19. }
  20. CCommentNodeAnimator::~CCommentNodeAnimator()
  21. {
  22. m_pCommentNode = nullptr;
  23. }
  24. void CCommentNodeAnimator::Animate(CTrackViewAnimNode* pNode, const SAnimContext& ac)
  25. {
  26. if (pNode != m_pCommentNode || pNode->IsDisabled())
  27. {
  28. return;
  29. }
  30. CTrackViewTrackBundle tracks = pNode->GetAllTracks();
  31. int trackCount = tracks.GetCount();
  32. Vec2 pos(0, 0);
  33. for (int i = 0; i < trackCount; ++i)
  34. {
  35. CTrackViewTrack* pTrack = tracks.GetTrack(i);
  36. if (pTrack->IsMasked(ac.trackMask))
  37. {
  38. continue;
  39. }
  40. switch (pTrack->GetParameterType().GetType())
  41. {
  42. case AnimParamType::CommentText:
  43. {
  44. AnimateCommentTextTrack(pTrack, ac);
  45. }
  46. break;
  47. case AnimParamType::PositionX:
  48. {
  49. pTrack->GetValue(ac.time, pos.x);
  50. }
  51. break;
  52. case AnimParamType::PositionY:
  53. {
  54. pTrack->GetValue(ac.time, pos.y);
  55. }
  56. break;
  57. }
  58. }
  59. // Position mapping from [0,100] to [-1,1]
  60. pos = (pos - Vec2(50.0f, 50.0f)) / 50.0f;
  61. m_commentContext.m_unitPos = pos;
  62. }
  63. void CCommentNodeAnimator::AnimateCommentTextTrack(CTrackViewTrack* pTrack, const SAnimContext& ac)
  64. {
  65. if (pTrack->GetKeyCount() == 0)
  66. {
  67. return;
  68. }
  69. CTrackViewKeyHandle keyHandle = GetActiveKeyHandle(pTrack, ac.time);
  70. if (keyHandle.IsValid())
  71. {
  72. ICommentKey commentKey;
  73. keyHandle.GetKey(&commentKey);
  74. if (commentKey.m_duration > 0 && ac.time < keyHandle.GetTime() + commentKey.m_duration)
  75. {
  76. m_commentContext.m_strComment = commentKey.m_strComment;
  77. azstrcpy(m_commentContext.m_strFont, AZ_ARRAY_SIZE(m_commentContext.m_strFont), commentKey.m_strFont.c_str());
  78. m_commentContext.m_color = commentKey.m_color;
  79. m_commentContext.m_align = commentKey.m_align;
  80. m_commentContext.m_size = commentKey.m_size;
  81. }
  82. else
  83. {
  84. m_commentContext.m_strComment.clear();
  85. }
  86. }
  87. else
  88. {
  89. m_commentContext.m_strComment.clear();
  90. }
  91. }
  92. CTrackViewKeyHandle CCommentNodeAnimator::GetActiveKeyHandle(CTrackViewTrack* pTrack, float fTime)
  93. {
  94. const int nkeys = pTrack->GetKeyCount();
  95. if (nkeys == 0)
  96. {
  97. return CTrackViewKeyHandle();
  98. }
  99. const CTrackViewKeyHandle& firstKeyHandle = pTrack->GetKey(0);
  100. // Time is before first key.
  101. if (firstKeyHandle.GetTime() > fTime)
  102. {
  103. return CTrackViewKeyHandle();
  104. }
  105. for (int i = 0; i < nkeys; i++)
  106. {
  107. const CTrackViewKeyHandle& keyHandle = pTrack->GetKey(i);
  108. if (fTime >= keyHandle.GetTime())
  109. {
  110. if ((i == nkeys - 1) || (fTime < pTrack->GetKey(i + 1).GetTime()))
  111. {
  112. return keyHandle;
  113. }
  114. }
  115. else
  116. {
  117. break;
  118. }
  119. }
  120. return CTrackViewKeyHandle();
  121. }
  122. void CCommentNodeAnimator::Render(CTrackViewAnimNode* pNode, [[maybe_unused]] const SAnimContext& ac)
  123. {
  124. if (!pNode->IsDisabled())
  125. {
  126. CCommentContext* cc = &m_commentContext;
  127. if (!cc->m_strComment.empty())
  128. {
  129. Vec3 color(cc->m_color.GetR(), cc->m_color.GetG(), cc->m_color.GetB());
  130. DrawText(cc->m_strFont, cc->m_size, cc->m_unitPos, color, cc->m_strComment.c_str(), cc->m_align);
  131. }
  132. }
  133. }
  134. Vec2 CCommentNodeAnimator::GetScreenPosFromNormalizedPos(const Vec2&)
  135. {
  136. AZ_Error("CryLegacy", false, "CCommentNodeAnimator::GetScreenPosFromNormalizedPos not supported");
  137. return Vec2(0, 0);
  138. }
  139. void CCommentNodeAnimator::DrawText(const char* szFontName, float fSize, const Vec2& unitPos, const ColorF col, const char* szText, int align)
  140. {
  141. IFFont* pFont = gEnv->pCryFont->GetFont(szFontName);
  142. if (!pFont)
  143. {
  144. pFont = gEnv->pCryFont->GetFont("default");
  145. }
  146. if (pFont)
  147. {
  148. STextDrawContext ctx;
  149. ctx.SetSizeIn800x600(false);
  150. ctx.SetSize(Vec2(UIDRAW_TEXTSIZEFACTOR * fSize, UIDRAW_TEXTSIZEFACTOR * fSize));
  151. ctx.SetCharWidthScale(0.5f);
  152. ctx.SetProportional(false);
  153. ctx.SetFlags(align);
  154. // alignment
  155. Vec2 pos = GetScreenPosFromNormalizedPos(unitPos);
  156. if (align & eDrawText_Center)
  157. {
  158. pos.x -= pFont->GetTextSize(szText, true, ctx).x * 0.5f;
  159. }
  160. else if (align & eDrawText_Right)
  161. {
  162. pos.x -= pFont->GetTextSize(szText, true, ctx).x;
  163. }
  164. // Color
  165. ctx.SetColor(col);
  166. pFont->DrawString(pos.x, pos.y, szText, true, ctx);
  167. }
  168. }