From 27cc659ab9cec7a37caf2868e46656bd7f8290c9 Mon Sep 17 00:00:00 2001 From: chcurran <82187351+carlitosan@users.noreply.github.com> Date: Tue, 23 Nov 2021 10:31:26 -0800 Subject: [PATCH] Editor Script Component simplified and working Signed-off-by: chcurran <82187351+carlitosan@users.noreply.github.com> --- .../Code/Builder/ScriptCanvasBuilder.cpp | 18 +++++-- .../Assets/ScriptCanvasFileHandling.cpp | 49 ++++++++++--------- .../EditorScriptCanvasComponent.cpp | 46 ++++++++--------- .../Components/EditorScriptCanvasComponent.h | 10 +++- .../ScriptCanvas/Variable/GraphVariable.cpp | 5 ++ .../ScriptCanvas/Variable/GraphVariable.h | 2 + 6 files changed, 74 insertions(+), 56 deletions(-) diff --git a/Gems/ScriptCanvas/Code/Builder/ScriptCanvasBuilder.cpp b/Gems/ScriptCanvas/Code/Builder/ScriptCanvasBuilder.cpp index bb2b94bb50..fe89c57009 100644 --- a/Gems/ScriptCanvas/Code/Builder/ScriptCanvasBuilder.cpp +++ b/Gems/ScriptCanvas/Code/Builder/ScriptCanvasBuilder.cpp @@ -30,11 +30,10 @@ namespace ScriptCanvasBuilder void BuildVariableOverrides::CopyPreviousOverriddenValues(const BuildVariableOverrides& source) { - auto isEqual = [](const ScriptCanvas::GraphVariable& overrideValue, const ScriptCanvas::GraphVariable& candidate) + auto isEqual = [](const ScriptCanvas::GraphVariable& lhs, const ScriptCanvas::GraphVariable& rhs) { - return candidate.GetVariableId() == overrideValue.GetVariableId() - || (candidate.GetVariableName() == overrideValue.GetVariableName() - && candidate.GetDataType() == overrideValue.GetDataType()); + return (lhs.GetVariableId() == rhs.GetVariableId() && lhs.GetDataType() == rhs.GetDataType()) + || (lhs.GetVariableName() == rhs.GetVariableName() && lhs.GetDataType() == rhs.GetDataType()); }; auto copyPreviousIfFound = [isEqual](ScriptCanvas::GraphVariable& overriddenValue, const AZStd::vector& source) @@ -44,7 +43,7 @@ namespace ScriptCanvasBuilder if (iter != source.end()) { - overriddenValue.DeepCopy(*iter); + overriddenValue.ModDatum().DeepCopyDatum(*iter->GetDatum()); overriddenValue.SetScriptInputControlVisibility(AZ::Edit::PropertyVisibility::Hide); overriddenValue.SetAllowSignalOnChange(false); return true; @@ -64,6 +63,15 @@ namespace ScriptCanvasBuilder } } + for (auto& overriddenValue : m_overridesUnused) + { + if (!copyPreviousIfFound(overriddenValue, source.m_overridesUnused)) + { + // the variable in question may have been previously used, and is now unused, so copy the previous value over + copyPreviousIfFound(overriddenValue, source.m_overrides); + } + } + ////////////////////////////////////////////////////////////////////////// // #functions2 provide an identifier for the node/variable in the source that caused the dependency. the root will not have one. // the above will provide the data to handle the cases where only certain dependency nodes were removed diff --git a/Gems/ScriptCanvas/Code/Editor/Assets/ScriptCanvasFileHandling.cpp b/Gems/ScriptCanvas/Code/Editor/Assets/ScriptCanvasFileHandling.cpp index e5d9e86fbe..8cecdc86d9 100644 --- a/Gems/ScriptCanvas/Code/Editor/Assets/ScriptCanvasFileHandling.cpp +++ b/Gems/ScriptCanvas/Code/Editor/Assets/ScriptCanvasFileHandling.cpp @@ -23,7 +23,8 @@ #include #include #include - +#include + namespace ScriptCanvasFileHandlingCpp { void AppendTabs(AZStd::string& result, size_t depth) @@ -135,26 +136,19 @@ namespace ScriptCanvasEditor return AZ::Failure(AZStd::string::format("LoadEditorAssetTree failed to describe graph from %s", handle.ToString().c_str())); } - auto loadAssetOutcome = LoadFromFile(handle.Path().c_str()); - if (!loadAssetOutcome.IsSuccess()) + if (!handle.Get()) { - return AZ::Failure(AZStd::string::format("LoadEditorAssetTree failed to load graph from %s: %s" - , handle.ToString().c_str(), loadAssetOutcome.GetError().c_str())); - } - - AZStd::vector dependentAssets; - - auto filterCB = [&dependentAssets](const AZ::Data::AssetFilterInfo& filterInfo)->bool - { - if (filterInfo.m_assetType == azrtti_typeid() - || filterInfo.m_assetType == azrtti_typeid()) + auto loadAssetOutcome = LoadFromFile(handle.Path().c_str()); + if (!loadAssetOutcome.IsSuccess()) { - dependentAssets.push_back(SourceHandle(nullptr, filterInfo.m_assetId.m_guid, {})); + return AZ::Failure(AZStd::string::format("LoadEditorAssetTree failed to load graph from %s: %s" + , handle.ToString().c_str(), loadAssetOutcome.GetError().c_str())); } - return true; - }; + handle = SourceHandle(loadAssetOutcome.GetValue(), handle.Id(), handle.Path().c_str()); + } + AZStd::vector dependentAssets; const auto subgraphInterfaceAssetTypeID = azrtti_typeid>(); auto beginElementCB = [&subgraphInterfaceAssetTypeID, &dependentAssets] @@ -170,12 +164,13 @@ namespace ScriptCanvasEditor // if ptr is a pointer-to-pointer, cast its value to a void* (or const void*) and dereference to get to the actual object pointer. instance = *(void**)(instance); } + } - if (classData->m_typeId == subgraphInterfaceAssetTypeID) - { - auto id = reinterpret_cast*>(instance)->GetId(); - dependentAssets.push_back(SourceHandle(nullptr, id.m_guid, {})); - } + if (classData->m_typeId == subgraphInterfaceAssetTypeID) + { + auto asset = reinterpret_cast*>(instance); + auto id = asset->GetId(); + dependentAssets.push_back(SourceHandle(nullptr, id.m_guid, {})); } return true; @@ -183,7 +178,10 @@ namespace ScriptCanvasEditor AZ::SerializeContext* serializeContext = nullptr; AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext); - serializeContext->EnumerateObject( handle.Get(), beginElementCB, nullptr, AZ::SerializeContext::ENUM_ACCESS_FOR_READ); + AZ_Assert(serializeContext, "LoadEditorAssetTree() ailed to retrieve serialize context!"); + + const ScriptCanvasEditor::Graph* graph = handle.Get(); + serializeContext->EnumerateObject(graph, beginElementCB, nullptr, AZ::SerializeContext::ENUM_ACCESS_FOR_READ); EditorAssetTree result; @@ -204,8 +202,7 @@ namespace ScriptCanvasEditor result.SetParent(*parent); } - result.m_asset = loadAssetOutcome.TakeValue(); - + result.m_asset = AZStd::move(handle); return AZ::Success(result); } @@ -252,6 +249,10 @@ namespace ScriptCanvasEditor if (auto entity = scriptCanvasData->GetScriptCanvasEntity()) { + AZ_Assert(entity->GetState() == AZ::Entity::State::Constructed, "Entity loaded in bad state"); + AZ::u64 entityId = + aznumeric_caster(ScriptCanvas::MathNodeUtilities::GetRandomIntegral(1, std::numeric_limits::max())); + entity->SetId(AZ::EntityId(entityId)); entity->Init(); entity->Activate(); diff --git a/Gems/ScriptCanvas/Code/Editor/Components/EditorScriptCanvasComponent.cpp b/Gems/ScriptCanvas/Code/Editor/Components/EditorScriptCanvasComponent.cpp index fd2aa3e884..26fbf85a82 100644 --- a/Gems/ScriptCanvas/Code/Editor/Components/EditorScriptCanvasComponent.cpp +++ b/Gems/ScriptCanvas/Code/Editor/Components/EditorScriptCanvasComponent.cpp @@ -377,7 +377,7 @@ namespace ScriptCanvasEditor m_sourceHandle = *completeAsset; } - OnScriptCanvasAssetChanged(m_sourceHandle); + OnScriptCanvasAssetChanged(SourceChangeDescription::SelectionChanged); SetName(m_sourceHandle.Path().Filename().Native()); AzToolsFramework::ToolsApplicationEvents::Bus::Broadcast(&AzToolsFramework::ToolsApplicationEvents::InvalidatePropertyDisplay, AzToolsFramework::Refresh_AttributesAndValues); } @@ -391,26 +391,16 @@ namespace ScriptCanvasEditor { m_sourceHandle = SourceHandle(nullptr, m_sourceHandle.Path()); CompleteDescriptionInPlace(m_sourceHandle); - m_previousHandle = {}; m_removedHandle = {}; - - if (m_sourceHandle.IsDescriptionValid()) - { - OnScriptCanvasAssetChanged(m_sourceHandle); - } - else - { - ClearVariables(); - } - + OnScriptCanvasAssetChanged(SourceChangeDescription::SelectionChanged); return AZ::Edit::PropertyRefreshLevels::EntireTree; } - void EditorScriptCanvasComponent::OnScriptCanvasAssetChanged(const SourceHandle& assetId) + void EditorScriptCanvasComponent::OnScriptCanvasAssetChanged(SourceChangeDescription changeDescription) { ScriptCanvas::GraphIdentifier newIdentifier = GetGraphIdentifier(); - newIdentifier.m_assetId = assetId.Id(); + newIdentifier.m_assetId = m_sourceHandle.Id(); ScriptCanvas::GraphIdentifier oldIdentifier = GetGraphIdentifier(); oldIdentifier.m_assetId = m_previousHandle.Id(); @@ -419,21 +409,24 @@ namespace ScriptCanvasEditor m_previousHandle = m_sourceHandle.Describe(); - // Only clear our variables when we are given a new asset id - // or when the asset was explicitly set to empty. - // - // i.e. do not clear variables when we lose the catalog asset. - if ((assetId.IsDescriptionValid() && assetId.Describe() != m_removedHandle.Describe()) - || (!assetId.IsDescriptionValid() && !m_removedHandle.IsDescriptionValid())) + if (changeDescription == SourceChangeDescription::SelectionChanged) { ClearVariables(); } - if (assetId.IsDescriptionValid()) + if (m_sourceHandle.IsDescriptionValid()) { - if (auto loaded = LoadFromFile(assetId.Path().c_str()); loaded.IsSuccess()) + if (!m_sourceHandle.Get()) { - UpdatePropertyDisplay(loaded.GetValue()); + if (auto loaded = LoadFromFile(m_sourceHandle.Path().c_str()); loaded.IsSuccess()) + { + m_sourceHandle = SourceHandle(loaded.TakeValue(), m_sourceHandle.Id(), m_sourceHandle.Path().c_str()); + } + } + + if (m_sourceHandle.Get()) + { + UpdatePropertyDisplay(m_sourceHandle); } } @@ -467,8 +460,9 @@ namespace ScriptCanvasEditor { if (auto handle = CompleteDescription(SourceHandle(nullptr, fileAssetId, {}))) { + m_sourceHandle = *handle; // consider queueing on tick bus - OnScriptCanvasAssetChanged(*handle); + OnScriptCanvasAssetChanged(SourceChangeDescription::Modified); } } } @@ -479,7 +473,7 @@ namespace ScriptCanvasEditor if (fileAssetId == m_sourceHandle.Id()) { m_removedHandle = m_sourceHandle; - OnScriptCanvasAssetChanged(m_removedHandle); + OnScriptCanvasAssetChanged(SourceChangeDescription::Removed); } } @@ -489,7 +483,7 @@ namespace ScriptCanvasEditor if (fileAssetId == m_sourceHandle.Id()) { m_removedHandle = m_sourceHandle; - OnScriptCanvasAssetChanged(m_removedHandle); + OnScriptCanvasAssetChanged(SourceChangeDescription::Error); } } diff --git a/Gems/ScriptCanvas/Code/Editor/Include/ScriptCanvas/Components/EditorScriptCanvasComponent.h b/Gems/ScriptCanvas/Code/Editor/Include/ScriptCanvas/Components/EditorScriptCanvasComponent.h index 19347e25eb..52cf49070b 100644 --- a/Gems/ScriptCanvas/Code/Editor/Include/ScriptCanvas/Components/EditorScriptCanvasComponent.h +++ b/Gems/ScriptCanvas/Code/Editor/Include/ScriptCanvas/Components/EditorScriptCanvasComponent.h @@ -98,6 +98,14 @@ namespace ScriptCanvasEditor void OnStopPlayInEditor() override; protected: + enum class SourceChangeDescription : AZ::u8 + { + Error, + Modified, + Removed, + SelectionChanged, + }; + static void Reflect(AZ::ReflectContext* context); static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) @@ -123,7 +131,7 @@ namespace ScriptCanvasEditor AZ::u32 OnFileSelectionChanged(); - void OnScriptCanvasAssetChanged(const SourceHandle& sourceHandle); + void OnScriptCanvasAssetChanged(SourceChangeDescription changeDescription); void UpdateName(); diff --git a/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Variable/GraphVariable.cpp b/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Variable/GraphVariable.cpp index 8b8dfdc3fc..59b924196b 100644 --- a/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Variable/GraphVariable.cpp +++ b/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Variable/GraphVariable.cpp @@ -335,6 +335,11 @@ namespace ScriptCanvas return &m_datum; } + Datum& GraphVariable::ModDatum() + { + return m_datum; + } + void GraphVariable::ConfigureDatumView(ModifiableDatumView& datumView) { datumView.ConfigureView((*this)); diff --git a/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Variable/GraphVariable.h b/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Variable/GraphVariable.h index e7829ca6c1..db83076f67 100644 --- a/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Variable/GraphVariable.h +++ b/Gems/ScriptCanvas/Code/Include/ScriptCanvas/Variable/GraphVariable.h @@ -127,6 +127,8 @@ namespace ScriptCanvas const Datum* GetDatum() const; + Datum& ModDatum(); + bool IsComponentProperty() const; void ConfigureDatumView(ModifiableDatumView& accessController);