DataSlotComponent.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 <Components/Slots/Data/DataSlotComponent.h>
  9. #include <Components/Connections/ConnectionComponent.h>
  10. #include <Components/Connections/DataConnections/DataConnectionComponent.h>
  11. #include <Components/Slots/Data/DataSlotConnectionPin.h>
  12. #include <Components/Slots/Data/DataSlotLayoutComponent.h>
  13. #include <Components/Slots/SlotConnectionFilterComponent.h>
  14. #include <Components/StylingComponent.h>
  15. #include <GraphCanvas/Components/Connections/ConnectionFilters/ConnectionFilters.h>
  16. #include <GraphCanvas/Components/Connections/ConnectionFilters/DataConnectionFilters.h>
  17. #include <GraphCanvas/Components/StyleBus.h>
  18. namespace GraphCanvas
  19. {
  20. //////////////////////
  21. // DataSlotComponent
  22. //////////////////////
  23. void DataSlotComponent::Reflect(AZ::ReflectContext* reflectContext)
  24. {
  25. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflectContext);
  26. if (serializeContext)
  27. {
  28. serializeContext->Class<DataSlotComponent, SlotComponent>()
  29. ->Version(6)
  30. ->Field("TypeId", &DataSlotComponent::m_dataTypeId)
  31. ->Field("DataSlotType", &DataSlotComponent::m_dataSlotType)
  32. ->Field("CanConvertSlotTypes", &DataSlotComponent::m_canConvertSlotTypes)
  33. ->Field("ContainedTypeIds", &DataSlotComponent::m_containedTypeIds)
  34. ->Field("DataValueType", &DataSlotComponent::m_valueType)
  35. ->Field("IsUserSlot", &DataSlotComponent::m_isUserSlot)
  36. ;
  37. }
  38. }
  39. AZ::Entity* DataSlotComponent::CreateDataSlot(const AZ::EntityId& nodeId, const DataSlotConfiguration& dataSlotConfiguration)
  40. {
  41. AZ::Entity* entity = SlotComponent::CreateCoreSlotEntity();
  42. DataSlotComponent* dataSlot = aznew DataSlotComponent(dataSlotConfiguration);
  43. if (!entity->AddComponent(dataSlot))
  44. {
  45. delete dataSlot;
  46. delete entity;
  47. return nullptr;
  48. }
  49. entity->CreateComponent<DataSlotLayoutComponent>();
  50. AZStd::string styleClass;
  51. StyledEntityRequestBus::EventResult(styleClass, nodeId, &StyledEntityRequests::GetClass);
  52. entity->CreateComponent<StylingComponent>(Styling::Elements::DataSlot, nodeId, styleClass);
  53. SlotConnectionFilterComponent* connectionFilter = entity->CreateComponent<SlotConnectionFilterComponent>();
  54. SlotTypeFilter* slotTypeFilter = aznew SlotTypeFilter(ConnectionFilterType::Include);
  55. slotTypeFilter->AddSlotType(SlotTypes::DataSlot);
  56. connectionFilter->AddFilter(slotTypeFilter);
  57. ConnectionTypeFilter* connectionTypeFilter = aznew ConnectionTypeFilter(ConnectionFilterType::Include);
  58. switch (dataSlot->GetConnectionType())
  59. {
  60. case ConnectionType::CT_Input:
  61. connectionTypeFilter->AddConnectionType(CT_Output);
  62. break;
  63. case ConnectionType::CT_Output:
  64. connectionTypeFilter->AddConnectionType(CT_Input);
  65. break;
  66. default:
  67. break;
  68. };
  69. connectionFilter->AddFilter(connectionTypeFilter);
  70. connectionFilter->AddFilter(aznew DataSlotTypeFilter());
  71. return entity;
  72. }
  73. DataSlotComponent::DataSlotComponent()
  74. : SlotComponent(SlotTypes::DataSlot)
  75. , m_canConvertSlotTypes(false)
  76. , m_dataSlotType(DataSlotType::Value)
  77. , m_valueType(DataValueType::Primitive)
  78. , m_dataTypeId(AZ::Uuid::CreateNull())
  79. , m_previousDataSlotType(DataSlotType::Unknown)
  80. , m_isUserSlot(false)
  81. {
  82. if (m_slotConfiguration.m_slotGroup == SlotGroups::Invalid)
  83. {
  84. m_slotConfiguration.m_slotGroup = SlotGroups::DataGroup;
  85. }
  86. }
  87. DataSlotComponent::DataSlotComponent(const DataSlotConfiguration& dataSlotConfiguration)
  88. : SlotComponent(SlotTypes::DataSlot, dataSlotConfiguration)
  89. , m_canConvertSlotTypes(dataSlotConfiguration.m_canConvertTypes)
  90. , m_dataSlotType(dataSlotConfiguration.m_dataSlotType)
  91. , m_valueType(dataSlotConfiguration.m_dataValueType)
  92. , m_dataTypeId(dataSlotConfiguration.m_typeId)
  93. , m_containedTypeIds(dataSlotConfiguration.m_containerTypeIds)
  94. , m_previousDataSlotType(DataSlotType::Unknown)
  95. , m_isUserSlot(dataSlotConfiguration.m_isUserAdded)
  96. {
  97. if (m_slotConfiguration.m_slotGroup == SlotGroups::Invalid)
  98. {
  99. m_slotConfiguration.m_slotGroup = SlotGroups::DataGroup;
  100. }
  101. }
  102. DataSlotComponent::~DataSlotComponent()
  103. {
  104. }
  105. void DataSlotComponent::Init()
  106. {
  107. SlotComponent::Init();
  108. }
  109. void DataSlotComponent::Activate()
  110. {
  111. SlotComponent::Activate();
  112. DataSlotRequestBus::Handler::BusConnect(GetEntityId());
  113. // Will usually happen in a copy/paste scenario
  114. if (m_valueType == DataValueType::Container)
  115. {
  116. SetDataAndContainedTypeIds(m_dataTypeId, m_containedTypeIds, m_valueType);
  117. }
  118. }
  119. void DataSlotComponent::Deactivate()
  120. {
  121. SlotComponent::Deactivate();
  122. DataSlotRequestBus::Handler::BusDisconnect();
  123. }
  124. void DataSlotComponent::DisplayProposedConnection(const AZ::EntityId& connectionId, const Endpoint& endpoint)
  125. {
  126. const bool updateDisplay = false;
  127. RestoreDisplay(updateDisplay);
  128. DataSlotType slotType = DataSlotType::Unknown;
  129. DataSlotRequestBus::EventResult(slotType, endpoint.GetSlotId(), &DataSlotRequests::GetDataSlotType);
  130. m_displayedConnection = connectionId;
  131. m_connections.emplace_back(m_displayedConnection);
  132. m_previousDataSlotType = m_dataSlotType;
  133. bool isDisabled = false;
  134. if (slotType == DataSlotType::Value)
  135. {
  136. m_dataSlotType = DataSlotType::Value;
  137. isDisabled = HasConnections();
  138. }
  139. else if (slotType == DataSlotType::Reference)
  140. {
  141. if (DataSlotUtils::IsValueDataReferenceType(m_dataSlotType) || CanConvertToReference())
  142. {
  143. m_dataSlotType = DataSlotType::Reference;
  144. }
  145. }
  146. if (m_previousDataSlotType != m_dataSlotType)
  147. {
  148. DataSlotNotificationBus::Event(GetEntityId(), &DataSlotNotifications::OnDataSlotTypeChanged, m_dataSlotType);
  149. }
  150. NodePropertyRequestBus::Event(GetEntityId(), &NodePropertyRequests::SetDisabled, isDisabled);
  151. UpdateDisplay();
  152. }
  153. void DataSlotComponent::RemoveProposedConnection(const AZ::EntityId& /*connectionId*/, const Endpoint& /*endpoint*/)
  154. {
  155. RestoreDisplay(true);
  156. }
  157. void DataSlotComponent::AddConnectionId(const AZ::EntityId& connectionId, const Endpoint& endpoint)
  158. {
  159. SlotComponent::AddConnectionId(connectionId, endpoint);
  160. if (m_dataSlotType == DataSlotType::Value)
  161. {
  162. NodePropertyRequestBus::Event(GetEntityId(), &NodePropertyRequests::SetDisabled, true);
  163. }
  164. else if (m_dataSlotType == DataSlotType::Reference)
  165. {
  166. NodePropertyRequestBus::Event(GetEntityId(), &NodePropertyRequests::SetDisabled, false);
  167. }
  168. else
  169. {
  170. NodePropertyRequestBus::Event(GetEntityId(), &NodePropertyRequests::SetDisabled, HasConnections());
  171. }
  172. }
  173. void DataSlotComponent::RemoveConnectionId(const AZ::EntityId& connectionId, const Endpoint& endpoint)
  174. {
  175. SlotComponent::RemoveConnectionId(connectionId, endpoint);
  176. NodePropertyRequestBus::Event(GetEntityId(), &NodePropertyRequests::SetDisabled, HasConnections());
  177. }
  178. void DataSlotComponent::SetNode(const AZ::EntityId& nodeId)
  179. {
  180. SlotComponent::SetNode(nodeId);
  181. }
  182. SlotConfiguration* DataSlotComponent::CloneSlotConfiguration() const
  183. {
  184. DataSlotConfiguration* slotConfiguration = aznew DataSlotConfiguration();
  185. slotConfiguration->m_dataSlotType = GetDataSlotType();
  186. slotConfiguration->m_typeId = GetDataTypeId();
  187. slotConfiguration->m_containerTypeIds = m_containedTypeIds;
  188. slotConfiguration->m_isUserAdded = m_isUserSlot;
  189. PopulateSlotConfiguration((*slotConfiguration));
  190. return slotConfiguration;
  191. }
  192. void DataSlotComponent::UpdateDisplay()
  193. {
  194. DataSlotLayoutRequestBus::Event(GetEntityId(), &DataSlotLayoutRequests::UpdateDisplay);
  195. }
  196. void DataSlotComponent::RestoreDisplay(bool updateDisplay)
  197. {
  198. if (m_previousDataSlotType != DataSlotType::Unknown)
  199. {
  200. bool typeChanged = m_dataSlotType != m_previousDataSlotType;
  201. m_dataSlotType = m_previousDataSlotType;
  202. if (m_displayedConnection.IsValid())
  203. {
  204. for (auto connectionIter = m_connections.begin(); connectionIter != m_connections.end(); ++connectionIter)
  205. {
  206. if (*connectionIter == m_displayedConnection)
  207. {
  208. m_connections.erase(connectionIter);
  209. break;
  210. }
  211. }
  212. }
  213. if (updateDisplay)
  214. {
  215. UpdatePropertyDisplayState();
  216. if (typeChanged)
  217. {
  218. DataSlotNotificationBus::Event(GetEntityId(), &DataSlotNotifications::OnDataSlotTypeChanged, m_dataSlotType);
  219. }
  220. UpdateDisplay();
  221. }
  222. m_previousDataSlotType = DataSlotType::Unknown;
  223. m_displayedConnection.SetInvalid();
  224. }
  225. }
  226. void DataSlotComponent::OnFinalizeDisplay()
  227. {
  228. UpdatePropertyDisplayState();
  229. }
  230. void DataSlotComponent::UpdatePropertyDisplayState()
  231. {
  232. if (m_dataSlotType == DataSlotType::Reference)
  233. {
  234. NodePropertyRequestBus::Event(GetEntityId(), &NodePropertyRequests::SetDisabled, false);
  235. }
  236. else if (DataSlotUtils::IsValueDataSlotType(m_dataSlotType))
  237. {
  238. NodePropertyRequestBus::Event(GetEntityId(), &NodePropertyRequests::SetDisabled, HasConnections());
  239. }
  240. }
  241. bool DataSlotComponent::ConvertToReference(bool isNewSlot)
  242. {
  243. if (CanConvertToReference(isNewSlot))
  244. {
  245. AZ::EntityId nodeId = GetNode();
  246. GraphId graphId;
  247. SceneMemberRequestBus::EventResult(graphId, nodeId, &SceneMemberRequests::GetScene);
  248. {
  249. ScopedGraphUndoBlocker undoBlocker(graphId);
  250. bool convertedToReference = false;
  251. GraphModelRequestBus::EventResult(convertedToReference, graphId, &GraphModelRequests::ConvertSlotToReference, Endpoint(nodeId, GetEntityId()), isNewSlot);
  252. if (convertedToReference)
  253. {
  254. m_dataSlotType = DataSlotType::Reference;
  255. DataSlotNotificationBus::Event(GetEntityId(), &DataSlotNotifications::OnDataSlotTypeChanged, m_dataSlotType);
  256. NodePropertyRequestBus::Event(GetEntityId(), &NodePropertyRequests::SetDisabled, false);
  257. }
  258. }
  259. if (m_dataSlotType == DataSlotType::Reference)
  260. {
  261. GraphModelRequestBus::Event(graphId, &GraphModelRequests::RequestUndoPoint);
  262. }
  263. }
  264. return m_dataSlotType == DataSlotType::Reference;
  265. }
  266. bool DataSlotComponent::CanConvertToReference([[maybe_unused]] bool isNewSlot) const
  267. {
  268. bool canToggleReference = false;
  269. if (m_canConvertSlotTypes && DataSlotUtils::IsValueDataSlotType(m_dataSlotType) && !HasConnections())
  270. {
  271. AZ::EntityId nodeId = GetNode();
  272. GraphId graphId;
  273. SceneMemberRequestBus::EventResult(graphId, nodeId, &SceneMemberRequests::GetScene);
  274. GraphModelRequestBus::EventResult(canToggleReference, graphId, &GraphModelRequests::CanConvertSlotToReference, Endpoint(nodeId, GetEntityId()), isNewSlot);
  275. }
  276. return canToggleReference;
  277. }
  278. bool DataSlotComponent::ConvertToValue()
  279. {
  280. if (CanConvertToValue())
  281. {
  282. AZ::EntityId nodeId = GetNode();
  283. GraphId graphId;
  284. SceneMemberRequestBus::EventResult(graphId, nodeId, &SceneMemberRequests::GetScene);
  285. {
  286. ScopedGraphUndoBlocker undoBlocker(graphId);
  287. bool converted = false;
  288. GraphModelRequestBus::EventResult(converted, graphId, &GraphModelRequests::ConvertSlotToValue, Endpoint(nodeId, GetEntityId()));
  289. if (converted)
  290. {
  291. m_dataSlotType = DataSlotType::Value;
  292. DataSlotNotificationBus::Event(GetEntityId(), &DataSlotNotifications::OnDataSlotTypeChanged, m_dataSlotType);
  293. NodePropertyRequestBus::Event(GetEntityId(), &NodePropertyRequests::SetDisabled, HasConnections());
  294. }
  295. }
  296. if (DataSlotUtils::IsValueDataSlotType(m_dataSlotType))
  297. {
  298. GraphModelRequestBus::Event(graphId, &GraphModelRequests::RequestUndoPoint);
  299. }
  300. }
  301. return DataSlotUtils::IsValueDataSlotType(m_dataSlotType);
  302. }
  303. bool DataSlotComponent::CanConvertToValue() const
  304. {
  305. bool canConvertToValue = false;
  306. if (m_canConvertSlotTypes && m_dataSlotType == DataSlotType::Reference)
  307. {
  308. AZ::EntityId nodeId = GetNode();
  309. GraphId graphId;
  310. SceneMemberRequestBus::EventResult(graphId, nodeId, &SceneMemberRequests::GetScene);
  311. GraphModelRequestBus::EventResult(canConvertToValue, graphId, &GraphModelRequests::CanConvertSlotToValue, Endpoint(nodeId, GetEntityId()));
  312. }
  313. return canConvertToValue;
  314. }
  315. DataSlotType DataSlotComponent::GetDataSlotType() const
  316. {
  317. return m_dataSlotType;
  318. }
  319. DataValueType DataSlotComponent::GetDataValueType() const
  320. {
  321. return m_valueType;
  322. }
  323. AZ::Uuid DataSlotComponent::GetDataTypeId() const
  324. {
  325. return m_dataTypeId;
  326. }
  327. void DataSlotComponent::SetDataTypeId(AZ::Uuid typeId)
  328. {
  329. if (m_dataTypeId != typeId)
  330. {
  331. m_dataTypeId = typeId;
  332. UpdateDisplay();
  333. }
  334. }
  335. bool DataSlotComponent::IsUserSlot() const
  336. {
  337. return m_isUserSlot;
  338. }
  339. const Styling::StyleHelper* DataSlotComponent::GetDataColorPalette() const
  340. {
  341. AZ::EntityId sceneId;
  342. SceneMemberRequestBus::EventResult(sceneId, GetEntityId(), &SceneMemberRequests::GetScene);
  343. EditorId editorId;
  344. SceneRequestBus::EventResult(editorId, sceneId, &SceneRequests::GetEditorId);
  345. const Styling::StyleHelper* stylingHelper = nullptr;
  346. StyleManagerRequestBus::EventResult(stylingHelper, editorId, &StyleManagerRequests::FindDataColorPalette, m_dataTypeId);
  347. return stylingHelper;
  348. }
  349. size_t DataSlotComponent::GetContainedTypesCount() const
  350. {
  351. return m_containedTypeIds.size();
  352. }
  353. AZ::Uuid DataSlotComponent::GetContainedTypeId(size_t index) const
  354. {
  355. return m_containedTypeIds[index];
  356. }
  357. const GraphCanvas::Styling::StyleHelper* DataSlotComponent::GetContainedTypeColorPalette(size_t index) const
  358. {
  359. AZ::Uuid dataTypeId = m_containedTypeIds[index];
  360. AZ::EntityId sceneId;
  361. SceneMemberRequestBus::EventResult(sceneId, GetEntityId(), &SceneMemberRequests::GetScene);
  362. EditorId editorId;
  363. SceneRequestBus::EventResult(editorId, sceneId, &SceneRequests::GetEditorId);
  364. const Styling::StyleHelper* stylingHelper = nullptr;
  365. StyleManagerRequestBus::EventResult(stylingHelper, editorId, &StyleManagerRequests::FindDataColorPalette, dataTypeId);
  366. return stylingHelper;
  367. }
  368. void DataSlotComponent::SetDataAndContainedTypeIds(AZ::Uuid typeId, const AZStd::vector<AZ::Uuid>& typeIds, DataValueType valueType)
  369. {
  370. if (m_dataTypeId != typeId)
  371. {
  372. m_dataTypeId = typeId;
  373. // Handles a weird case with string.
  374. // Since it's a primitive, but is a container of chars.
  375. if (valueType == DataValueType::Primitive)
  376. {
  377. m_containedTypeIds.clear();
  378. }
  379. else
  380. {
  381. m_containedTypeIds = typeIds;
  382. }
  383. m_valueType = valueType;
  384. DataSlotNotificationBus::Event(GetEntityId(), &DataSlotNotifications::OnDisplayTypeChanged, m_dataTypeId, m_containedTypeIds);
  385. UpdatePropertyDisplayState();
  386. }
  387. }
  388. AZ::Entity* DataSlotComponent::ConstructConnectionEntity(const Endpoint& sourceEndpoint, const Endpoint& targetEndpoint, bool createModelConnection)
  389. {
  390. const AZStd::string k_valueConnectionSubStyle = ".varFlow";
  391. const AZStd::string k_referenceConnectionSubStyle = ".referenceFlow";
  392. bool isReference = GetDataSlotType() == DataSlotType::Reference;
  393. // Create this Connection's entity.
  394. return DataConnectionComponent::CreateDataConnection(sourceEndpoint, targetEndpoint, createModelConnection, isReference ? k_referenceConnectionSubStyle : k_valueConnectionSubStyle);
  395. }
  396. }