ScriptCanvasPropertyDataInterface.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #pragma once
  9. #include <GraphCanvas/Components/NodePropertyDisplay/DataInterface.h>
  10. #include <ScriptCanvas/Core/Datum.h>
  11. #include <ScriptCanvas/Core/Node.h>
  12. #include <ScriptCanvas/Core/NodeBus.h>
  13. #include <ScriptCanvas/Core/Slot.h>
  14. #include <Editor/View/Widgets/PropertyGridBus.h>
  15. // ComboBoxDataInterface
  16. #include <GraphCanvas/Components/NodePropertyDisplay/ComboBoxDataInterface.h>
  17. #include <GraphCanvas/Widgets/ComboBox/ComboBoxItemModelInterface.h>
  18. #include <GraphCanvas/Widgets/ComboBox/ComboBoxItemModels.h>
  19. ////
  20. namespace ScriptCanvasEditor
  21. {
  22. template<class InterfaceType, typename DataType>
  23. class ScriptCanvasPropertyDataInterface
  24. : public ScriptCanvas::NodePropertyInterfaceListener
  25. , public InterfaceType
  26. {
  27. protected:
  28. ScriptCanvasPropertyDataInterface(const AZ::EntityId& nodeId, ScriptCanvas::TypedNodePropertyInterface<DataType>* nodePropertyInterface)
  29. : m_nodeId(nodeId)
  30. , m_nodePropertyInterface(nodePropertyInterface)
  31. {
  32. static_assert((AZStd::is_base_of<GraphCanvas::DataInterface, InterfaceType>::value), "ScriptCanvasPropertyDataInterface given invalid GraphCanvas::DataInterface to inherit.");
  33. if (m_nodePropertyInterface)
  34. {
  35. m_nodePropertyInterface->RegisterListener(this);
  36. }
  37. }
  38. using InterfaceType::SignalValueChanged;
  39. public:
  40. virtual ~ScriptCanvasPropertyDataInterface() = default;
  41. const ScriptCanvas::ScriptCanvasId GetScriptCanvasId() const
  42. {
  43. ScriptCanvas::ScriptCanvasId scriptCanvasId;
  44. ScriptCanvas::NodeRequestBus::EventResult(scriptCanvasId, GetNodeId(), &ScriptCanvas::NodeRequests::GetOwningScriptCanvasId);
  45. return scriptCanvasId;
  46. }
  47. const GraphCanvas::GraphId GetGraphCanvasGraphId() const
  48. {
  49. GraphCanvas::GraphId graphCanvasGraphId;
  50. GeneralRequestBus::BroadcastResult(graphCanvasGraphId, &GeneralRequests::GetGraphCanvasGraphId, GetScriptCanvasId());
  51. return graphCanvasGraphId;
  52. }
  53. const AZ::EntityId& GetNodeId() const
  54. {
  55. return m_nodeId;
  56. }
  57. ScriptCanvas::TypedNodePropertyInterface<DataType>* GetNodePropertyInterface()
  58. {
  59. return m_nodePropertyInterface;
  60. }
  61. // NodeNotificationsBus
  62. void OnPropertyChanged() override
  63. {
  64. SignalValueChanged();
  65. }
  66. void PushUndoBlock()
  67. {
  68. GeneralRequestBus::Broadcast(&GeneralRequests::PushPreventUndoStateUpdate);
  69. }
  70. void PopUndoBlock()
  71. {
  72. GeneralRequestBus::Broadcast(&GeneralRequests::PopPreventUndoStateUpdate);
  73. }
  74. void PostUndoPoint()
  75. {
  76. GeneralRequestBus::Broadcast(&GeneralRequests::PostUndoPoint, GetScriptCanvasId());
  77. }
  78. ////
  79. void SetValue(const DataType& valueType)
  80. {
  81. PushUndoBlock();
  82. m_nodePropertyInterface->SetPropertyData(valueType);
  83. PopUndoBlock();
  84. PostUndoPoint();
  85. PropertyGridRequestBus::Broadcast(&PropertyGridRequests::RefreshPropertyGrid);
  86. }
  87. const DataType& GetValue() const
  88. {
  89. static DataType s_staticReturn = {};
  90. if (m_nodePropertyInterface)
  91. {
  92. const DataType* dataValue = m_nodePropertyInterface->GetPropertyData();
  93. if (dataValue)
  94. {
  95. return (*dataValue);
  96. }
  97. }
  98. return s_staticReturn;
  99. }
  100. private:
  101. ScriptCanvas::TypedNodePropertyInterface<DataType>* m_nodePropertyInterface;
  102. AZ::EntityId m_nodeId;
  103. };
  104. // ComboBoxDataInterface
  105. template<typename DataType>
  106. class ScriptCanvasComboBoxPropertyDataInterface
  107. : public ScriptCanvasPropertyDataInterface<GraphCanvas::ComboBoxDataInterface, DataType>
  108. {
  109. public:
  110. AZ_CLASS_ALLOCATOR(ScriptCanvasComboBoxPropertyDataInterface<DataType>, AZ::SystemAllocator);
  111. ScriptCanvasComboBoxPropertyDataInterface(AZ::EntityId scriptCanvasNodeId, ScriptCanvas::TypedComboBoxNodePropertyInterface<DataType>* propertyInterface)
  112. : ScriptCanvasPropertyDataInterface<GraphCanvas::ComboBoxDataInterface, DataType>(scriptCanvasNodeId, propertyInterface)
  113. , m_propertyInterface(propertyInterface)
  114. {
  115. const auto& displaySet = m_propertyInterface->GetValueSet();
  116. for (const auto& displayPair : displaySet)
  117. {
  118. QString displayString = displayPair.first.c_str();
  119. m_comboBoxModel.AddElement(displayPair.second, displayString);
  120. }
  121. }
  122. ~ScriptCanvasComboBoxPropertyDataInterface() = default;
  123. // GraphCanvas::ComboBoxDataInterface
  124. // Necessary to deal with Clang invoking base methods from this templated class
  125. using ScriptCanvasPropertyDataInterface<GraphCanvas::ComboBoxDataInterface, DataType>::GetValue;
  126. using ScriptCanvasPropertyDataInterface<GraphCanvas::ComboBoxDataInterface, DataType>::SetValue;
  127. // Returns the EnumModel used to populate the DropDown and AutoCompleter Menu
  128. GraphCanvas::ComboBoxItemModelInterface* GetItemInterface() override
  129. {
  130. return &m_comboBoxModel;
  131. }
  132. // Interfaces for manipulating the values. Indexes will refer to the elements within the ComboBoxModel
  133. void AssignIndex(const QModelIndex& index) override
  134. {
  135. DataType dataValue = m_comboBoxModel.GetValueForIndex(index);
  136. this->SetValue(dataValue);
  137. }
  138. QModelIndex GetAssignedIndex() const override
  139. {
  140. DataType dataValue = this->GetValue();
  141. return m_comboBoxModel.GetIndexForValue(dataValue);
  142. }
  143. QString GetDisplayString() const override
  144. {
  145. DataType dataValue = this->GetValue();
  146. return m_comboBoxModel.GetNameForValue(dataValue);
  147. }
  148. ////
  149. private:
  150. ScriptCanvas::TypedComboBoxNodePropertyInterface<DataType>* m_propertyInterface;
  151. GraphCanvas::GraphCanvasListComboBoxModel<DataType> m_comboBoxModel;
  152. };
  153. ////
  154. }