Skip to content

Commit

Permalink
#215 fixed copying Comment node
Browse files Browse the repository at this point in the history
It was indeed broken by Flow AddOn refactor.
Applied static analysis fixes while inspecting code.
  • Loading branch information
MothDoctor committed Dec 15, 2024
1 parent de2e789 commit 2fe336f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 48 deletions.
80 changes: 35 additions & 45 deletions Source/FlowEditor/Private/Graph/FlowGraphEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

#include "Graph/FlowGraphEditor.h"

#include "FlowEditorCommands.h"
#include "FlowEditorModule.h"

#include "Asset/FlowAssetEditor.h"
#include "Asset/FlowDebuggerSubsystem.h"
#include "FlowEditorCommands.h"
#include "Graph/FlowGraphEditorSettings.h"
#include "Graph/FlowGraphSchema_Actions.h"
#include "Graph/Nodes/FlowGraphNode.h"
Expand Down Expand Up @@ -572,6 +570,10 @@ void SFlowGraphEditor::CopySelectedNodes() const
constexpr int32 RootEdNodeParentIndex = INDEX_NONE;
PrepareFlowGraphNodeForCopy(*FlowGraphNode, RootEdNodeParentIndex, NewSelectedNodes);
}
else
{
NewSelectedNodes.Add(*SelectedIter);
}
}

FString ExportedText;
Expand All @@ -581,15 +583,14 @@ void SFlowGraphEditor::CopySelectedNodes() const

for (FGraphPanelSelectionSet::TIterator SelectedIter(NewSelectedNodes); SelectedIter; ++SelectedIter)
{
UFlowGraphNode* FlowGraphNode = Cast<UFlowGraphNode>(*SelectedIter);
if (FlowGraphNode)
if (UFlowGraphNode* FlowGraphNode = Cast<UFlowGraphNode>(*SelectedIter))
{
FlowGraphNode->PostCopyNode();
}
}
}

