3
0

EditorSequenceAgentComponent.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "EditorSequenceAgentComponent.h"
  9. #include "SequenceAgentComponent.h"
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. #include <AzCore/std/containers/set.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <AzCore/Serialization/EditContext.h>
  14. #include <AzCore/Component/ComponentApplicationBus.h>
  15. #include <AzCore/Serialization/SerializeContext.h>
  16. #include <AzFramework/API/ApplicationAPI.h>
  17. #include <AzToolsFramework/ToolsComponents/GenericComponentWrapper.h>
  18. #include <AzCore/Component/Entity.h>
  19. #include <AzToolsFramework/API/EntityCompositionRequestBus.h>
  20. #include <Maestro/Types/AnimParamType.h>
  21. #include <AzToolsFramework/ToolsComponents/EditorDisabledCompositionBus.h>
  22. #include <AzToolsFramework/ToolsComponents/EditorPendingCompositionComponent.h>
  23. #include <AzToolsFramework/Undo/UndoCacheInterface.h>
  24. namespace Maestro
  25. {
  26. void EditorSequenceAgentComponent::Reflect(AZ::ReflectContext* context)
  27. {
  28. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  29. if (serializeContext)
  30. {
  31. serializeContext->Class<EditorSequenceAgentComponent, EditorComponentBase>()
  32. ->Field("SequenceComponentEntityIds", &EditorSequenceAgentComponent::m_sequenceEntityIds)
  33. ->Version(3);
  34. AZ::EditContext* editContext = serializeContext->GetEditContext();
  35. if (editContext)
  36. {
  37. editContext->Class<EditorSequenceAgentComponent>(
  38. "SequenceAgent", "Maps Director Component Animations to Behavior Properties on this Entity")
  39. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  40. ->Attribute(AZ::Edit::Attributes::Category, "Cinematics")
  41. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/SequenceAgent.png")
  42. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/Components/Viewport/SequenceAgent.png")
  43. ->Attribute(AZ::Edit::Attributes::AddableByUser, false) // SequenceAgents are only added by TrackView
  44. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  45. }
  46. }
  47. }
  48. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  49. AZ::TypeId EditorSequenceAgentComponent::GetComponentTypeUuid(const AZ::Component& component) const
  50. {
  51. return AzToolsFramework::GetUnderlyingComponentType(component);
  52. }
  53. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  54. void EditorSequenceAgentComponent::GetEntityComponents(AZ::Entity::ComponentArrayType& entityComponents) const
  55. {
  56. AZ::Entity* entity = GetEntity();
  57. AZ_Assert(entity, "Expected valid entity.");
  58. if (entity)
  59. {
  60. // Add all enabled components
  61. const AZ::Entity::ComponentArrayType& enabledComponents = entity->GetComponents();
  62. for (AZ::Component* component : enabledComponents)
  63. {
  64. entityComponents.push_back(component);
  65. }
  66. // Add all disabled components
  67. AZ::Entity::ComponentArrayType disabledComponents;
  68. AzToolsFramework::EditorDisabledCompositionRequestBus::Event(entity->GetId(), &AzToolsFramework::EditorDisabledCompositionRequests::GetDisabledComponents, disabledComponents);
  69. for (AZ::Component* component : disabledComponents)
  70. {
  71. entityComponents.push_back(component);
  72. }
  73. // Add all pending components
  74. AZ::Entity::ComponentArrayType pendingComponents;
  75. AzToolsFramework::EditorPendingCompositionRequestBus::Event(entity->GetId(), &AzToolsFramework::EditorPendingCompositionRequests::GetPendingComponents, pendingComponents);
  76. for (AZ::Component* component : pendingComponents)
  77. {
  78. entityComponents.push_back(component);
  79. }
  80. }
  81. }
  82. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  83. void EditorSequenceAgentComponent::Activate()
  84. {
  85. // cache pointers and animatable addresses for animation
  86. //
  87. CacheAllVirtualPropertiesFromBehaviorContext();
  88. ConnectAllSequences();
  89. EditorComponentBase::Activate();
  90. // Notify the sequence agent was just connected to the sequence.
  91. Maestro::EditorSequenceAgentComponentNotificationBus::Event(
  92. GetEntityId(),
  93. &Maestro::EditorSequenceAgentComponentNotificationBus::Events::OnSequenceAgentConnected
  94. );
  95. }
  96. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  97. void EditorSequenceAgentComponent::Deactivate()
  98. {
  99. // invalidate all cached pointers and address
  100. m_addressToBehaviorVirtualPropertiesMap.clear();
  101. DisconnectAllSequences();
  102. EditorComponentBase::Deactivate();
  103. }
  104. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  105. void EditorSequenceAgentComponent::ConnectSequence(const AZ::EntityId& sequenceEntityId)
  106. {
  107. // check that we aren't already connected to this SequenceComponent - add it if we aren't
  108. if (m_sequenceEntityIds.find(sequenceEntityId) == m_sequenceEntityIds.end())
  109. {
  110. m_sequenceEntityIds.insert(sequenceEntityId);
  111. // connect to EBus between the given SequenceComponent and me
  112. Maestro::SequenceAgentEventBusId busId(sequenceEntityId, GetEntityId());
  113. EditorSequenceAgentComponentRequestBus::MultiHandler::BusConnect(busId);
  114. SequenceAgentComponentRequestBus::MultiHandler::BusConnect(busId);
  115. }
  116. }
  117. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  118. void EditorSequenceAgentComponent::DisconnectSequence()
  119. {
  120. const Maestro::SequenceAgentEventBusId* busIdToDisconnect = SequenceAgentComponentRequestBus::GetCurrentBusId();
  121. if (!busIdToDisconnect)
  122. {
  123. return;
  124. }
  125. AZ::EntityId sequenceEntityId = busIdToDisconnect->first;
  126. // we only process DisconnectSequence events sent over an ID'ed bus - otherwise we don't know which SequenceComponent to disconnect
  127. [[maybe_unused]] auto findIter = m_sequenceEntityIds.find(sequenceEntityId);
  128. AZ_Assert(findIter != m_sequenceEntityIds.end(), "A sequence not connected to SequenceAgentComponent on %s is requesting a disconnection", GetEntity()->GetName().c_str());
  129. m_sequenceEntityIds.erase(sequenceEntityId);
  130. // Disconnect from the bus between the SequenceComponent and me
  131. // Make a copy because calling BusDisconnect destroy the current bus id
  132. const Maestro::SequenceAgentEventBusId busIdToDisconnectCopy = *busIdToDisconnect;
  133. EditorSequenceAgentComponentRequestBus::MultiHandler::BusDisconnect(busIdToDisconnectCopy);
  134. SequenceAgentComponentRequestBus::MultiHandler::BusDisconnect(busIdToDisconnectCopy);
  135. if (m_sequenceEntityIds.size())
  136. {
  137. return;
  138. }
  139. AZ::EntityId curEntityId = GetEntityId();
  140. // remove this SequenceAgent from this entity if no sequenceComponents are connected to it
  141. AzToolsFramework::EntityCompositionRequestBus::Broadcast(&AzToolsFramework::EntityCompositionRequests::RemoveComponents, AZ::Entity::ComponentArrayType{this});
  142. // Let any currently-active undo operations know that this entity has changed state.
  143. auto undoCacheInterface = AZ::Interface<AzToolsFramework::UndoSystem::UndoCacheInterface>::Get();
  144. if (undoCacheInterface)
  145. {
  146. undoCacheInterface->UpdateCache(curEntityId);
  147. }
  148. // CAUTION!
  149. // THIS CLASS INSTANCE IS NOW DEAD DUE TO DELETION BY THE ENTITY DURING RemoveComponents!
  150. }
  151. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  152. void EditorSequenceAgentComponent::ConnectAllSequences()
  153. {
  154. // Connect all buses
  155. for (auto iter = m_sequenceEntityIds.begin(); iter != m_sequenceEntityIds.end(); iter++)
  156. {
  157. Maestro::SequenceAgentEventBusId busIdToConnect(*iter, GetEntityId());
  158. EditorSequenceAgentComponentRequestBus::MultiHandler::BusConnect(busIdToConnect);
  159. SequenceAgentComponentRequestBus::MultiHandler::BusConnect(busIdToConnect);
  160. }
  161. }
  162. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  163. void EditorSequenceAgentComponent::DisconnectAllSequences()
  164. {
  165. // disconnect all buses
  166. for (auto iter = m_sequenceEntityIds.begin(); iter != m_sequenceEntityIds.end(); iter++)
  167. {
  168. Maestro::SequenceAgentEventBusId busIdToDisconnect(*iter, GetEntityId());
  169. EditorSequenceAgentComponentRequestBus::MultiHandler::BusDisconnect(busIdToDisconnect);
  170. SequenceAgentComponentRequestBus::MultiHandler::BusDisconnect(busIdToDisconnect);
  171. }
  172. }
  173. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  174. void EditorSequenceAgentComponent::BuildGameEntity(AZ::Entity* gameEntity)
  175. {
  176. SequenceAgentComponent *sequenceAgentComponent = gameEntity->CreateComponent<SequenceAgentComponent>();
  177. if (sequenceAgentComponent)
  178. {
  179. // TODO: when we have code which only allows animation of properties that are common between the SequenceAgent and EditorSequenceAgent,
  180. // transfer the mappings to the game behaviors here
  181. sequenceAgentComponent->m_sequenceEntityIds = m_sequenceEntityIds;
  182. }
  183. }
  184. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  185. void EditorSequenceAgentComponent::GetAllAnimatableProperties(IAnimNode::AnimParamInfos& properties, AZ::ComponentId componentId)
  186. {
  187. // add all properties found during Activate() that match the given componentId
  188. for (auto propertyIter = m_addressToBehaviorVirtualPropertiesMap.begin(); propertyIter != m_addressToBehaviorVirtualPropertiesMap.end(); propertyIter++)
  189. {
  190. if (propertyIter->first.GetComponentId() == componentId)
  191. {
  192. AZ::BehaviorEBus::VirtualProperty* virtualProperty = propertyIter->second;
  193. // all behavior properties as string params with the virtual property name as the string
  194. IAnimNode::SParamInfo paramInfo;
  195. // by default set up paramType as an AnimParamType::ByString with the name as the Virtual Property name
  196. paramInfo.paramType = propertyIter->first.GetVirtualPropertyName();
  197. // check for paramType specialization attributes on the getter method of the virtual property. if found, reset
  198. // to the eAnimParamType enum - this leaves the paramType name unchanged but changes the type.
  199. for (int i = static_cast<int>(virtualProperty->m_getter->m_attributes.size()); --i >= 0;)
  200. {
  201. if (virtualProperty->m_getter->m_attributes[i].first == AZ::Edit::Attributes::PropertyPosition)
  202. {
  203. paramInfo.paramType = AnimParamType::Position;
  204. break;
  205. }
  206. else if (virtualProperty->m_getter->m_attributes[i].first == AZ::Edit::Attributes::PropertyRotation)
  207. {
  208. paramInfo.paramType = AnimParamType::Rotation;
  209. break;
  210. }
  211. else if (virtualProperty->m_getter->m_attributes[i].first == AZ::Edit::Attributes::PropertyScale)
  212. {
  213. paramInfo.paramType = AnimParamType::Scale;
  214. break;
  215. }
  216. else if (virtualProperty->m_getter->m_attributes[i].first == AZ::Edit::Attributes::PropertyHidden)
  217. {
  218. paramInfo.flags = static_cast<IAnimNode::ESupportedParamFlags>(paramInfo.flags | IAnimNode::eSupportedParamFlags_Hidden);
  219. break;
  220. }
  221. }
  222. properties.push_back(paramInfo);
  223. }
  224. }
  225. }
  226. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  227. void EditorSequenceAgentComponent::GetAnimatableComponents(AZStd::vector<AZ::ComponentId>& animatableComponentIds)
  228. {
  229. AZStd::set<AZ::ComponentId> appendedComponentIds;
  230. // go through all known properties found during Activate() - insert all unique components found
  231. for (auto address = m_addressToBehaviorVirtualPropertiesMap.begin(); address != m_addressToBehaviorVirtualPropertiesMap.end(); address++)
  232. {
  233. // only append component if it's not already been appended
  234. auto findIter = appendedComponentIds.find(address->first.GetComponentId());
  235. if (findIter == appendedComponentIds.end())
  236. {
  237. animatableComponentIds.push_back(address->first.GetComponentId());
  238. appendedComponentIds.insert(address->first.GetComponentId());
  239. }
  240. }
  241. }
  242. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  243. AZ::Uuid EditorSequenceAgentComponent::GetAnimatedAddressTypeId(const AnimatablePropertyAddress& animatableAddress)
  244. {
  245. return GetVirtualPropertyTypeId(animatableAddress);
  246. }
  247. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  248. void EditorSequenceAgentComponent::GetAnimatedPropertyValue(AnimatedValue& returnValue, const AnimatablePropertyAddress& animatableAddress)
  249. {
  250. SequenceAgent::GetAnimatedPropertyValue(returnValue, GetEntityId(), animatableAddress);
  251. }
  252. bool EditorSequenceAgentComponent::SetAnimatedPropertyValue(const AnimatablePropertyAddress& animatableAddress, const AnimatedValue& value)
  253. {
  254. return SequenceAgent::SetAnimatedPropertyValue(GetEntityId(), animatableAddress, value);
  255. }
  256. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  257. void EditorSequenceAgentComponent::GetAssetDuration(AnimatedValue& returnValue, AZ::ComponentId componentId, const AZ::Data::AssetId& assetId)
  258. {
  259. SequenceAgent::GetAssetDuration(returnValue, componentId, assetId);
  260. }
  261. } // namespace Maestro