BarrierInputSystemComponent.cpp 7.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 <BarrierInputSystemComponent.h>
  9. #include <BarrierInputKeyboard.h>
  10. #include <BarrierInputMouse.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/Serialization/EditContextConstants.inl>
  14. #include <AzCore/Console/IConsole.h>
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////
  16. namespace BarrierInput
  17. {
  18. ////////////////////////////////////////////////////////////////////////////////////////////////
  19. template<class T>
  20. void OnBarrierConnectionCVarChanged(const T&)
  21. {
  22. BarrierInputConnectionNotificationBus::Broadcast(&BarrierInputConnectionNotifications::OnBarrierConnectionCVarChanged);
  23. }
  24. ////////////////////////////////////////////////////////////////////////////////////////////////
  25. AZ_CVAR(AZ::CVarFixedString,
  26. barrier_clientScreenName,
  27. "",
  28. OnBarrierConnectionCVarChanged<AZ::CVarFixedString>,
  29. AZ::ConsoleFunctorFlags::DontReplicate,
  30. "The Barrier screen name assigned to this client.");
  31. ////////////////////////////////////////////////////////////////////////////////////////////////
  32. AZ_CVAR(AZ::CVarFixedString,
  33. barrier_serverHostName,
  34. "",
  35. OnBarrierConnectionCVarChanged<AZ::CVarFixedString>,
  36. AZ::ConsoleFunctorFlags::DontReplicate,
  37. "The IP or hostname of the Barrier server to connect to.");
  38. ////////////////////////////////////////////////////////////////////////////////////////////////
  39. AZ_CVAR(AZ::u32,
  40. barrier_connectionPort,
  41. BarrierClient::DEFAULT_BARRIER_CONNECTION_PORT_NUMBER,
  42. OnBarrierConnectionCVarChanged<AZ::u32>,
  43. AZ::ConsoleFunctorFlags::DontReplicate,
  44. "The port number over which to connect to the Barrier server.");
  45. ////////////////////////////////////////////////////////////////////////////////////////////////
  46. void BarrierInputSystemComponent::Reflect(AZ::ReflectContext* context)
  47. {
  48. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  49. {
  50. serialize->Class<BarrierInputSystemComponent, AZ::Component>()
  51. ->Version(0);
  52. if (AZ::EditContext* ec = serialize->GetEditContext())
  53. {
  54. ec->Class<BarrierInputSystemComponent>("BarrierInput", "Provides functionality related to Barrier input.")
  55. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  56. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  57. ;
  58. }
  59. }
  60. }
  61. ////////////////////////////////////////////////////////////////////////////////////////////////
  62. void BarrierInputSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  63. {
  64. provided.push_back(AZ_CRC("BarrierInputService"));
  65. }
  66. ////////////////////////////////////////////////////////////////////////////////////////////////
  67. void BarrierInputSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  68. {
  69. incompatible.push_back(AZ_CRC("BarrierInputService"));
  70. }
  71. ////////////////////////////////////////////////////////////////////////////////////////////////
  72. void BarrierInputSystemComponent::Activate()
  73. {
  74. TryCreateBarrierClientAndInputDeviceImplementations();
  75. BarrierInputConnectionNotificationBus::Handler::BusConnect();
  76. }
  77. ////////////////////////////////////////////////////////////////////////////////////////////////
  78. void BarrierInputSystemComponent::Deactivate()
  79. {
  80. BarrierInputConnectionNotificationBus::Handler::BusDisconnect();
  81. DestroyBarrierClientAndInputDeviceImplementations();
  82. }
  83. ////////////////////////////////////////////////////////////////////////////////////////////////
  84. void BarrierInputSystemComponent::OnBarrierConnectionCVarChanged()
  85. {
  86. TryCreateBarrierClientAndInputDeviceImplementations();
  87. }
  88. ////////////////////////////////////////////////////////////////////////////////////////////////
  89. void BarrierInputSystemComponent::TryCreateBarrierClientAndInputDeviceImplementations()
  90. {
  91. // Destroy any existing Barrier client and input device implementations.
  92. DestroyBarrierClientAndInputDeviceImplementations();
  93. const AZ::CVarFixedString barrierClientScreenNameCVar = static_cast<AZ::CVarFixedString>(barrier_clientScreenName);
  94. const AZ::CVarFixedString barrierServerHostNameCVar = static_cast<AZ::CVarFixedString>(barrier_serverHostName);
  95. const AZ::u32 barrierConnectionPort = static_cast<AZ::u32>(barrier_connectionPort);
  96. if (!barrierClientScreenNameCVar.empty() && !barrierServerHostNameCVar.empty() && barrierConnectionPort)
  97. {
  98. // Enable the Barrier keyboard/mouse input device implementations.
  99. AzFramework::InputDeviceImplementationRequest<AzFramework::InputDeviceKeyboard>::Bus::Event(
  100. AzFramework::InputDeviceKeyboard::Id,
  101. &AzFramework::InputDeviceImplementationRequest<AzFramework::InputDeviceKeyboard>::SetCustomImplementation,
  102. BarrierInput::InputDeviceKeyboardBarrier::Create);
  103. AzFramework::InputDeviceImplementationRequest<AzFramework::InputDeviceMouse>::Bus::Event(
  104. AzFramework::InputDeviceMouse::Id,
  105. &AzFramework::InputDeviceImplementationRequest<AzFramework::InputDeviceMouse>::SetCustomImplementation,
  106. BarrierInput::InputDeviceMouseBarrier::Create);
  107. // Create the Barrier client instance.
  108. m_barrierClient = AZStd::make_unique<BarrierClient>(barrierClientScreenNameCVar.c_str(), barrierServerHostNameCVar.c_str(), barrierConnectionPort);
  109. }
  110. }
  111. ////////////////////////////////////////////////////////////////////////////////////////////////
  112. void BarrierInputSystemComponent::DestroyBarrierClientAndInputDeviceImplementations()
  113. {
  114. if (m_barrierClient)
  115. {
  116. // Destroy the Barrier client instance.
  117. m_barrierClient.reset();
  118. // Reset to the default keyboard/mouse input device implementations.
  119. AzFramework::InputDeviceImplementationRequest<AzFramework::InputDeviceKeyboard>::Bus::Event(
  120. AzFramework::InputDeviceKeyboard::Id,
  121. &AzFramework::InputDeviceImplementationRequest<AzFramework::InputDeviceKeyboard>::SetCustomImplementation,
  122. AzFramework::InputDeviceKeyboard::Implementation::Create);
  123. AzFramework::InputDeviceImplementationRequest<AzFramework::InputDeviceMouse>::Bus::Event(
  124. AzFramework::InputDeviceMouse::Id,
  125. &AzFramework::InputDeviceImplementationRequest<AzFramework::InputDeviceMouse>::SetCustomImplementation,
  126. AzFramework::InputDeviceMouse::Implementation::Create);
  127. }
  128. }
  129. } // namespace BarrierInput