3
0

SequenceAgent.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 "SequenceAgent.h"
  9. #include <AzCore/Math/Color.h>
  10. #include <AzCore/Component/ComponentApplicationBus.h>
  11. #include <AzCore/Component/Entity.h>
  12. namespace Maestro
  13. {
  14. namespace SequenceAgentHelper
  15. {
  16. template <typename T>
  17. AZ_INLINE void DoSafeSet(AZ::BehaviorEBus::VirtualProperty* prop, AZ::EntityId entityId, T& data)
  18. {
  19. if (!prop->m_setter)
  20. {
  21. return;
  22. }
  23. if (prop->m_setter->m_event)
  24. {
  25. prop->m_setter->m_event->Invoke(entityId, data);
  26. }
  27. else if (prop->m_setter->m_broadcast)
  28. {
  29. prop->m_setter->m_broadcast->Invoke(data);
  30. }
  31. }
  32. template <typename T>
  33. AZ_INLINE void DoSafeGet(AZ::BehaviorEBus::VirtualProperty* prop, AZ::EntityId entityId, T& data)
  34. {
  35. if (!prop->m_getter)
  36. {
  37. return;
  38. }
  39. if (prop->m_getter->m_event)
  40. {
  41. prop->m_getter->m_event->InvokeResult(data, entityId);
  42. }
  43. else if (prop->m_getter->m_broadcast)
  44. {
  45. prop->m_getter->m_broadcast->InvokeResult(data);
  46. }
  47. }
  48. }
  49. /////////////////////////////////////////////////////////////////////////////////////////////
  50. void SequenceAgent::CacheAllVirtualPropertiesFromBehaviorContext()
  51. {
  52. AZ::BehaviorContext* behaviorContext = nullptr;
  53. AZ::ComponentApplicationBus::BroadcastResult(behaviorContext, &AZ::ComponentApplicationBus::Events::GetBehaviorContext);
  54. AZ::Entity::ComponentArrayType entityComponents;
  55. GetEntityComponents(entityComponents);
  56. m_addressToBehaviorVirtualPropertiesMap.clear();
  57. for (AZ::Component* component : entityComponents)
  58. {
  59. auto findClassIter = behaviorContext->m_typeToClassMap.find(GetComponentTypeUuid(*component));
  60. if (findClassIter != behaviorContext->m_typeToClassMap.end())
  61. {
  62. AZ::BehaviorClass* behaviorClass = findClassIter->second;
  63. // go through all ebuses for this class and find all virtual properties
  64. for (auto reqBusName = behaviorClass->m_requestBuses.begin(); reqBusName != behaviorClass->m_requestBuses.end(); reqBusName++)
  65. {
  66. auto findBusIter = behaviorContext->m_ebuses.find(*reqBusName);
  67. if (findBusIter != behaviorContext->m_ebuses.end())
  68. {
  69. AZ::BehaviorEBus* behaviorEbus = findBusIter->second;
  70. for (auto virtualPropertyIter = behaviorEbus->m_virtualProperties.begin(); virtualPropertyIter != behaviorEbus->m_virtualProperties.end(); virtualPropertyIter++)
  71. {
  72. Maestro::SequenceComponentRequests::AnimatablePropertyAddress address(component->GetId(), virtualPropertyIter->first);
  73. m_addressToBehaviorVirtualPropertiesMap[address] = &virtualPropertyIter->second;
  74. }
  75. // Check for the GetAssetDuration event is present and cache it
  76. for (auto eventIter = behaviorEbus->m_events.begin(); eventIter != behaviorEbus->m_events.end(); eventIter++)
  77. {
  78. if (eventIter->first == "GetAssetDuration")
  79. {
  80. AZ::BehaviorEBusEventSender* sender = &eventIter->second;
  81. // Sanity check, Shouldn't be able to get here.
  82. if (nullptr == sender)
  83. {
  84. AZ_Error("SequenceAgent", false, "EBus %s, GetAssetDuration sender is null.", behaviorEbus->m_name.c_str());
  85. continue;
  86. }
  87. // Sanity check, m_broadcast should always be valid.
  88. if (nullptr == sender->m_broadcast)
  89. {
  90. AZ_Error("SequenceAgent", false, "EBus %s, GetAssetDuration sender->m_broadcast is null.", behaviorEbus->m_name.c_str());
  91. continue;
  92. }
  93. // GetAssetDuration shoulder return a float.
  94. if (!sender->m_broadcast->HasResult())
  95. {
  96. AZ_Error("SequenceAgent", false, "EBus %s, GetAssetDuration should return result", behaviorEbus->m_name.c_str());
  97. continue;
  98. }
  99. // GetAssetDuration should take an asset id.
  100. if (sender->m_broadcast->GetNumArguments() != 1)
  101. {
  102. AZ_Error("SequenceAgent", false, "EBus %s, GetAssetDuration should take one argument, an asset id.", behaviorEbus->m_name.c_str());
  103. continue;
  104. }
  105. m_addressToGetAssetDurationMap[component->GetId()] = sender;
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. /////////////////////////////////////////////////////////////////////////////////////////////
  114. AZ::Uuid SequenceAgent::GetVirtualPropertyTypeId(const Maestro::SequenceComponentRequests::AnimatablePropertyAddress& animatableAddress) const
  115. {
  116. AZ::Uuid retTypeUuid = AZ::Uuid::CreateNull();
  117. auto findIter = m_addressToBehaviorVirtualPropertiesMap.find(animatableAddress);
  118. if (findIter != m_addressToBehaviorVirtualPropertiesMap.end())
  119. {
  120. if (findIter->second->m_getter->m_event)
  121. {
  122. retTypeUuid = findIter->second->m_getter->m_event->GetResult()->m_typeId;
  123. }
  124. else if(findIter->second->m_getter->m_broadcast)
  125. {
  126. retTypeUuid = findIter->second->m_getter->m_broadcast->GetResult()->m_typeId;
  127. }
  128. }
  129. return retTypeUuid;
  130. }
  131. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  132. bool SequenceAgent::SetAnimatedPropertyValue(AZ::EntityId entityId, const Maestro::SequenceComponentRequests::AnimatablePropertyAddress& animatableAddress, const Maestro::SequenceComponentRequests::AnimatedValue& value)
  133. {
  134. bool changed = false;
  135. const AZ::Uuid propertyTypeId = GetVirtualPropertyTypeId(animatableAddress);
  136. auto findIter = m_addressToBehaviorVirtualPropertiesMap.find(animatableAddress);
  137. if (findIter != m_addressToBehaviorVirtualPropertiesMap.end())
  138. {
  139. if (propertyTypeId == AZ::Vector3::TYPEINFO_Uuid())
  140. {
  141. AZ::Vector3 vector3Value(.0f, .0f, .0f);
  142. value.GetValue(vector3Value); // convert the generic value to a Vector3
  143. SequenceAgentHelper::DoSafeSet(findIter->second, entityId, vector3Value);
  144. changed = true;
  145. }
  146. else if (propertyTypeId == AZ::Color::TYPEINFO_Uuid())
  147. {
  148. AZ::Vector3 vector3Value(.0f, .0f, .0f);
  149. value.GetValue(vector3Value); // convert the generic value to a Vector3
  150. AZ::Color colorValue(AZ::Color::CreateFromVector3(vector3Value));
  151. SequenceAgentHelper::DoSafeSet(findIter->second, entityId, colorValue);
  152. changed = true;
  153. }
  154. else if (propertyTypeId == AZ::Quaternion::TYPEINFO_Uuid())
  155. {
  156. AZ::Quaternion quaternionValue(AZ::Quaternion::CreateIdentity());
  157. value.GetValue(quaternionValue);
  158. SequenceAgentHelper::DoSafeSet(findIter->second, entityId, quaternionValue);
  159. changed = true;
  160. }
  161. else if (propertyTypeId == AZ::AzTypeInfo<bool>::Uuid())
  162. {
  163. bool boolValue = true;
  164. value.GetValue(boolValue); // convert the generic value to a bool
  165. SequenceAgentHelper::DoSafeSet(findIter->second, entityId, boolValue);
  166. changed = true;
  167. }
  168. else if (propertyTypeId == AZ::AzTypeInfo<AZ::s32>::Uuid())
  169. {
  170. AZ::s32 s32value = 0;
  171. value.GetValue(s32value);
  172. findIter->second->m_setter->m_event->Invoke(entityId, s32value);
  173. changed = true;
  174. }
  175. else if (propertyTypeId == AZ::AzTypeInfo<AZ::u32>::Uuid())
  176. {
  177. AZ::u32 u32value = 0;
  178. value.GetValue(u32value);
  179. findIter->second->m_setter->m_event->Invoke(entityId, u32value);
  180. changed = true;
  181. }
  182. else if (propertyTypeId == AZ::AzTypeInfo<AZ::Data::AssetId>::Uuid())
  183. {
  184. AZ::Data::AssetId assetIdValue;
  185. value.GetValue(assetIdValue);
  186. findIter->second->m_setter->m_event->Invoke(entityId, assetIdValue);
  187. changed = true;
  188. }
  189. else
  190. {
  191. // fall-through default is to cast to float
  192. float floatValue = .0f;
  193. value.GetValue(floatValue); // convert the generic value to a float
  194. SequenceAgentHelper::DoSafeSet(findIter->second, entityId, floatValue);
  195. changed = true;
  196. }
  197. }
  198. return changed;
  199. }
  200. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  201. void SequenceAgent::GetAnimatedPropertyValue(Maestro::SequenceComponentRequests::AnimatedValue& returnValue, AZ::EntityId entityId, const Maestro::SequenceComponentRequests::AnimatablePropertyAddress& animatableAddress)
  202. {
  203. const AZ::Uuid propertyTypeId = GetVirtualPropertyTypeId(animatableAddress);
  204. auto findIter = m_addressToBehaviorVirtualPropertiesMap.find(animatableAddress);
  205. if (findIter != m_addressToBehaviorVirtualPropertiesMap.end())
  206. {
  207. if (propertyTypeId == AZ::Vector3::TYPEINFO_Uuid())
  208. {
  209. AZ::Vector3 vector3Value(AZ::Vector3::CreateZero());
  210. SequenceAgentHelper::DoSafeGet(findIter->second, entityId, vector3Value);
  211. returnValue.SetValue(vector3Value);
  212. }
  213. else if (propertyTypeId == AZ::Color::TYPEINFO_Uuid())
  214. {
  215. AZ::Color colorValue(AZ::Color::CreateZero());
  216. SequenceAgentHelper::DoSafeGet(findIter->second, entityId, colorValue);
  217. returnValue.SetValue((AZ::Vector3)colorValue);
  218. }
  219. else if (propertyTypeId == AZ::Quaternion::TYPEINFO_Uuid())
  220. {
  221. AZ::Quaternion quaternionValue(AZ::Quaternion::CreateIdentity());
  222. SequenceAgentHelper::DoSafeGet(findIter->second, entityId, quaternionValue);
  223. returnValue.SetValue(quaternionValue);
  224. }
  225. else if (propertyTypeId == AZ::AzTypeInfo<bool>::Uuid())
  226. {
  227. bool boolValue = false;
  228. SequenceAgentHelper::DoSafeGet(findIter->second, entityId, boolValue);
  229. returnValue.SetValue(boolValue);
  230. }
  231. else if (propertyTypeId == AZ::AzTypeInfo<AZ::s32>::Uuid())
  232. {
  233. AZ::s32 s32Value;
  234. findIter->second->m_getter->m_event->InvokeResult(s32Value, entityId);
  235. returnValue.SetValue(s32Value);
  236. }
  237. else if (propertyTypeId == AZ::AzTypeInfo<AZ::u32>::Uuid())
  238. {
  239. AZ::u32 u32Value;
  240. findIter->second->m_getter->m_event->InvokeResult(u32Value, entityId);
  241. returnValue.SetValue(u32Value);
  242. }
  243. else if (propertyTypeId == AZ::AzTypeInfo<AZ::Data::AssetId>::Uuid())
  244. {
  245. AZ::Data::AssetId assetIdValue;
  246. findIter->second->m_getter->m_event->InvokeResult(assetIdValue, entityId);
  247. returnValue.SetValue(assetIdValue);
  248. }
  249. else
  250. {
  251. // fall-through default is to cast to float
  252. float floatValue = .0f;
  253. SequenceAgentHelper::DoSafeGet(findIter->second, entityId, floatValue);
  254. returnValue.SetValue(floatValue);
  255. }
  256. }
  257. }
  258. void SequenceAgent::GetAssetDuration(Maestro::SequenceComponentRequests::AnimatedValue& returnValue, AZ::ComponentId componentId, const AZ::Data::AssetId& assetId)
  259. {
  260. auto findIter = m_addressToGetAssetDurationMap.find(componentId);
  261. if (findIter != m_addressToGetAssetDurationMap.end())
  262. {
  263. if (nullptr != findIter->second)
  264. {
  265. AZ::BehaviorEBusEventSender* sender = findIter->second;
  266. if (nullptr != sender)
  267. {
  268. AZ::BehaviorMethod* broadcast = sender->m_broadcast;
  269. if (nullptr != broadcast)
  270. {
  271. float floatValue = 0.0f;
  272. broadcast->InvokeResult(floatValue, assetId);
  273. returnValue.SetValue(floatValue);
  274. }
  275. }
  276. }
  277. }
  278. }
  279. }// namespace Maestro