2
0

XRControllerAnimationsComponent.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "XRControllerAnimationsComponent.h"
  9. #include <AzCore/Component/TransformBus.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <OpenXRVk/OpenXRVkActionsInterface.h>
  13. #include <Integration/ActorComponentBus.h>
  14. #include <Integration/AnimGraphComponentBus.h>
  15. namespace OpenXRVk
  16. {
  17. void XRControllerAnimationsComponent::Reflect(AZ::ReflectContext* context)
  18. {
  19. XRControllersConfig::Reflect(context);
  20. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  21. {
  22. serializeContext->RegisterGenericType<XRControllersConfig>();
  23. serializeContext->Class<XRControllerAnimationsComponent, AZ::Component>()
  24. ->Version(1)
  25. ->Field("Controller Items config", &XRControllerAnimationsComponent::m_controllersConfig)
  26. ;
  27. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  28. {
  29. editContext->Class<XRControllerAnimationsComponent>("XR Controller Animation", "Provides animations for controls on VR controller")
  30. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  31. ->Attribute(AZ::Edit::Attributes::Category, "XR")
  32. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Component_Placeholder.svg")
  33. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  34. ->DataElement(AZ::Edit::UIHandlers::Default, &XRControllerAnimationsComponent::m_controllersConfig, "Controller Items config", "Configuration for each item of the controller")
  35. ;
  36. }
  37. }
  38. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  39. {
  40. behaviorContext->Class<XRControllerAnimationsComponent>("XR Component Animation Group")
  41. ->Attribute(AZ::Script::Attributes::Category, "OpenXRVk Gem Group")
  42. ;
  43. }
  44. }
  45. void XRControllerAnimationsComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  46. {
  47. provided.push_back(AZ_CRC_CE("XRControllerAnimationService"));
  48. }
  49. void XRControllerAnimationsComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  50. {
  51. incompatible.push_back(AZ_CRC_CE("XRControllerAnimationService"));
  52. }
  53. void XRControllerAnimationsComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  54. {
  55. required.push_back(AZ_CRC_CE("TransformService"));
  56. }
  57. void XRControllerAnimationsComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  58. {
  59. }
  60. void XRControllerAnimationsComponent::Activate()
  61. {
  62. AZ::TickBus::Handler::BusConnect();
  63. }
  64. void XRControllerAnimationsComponent::Deactivate()
  65. {
  66. if (AZ::TickBus::Handler::BusIsConnected())
  67. {
  68. AZ::TickBus::Handler::BusDisconnect();
  69. }
  70. }
  71. extern AZ::Transform ReadActionHandlePose(IOpenXRActions* iface, IOpenXRActions::ActionHandle actionHandle);
  72. extern float ReadActionHandleFloat(IOpenXRActions* iface, IOpenXRActions::ActionHandle actionHandle, float deadZone);
  73. void XRControllerAnimationsComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
  74. {
  75. auto actionsIFace = OpenXRActionsInterface::Get();
  76. if (!actionsIFace)
  77. {
  78. return;
  79. }
  80. for (XRControllersConfig& cfg : m_controllersConfig)
  81. {
  82. if (!cfg.m_actionHandle.IsValid())
  83. {
  84. cfg.m_actionHandle = actionsIFace->GetActionHandle("main_action_set", cfg.m_actionName);
  85. if (!cfg.m_actionHandle.IsValid())
  86. {
  87. continue;
  88. }
  89. }
  90. switch (cfg.m_controlItemType)
  91. {
  92. case XRControllersConfig::ControlItemType::Boolean:
  93. {
  94. auto outcome = actionsIFace->GetActionStateBoolean(cfg.m_actionHandle);
  95. if (outcome.IsSuccess())
  96. {
  97. bool res = outcome.GetValue();
  98. if (res != cfg.m_prevBoolean)
  99. {
  100. EMotionFX::Integration::AnimGraphComponentRequestBus::Event(GetEntityId(), &EMotionFX::Integration::AnimGraphComponentRequestBus::Events::SetNamedParameterBool, cfg.m_animGraphParameter.c_str(), res);
  101. cfg.m_prevBoolean = res;
  102. }
  103. }
  104. break;
  105. }
  106. case XRControllersConfig::ControlItemType::Float:
  107. {
  108. float res = ReadActionHandleFloat(actionsIFace, cfg.m_actionHandle, 0.001f);
  109. if (res != cfg.m_prevFloat)
  110. {
  111. EMotionFX::Integration::AnimGraphComponentRequestBus::Event(GetEntityId(), &EMotionFX::Integration::AnimGraphComponentRequestBus::Events::SetNamedParameterFloat, cfg.m_animGraphParameter.c_str(), res);
  112. cfg.m_prevFloat = res;
  113. }
  114. break;
  115. }
  116. case XRControllersConfig::ControlItemType::Vector2:
  117. {
  118. float res = (ReadActionHandleFloat(actionsIFace, cfg.m_actionHandle, 0.001f) + 1.0f) / 2.0f;
  119. if (res != cfg.m_prevFloat)
  120. {
  121. EMotionFX::Integration::AnimGraphComponentRequestBus::Event(GetEntityId(), &EMotionFX::Integration::AnimGraphComponentRequestBus::Events::SetNamedParameterFloat, cfg.m_animGraphParameter.c_str(), res);
  122. cfg.m_prevFloat = res;
  123. }
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. } // namespace OpenXRVk