VirtualGamepadButtonComponent.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "VirtualGamepadButtonComponent.h"
  9. #include <VirtualGamepad/VirtualGamepadBus.h>
  10. #include <LyShine/Bus/UiInteractableBus.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/std/sort.h>
  14. ////////////////////////////////////////////////////////////////////////////////////////////////////
  15. namespace VirtualGamepad
  16. {
  17. ////////////////////////////////////////////////////////////////////////////////////////////////
  18. void VirtualGamepadButtonComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  19. {
  20. provided.push_back(AZ_CRC_CE("VirtualGamepadButtonService"));
  21. }
  22. ////////////////////////////////////////////////////////////////////////////////////////////////
  23. void VirtualGamepadButtonComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  24. {
  25. incompatible.push_back(AZ_CRC_CE("VirtualGamepadButtonService"));
  26. }
  27. ////////////////////////////////////////////////////////////////////////////////////////////////
  28. void VirtualGamepadButtonComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  29. {
  30. required.push_back(AZ_CRC_CE("UiInteractableService"));
  31. }
  32. ////////////////////////////////////////////////////////////////////////////////////////////////
  33. void VirtualGamepadButtonComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  34. {
  35. AZ_UNUSED(dependent);
  36. }
  37. ////////////////////////////////////////////////////////////////////////////////////////////////
  38. void VirtualGamepadButtonComponent::Reflect(AZ::ReflectContext* context)
  39. {
  40. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  41. {
  42. serialize->Class<VirtualGamepadButtonComponent, AZ::Component>()
  43. ->Version(0)
  44. ->Field("AssignedInputChannelName", &VirtualGamepadButtonComponent::m_assignedInputChannelName)
  45. ;
  46. if (AZ::EditContext* ec = serialize->GetEditContext())
  47. {
  48. ec->Class<VirtualGamepadButtonComponent>("VirtualGamepadButton", "A component that designates this entity as a virtual gamepad button")
  49. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  50. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/UiVirtualButton.png")
  51. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/UiVirtualButton.png")
  52. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("UI"))
  53. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  54. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &VirtualGamepadButtonComponent::m_assignedInputChannelName,
  55. "Input Channel", "The input channel that will be updated when the user interacts with this virtual control")
  56. ->Attribute(AZ::Edit::Attributes::StringList, &VirtualGamepadButtonComponent::GetAssignableInputChannelNames)
  57. ;
  58. }
  59. }
  60. }
  61. ////////////////////////////////////////////////////////////////////////////////////////////////
  62. void VirtualGamepadButtonComponent::Init()
  63. {
  64. }
  65. ////////////////////////////////////////////////////////////////////////////////////////////////
  66. void VirtualGamepadButtonComponent::Activate()
  67. {
  68. VirtualGamepadButtonRequestBus::Handler::BusConnect(m_assignedInputChannelName);
  69. }
  70. ////////////////////////////////////////////////////////////////////////////////////////////////
  71. void VirtualGamepadButtonComponent::Deactivate()
  72. {
  73. VirtualGamepadButtonRequestBus::Handler::BusDisconnect(m_assignedInputChannelName);
  74. }
  75. ////////////////////////////////////////////////////////////////////////////////////////////////
  76. bool VirtualGamepadButtonComponent::IsPressed() const
  77. {
  78. bool isPressed = false;
  79. UiInteractableBus::EventResult(isPressed,
  80. GetEntityId(),
  81. &UiInteractableInterface::IsPressed);
  82. return isPressed;
  83. }
  84. ////////////////////////////////////////////////////////////////////////////////////////////////
  85. AZStd::vector<AZStd::string> VirtualGamepadButtonComponent::GetAssignableInputChannelNames() const
  86. {
  87. AZStd::unordered_set<AZStd::string> buttonNames;
  88. VirtualGamepadRequestBus::BroadcastResult(buttonNames, &VirtualGamepadRequests::GetButtonNames);
  89. AZStd::vector<AZStd::string> assignableInputChannelNames;
  90. for (const AZStd::string& buttonName : buttonNames)
  91. {
  92. assignableInputChannelNames.push_back(buttonName);
  93. }
  94. AZStd::sort(assignableInputChannelNames.begin(), assignableInputChannelNames.end());
  95. return assignableInputChannelNames;
  96. }
  97. } // namespace VirtualGamepad