VirtualGamepadSystemComponent.cpp 4.9 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 "VirtualGamepadSystemComponent.h"
  9. #include "InputDeviceVirtualGamepad.h"
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////
  13. namespace VirtualGamepad
  14. {
  15. ////////////////////////////////////////////////////////////////////////////////////////////////
  16. void VirtualGamepadSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  17. {
  18. provided.push_back(AZ_CRC_CE("VirtualGamepadService"));
  19. }
  20. ////////////////////////////////////////////////////////////////////////////////////////////////
  21. void VirtualGamepadSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  22. {
  23. incompatible.push_back(AZ_CRC_CE("VirtualGamepadService"));
  24. }
  25. ////////////////////////////////////////////////////////////////////////////////////////////////
  26. void VirtualGamepadSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  27. {
  28. required.push_back(AZ_CRC_CE("InputSystemService"));
  29. required.push_back(AZ_CRC_CE("LyShineService"));
  30. }
  31. ////////////////////////////////////////////////////////////////////////////////////////////////
  32. void VirtualGamepadSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  33. {
  34. AZ_UNUSED(dependent);
  35. }
  36. ////////////////////////////////////////////////////////////////////////////////////////////////
  37. void VirtualGamepadSystemComponent::Reflect(AZ::ReflectContext* context)
  38. {
  39. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  40. {
  41. serialize->Class<VirtualGamepadSystemComponent, AZ::Component>()
  42. ->Version(0)
  43. ->Field("ButtonNames", &VirtualGamepadSystemComponent::m_buttonNames)
  44. ->Field("ThumbStickNames", &VirtualGamepadSystemComponent::m_thumbStickNames);
  45. if (AZ::EditContext* ec = serialize->GetEditContext())
  46. {
  47. ec->Class<VirtualGamepadSystemComponent>("VirtualGamepad", "Provides an example of a virtual gamepad that can be used by mobile devices with touch screens in place of a physical gamepad.")
  48. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  49. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  50. ->DataElement(0, &VirtualGamepadSystemComponent::m_buttonNames, "Button Names", "The button names made available by the virtual gamepad.")
  51. ->DataElement(0, &VirtualGamepadSystemComponent::m_thumbStickNames, "Thumb-Stick Names", "The thumb-stick names made available by the virtual gamepad.")
  52. ;
  53. }
  54. }
  55. }
  56. ////////////////////////////////////////////////////////////////////////////////////////////////
  57. VirtualGamepadSystemComponent::VirtualGamepadSystemComponent()
  58. {
  59. m_buttonNames =
  60. {
  61. "virtual_gamepad_button_a",
  62. "virtual_gamepad_button_b",
  63. "virtual_gamepad_button_x",
  64. "virtual_gamepad_button_y"
  65. };
  66. m_thumbStickNames =
  67. {
  68. "virtual_gamepad_thumbstick_l",
  69. "virtual_gamepad_thumbstick_r"
  70. };
  71. }
  72. ////////////////////////////////////////////////////////////////////////////////////////////////
  73. void VirtualGamepadSystemComponent::Init()
  74. {
  75. }
  76. ////////////////////////////////////////////////////////////////////////////////////////////////
  77. void VirtualGamepadSystemComponent::Activate()
  78. {
  79. VirtualGamepadRequestBus::Handler::BusConnect();
  80. m_virtualGamepad.reset(aznew InputDeviceVirtualGamepad(m_buttonNames,
  81. m_thumbStickNames));
  82. }
  83. ////////////////////////////////////////////////////////////////////////////////////////////////
  84. void VirtualGamepadSystemComponent::Deactivate()
  85. {
  86. m_virtualGamepad.reset(nullptr);
  87. VirtualGamepadRequestBus::Handler::BusDisconnect();
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////////////////////
  90. const AZStd::unordered_set<AZStd::string>& VirtualGamepadSystemComponent::GetButtonNames() const
  91. {
  92. return m_buttonNames;
  93. }
  94. ////////////////////////////////////////////////////////////////////////////////////////////////
  95. const AZStd::unordered_set<AZStd::string>& VirtualGamepadSystemComponent::GetThumbStickNames() const
  96. {
  97. return m_thumbStickNames;
  98. }
  99. } // namespace VirtualGamepad