CharacterKeyUIControls.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // CryCommon
  10. #include <CryCommon/Maestro/Types/AnimNodeType.h> // for AnimNodeType
  11. #include <CryCommon/Maestro/Types/AnimValueType.h> // for AnimValueType
  12. #include <CryCommon/Maestro/Types/AnimParamType.h> // for AnimParamType
  13. // Editor
  14. #include "Controls/ReflectedPropertyControl/ReflectedPropertyItem.h" // for ReflectedPropertyItem
  15. #include "TrackViewKeyPropertiesDlg.h" // for CTrackViewKeyUIControls
  16. //////////////////////////////////////////////////////////////////////////
  17. class CCharacterKeyUIControls
  18. : public CTrackViewKeyUIControls
  19. {
  20. public:
  21. CSmartVariableArray mv_table;
  22. CSmartVariable<QString> mv_animation;
  23. CSmartVariable<bool> mv_loop;
  24. CSmartVariable<bool> mv_blendGap;
  25. CSmartVariable<bool> mv_inplace;
  26. CSmartVariable<float> mv_startTime;
  27. CSmartVariable<float> mv_endTime;
  28. CSmartVariable<float> mv_timeScale;
  29. void OnCreateVars() override
  30. {
  31. AddVariable(mv_table, "Key Properties");
  32. AddVariable(mv_table, mv_animation, "Animation", IVariable::DT_ANIMATION);
  33. AddVariable(mv_table, mv_loop, "Loop");
  34. AddVariable(mv_table, mv_blendGap, "Blend Gap");
  35. AddVariable(mv_table, mv_inplace, "In Place");
  36. AddVariable(mv_table, mv_startTime, "Start Time");
  37. AddVariable(mv_table, mv_endTime, "End Time");
  38. AddVariable(mv_table, mv_timeScale, "Time Scale");
  39. mv_timeScale->SetLimits(0.001f, 100.f);
  40. }
  41. bool SupportTrackType(const CAnimParamType& paramType, [[maybe_unused]] EAnimCurveType trackType, AnimValueType valueType) const override
  42. {
  43. return paramType == AnimParamType::Animation || valueType == AnimValueType::CharacterAnim;
  44. }
  45. bool OnKeySelectionChange(CTrackViewKeyBundle& selectedKeys) override;
  46. void OnUIChange(IVariable* pVar, CTrackViewKeyBundle& selectedKeys) override;
  47. unsigned int GetPriority() const override { return 1; }
  48. static const GUID& GetClassID()
  49. {
  50. // {EAA26453-6B74-4771-8FD1-14CDFF88E723}
  51. static const GUID guid =
  52. {
  53. 0xeaa26453, 0x6b74, 0x4771, { 0x8f, 0xd1, 0x14, 0xcd, 0xff, 0x88, 0xe7, 0x23 }
  54. };
  55. return guid;
  56. }
  57. protected:
  58. void ResetStartEndLimits(float characterKeyDuration);
  59. };
  60. //////////////////////////////////////////////////////////////////////////
  61. void CCharacterKeyUIControls::ResetStartEndLimits(float characterKeyDuration)
  62. {
  63. const float time_zero = .0f;
  64. float step = ReflectedPropertyItem::ComputeSliderStep(time_zero, characterKeyDuration);
  65. mv_startTime.GetVar()->SetLimits(time_zero, characterKeyDuration, step, true, true);
  66. mv_endTime.GetVar()->SetLimits(time_zero, characterKeyDuration, step, true, true);
  67. }
  68. bool CCharacterKeyUIControls::OnKeySelectionChange(CTrackViewKeyBundle& selectedKeys)
  69. {
  70. if (!selectedKeys.AreAllKeysOfSameType())
  71. {
  72. return false;
  73. }
  74. bool bAssigned = false;
  75. if (selectedKeys.GetKeyCount() == 1)
  76. {
  77. const CTrackViewKeyHandle& keyHandle = selectedKeys.GetKey(0);
  78. CAnimParamType paramType = keyHandle.GetTrack()->GetParameterType();
  79. if (paramType == AnimParamType::Animation || keyHandle.GetTrack()->GetValueType() == AnimValueType::CharacterAnim)
  80. {
  81. ICharacterKey charKey;
  82. keyHandle.GetKey(&charKey);
  83. // Find editor object who owns this node.
  84. const CTrackViewTrack* pTrack = keyHandle.GetTrack();
  85. if (pTrack->GetAnimNode()->GetType() == AnimNodeType::Component)
  86. {
  87. // no legacy entity was returned and the track's animNode is a component - try to get the AZ::EntityId from the component node's parent
  88. CTrackViewAnimNode* parentNode = static_cast<CTrackViewAnimNode*>(pTrack->GetAnimNode()->GetParentNode());
  89. if (parentNode)
  90. {
  91. AZ::EntityId azEntityId = parentNode->GetAzEntityId();
  92. if (azEntityId.IsValid())
  93. {
  94. static_assert(sizeof(AZ::EntityId) <= sizeof(AZ::u64), "Can't pack AZ::EntityId into a AZ::u64 in CCharacterKeyUIControls::OnKeySelectionChange.");
  95. mv_animation->SetUserData(static_cast<AZ::u64>(azEntityId));
  96. }
  97. }
  98. }
  99. mv_animation = charKey.m_animation.c_str();
  100. mv_loop = charKey.m_bLoop;
  101. mv_blendGap = charKey.m_bBlendGap;
  102. mv_inplace = charKey.m_bInPlace;
  103. mv_endTime = charKey.m_endTime;
  104. mv_startTime = charKey.m_startTime;
  105. mv_timeScale = charKey.m_speed;
  106. bAssigned = true;
  107. }
  108. }
  109. return bAssigned;
  110. }
  111. // Called when UI variable changes.
  112. void CCharacterKeyUIControls::OnUIChange(IVariable* pVar, CTrackViewKeyBundle& selectedKeys)
  113. {
  114. CTrackViewSequence* pSequence = GetIEditor()->GetAnimation()->GetSequence();
  115. if (!pSequence || !selectedKeys.AreAllKeysOfSameType())
  116. {
  117. return;
  118. }
  119. for (unsigned int keyIndex = 0, num = (int)selectedKeys.GetKeyCount(); keyIndex < num; keyIndex++)
  120. {
  121. CTrackViewKeyHandle keyHandle = selectedKeys.GetKey(keyIndex);
  122. CTrackViewTrack* pTrack = keyHandle.GetTrack();
  123. const CAnimParamType paramType = pTrack->GetParameterType();
  124. if (paramType == AnimParamType::Animation || keyHandle.GetTrack()->GetValueType() == AnimValueType::CharacterAnim)
  125. {
  126. ICharacterKey charKey;
  127. keyHandle.GetKey(&charKey);
  128. if (mv_animation.GetVar() == pVar)
  129. {
  130. charKey.m_animation = ((QString)mv_animation).toUtf8().data();
  131. // This call is required to make sure that the newly set animation is properly triggered.
  132. pTrack->GetSequence()->Reset(false);
  133. }
  134. SyncValue(mv_loop, charKey.m_bLoop, false, pVar);
  135. SyncValue(mv_blendGap, charKey.m_bBlendGap, false, pVar);
  136. SyncValue(mv_inplace, charKey.m_bInPlace, false, pVar);
  137. SyncValue(mv_startTime, charKey.m_startTime, false, pVar);
  138. SyncValue(mv_endTime, charKey.m_endTime, false, pVar);
  139. SyncValue(mv_timeScale, charKey.m_speed, false, pVar);
  140. keyHandle.SetKey(&charKey);
  141. }
  142. }
  143. }
  144. REGISTER_QT_CLASS_DESC(CCharacterKeyUIControls, "TrackView.KeyUI.Character", "TrackViewKeyUI");