StartingPointInputGem.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "InputConfigurationComponent.h"
  9. #include "InputEventBindings.h"
  10. #include "InputEventMap.h"
  11. #include <AzCore/Module/Module.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <AzCore/Module/Environment.h>
  14. #include <AzCore/Component/Component.h>
  15. #include <AzFramework/Asset/GenericAssetHandler.h>
  16. namespace StartingPointInput
  17. {
  18. static bool ConvertToInputEventMap(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  19. {
  20. // Capture the old values
  21. AZStd::string deviceType;
  22. classElement.GetChildData(AZ::Crc32("Input Device Type"), deviceType);
  23. AZStd::string inputName;
  24. classElement.GetChildData(AZ::Crc32("Input Name"), inputName);
  25. float eventValueMultiplier;
  26. classElement.GetChildData(AZ::Crc32("Event Value Multiplier"), eventValueMultiplier);
  27. float deadZone;
  28. classElement.GetChildData(AZ::Crc32("Dead Zone"), deadZone);
  29. // Convert to the new class
  30. classElement.Convert(context, AZ::AzTypeInfo<InputEventMap>::Uuid());
  31. // Add the old values to the new class
  32. classElement.AddElementWithData(context, "Input Device Type", deviceType);
  33. classElement.AddElementWithData(context, "Input Name", inputName);
  34. classElement.AddElementWithData(context, "Event Value Multiplier", eventValueMultiplier);
  35. classElement.AddElementWithData(context, "Dead Zone", deadZone);
  36. return true;
  37. }
  38. class BehaviorInputEventNotificationBusHandler : public InputEventNotificationBus::Handler, public AZ::BehaviorEBusHandler
  39. {
  40. public:
  41. AZ_EBUS_BEHAVIOR_BINDER(BehaviorInputEventNotificationBusHandler, "{8AAEEB1A-21E2-4D2E-A719-73552D41F506}", AZ::SystemAllocator,
  42. OnPressed, OnHeld, OnReleased);
  43. void OnPressed(float value) override
  44. {
  45. Call(FN_OnPressed, value);
  46. }
  47. void OnHeld(float value) override
  48. {
  49. Call(FN_OnHeld, value);
  50. }
  51. void OnReleased(float value) override
  52. {
  53. Call(FN_OnReleased, value);
  54. }
  55. };
  56. void InputEventNonIntrusiveConstructor(InputEventNotificationId* thisOutPtr, AZ::ScriptDataContext& dc)
  57. {
  58. if (dc.GetNumArguments() == 0)
  59. {
  60. // Use defaults.
  61. }
  62. else if (dc.GetNumArguments() == 1 && dc.IsString(0))
  63. {
  64. thisOutPtr->m_localUserId = AzFramework::LocalUserIdAny;
  65. const char* actionName = nullptr;
  66. dc.ReadArg(0, actionName);
  67. thisOutPtr->m_actionNameCrc = AZ::Crc32(actionName);
  68. }
  69. else if (dc.GetNumArguments() == 2 && dc.IsClass<AZ::Crc32>(0) && dc.IsString(1))
  70. {
  71. AzFramework::LocalUserId localUserId = 0;
  72. dc.ReadArg(0, localUserId);
  73. thisOutPtr->m_localUserId = localUserId;
  74. const char* actionName = nullptr;
  75. dc.ReadArg(1, actionName);
  76. thisOutPtr->m_actionNameCrc = AZ::Crc32(actionName);
  77. }
  78. else
  79. {
  80. AZ_Error("InputEventNotificationId", false, "The InputEventNotificationId takes one or two args. 1 argument: a string representing the input events name (determined by the event group). 2 arguments: a Crc of the profile channel, and a string representing the input event's name");
  81. }
  82. }
  83. class StartingPointInputSystemComponent : public AZ::Component
  84. {
  85. public:
  86. AZ_COMPONENT(StartingPointInputSystemComponent, "{95DE3485-5E51-42A9-899D-433EC3448AA3}");
  87. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  88. {
  89. required.push_back(AZ_CRC("AssetDatabaseService"));
  90. required.push_back(AZ_CRC("AssetCatalogService"));
  91. }
  92. static void Reflect(AZ::ReflectContext* context)
  93. {
  94. InputEventBindingsAsset::Reflect(context);
  95. InputEventBindings::Reflect(context);
  96. InputEventGroup::Reflect(context);
  97. InputEventMap::Reflect(context);
  98. ThumbstickInputEventMap::Reflect(context);
  99. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  100. {
  101. serializeContext->Class<StartingPointInputSystemComponent, AZ::Component>()
  102. ->Version(1)
  103. ;
  104. serializeContext->ClassDeprecate("Input", AZ::Uuid("{546C9EBC-90EF-4F03-891A-0736BE2A487E}"), &ConvertToInputEventMap);
  105. serializeContext->Class<InputEventNotificationId>()
  106. ->Version(1)
  107. ->Field("LocalUserId", &InputEventNotificationId::m_localUserId)
  108. ->Field("ActionName", &InputEventNotificationId::m_actionNameCrc)
  109. ;
  110. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  111. {
  112. editContext->Class<StartingPointInputSystemComponent>(
  113. "Starting point input", "Manages input bindings and events")
  114. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  115. ->Attribute(AZ::Edit::Attributes::Category, "Editor")
  116. ;
  117. }
  118. }
  119. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  120. {
  121. behaviorContext->Class<InputEventNotificationId>("InputEventNotificationId")
  122. ->Constructor<const char*>()
  123. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  124. ->Attribute(AZ::Script::Attributes::ConstructorOverride, &InputEventNonIntrusiveConstructor)
  125. ->Property("actionNameCrc", BehaviorValueProperty(&InputEventNotificationId::m_actionNameCrc))
  126. ->Property("localUserId", BehaviorValueProperty(&InputEventNotificationId::m_localUserId))
  127. ->Method("ToString", &InputEventNotificationId::ToString)
  128. ->Attribute(AZ::Script::Attributes::Operator, AZ::Script::Attributes::OperatorType::ToString)
  129. ->Method("Equal", &InputEventNotificationId::operator==)
  130. ->Attribute(AZ::Script::Attributes::Operator, AZ::Script::Attributes::OperatorType::Equal)
  131. ->Method("Clone", &InputEventNotificationId::Clone)
  132. ->Property("actionName", nullptr, [](InputEventNotificationId* thisPtr, AZStd::string_view value) { *thisPtr = InputEventNotificationId(value.data()); })
  133. ->Method("CreateInputEventNotificationId", [](AzFramework::LocalUserId localUserId, AZStd::string_view value) -> InputEventNotificationId { return InputEventNotificationId(localUserId, value.data()); },
  134. { { { "localUserId", "Local User ID" },
  135. { "actionName", "The name of the Input event action used to create an InputEventNotificationId" } } });
  136. behaviorContext->EBus<InputEventNotificationBus>("InputEventNotificationBus")
  137. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::List)
  138. ->Handler<BehaviorInputEventNotificationBusHandler>()
  139. ->Event("OnPressed", &InputEventNotificationBus::Events::OnPressed)
  140. ->Event("OnHeld", &InputEventNotificationBus::Events::OnHeld)
  141. ->Event("OnReleased", &InputEventNotificationBus::Events::OnReleased);
  142. }
  143. }
  144. void Init() override
  145. {
  146. }
  147. void Activate() override
  148. {
  149. // Register asset handlers. Requires "AssetDatabaseService"
  150. AZ_Assert(AZ::Data::AssetManager::IsReady(), "Asset manager isn't ready!");
  151. m_inputEventBindingsAssetHandler = aznew AzFramework::GenericAssetHandler<InputEventBindingsAsset>("Input Bindings", "Other", "inputbindings", AZ::AzTypeInfo<InputConfigurationComponent>::Uuid());
  152. m_inputEventBindingsAssetHandler->Register();
  153. }
  154. void Deactivate() override
  155. {
  156. delete m_inputEventBindingsAssetHandler;
  157. m_inputEventBindingsAssetHandler = nullptr;
  158. }
  159. private:
  160. AzFramework::GenericAssetHandler<InputEventBindingsAsset>* m_inputEventBindingsAssetHandler = nullptr;
  161. };
  162. class StartingPointInputModule
  163. : public AZ::Module
  164. {
  165. public:
  166. AZ_RTTI(StartingPointInputModule, "{B30D421E-127D-4C46-90B1-AC3DDF3EC1D9}", AZ::Module);
  167. StartingPointInputModule()
  168. : AZ::Module()
  169. {
  170. m_descriptors.insert(m_descriptors.end(), {
  171. InputConfigurationComponent::CreateDescriptor(),
  172. StartingPointInputSystemComponent::CreateDescriptor(),
  173. });
  174. }
  175. AZ::ComponentTypeList GetRequiredSystemComponents() const override
  176. {
  177. return AZ::ComponentTypeList({ StartingPointInputSystemComponent::RTTI_Type() });
  178. }
  179. };
  180. }
  181. #if defined(O3DE_GEM_NAME)
  182. AZ_DECLARE_MODULE_CLASS(AZ_JOIN(Gem_, O3DE_GEM_NAME), StartingPointInput::StartingPointInputModule)
  183. #else
  184. AZ_DECLARE_MODULE_CLASS(Gem_StartingPointInput, StartingPointInput::StartingPointInputModule)
  185. #endif