EditorGraphVariableManagerComponent.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <qstring.h>
  9. #include <ScriptCanvas/Components/EditorGraphVariableManagerComponent.h>
  10. namespace ScriptCanvasEditor
  11. {
  12. /////////////////////////////////
  13. // EditorGraphVariableItemModel
  14. /////////////////////////////////
  15. void EditorGraphVariableItemModel::Activate(const ScriptCanvas::ScriptCanvasId& busId)
  16. {
  17. m_busId = busId;
  18. ScriptCanvas::GraphVariableManagerNotificationBus::Handler::BusDisconnect();
  19. ScriptCanvas::GraphVariableManagerNotificationBus::Handler::BusConnect(m_busId);
  20. removeRows(0, static_cast<int>(m_variableIds.size()));
  21. m_variableIds.clear();
  22. const ScriptCanvas::GraphVariableMapping* variableMapping = nullptr;
  23. ScriptCanvas::GraphVariableManagerRequestBus::EventResult(variableMapping, m_busId, &ScriptCanvas::GraphVariableManagerRequests::GetVariables);
  24. if (variableMapping)
  25. {
  26. beginInsertRows(QModelIndex(), 0, static_cast<int>(variableMapping->size()));
  27. m_variableIds.reserve(variableMapping->size());
  28. for (const auto& mapPair : (*variableMapping))
  29. {
  30. m_variableIds.emplace_back(mapPair.first);
  31. }
  32. endInsertRows();
  33. }
  34. }
  35. ScriptCanvas::VariableId EditorGraphVariableItemModel::FindVariableIdForIndex(const QModelIndex& modelIndex) const
  36. {
  37. ScriptCanvas::VariableId variableId;
  38. if (modelIndex.row() >= 0 && modelIndex.row() < m_variableIds.size())
  39. {
  40. variableId = m_variableIds[modelIndex.row()];
  41. }
  42. return variableId;
  43. }
  44. QModelIndex EditorGraphVariableItemModel::index(int row, int column, [[maybe_unused]] const QModelIndex& parent) const
  45. {
  46. return createIndex(row, column);
  47. }
  48. QModelIndex EditorGraphVariableItemModel::parent(const QModelIndex&) const
  49. {
  50. return QModelIndex();
  51. }
  52. int EditorGraphVariableItemModel::columnCount([[maybe_unused]] const QModelIndex& parent) const
  53. {
  54. return 1;
  55. }
  56. int EditorGraphVariableItemModel::rowCount([[maybe_unused]] const QModelIndex& parent) const
  57. {
  58. return static_cast<int>(m_variableIds.size());
  59. }
  60. QVariant EditorGraphVariableItemModel::data(const QModelIndex& index, int role) const
  61. {
  62. ScriptCanvas::VariableId variableId = FindVariableIdForIndex(index);
  63. if (!variableId.IsValid())
  64. {
  65. return QVariant();
  66. }
  67. if (role == Qt::DisplayRole
  68. || role == Qt::EditRole)
  69. {
  70. AZStd::string variableName;
  71. ScriptCanvas::GraphVariableManagerRequestBus::EventResult(variableName, m_busId, &ScriptCanvas::GraphVariableManagerRequests::GetVariableName, variableId);
  72. return QVariant(variableName.c_str());
  73. }
  74. return QVariant();
  75. }
  76. void EditorGraphVariableItemModel::OnVariableAddedToGraph(const ScriptCanvas::VariableId& variableId, AZStd::string_view /*variableName*/)
  77. {
  78. beginInsertRows(QModelIndex(), rowCount(), rowCount());
  79. m_variableIds.emplace_back(variableId);
  80. endInsertRows();
  81. }
  82. void EditorGraphVariableItemModel::OnVariableRemovedFromGraph(const ScriptCanvas::VariableId& variableId, AZStd::string_view /*variableName*/)
  83. {
  84. int index = -1;
  85. for (unsigned int i = 0; i < m_variableIds.size(); ++i)
  86. {
  87. if (m_variableIds[i] == variableId)
  88. {
  89. index = i;
  90. break;
  91. }
  92. }
  93. if (index >= 0)
  94. {
  95. beginRemoveRows(QModelIndex(), index, index);
  96. m_variableIds.erase(m_variableIds.begin() + index);
  97. endRemoveRows();
  98. }
  99. else
  100. {
  101. AZ_Error("Script Canvas", false, "Failed to find index which contains variable id %s. This indicates that the GraphVariableManagerNotification::OnVariableRemoved function"
  102. " was invoked twice for the same variable without it being added back to the GraphVariableManager. This should not occur and likely indicates an issue in the GraphVariableManager",
  103. variableId.ToString().data());
  104. }
  105. }
  106. ////////////////////////////////////////
  107. // EditorGraphVariableManagerComponent
  108. ////////////////////////////////////////
  109. void EditorGraphVariableManagerComponent::Reflect(AZ::ReflectContext* context)
  110. {
  111. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  112. {
  113. serializeContext->Class<EditorGraphVariableManagerComponent, ScriptCanvas::GraphVariableManagerComponent>()
  114. ->Version(0)
  115. ;
  116. }
  117. }
  118. EditorGraphVariableManagerComponent::EditorGraphVariableManagerComponent(ScriptCanvas::ScriptCanvasId graphScopeId)
  119. : GraphVariableManagerComponent(graphScopeId)
  120. {
  121. }
  122. void EditorGraphVariableManagerComponent::ConfigureScriptCanvasId(const ScriptCanvas::ScriptCanvasId& executionId)
  123. {
  124. ScriptCanvas::GraphVariableManagerComponent::ConfigureScriptCanvasId(executionId);
  125. m_variableModel.Activate(GetScriptCanvasId());
  126. EditorSceneVariableManagerRequestBus::Handler::BusDisconnect();
  127. EditorSceneVariableManagerRequestBus::Handler::BusConnect(GetScriptCanvasId());
  128. }
  129. QAbstractItemModel* EditorGraphVariableManagerComponent::GetVariableItemModel()
  130. {
  131. return &m_variableModel;
  132. }
  133. }