ManualControlEventHandler.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #pragma once
  9. #include <AzCore/std/function/function_template.h>
  10. #include <ROS2Controllers/VehicleDynamics/VehicleInputControlBus.h>
  11. #include <StartingPointInput/InputEventNotificationBus.h>
  12. namespace ROS2Controllers::VehicleDynamics
  13. {
  14. //! A handler for a single input event.
  15. class ManualControlSingleEventHandler : private StartingPointInput::InputEventNotificationBus::Handler
  16. {
  17. public:
  18. using OnHeldHandlerFunction = AZStd::function<void(float)>;
  19. //! Construct the event handler.
  20. //! @param eventName which event to handle (eg "steering", "accelerate")
  21. //! @param handler a function which handles the input, typically through re-publishing it to a vehicle input bus.
  22. ManualControlSingleEventHandler(const AZStd::string& eventName, OnHeldHandlerFunction handler)
  23. : m_eventName(eventName)
  24. , m_handler(AZStd::move(handler))
  25. {
  26. }
  27. void Activate()
  28. {
  29. StartingPointInput::InputEventNotificationBus::Handler::BusConnect(
  30. StartingPointInput::InputEventNotificationId(m_eventName.c_str()));
  31. }
  32. void Deactivate()
  33. {
  34. StartingPointInput::InputEventNotificationBus::Handler::BusDisconnect(
  35. StartingPointInput::InputEventNotificationId(m_eventName.c_str()));
  36. }
  37. private:
  38. AZStd::string m_eventName;
  39. OnHeldHandlerFunction m_handler;
  40. void OnPressed(float value) override
  41. {
  42. m_handler(value);
  43. }
  44. void OnHeld(float value) override
  45. {
  46. m_handler(value);
  47. }
  48. void OnReleased([[maybe_unused]] float value) override
  49. {
  50. m_handler(0);
  51. }
  52. };
  53. //! Registers to "steering" and "acceleration" input events, and translates them into vehicle inputs
  54. class ManualControlEventHandler
  55. {
  56. public:
  57. void Activate(AZ::EntityId ownerEntityId)
  58. {
  59. m_eventHandlers.push_back(ManualControlSingleEventHandler(
  60. "steering",
  61. [ownerEntityId](float inputValue)
  62. {
  63. VehicleInputControlRequestBus::Event(
  64. ownerEntityId, &VehicleInputControlRequests::SetTargetSteeringFraction, inputValue);
  65. }));
  66. m_eventHandlers.push_back(ManualControlSingleEventHandler(
  67. "accelerate",
  68. [ownerEntityId](float inputValue)
  69. {
  70. VehicleInputControlRequestBus::Event(
  71. ownerEntityId, &VehicleInputControlRequests::SetTargetLinearSpeedFraction, inputValue);
  72. }));
  73. m_eventHandlers.push_back(ManualControlSingleEventHandler(
  74. "rotate",
  75. [ownerEntityId](float inputValue)
  76. {
  77. VehicleInputControlRequestBus::Event(
  78. ownerEntityId, &VehicleInputControlRequests::SetTargetAngularSpeedFraction, inputValue);
  79. }));
  80. for (auto& handler : m_eventHandlers)
  81. {
  82. handler.Activate();
  83. }
  84. }
  85. void Deactivate()
  86. {
  87. for (auto& handler : m_eventHandlers)
  88. {
  89. handler.Deactivate();
  90. }
  91. }
  92. private:
  93. AZStd::vector<ManualControlSingleEventHandler> m_eventHandlers;
  94. };
  95. } // namespace ROS2Controllers::VehicleDynamics