ReflectScriptableEvents.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "Events/ReflectScriptableEvents.h"
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Script/ScriptContext.h>
  12. #include <AzCore/Script/ScriptContextAttributes.h>
  13. #include <LmbrCentral/Scripting/GameplayNotificationBus.h>
  14. #include <AzCore/Math/Vector3.h>
  15. #include <LmbrCentral/Shape/ShapeComponentBus.h>
  16. #include <AzCore/Math/Transform.h>
  17. #include <AzCore/Math/Quaternion.h>
  18. namespace LmbrCentral
  19. {
  20. /// BahaviorContext forwarder for FloatGameplayNotificationBus
  21. class BehaviorGameplayNotificationBusHandler : public AZ::GameplayNotificationBus::Handler, public AZ::BehaviorEBusHandler
  22. {
  23. public:
  24. AZ_EBUS_BEHAVIOR_BINDER(BehaviorGameplayNotificationBusHandler, "{227DCFE6-B527-4FED-8A4D-5D723B07EAA5}", AZ::SystemAllocator,
  25. OnEventBegin, OnEventUpdating, OnEventEnd);
  26. void OnEventBegin(const AZStd::any& value) override
  27. {
  28. Call(FN_OnEventBegin, value);
  29. }
  30. void OnEventUpdating(const AZStd::any& value) override
  31. {
  32. Call(FN_OnEventUpdating, value);
  33. }
  34. void OnEventEnd(const AZStd::any& value) override
  35. {
  36. Call(FN_OnEventEnd, value);
  37. }
  38. };
  39. class MathUtils
  40. {
  41. public:
  42. AZ_TYPE_INFO(MathUtils, "{BB7F7465-B355-4435-BB9D-44D8F586EE8B}");
  43. AZ_CLASS_ALLOCATOR(MathUtils, AZ::SystemAllocator);
  44. MathUtils() = default;
  45. ~MathUtils() = default;
  46. };
  47. class AxisWrapper
  48. {
  49. public:
  50. AZ_TYPE_INFO(AxisWrapper, "{86817913-7D0C-4883-8EDC-2B0DE643392B}");
  51. AZ_CLASS_ALLOCATOR(AxisWrapper, AZ::SystemAllocator);
  52. AxisWrapper() = default;
  53. ~AxisWrapper() = default;
  54. };
  55. void GameplayEventIdNonIntrusiveConstructor(AZ::GameplayNotificationId* outData, AZ::ScriptDataContext& dc)
  56. {
  57. static const int s_channelIndex = 0;
  58. static const int s_actionNameIndex = 1;
  59. static const int s_payloadTypeIndex = 2;
  60. static const int s_defaultConstructorArgCount = 0;
  61. static const int s_deprecatedConstructorArgCount = 2;
  62. static const int s_verboseConstructorArgCount = 3;
  63. static const bool s_showCallStack = true;
  64. if (dc.GetNumArguments() == s_defaultConstructorArgCount)
  65. {
  66. // Use defaults.
  67. outData->m_channel.SetInvalid();
  68. outData->m_actionNameCrc = 0;
  69. outData->m_payloadTypeId = AZ::Uuid::CreateNull();
  70. }
  71. else if (dc.GetNumArguments() == s_deprecatedConstructorArgCount
  72. && dc.IsClass<AZ::EntityId>(s_channelIndex)
  73. && dc.IsString(s_actionNameIndex))
  74. {
  75. AZ::EntityId channel(0);
  76. dc.ReadArg(s_channelIndex, channel);
  77. outData->m_channel = channel;
  78. const char* actionName = nullptr;
  79. dc.ReadArg(s_actionNameIndex, actionName);
  80. outData->m_actionNameCrc = AZ_CRC(actionName);
  81. outData->m_payloadTypeId = AZ::Uuid::CreateNull();
  82. AZ::ScriptContext::FromNativeContext(dc.GetNativeContext())->Error(
  83. AZ::ScriptContext::ErrorType::Warning,
  84. s_showCallStack,
  85. "This constructor has been deprecated. Please add the name of the type you wish to send/receive, example \"float\""
  86. );
  87. }
  88. else if (dc.GetNumArguments() == s_verboseConstructorArgCount
  89. && dc.IsClass<AZ::EntityId>(s_channelIndex)
  90. && (dc.IsString(s_actionNameIndex) || dc.IsClass<AZ::Crc32>(s_actionNameIndex))
  91. && (dc.IsString(s_payloadTypeIndex) || dc.IsClass<AZ::Crc32>(s_payloadTypeIndex) || dc.IsClass<AZ::Uuid>(s_payloadTypeIndex)))
  92. {
  93. dc.ReadArg(s_channelIndex, outData->m_channel);
  94. if (dc.IsString(s_actionNameIndex))
  95. {
  96. const char* actionName = nullptr;
  97. dc.ReadArg(s_actionNameIndex, actionName);
  98. outData->m_actionNameCrc = AZ_CRC(actionName);
  99. }
  100. else
  101. {
  102. dc.ReadArg(s_actionNameIndex, outData->m_actionNameCrc);
  103. }
  104. if (dc.IsClass<AZ::Uuid>(s_payloadTypeIndex))
  105. {
  106. dc.ReadArg(s_payloadTypeIndex, outData->m_payloadTypeId);
  107. }
  108. else
  109. {
  110. AZ::BehaviorContext* behaviorContext = nullptr;
  111. AZ::ComponentApplicationBus::BroadcastResult(behaviorContext, &AZ::ComponentApplicationBus::Events::GetBehaviorContext);
  112. const char* payloadClassName = nullptr;
  113. if (dc.IsString(s_payloadTypeIndex))
  114. {
  115. dc.ReadArg(s_payloadTypeIndex, payloadClassName);
  116. // Specifically handling float for gameplay event bus support.
  117. // LuaNumber is a double, so typeid(1) will return double, and fundamental numerics are not exposed to BehaviorContext.
  118. // This is case sensitive since it is possible to have float and Float and FlOaT, etc.
  119. if (strcmp(payloadClassName, "float") == 0)
  120. {
  121. outData->m_payloadTypeId = AZ::AzTypeInfo<float>::Uuid();
  122. }
  123. else
  124. {
  125. auto&& behaviorClassIterator = behaviorContext->m_classes.find(payloadClassName);
  126. if (behaviorClassIterator != behaviorContext->m_classes.end())
  127. {
  128. outData->m_payloadTypeId = behaviorClassIterator->second->m_typeId;
  129. }
  130. else
  131. {
  132. AZ::ScriptContext::FromNativeContext(dc.GetNativeContext())->Error(
  133. AZ::ScriptContext::ErrorType::Warning,
  134. s_showCallStack,
  135. "Class \"%s\" not found in behavior context. Ensure your type is reflected to behavior context or consider using typeid(type).",
  136. payloadClassName
  137. );
  138. }
  139. }
  140. }
  141. else
  142. {
  143. AZ::Crc32 requestedCrc;
  144. dc.ReadArg(s_payloadTypeIndex, requestedCrc);
  145. AZ::ScriptContext::FromNativeContext(dc.GetNativeContext())->Error(
  146. AZ::ScriptContext::ErrorType::Warning,
  147. s_showCallStack,
  148. "Constructing a GameplayNotificationId with a Crc32 for payload type is expensive. Consider using string name or typeid instead."
  149. );
  150. for (auto&& behaviorClassPair : behaviorContext->m_classes)
  151. {
  152. AZ::Crc32 currentBehaviorClassCrc = AZ::Crc32(behaviorClassPair.first.c_str());
  153. if (currentBehaviorClassCrc == requestedCrc)
  154. {
  155. outData->m_payloadTypeId = behaviorClassPair.second->m_typeId;
  156. }
  157. }
  158. }
  159. }
  160. }
  161. else
  162. {
  163. AZ::ScriptContext::FromNativeContext(dc.GetNativeContext())->Error(
  164. AZ::ScriptContext::ErrorType::Error,
  165. s_showCallStack,
  166. "The GameplayNotificationId takes 3 arguments: an entityId representing the channel, a string or crc representing the action's name, and a string or uuid for the type"
  167. );
  168. }
  169. }
  170. void ReflectScriptableEvents::Reflect(AZ::ReflectContext* context)
  171. {
  172. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  173. {
  174. serializeContext->Class<AZ::GameplayNotificationId>()
  175. ->Version(1)
  176. ->Field("Channel", &AZ::GameplayNotificationId::m_channel)
  177. ->Field("ActionName", &AZ::GameplayNotificationId::m_actionNameCrc)
  178. ->Field("PayloadType", &AZ::GameplayNotificationId::m_payloadTypeId)
  179. ;
  180. }
  181. ShapeComponentConfig::Reflect(context);
  182. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  183. {
  184. behaviorContext->Class<AZ::GameplayNotificationId>("GameplayNotificationId")
  185. ->Attribute(AZ::Script::Attributes::Deprecated, true)
  186. ->Constructor<AZ::EntityId, AZ::Crc32>()
  187. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  188. ->Attribute(AZ::Script::Attributes::ConstructorOverride, &GameplayEventIdNonIntrusiveConstructor)
  189. ->Property("actionNameCrc", BehaviorValueProperty(&AZ::GameplayNotificationId::m_actionNameCrc))
  190. ->Property("channel", BehaviorValueProperty(&AZ::GameplayNotificationId::m_channel))
  191. ->Property("payloadTypeId", BehaviorValueProperty(&AZ::GameplayNotificationId::m_payloadTypeId))
  192. ->Method("ToString", &AZ::GameplayNotificationId::ToString)
  193. ->Attribute(AZ::Script::Attributes::Operator, AZ::Script::Attributes::OperatorType::ToString)
  194. ->Method("Equal", &AZ::GameplayNotificationId::operator==)
  195. ->Attribute(AZ::Script::Attributes::Operator, AZ::Script::Attributes::OperatorType::Equal)
  196. ->Method("Clone", &AZ::GameplayNotificationId::Clone);
  197. behaviorContext->EBus<AZ::GameplayNotificationBus>("GameplayNotificationBus")
  198. ->Attribute(AZ::Script::Attributes::Deprecated, true)
  199. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::List)
  200. ->Handler<BehaviorGameplayNotificationBusHandler>()
  201. ->Event("OnEventBegin", &AZ::GameplayNotificationBus::Events::OnEventBegin)
  202. ->Event("OnEventUpdating", &AZ::GameplayNotificationBus::Events::OnEventUpdating)
  203. ->Event("OnEventEnd", &AZ::GameplayNotificationBus::Events::OnEventEnd);
  204. behaviorContext->Class<AxisWrapper>("AxisType")
  205. ->Constant("XPositive", BehaviorConstant(AZ::Transform::Axis::XPositive))
  206. ->Constant("XNegative", BehaviorConstant(AZ::Transform::Axis::XNegative))
  207. ->Constant("YPositive", BehaviorConstant(AZ::Transform::Axis::YPositive))
  208. ->Constant("YNegative", BehaviorConstant(AZ::Transform::Axis::YNegative))
  209. ->Constant("ZPositive", BehaviorConstant(AZ::Transform::Axis::ZPositive))
  210. ->Constant("ZNegative", BehaviorConstant(AZ::Transform::Axis::ZNegative));
  211. behaviorContext->Class<MathUtils>("MathUtils")
  212. ->Method("ConvertTransformToEulerDegrees", &AZ::ConvertTransformToEulerDegrees)
  213. ->Method("ConvertTransformToEulerRadians", &AZ::ConvertTransformToEulerRadians)
  214. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
  215. ->Method("ConvertEulerDegreesToTransform", &AZ::ConvertEulerDegreesToTransform)
  216. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
  217. ->Method("ConvertEulerDegreesToTransformPrecise", &AZ::ConvertEulerDegreesToTransform)
  218. ->Method("ConvertQuaternionToEulerDegrees", &AZ::ConvertQuaternionToEulerDegrees)
  219. ->Method("ConvertQuaternionToEulerRadians", &AZ::ConvertQuaternionToEulerRadians)
  220. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
  221. ->Method("ConvertEulerRadiansToQuaternion", &AZ::ConvertEulerRadiansToQuaternion)
  222. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
  223. ->Method("ConvertEulerDegreesToQuaternion", &AZ::ConvertEulerDegreesToQuaternion)
  224. ->Method("CreateLookAt", &AZ::Transform::CreateLookAt);
  225. ShapeComponentGeneric::Reflect(behaviorContext);
  226. }
  227. }
  228. }