EditorComponentAPIComponent.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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 <AzToolsFramework/Component/EditorComponentAPIComponent.h>
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/Serialization/Utils.h>
  11. #include <AzToolsFramework/API/EntityCompositionRequestBus.h>
  12. #include <AzToolsFramework/ToolsComponents/EditorDisabledCompositionBus.h>
  13. #include <AzToolsFramework/ToolsComponents/EditorPendingCompositionBus.h>
  14. #include <AzToolsFramework/Entity/EditorEntityActionComponent.h>
  15. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  16. #include <AzToolsFramework/UI/PropertyEditor/InstanceDataHierarchy.h>
  17. #include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
  18. namespace AzToolsFramework
  19. {
  20. namespace Components
  21. {
  22. //! Helper class for scripting.
  23. //! Use it to provide the right enum value when
  24. //! calling EditorComponentAPIRequests::BuildComponentTypeNameListByEntityType or EditorComponentAPIRequests::FindComponentTypeIdsByEntityType
  25. //! example of Python code:
  26. //! #-------------------------------------------------------------------
  27. //! import azlmbr.bus as bus
  28. //! import azlmbr.editor as editor
  29. //! from azlmbr.entity import EntityType
  30. //! levelComponentsList = editor.EditorComponentAPIBus(bus.Broadcast, 'BuildComponentTypeNameListByEntityType', EntityType().Level)
  31. //! gameComponentsList = editor.EditorComponentAPIBus(bus.Broadcast, 'BuildComponentTypeNameListByEntityType', EntityType().Game)
  32. //! #-------------------------------------------------------------------
  33. class EditorEntityType final
  34. {
  35. public:
  36. AZ_CLASS_ALLOCATOR(EditorEntityType, AZ::SystemAllocator, 0);
  37. AZ_RTTI(EditorEntityType, "{9761CD58-D86E-4EA1-AE67-5302AECD54A4}");
  38. EditorEntityType() = default;
  39. ~EditorEntityType() = default;
  40. static void ReflectContext(AZ::ReflectContext* context)
  41. {
  42. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  43. {
  44. behaviorContext->Class<EditorEntityType>("EntityType")
  45. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  46. ->Attribute(AZ::Script::Attributes::Category, "Components")
  47. ->Attribute(AZ::Script::Attributes::Module, "entity")
  48. ->Constructor()
  49. ->Constant("Game", &EditorEntityType::Game)
  50. ->Constant("System", &EditorEntityType::System)
  51. ->Constant("Layer", &EditorEntityType::Layer)
  52. ->Constant("Level", &EditorEntityType::Level)
  53. ;
  54. }
  55. }
  56. EditorComponentAPIRequests::EntityType Game() { return EditorComponentAPIRequests::EntityType::Game; }
  57. EditorComponentAPIRequests::EntityType System() { return EditorComponentAPIRequests::EntityType::System; }
  58. EditorComponentAPIRequests::EntityType Layer() { return EditorComponentAPIRequests::EntityType::Layer; }
  59. EditorComponentAPIRequests::EntityType Level() { return EditorComponentAPIRequests::EntityType::Level; }
  60. };
  61. void EditorComponentAPIComponent::Reflect(AZ::ReflectContext* context)
  62. {
  63. EditorEntityType::ReflectContext(context);
  64. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  65. {
  66. serializeContext->Class<EditorComponentAPIComponent, AZ::Component>();
  67. serializeContext->RegisterGenericType<AZStd::vector<AZ::EntityComponentIdPair>>();
  68. serializeContext->RegisterGenericType<AZStd::vector<AZ::ComponentServiceType>>();
  69. }
  70. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  71. {
  72. behaviorContext->Class<AZ::EntityComponentIdPair>("EntityComponentIdPair")
  73. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  74. ->Attribute(AZ::Script::Attributes::Category, "Components")
  75. ->Attribute(AZ::Script::Attributes::Module, "entity")
  76. ->Method("GetEntityId", &AZ::EntityComponentIdPair::GetEntityId)
  77. ->Attribute(AZ::Script::Attributes::Alias, "get_entity_id")
  78. ->Method("Equal", &AZ::EntityComponentIdPair::operator==)
  79. ->Attribute(AZ::Script::Attributes::Operator, AZ::Script::Attributes::OperatorType::Equal)
  80. ->Method("ToString", [](const AZ::EntityComponentIdPair* self) {
  81. return AZStd::string::format("[ %s - %s ]", self->GetEntityId().ToString().c_str(), AZStd::to_string(self->GetComponentId()).c_str());
  82. })
  83. ->Attribute(AZ::Script::Attributes::Alias, "to_string")
  84. ;
  85. behaviorContext->EBus<EditorComponentAPIBus>("EditorComponentAPIBus")
  86. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  87. ->Attribute(AZ::Script::Attributes::Category, "Components")
  88. ->Attribute(AZ::Script::Attributes::Module, "editor")
  89. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
  90. ->Event("FindComponentTypeIdsByEntityType", &EditorComponentAPIRequests::FindComponentTypeIdsByEntityType)
  91. ->Event("FindComponentTypeIdsByService", &EditorComponentAPIRequests::FindComponentTypeIdsByService)
  92. ->Event("FindComponentTypeNames", &EditorComponentAPIRequests::FindComponentTypeNames)
  93. ->Event("BuildComponentTypeNameListByEntityType", &EditorComponentAPIRequests::BuildComponentTypeNameListByEntityType)
  94. ->Event("AddComponentsOfType", &EditorComponentAPIRequests::AddComponentsOfType)
  95. ->Event("AddComponentOfType", &EditorComponentAPIRequests::AddComponentOfType)
  96. ->Event("HasComponentOfType", &EditorComponentAPIRequests::HasComponentOfType)
  97. ->Event("CountComponentsOfType", &EditorComponentAPIRequests::CountComponentsOfType)
  98. ->Event("GetComponentOfType", &EditorComponentAPIRequests::GetComponentOfType)
  99. ->Event("GetComponentsOfType", &EditorComponentAPIRequests::GetComponentsOfType)
  100. ->Event("IsValid", &EditorComponentAPIRequests::IsValid)
  101. ->Event("EnableComponents", &EditorComponentAPIRequests::EnableComponents)
  102. ->Event("IsComponentEnabled", &EditorComponentAPIRequests::IsComponentEnabled)
  103. ->Event("DisableComponents", &EditorComponentAPIRequests::DisableComponents)
  104. ->Event("RemoveComponents", &EditorComponentAPIRequests::RemoveComponents)
  105. ->Event("BuildComponentPropertyTreeEditor", &EditorComponentAPIRequests::BuildComponentPropertyTreeEditor)
  106. ->Event("GetComponentProperty", &EditorComponentAPIRequests::GetComponentProperty)
  107. ->Event("SetComponentProperty", &EditorComponentAPIRequests::SetComponentProperty)
  108. ->Event("CompareComponentProperty", &EditorComponentAPIRequests::CompareComponentProperty)
  109. ->Event("BuildComponentPropertyList", &EditorComponentAPIRequests::BuildComponentPropertyList)
  110. ->Event("SetVisibleEnforcement", &EditorComponentAPIRequests::SetVisibleEnforcement)
  111. ;
  112. }
  113. }
  114. void EditorComponentAPIComponent::Activate()
  115. {
  116. EditorComponentAPIBus::Handler::BusConnect();
  117. AZ::ComponentApplicationBus::BroadcastResult(m_serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext);
  118. AZ_Error("Editor", m_serializeContext, "Serialize context not available");
  119. }
  120. void EditorComponentAPIComponent::Deactivate()
  121. {
  122. EditorComponentAPIBus::Handler::BusDisconnect();
  123. }
  124. void EditorComponentAPIComponent::SetVisibleEnforcement(bool enforceVisiblity)
  125. {
  126. m_usePropertyVisibility = enforceVisiblity;
  127. }
  128. AZStd::vector<AZ::Uuid> EditorComponentAPIComponent::FindComponentTypeIdsByEntityType(const AZStd::vector<AZStd::string>& componentTypeNames, EditorComponentAPIRequests::EntityType entityType)
  129. {
  130. AZStd::vector<AZ::Uuid> foundTypeIds;
  131. size_t typesCount = componentTypeNames.size();
  132. size_t counter = 0;
  133. foundTypeIds.resize(typesCount, AZ::Uuid::CreateNull());
  134. m_serializeContext->EnumerateDerived<AZ::Component>(
  135. [&counter, typesCount, componentTypeNames, &foundTypeIds, entityType](const AZ::SerializeContext::ClassData* componentClass, const AZ::Uuid& knownType) -> bool
  136. {
  137. (void)knownType;
  138. if (componentClass->m_editData)
  139. {
  140. switch (entityType)
  141. {
  142. case EditorComponentAPIRequests::EntityType::Game:
  143. if (!AzToolsFramework::AppearsInGameComponentMenu(*componentClass))
  144. {
  145. return true;
  146. }
  147. break;
  148. case EditorComponentAPIRequests::EntityType::Level:
  149. if (!AzToolsFramework::AppearsInLevelComponentMenu(*componentClass))
  150. {
  151. return true;
  152. }
  153. break;
  154. case EditorComponentAPIRequests::EntityType::Layer:
  155. if (!AzToolsFramework::AppearsInLayerComponentMenu(*componentClass))
  156. {
  157. return true;
  158. }
  159. break;
  160. default:
  161. if (!AzToolsFramework::AppearsInSystemComponentMenu(*componentClass))
  162. {
  163. return true;
  164. }
  165. break;
  166. }
  167. for (int i = 0; i < typesCount; ++i)
  168. {
  169. if (componentClass->m_editData->m_name == componentTypeNames[i])
  170. {
  171. // Although it is rare, it can happen that two (or more) components can have the same name.
  172. // We should only count the first occurrence, so that none of the names in componentTypeNames
  173. // get skipped, but whichever component type that is encountered last will be the one
  174. // that is captured in order to preserve the pre-existing behavior.
  175. if (foundTypeIds[i].IsNull())
  176. {
  177. ++counter;
  178. }
  179. foundTypeIds[i] = componentClass->m_typeId;
  180. return true;
  181. }
  182. }
  183. if (counter >= typesCount)
  184. {
  185. return false;
  186. }
  187. }
  188. return true;
  189. });
  190. AZ_Warning("EditorComponentAPI", (counter >= typesCount), "FindComponentTypeIds - Not all Type Names provided could be converted to Type Ids.");
  191. return foundTypeIds;
  192. }
  193. AZStd::vector<AZ::Uuid> EditorComponentAPIComponent::FindComponentTypeIdsByService(const AZStd::vector<AZ::ComponentServiceType>& serviceFilter, const AZStd::vector<AZ::ComponentServiceType>& incompatibleServiceFilter)
  194. {
  195. AZStd::vector<AZ::Uuid> foundTypeIds;
  196. m_serializeContext->EnumerateDerived<AZ::Component>(
  197. [&foundTypeIds, serviceFilter, incompatibleServiceFilter](const AZ::SerializeContext::ClassData* componentClass, const AZ::Uuid& knownType) -> bool
  198. {
  199. AZ_UNUSED(knownType);
  200. if (componentClass->m_editData)
  201. {
  202. // If none of the required services are offered by this component, or the component
  203. // can not be added by the user, skip to the next component
  204. if (!OffersRequiredServices(componentClass, serviceFilter, incompatibleServiceFilter))
  205. {
  206. return true;
  207. }
  208. foundTypeIds.push_back(componentClass->m_typeId);
  209. }
  210. return true;
  211. });
  212. return foundTypeIds;
  213. }
  214. AZStd::vector<AZStd::string> EditorComponentAPIComponent::FindComponentTypeNames(const AZ::ComponentTypeList& componentTypeIds)
  215. {
  216. AZStd::vector<AZStd::string> foundTypeNames;
  217. size_t typesCount = componentTypeIds.size();
  218. size_t counter = 0;
  219. foundTypeNames.resize(typesCount);
  220. m_serializeContext->EnumerateDerived<AZ::Component>(
  221. [&counter, typesCount, componentTypeIds, &foundTypeNames](const AZ::SerializeContext::ClassData* componentClass, const AZ::Uuid& knownType) -> bool
  222. {
  223. (void)knownType;
  224. if (componentClass->m_editData)
  225. {
  226. for (int i = 0; i < typesCount; ++i)
  227. {
  228. if (componentClass->m_typeId == componentTypeIds[i])
  229. {
  230. foundTypeNames[i] = componentClass->m_editData->m_name;
  231. ++counter;
  232. }
  233. }
  234. if (counter >= typesCount)
  235. {
  236. return false;
  237. }
  238. }
  239. return true;
  240. });
  241. AZ_Warning("EditorComponentAPI", (counter >= typesCount), "FindComponentTypeNames - Not all Type Ids provided could be converted to Type Names.");
  242. return foundTypeNames;
  243. }
  244. AZStd::vector<AZStd::string> EditorComponentAPIComponent::BuildComponentTypeNameListByEntityType(EditorComponentAPIRequests::EntityType entityType)
  245. {
  246. AZStd::vector<AZStd::string> typeNameList;
  247. m_serializeContext->EnumerateDerived<AZ::Component>(
  248. [&typeNameList, entityType](const AZ::SerializeContext::ClassData* componentClass, const AZ::Uuid& knownType) -> bool
  249. {
  250. AZ_UNUSED(knownType);
  251. if (!componentClass->m_editData)
  252. {
  253. return true;
  254. }
  255. switch (entityType)
  256. {
  257. case EditorComponentAPIRequests::EntityType::Game:
  258. if (AzToolsFramework::AppearsInGameComponentMenu(*componentClass))
  259. {
  260. typeNameList.push_back(componentClass->m_editData->m_name);
  261. }
  262. break;
  263. case EditorComponentAPIRequests::EntityType::Level:
  264. if (AzToolsFramework::AppearsInLevelComponentMenu(*componentClass))
  265. {
  266. typeNameList.push_back(componentClass->m_editData->m_name);
  267. }
  268. break;
  269. case EditorComponentAPIRequests::EntityType::Layer:
  270. if (AzToolsFramework::AppearsInLayerComponentMenu(*componentClass))
  271. {
  272. typeNameList.push_back(componentClass->m_editData->m_name);
  273. }
  274. break;
  275. default:
  276. if (AzToolsFramework::AppearsInSystemComponentMenu(*componentClass))
  277. {
  278. typeNameList.push_back(componentClass->m_editData->m_name);
  279. }
  280. break;
  281. }
  282. return true;
  283. });
  284. return typeNameList;
  285. }
  286. // Returns an Outcome object with the Component Id if successful, and the cause of the failure otherwise
  287. EditorComponentAPIRequests::AddComponentsOutcome EditorComponentAPIComponent::AddComponentsOfType(AZ::EntityId entityId, const AZ::ComponentTypeList& componentTypeIds)
  288. {
  289. EditorEntityActionComponent::AddComponentsOutcome outcome;
  290. EntityCompositionRequestBus::BroadcastResult(outcome, &EntityCompositionRequests::AddComponentsToEntities, EntityIdList{ entityId }, componentTypeIds);
  291. AZ_Warning("EditorComponentAPI", outcome.IsSuccess(), "AddComponentsOfType - AddComponentsToEntities failed (%s).", outcome.GetError().c_str());
  292. if (!outcome.IsSuccess())
  293. {
  294. return AddComponentsOutcome( AZStd::string("AddComponentsOfType - AddComponentsToEntities failed (") + outcome.GetError().c_str() + ")." );
  295. }
  296. auto entityToComponentMap = outcome.GetValue();
  297. if (entityToComponentMap.find(entityId) == entityToComponentMap.end() || entityToComponentMap[entityId].m_componentsAdded.size() == 0)
  298. {
  299. AZ_Warning("EditorComponentAPI", false, "Malformed result from AddComponentsToEntities.");
  300. return AddComponentsOutcome( AZStd::string("Malformed result from AddComponentsToEntities.") );
  301. }
  302. AZStd::vector<AZ::EntityComponentIdPair> componentIds;
  303. for (AZ::Component* component : entityToComponentMap[entityId].m_componentsAdded)
  304. {
  305. if (!component)
  306. {
  307. AZ_Warning("EditorComponentAPI", false, "Invalid component returned in AddComponentsToEntities.");
  308. return AddComponentsOutcome( AZStd::string("Invalid component returned in AddComponentsToEntities.") );
  309. }
  310. else
  311. {
  312. componentIds.push_back(AZ::EntityComponentIdPair(entityId, component->GetId()));
  313. }
  314. }
  315. return AddComponentsOutcome( componentIds );
  316. }
  317. EditorComponentAPIRequests::AddComponentsOutcome EditorComponentAPIComponent::AddComponentOfType(AZ::EntityId entityId, const AZ::Uuid& componentTypeId)
  318. {
  319. return AddComponentsOfType(entityId, { componentTypeId });
  320. }
  321. bool EditorComponentAPIComponent::HasComponentOfType(AZ::EntityId entityId, AZ::Uuid componentTypeId)
  322. {
  323. GetComponentOutcome outcome = GetComponentOfType(entityId, componentTypeId);
  324. return outcome.IsSuccess() && outcome.GetValue().GetComponentId() != AZ::InvalidComponentId;
  325. }
  326. size_t EditorComponentAPIComponent::CountComponentsOfType(AZ::EntityId entityId, AZ::Uuid componentTypeId)
  327. {
  328. AZStd::vector<AZ::Component*> components = FindComponents(entityId, componentTypeId);
  329. return components.size();
  330. }
  331. EditorComponentAPIRequests::GetComponentOutcome EditorComponentAPIComponent::GetComponentOfType(AZ::EntityId entityId, AZ::Uuid componentTypeId)
  332. {
  333. AZ::Component* component = FindComponent(entityId, componentTypeId);
  334. if (component)
  335. {
  336. return GetComponentOutcome( AZ::EntityComponentIdPair(entityId, component->GetId()) );
  337. }
  338. else
  339. {
  340. return GetComponentOutcome( AZStd::string("GetComponentOfType - Component type of id ") + componentTypeId.ToString<AZStd::string>() + " not found on Entity" );
  341. }
  342. }
  343. EditorComponentAPIRequests::GetComponentsOutcome EditorComponentAPIComponent::GetComponentsOfType(AZ::EntityId entityId, AZ::Uuid componentTypeId)
  344. {
  345. AZStd::vector<AZ::Component*> components = FindComponents(entityId, componentTypeId);
  346. if (components.empty())
  347. {
  348. return GetComponentsOutcome( AZStd::string("GetComponentOfType - Component type not found on Entity") );
  349. }
  350. AZStd::vector<AZ::EntityComponentIdPair> componentIds;
  351. componentIds.reserve(components.size());
  352. for (AZ::Component* component : components)
  353. {
  354. componentIds.push_back(AZ::EntityComponentIdPair(entityId, component->GetId()));
  355. }
  356. return {componentIds};
  357. }
  358. bool EditorComponentAPIComponent::IsValid(AZ::EntityComponentIdPair componentInstance)
  359. {
  360. AZ::Component* component = FindComponent(componentInstance.GetEntityId() , componentInstance.GetComponentId());
  361. return component != nullptr;
  362. }
  363. bool EditorComponentAPIComponent::EnableComponents(const AZStd::vector<AZ::EntityComponentIdPair>& componentInstances)
  364. {
  365. AZStd::vector<AZ::Component*> components;
  366. for (const AZ::EntityComponentIdPair& componentInstance : componentInstances)
  367. {
  368. AZ::Component* component = FindComponent(componentInstance.GetEntityId(), componentInstance.GetComponentId());
  369. if (component)
  370. {
  371. components.push_back(component);
  372. }
  373. else
  374. {
  375. AZ_Warning("EditorComponentAPI", false, "EnableComponent failed - could not find Component from the given entityId and componentId.");
  376. return false;
  377. }
  378. }
  379. EntityCompositionRequestBus::Broadcast(&EntityCompositionRequests::EnableComponents, components);
  380. for (const AZ::EntityComponentIdPair& componentInstance : componentInstances)
  381. {
  382. if (!IsComponentEnabled(componentInstance))
  383. {
  384. return false;
  385. }
  386. }
  387. return true;
  388. }
  389. bool EditorComponentAPIComponent::IsComponentEnabled(const AZ::EntityComponentIdPair& componentInstance)
  390. {
  391. // Get AZ::Entity*
  392. AZ::Entity* entityPtr = FindEntity(componentInstance.GetEntityId());
  393. if (!entityPtr)
  394. {
  395. AZ_Warning("EditorComponentAPI", false, "IsComponentEnabled failed - could not find Entity from the given entityId");
  396. return false;
  397. }
  398. // Get Component*
  399. AZ::Component* component = FindComponent(componentInstance.GetEntityId(), componentInstance.GetComponentId());
  400. if (!component)
  401. {
  402. AZ_Warning("EditorComponentAPI", false, "IsComponentEnabled failed - could not find Component from the given entityId and componentId.");
  403. return false;
  404. }
  405. const auto& entityComponents = entityPtr->GetComponents();
  406. if (AZStd::find(entityComponents.begin(), entityComponents.end(), component) != entityComponents.end())
  407. {
  408. return true;
  409. }
  410. return false;
  411. }
  412. bool EditorComponentAPIComponent::DisableComponents(const AZStd::vector<AZ::EntityComponentIdPair>& componentInstances)
  413. {
  414. AZStd::vector<AZ::Component*> components;
  415. for (const AZ::EntityComponentIdPair& componentInstance : componentInstances)
  416. {
  417. AZ::Component* component = FindComponent(componentInstance.GetEntityId(), componentInstance.GetComponentId());
  418. if (component)
  419. {
  420. components.push_back(component);
  421. }
  422. else
  423. {
  424. AZ_Warning("EditorComponentAPI", false, "DisableComponent failed - could not find Component from the given entityId and componentId.");
  425. return false;
  426. }
  427. }
  428. EntityCompositionRequestBus::Broadcast(&EntityCompositionRequests::DisableComponents, components);
  429. for (const AZ::EntityComponentIdPair& componentInstance : componentInstances)
  430. {
  431. if (IsComponentEnabled(componentInstance))
  432. {
  433. return false;
  434. }
  435. }
  436. return true;
  437. }
  438. bool EditorComponentAPIComponent::RemoveComponents(const AZStd::vector<AZ::EntityComponentIdPair>& componentInstances)
  439. {
  440. bool cumulativeSuccess = true;
  441. AZStd::vector<AZ::Component*> components;
  442. for (const AZ::EntityComponentIdPair& componentInstance : componentInstances)
  443. {
  444. AZ::Component* component = FindComponent(componentInstance.GetEntityId(), componentInstance.GetComponentId());
  445. if (component)
  446. {
  447. components.push_back(component);
  448. }
  449. else
  450. {
  451. AZ_Warning("EditorComponentAPI", false, "RemoveComponents - a component could not be found.");
  452. cumulativeSuccess = false;
  453. }
  454. }
  455. EditorEntityActionComponent::RemoveComponentsOutcome outcome;
  456. EntityCompositionRequestBus::BroadcastResult(outcome, &EntityCompositionRequests::RemoveComponents, components);
  457. if (!outcome.IsSuccess())
  458. {
  459. AZ_Warning("EditorComponentAPI", false, "RemoveComponents failed - components could not be removed from entity.");
  460. return false;
  461. }
  462. return cumulativeSuccess;
  463. }
  464. EditorComponentAPIRequests::PropertyTreeOutcome EditorComponentAPIComponent::BuildComponentPropertyTreeEditor(const AZ::EntityComponentIdPair& componentInstance)
  465. {
  466. // Verify the Component Instance still exists
  467. AZ::Component* component = FindComponent(componentInstance.GetEntityId(), componentInstance.GetComponentId());
  468. if (!component)
  469. {
  470. AZ_Error("EditorComponentAPIComponent", false, "BuildComponentPropertyTreeEditor - Component Instance is Invalid.");
  471. return {PropertyTreeOutcome::ErrorType("BuildComponentPropertyTreeEditor - Component Instance is Invalid.")};
  472. }
  473. return {PropertyTreeOutcome::ValueType(reinterpret_cast<void*>(component), component->GetUnderlyingComponentType())};
  474. }
  475. EditorComponentAPIRequests::PropertyOutcome EditorComponentAPIComponent::GetComponentProperty(const AZ::EntityComponentIdPair& componentInstance, const AZStd::string_view propertyPath)
  476. {
  477. // Verify the Component Instance still exists
  478. AZ::Component* component = FindComponent(componentInstance.GetEntityId(), componentInstance.GetComponentId());
  479. if (!component)
  480. {
  481. AZ_Error("EditorComponentAPIComponent", false, "GetComponentProperty - Component Instance is Invalid.");
  482. return { PropertyOutcome::ErrorType("GetComponentProperty - Component Instance is Invalid.") };
  483. }
  484. PropertyTreeEditor pte = PropertyTreeEditor(reinterpret_cast<void*>(component), component->GetUnderlyingComponentType());
  485. if (m_usePropertyVisibility)
  486. {
  487. pte.SetVisibleEnforcement(true);
  488. }
  489. return pte.GetProperty(propertyPath);
  490. }
  491. EditorComponentAPIRequests::PropertyOutcome EditorComponentAPIComponent::SetComponentProperty(const AZ::EntityComponentIdPair& componentInstance, const AZStd::string_view propertyPath, const AZStd::any& value)
  492. {
  493. // Verify the Component Instance still exists
  494. AZ::Component* component = FindComponent(componentInstance.GetEntityId(), componentInstance.GetComponentId());
  495. if (!component)
  496. {
  497. AZ_Error("EditorComponentAPIComponent", false, "SetComponentProperty - Component Instance is Invalid.");
  498. return {PropertyOutcome::ErrorType("SetComponentProperty - Component Instance is Invalid.")};
  499. }
  500. PropertyTreeEditor pte = PropertyTreeEditor(reinterpret_cast<void*>(component), component->GetUnderlyingComponentType());
  501. if (m_usePropertyVisibility)
  502. {
  503. pte.SetVisibleEnforcement(true);
  504. }
  505. ScopedUndoBatch undo("Modify Entity Property");
  506. PropertyOutcome result = pte.SetProperty(propertyPath, value);
  507. if (result.IsSuccess())
  508. {
  509. PropertyEditorEntityChangeNotificationBus::Event(componentInstance.GetEntityId(), &PropertyEditorEntityChangeNotifications::OnEntityComponentPropertyChanged, componentInstance.GetComponentId());
  510. }
  511. undo.MarkEntityDirty(componentInstance.GetEntityId());
  512. return result;
  513. }
  514. bool EditorComponentAPIComponent::CompareComponentProperty(const AZ::EntityComponentIdPair& componentInstance, const AZStd::string_view propertyPath, const AZStd::any& value)
  515. {
  516. // Verify the Component Instance still exists
  517. AZ::Component* component = FindComponent(componentInstance.GetEntityId(), componentInstance.GetComponentId());
  518. if (!component)
  519. {
  520. AZ_Error("EditorComponentAPIComponent", false, "CompareComponentProperty - Component Instance is Invalid.");
  521. return false;
  522. }
  523. PropertyTreeEditor pte = PropertyTreeEditor(reinterpret_cast<void*>(component), component->GetUnderlyingComponentType());
  524. if (m_usePropertyVisibility)
  525. {
  526. pte.SetVisibleEnforcement(true);
  527. }
  528. return pte.CompareProperty(propertyPath, value);
  529. }
  530. const AZStd::vector<AZStd::string> EditorComponentAPIComponent::BuildComponentPropertyList(const AZ::EntityComponentIdPair& componentInstance)
  531. {
  532. // Verify the Component Instance still exists
  533. AZ::Component* component = FindComponent(componentInstance.GetEntityId(), componentInstance.GetComponentId());
  534. if (!component)
  535. {
  536. AZ_Error("EditorComponentAPIComponent", false, "BuildComponentPropertyList - Component Instance is Invalid.");
  537. return { AZStd::string("BuildComponentPropertyList - Component Instance is Invalid.") };
  538. }
  539. PropertyTreeEditor pte = PropertyTreeEditor(reinterpret_cast<void*>(component), component->GetUnderlyingComponentType());
  540. if (m_usePropertyVisibility)
  541. {
  542. pte.SetVisibleEnforcement(true);
  543. }
  544. return pte.BuildPathsList();
  545. }
  546. AZ::Entity* EditorComponentAPIComponent::FindEntity(AZ::EntityId entityId)
  547. {
  548. AZ_Assert(entityId.IsValid(), "EditorComponentAPIComponent::FindEntity - Invalid EntityId provided.");
  549. if (!entityId.IsValid())
  550. {
  551. return nullptr;
  552. }
  553. AZ::Entity* entity = nullptr;
  554. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationRequests::FindEntity, entityId);
  555. return entity;
  556. }
  557. AZ::Component* EditorComponentAPIComponent::FindComponent(AZ::EntityId entityId, AZ::ComponentId componentId)
  558. {
  559. // Get AZ::Entity*
  560. AZ::Entity* entityPtr = FindEntity(entityId);
  561. if (!entityPtr)
  562. {
  563. AZ_Warning("EditorComponentAPI", false, "FindComponent failed - could not find entity pointer from entityId provided.");
  564. return nullptr;
  565. }
  566. // See if the component is on the entity proper (Active)
  567. const auto& entityComponents = entityPtr->GetComponents();
  568. for (AZ::Component* component : entityComponents)
  569. {
  570. if (component->GetId() == componentId)
  571. {
  572. return component;
  573. }
  574. }
  575. // Check for pending components
  576. AZStd::vector<AZ::Component*> pendingComponents;
  577. AzToolsFramework::EditorPendingCompositionRequestBus::Event(entityPtr->GetId(), &AzToolsFramework::EditorPendingCompositionRequests::GetPendingComponents, pendingComponents);
  578. for (AZ::Component* component : pendingComponents)
  579. {
  580. if (component->GetId() == componentId)
  581. {
  582. return component;
  583. }
  584. }
  585. // Check for disabled components
  586. AZStd::vector<AZ::Component*> disabledComponents;
  587. AzToolsFramework::EditorDisabledCompositionRequestBus::Event(entityPtr->GetId(), &AzToolsFramework::EditorDisabledCompositionRequests::GetDisabledComponents, disabledComponents);
  588. for (AZ::Component* component : disabledComponents)
  589. {
  590. if (component->GetId() == componentId)
  591. {
  592. return component;
  593. }
  594. }
  595. return nullptr;
  596. }
  597. AZ::Component* EditorComponentAPIComponent::FindComponent(AZ::EntityId entityId, AZ::Uuid componentTypeId)
  598. {
  599. // Get AZ::Entity*
  600. AZ::Entity* entityPtr = FindEntity(entityId);
  601. if (!entityPtr)
  602. {
  603. AZ_Warning("EditorComponentAPI", false, "FindComponent failed - could not find entity pointer from entityId provided.");
  604. return nullptr;
  605. }
  606. // See if the component is on the entity proper (Active)
  607. const auto& entityComponents = entityPtr->GetComponents();
  608. for (AZ::Component* component : entityComponents)
  609. {
  610. if (component->GetUnderlyingComponentType() == componentTypeId)
  611. {
  612. return component;
  613. }
  614. }
  615. // Check for pending components
  616. AZStd::vector<AZ::Component*> pendingComponents;
  617. AzToolsFramework::EditorPendingCompositionRequestBus::Event(entityPtr->GetId(), &AzToolsFramework::EditorPendingCompositionRequests::GetPendingComponents, pendingComponents);
  618. for (AZ::Component* component : pendingComponents)
  619. {
  620. if (component->GetUnderlyingComponentType() == componentTypeId)
  621. {
  622. return component;
  623. }
  624. }
  625. // Check for disabled components
  626. AZStd::vector<AZ::Component*> disabledComponents;
  627. AzToolsFramework::EditorDisabledCompositionRequestBus::Event(entityPtr->GetId(), &AzToolsFramework::EditorDisabledCompositionRequests::GetDisabledComponents, disabledComponents);
  628. for (AZ::Component* component : disabledComponents)
  629. {
  630. if (component->GetUnderlyingComponentType() == componentTypeId)
  631. {
  632. return component;
  633. }
  634. }
  635. return nullptr;
  636. }
  637. AZStd::vector<AZ::Component*> EditorComponentAPIComponent::FindComponents(AZ::EntityId entityId, AZ::Uuid componentTypeId)
  638. {
  639. AZStd::vector<AZ::Component*> components;
  640. // Get AZ::Entity*
  641. AZ::Entity* entityPtr = FindEntity(entityId);
  642. if (!entityPtr)
  643. {
  644. AZ_Warning("EditorComponentAPI", false, "FindComponents failed - could not find entity pointer from entityId provided.");
  645. return components;
  646. }
  647. // See if the component is on the entity proper (Active)
  648. const auto& entityComponents = entityPtr->GetComponents();
  649. for (AZ::Component* component : entityComponents)
  650. {
  651. if (component->GetUnderlyingComponentType() == componentTypeId)
  652. {
  653. components.push_back(component);
  654. }
  655. }
  656. // Check for pending components
  657. AZStd::vector<AZ::Component*> pendingComponents;
  658. AzToolsFramework::EditorPendingCompositionRequestBus::Event(entityId, &AzToolsFramework::EditorPendingCompositionRequests::GetPendingComponents, pendingComponents);
  659. for (AZ::Component* component : pendingComponents)
  660. {
  661. if (component->GetUnderlyingComponentType() == componentTypeId)
  662. {
  663. components.push_back(component);
  664. }
  665. }
  666. // Check for disabled components
  667. AZStd::vector<AZ::Component*> disabledComponents;
  668. AzToolsFramework::EditorDisabledCompositionRequestBus::Event(entityId, &AzToolsFramework::EditorDisabledCompositionRequests::GetDisabledComponents, disabledComponents);
  669. for (AZ::Component* component : disabledComponents)
  670. {
  671. if (component->GetUnderlyingComponentType() == componentTypeId)
  672. {
  673. components.push_back(component);
  674. }
  675. }
  676. return components;
  677. }
  678. } // Components
  679. } // AzToolsFramework