3
0

CVarNode.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "CVarNode.h"
  10. #include "AnimTrack.h"
  11. #include <ISystem.h>
  12. #include <IConsole.h>
  13. #include "Maestro/Types/AnimNodeType.h"
  14. #include "Maestro/Types/AnimValueType.h"
  15. #include "Maestro/Types/AnimParamType.h"
  16. //////////////////////////////////////////////////////////////////////////
  17. CAnimCVarNode::CAnimCVarNode(const int id)
  18. : CAnimNode(id, AnimNodeType::CVar)
  19. {
  20. SetFlags(GetFlags() | eAnimNodeFlags_CanChangeName);
  21. m_value = -1e-20f; //-1e-28;
  22. }
  23. CAnimCVarNode::CAnimCVarNode()
  24. : CAnimCVarNode(0)
  25. {
  26. }
  27. void CAnimCVarNode::CreateDefaultTracks()
  28. {
  29. CreateTrack(AnimParamType::Float);
  30. }
  31. void CAnimCVarNode::OnReset()
  32. {
  33. m_value = -1e-20f;
  34. }
  35. void CAnimCVarNode::OnResume()
  36. {
  37. OnReset();
  38. }
  39. //////////////////////////////////////////////////////////////////////////
  40. unsigned int CAnimCVarNode::GetParamCount() const
  41. {
  42. return 1;
  43. }
  44. //////////////////////////////////////////////////////////////////////////
  45. CAnimParamType CAnimCVarNode::GetParamType(unsigned int nIndex) const
  46. {
  47. if (nIndex == 0)
  48. {
  49. return AnimParamType::Float;
  50. }
  51. return AnimParamType::Invalid;
  52. }
  53. //////////////////////////////////////////////////////////////////////////
  54. int CAnimCVarNode::GetDefaultKeyTangentFlags() const
  55. {
  56. int retTangentFlags = SPLINE_KEY_TANGENT_UNIFIED;
  57. ICVar* var = gEnv->pConsole->GetCVar(GetName());
  58. // use step in tangents for int cvars
  59. if (var && var->GetType() == CVAR_INT)
  60. {
  61. // clear tangent flags
  62. retTangentFlags &= ~SPLINE_KEY_TANGENT_IN_MASK;
  63. retTangentFlags &= ~SPLINE_KEY_TANGENT_OUT_MASK;
  64. // set in tangents to step
  65. retTangentFlags |= (SPLINE_KEY_TANGENT_STEP << SPLINE_KEY_TANGENT_IN_SHIFT);
  66. retTangentFlags |= (SPLINE_KEY_TANGENT_CUSTOM << SPLINE_KEY_TANGENT_OUT_SHIFT);
  67. }
  68. return retTangentFlags;
  69. }
  70. //////////////////////////////////////////////////////////////////////////
  71. bool CAnimCVarNode::GetParamInfoFromType(const CAnimParamType& paramId, SParamInfo& info) const
  72. {
  73. if (paramId.GetType() == AnimParamType::Float)
  74. {
  75. info.flags = IAnimNode::ESupportedParamFlags(0);
  76. info.name = "Value";
  77. info.paramType = AnimParamType::Float;
  78. info.valueType = AnimValueType::Float;
  79. return true;
  80. }
  81. return false;
  82. }
  83. //////////////////////////////////////////////////////////////////////////
  84. void CAnimCVarNode::SetName(const char* name)
  85. {
  86. // Name of node is used as a name of console var.
  87. CAnimNode::SetName(name);
  88. ICVar* pVar = gEnv->pConsole->GetCVar(GetName());
  89. if (pVar)
  90. {
  91. m_value = pVar->GetFVal();
  92. }
  93. }
  94. //////////////////////////////////////////////////////////////////////////
  95. void CAnimCVarNode::Animate(SAnimContext& ec)
  96. {
  97. if (ec.resetting)
  98. {
  99. return;
  100. }
  101. float value = m_value;
  102. IAnimTrack* pValueTrack = GetTrackForParameter(AnimParamType::Float);
  103. if (!pValueTrack || (pValueTrack->GetFlags() & IAnimTrack::eAnimTrackFlags_Disabled))
  104. {
  105. return;
  106. }
  107. pValueTrack->GetValue(ec.time, value);
  108. if (value != m_value)
  109. {
  110. m_value = value;
  111. // Change console var value.
  112. ICVar* pVar = gEnv->pConsole->GetCVar(GetName());
  113. if (pVar)
  114. {
  115. pVar->Set(m_value);
  116. }
  117. }
  118. }
  119. //////////////////////////////////////////////////////////////////////////
  120. void CAnimCVarNode::Reflect(AZ::ReflectContext* context)
  121. {
  122. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  123. {
  124. serializeContext->Class<CAnimCVarNode, CAnimNode>()
  125. ->Version(1);
  126. }
  127. }