void SFlowGraphEditor::PrepareFlowGraphNodeForCopy(UFlowGraphNode& FlowGraphNode, int32 ParentEdNodeIndex, FGraphPanelSelectionSet& NewSelectedNodes) const
void SFlowGraphEditor::PrepareFlowGraphNodeForCopy(UFlowGraphNode& FlowGraphNode, const int32 ParentEdNodeIndex, FGraphPanelSelectionSet& NewSelectedNodes)
{
FlowGraphNode.PrepareForCopying();

Expand All @@ -600,14 +601,11 @@ void SFlowGraphEditor::PrepareFlowGraphNodeForCopy(UFlowGraphNode& FlowGraphNode
NewSelectedNodes.Add(&FlowGraphNode);

// append all subnodes for selection
const TArray<UFlowGraphNode*>& FlowGraphNodeSubNodes = FlowGraphNode.SubNodes;

for (int32 Idx = 0; Idx < FlowGraphNodeSubNodes.Num(); ++Idx)
for (UFlowGraphNode* SubNode : FlowGraphNode.SubNodes)
{
UFlowGraphNode* SubNodeCur = FlowGraphNodeSubNodes[Idx];
if (SubNodeCur)
if (SubNode)
{
PrepareFlowGraphNodeForCopy(*SubNodeCur, ThisFlowGraphNodeIndex, NewSelectedNodes);
PrepareFlowGraphNodeForCopy(*SubNode, ThisFlowGraphNodeIndex, NewSelectedNodes);
}
}
}
Expand Down Expand Up @@ -677,48 +675,47 @@ void SFlowGraphEditor::PasteNodesHere(const FVector2D& Location)

if (AvgCount > 0)
{
float InvNumNodes = 1.0f / float(AvgCount);
float InvNumNodes = 1.0f / static_cast<float>(AvgCount);
AvgNodePosition.X *= InvNumNodes;
AvgNodePosition.Y *= InvNumNodes;
}

bool bPastedParentNode = false;

TMap<int32, UFlowGraphNode*> EdNodeCopyIndexMap;
for (TSet<UEdGraphNode*>::TConstIterator It(NodesToPaste); It; ++It)
{
UEdGraphNode* PasteNode = *It;
UFlowGraphNode* PasteFlowGraphNode = Cast<UFlowGraphNode>(PasteNode);
UEdGraphNode* PastedNode = *It;

EdNodeCopyIndexMap.Add(PasteFlowGraphNode->CopySubNodeIndex, PasteFlowGraphNode);

if (PasteNode && (PasteFlowGraphNode == nullptr || !PasteFlowGraphNode->IsSubNode()))
UFlowGraphNode* PastedFlowGraphNode = Cast<UFlowGraphNode>(PastedNode);
if (PastedFlowGraphNode)
{
bPastedParentNode = true;
EdNodeCopyIndexMap.Add(PastedFlowGraphNode->CopySubNodeIndex, PastedFlowGraphNode);
}

if (PastedNode && (PastedFlowGraphNode == nullptr || !PastedFlowGraphNode->IsSubNode()))
{
// Select the newly pasted stuff
constexpr bool bSelectNodes = true;
SetNodeSelection(PasteNode, bSelectNodes);
SetNodeSelection(PastedNode, bSelectNodes);

PasteNode->NodePosX = (PasteNode->NodePosX - AvgNodePosition.X) + Location.X;
PasteNode->NodePosY = (PasteNode->NodePosY - AvgNodePosition.Y) + Location.Y;
PastedNode->NodePosX = (PastedNode->NodePosX - AvgNodePosition.X) + Location.X;
PastedNode->NodePosY = (PastedNode->NodePosY - AvgNodePosition.Y) + Location.Y;

PasteNode->SnapToGrid(16);
PastedNode->SnapToGrid(16);

// Give new node a different Guid from the old one
PasteNode->CreateNewGuid();
PastedNode->CreateNewGuid();
}

if (UFlowNode* FlowNode = Cast<UFlowNode>(PasteFlowGraphNode->GetFlowNodeBase()))
if (PastedFlowGraphNode)
{
if (UFlowNode* FlowNode = Cast<UFlowNode>(PastedFlowGraphNode->GetFlowNodeBase()))
{
// Only full FlowNodes are registered with the Asset
// (for now? perhaps we register AddOns in the future?)
FlowAsset->RegisterNode(PasteNode->NodeGuid, FlowNode);
FlowAsset->RegisterNode(PastedNode->NodeGuid, FlowNode);
}
}

if (PasteFlowGraphNode)
{
PasteFlowGraphNode->RemoveAllSubNodes();

PastedFlowGraphNode->RemoveAllSubNodes();
}
}

Expand All @@ -736,7 +733,6 @@ void SFlowGraphEditor::PasteNodesHere(const FVector2D& Location)
if (PasteNode->CopySubNodeParentIndex == INDEX_NONE)
{
// INDEX_NONE parent index indicates we should set the parent to the PasteTargetNode

if (PasteTargetNode)
{
PasteTargetNode->AddSubNode(PasteNode, FlowGraph);
Expand All @@ -759,15 +755,14 @@ void SFlowGraphEditor::PasteNodesHere(const FVector2D& Location)
// Update UI
NotifyGraphChanged();

UObject* GraphOwner = FlowGraph->GetOuter();
if (GraphOwner)
if (UObject* GraphOwner = FlowGraph->GetOuter())
{
GraphOwner->PostEditChange();
GraphOwner->MarkPackageDirty();
}
}

TSet<UEdGraphNode*> SFlowGraphEditor::ImportNodesToPasteFromClipboard(UFlowGraph& FlowGraph, FString& OutTextToImport) const
TSet<UEdGraphNode*> SFlowGraphEditor::ImportNodesToPasteFromClipboard(UFlowGraph& FlowGraph, FString& OutTextToImport)
{
// Grab the text to paste from the clipboard.
FPlatformApplicationMisc::ClipboardPaste(OutTextToImport);
Expand All @@ -786,7 +781,6 @@ TArray<UFlowGraphNode*> SFlowGraphEditor::DerivePasteTargetNodesFromSelectedNode
for (FGraphPanelSelectionSet::TConstIterator SelectedIter(SelectedNodes); SelectedIter; ++SelectedIter)
{
UFlowGraphNode* Node = Cast<UFlowGraphNode>(*SelectedIter);

if (IsValid(Node))
{
PasteTargetNodes.Add(Node);
Expand All @@ -810,7 +804,6 @@ bool SFlowGraphEditor::CanPasteNodes() const
if (!ensure(IsValid(FlowGraph)))
{
// We expect to have a legal FlowGraph pointer at this point

return false;
}

Expand All @@ -826,7 +819,6 @@ bool SFlowGraphEditor::CanPasteNodes() const
{
// NOTE (gtaylor) It's possible we could support multi-paste, but we'd need to rework PasteNodesHere()
// to understand how to paste copies onto each target node.

return false;
}

Expand All @@ -836,13 +828,12 @@ bool SFlowGraphEditor::CanPasteNodes() const
if (NodesToPaste.IsEmpty())
{
// Must have at least one node to paste

return false;
}

ON_SCOPE_EXIT
{
// We need to clean-up the nodes we built to test the paste operation
// We need to clean up the nodes we built to test the paste operation
for (TSet<UEdGraphNode*>::TConstIterator It(NodesToPaste); It; ++It)
{
UFlowGraphNode* NodeToPaste = Cast<UFlowGraphNode>(*It);
Expand All @@ -868,8 +859,7 @@ bool SFlowGraphEditor::CanPasteNodes() const
{
checkf(PasteTargetNodes.Num() == 1, TEXT("This is enforced earlier in this function, just confirming the code stays that way here."));

UFlowGraphNode* PasteTargetNode = PasteTargetNodes.Top();

const UFlowGraphNode* PasteTargetNode = PasteTargetNodes.Top();
if (!CanPasteNodesAsSubNodes(NodesToPaste, *PasteTargetNode))
{
return false;
Expand All @@ -879,7 +869,7 @@ bool SFlowGraphEditor::CanPasteNodes() const
return true;
}

bool SFlowGraphEditor::CanPasteNodesAsSubNodes(const TSet<UEdGraphNode*>& NodesToPaste, const UFlowGraphNode& PasteTargetNode) const
bool SFlowGraphEditor::CanPasteNodesAsSubNodes(const TSet<UEdGraphNode*>& NodesToPaste, const UFlowGraphNode& PasteTargetNode)
{
TSet<const UEdGraphNode*> AllRootSubNodesToPaste;
for (TSet<UEdGraphNode*>::TConstIterator It(NodesToPaste); It; ++It)
Expand Down
6 changes: 3 additions & 3 deletions Source/FlowEditor/Public/Graph/FlowGraphEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ class FLOWEDITOR_API SFlowGraphEditor : public SGraphEditor
virtual bool CanDeleteNodes() const;

virtual void CopySelectedNodes() const;
void PrepareFlowGraphNodeForCopy(UFlowGraphNode& FlowGraphNode, int32 ParentEdNodeIndex, FGraphPanelSelectionSet& NewSelectedNodes) const;
static void PrepareFlowGraphNodeForCopy(UFlowGraphNode& FlowGraphNode, const int32 ParentEdNodeIndex, FGraphPanelSelectionSet& NewSelectedNodes);
virtual bool CanCopyNodes() const;

virtual void CutSelectedNodes();
virtual bool CanCutNodes() const;

virtual void PasteNodes();

bool CanPasteNodesAsSubNodes(const TSet<UEdGraphNode*>& NodesToPaste, const UFlowGraphNode& PasteTargetNode) const;
TSet<UEdGraphNode*> ImportNodesToPasteFromClipboard(UFlowGraph& FlowGraph, FString& OutTextToImport) const;
static bool CanPasteNodesAsSubNodes(const TSet<UEdGraphNode*>& NodesToPaste, const UFlowGraphNode& PasteTargetNode);
static TSet<UEdGraphNode*> ImportNodesToPasteFromClipboard(UFlowGraph& FlowGraph, FString& OutTextToImport);
TArray<UFlowGraphNode*> DerivePasteTargetNodesFromSelectedNodes() const;

public:
Expand Down

0 comments on commit 2fe336f

Please sign in to comment.