A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
NewNode_DialogueGraphSchemaAction.cpp
Go to the documentation of this file.
1// Copyright Csaba Molnar, Daniel Butum. All Rights Reserved.
3
4#include "ScopedTransaction.h"
5
6#include "DlgDialogue.h"
8
9#define LOCTEXT_NAMESPACE "NewNode_DialogueGraphSchemaAction"
10
12// FNewNode_DialogueGraphSchemaAction
14 UEdGraph* ParentGraph,
15 UEdGraphPin* FromPin,
16 const FVector2D Location,
17 bool bSelectNewNode/* = true*/
18)
19{
20 const FScopedTransaction Transaction(LOCTEXT("DialogueditorNewDialgueNode", "Dialogue Editor: New Dialogue Node"));
21 UDlgDialogue* Dialogue = CastChecked<UDialogueGraph>(ParentGraph)->GetDialogue();
22
23 // Mark for modification
24 verify(ParentGraph->Modify());
25 if (FromPin)
26 {
27 verify(FromPin->Modify());
28 }
29 verify(Dialogue->Modify());
30
31 // Create node, without needing to compile it
32 UEdGraphNode* GraphNode = CreateNode(Dialogue, ParentGraph, FromPin, Location, bSelectNewNode);
33 Dialogue->PostEditChange();
34 Dialogue->MarkPackageDirty();
35 ParentGraph->NotifyGraphChanged();
36
37 return GraphNode;
38}
39
42 UEdGraph* ParentGraph,
43 UEdGraphPin* FromPin,
44 FVector2D Location,
45 bool bSelectNewNode
46)
47{
48 // Maximum distance a drag can be off a node edge to require 'push off' from node
49 static constexpr int32 NodeDistance = 60;
50
51 // Create the dialogue node
52 auto DialogueNode = Dialogue->ConstructDialogueNode<UDlgNode>(CreateNodeType);
53
54 // Set the Participant Name
55 if (FromPin)
56 {
57 if (UDialogueGraphNode* GraphNode = Cast<UDialogueGraphNode>(FromPin->GetOwningNode()))
58 {
59 if (GraphNode->IsRootNode())
60 {
61 // Root Node use the first node from the Array
62 if (Dialogue->GetNodes().Num() > 0)
63 {
64 DialogueNode->SetNodeParticipantName(Dialogue->GetNodes()[0]->GetNodeParticipantName());
65 }
66 }
67 else
68 {
69 // Use the node from which we spawned this
70 DialogueNode->SetNodeParticipantName(GraphNode->GetDialogueNode().GetNodeParticipantName());
71 }
72 }
73 }
74 else
75 {
76 // Use first Node from the Array
77 if (Dialogue->GetNodes().Num() > 0)
78 {
79 DialogueNode->SetNodeParticipantName(Dialogue->GetNodes()[0]->GetNodeParticipantName());
80 }
81 }
82
83 // Create the graph node
84 FGraphNodeCreator<UDialogueGraphNode> NodeCreator(*ParentGraph);
85 UDialogueGraphNode* GraphNode = NodeCreator.CreateUserInvokedNode(bSelectNewNode);
86
87 // Link dialogue node <-> graph node
88 DialogueNode->SetGraphNode(GraphNode);
89 const int32 DialogueNodeIndex = Dialogue->AddNode(DialogueNode);
90 GraphNode->SetDialogueNodeDataChecked(DialogueNodeIndex, DialogueNode);
91
92 // Finalize graph node creation
93 NodeCreator.Finalize(); // Calls on the node: CreateNewGuid, PostPlacedNewNode, AllocateDefaultPins
94 GraphNode->AutowireNewNode(FromPin);
95
96 // Position graph node
97 // For input pins, new node will generally overlap node being dragged off
98 // Work out if we want to visually push away from connected node
99 int32 XLocation = Location.X;
100 if (FromPin && FromPin->Direction == EGPD_Input)
101 {
102 UEdGraphNode* PinNode = FromPin->GetOwningNode();
103 const float XDelta = FMath::Abs(PinNode->NodePosX - Location.X);
104
105 if (XDelta < NodeDistance)
106 {
107 // Set location to edge of current node minus the max move distance
108 // to force node to push off from connect node enough to give selection handle
109 XLocation = PinNode->NodePosX - NodeDistance;
110 }
111 }
112
113 GraphNode->SetPosition(XLocation, Location.Y);
114 //ResultNode->SnapToGrid(SNAP_GRID);
115
116 return CastChecked<UEdGraphNode>(GraphNode);
117}
118
119#undef LOCTEXT_NAMESPACE
virtual void SetPosition(int32 X, int32 Y)
void SetDialogueNodeDataChecked(int32 InIndex, UDlgNode *InNode)
void AutowireNewNode(UEdGraphPin *FromPin) override
UCLASS(BlueprintType, Meta = (DisplayThumbnail = "true"))
Definition DlgDialogue.h:85
UCLASS(BlueprintType, Abstract, EditInlineNew, ClassGroup = "Dialogue")
Definition DlgNode.h:40
virtual void SetNodeParticipantName(FName InName)
Definition DlgNode.h:129
UEdGraphNode * CreateNode(UDlgDialogue *Dialogue, UEdGraph *ParentGraph, UEdGraphPin *FromPin, FVector2D Location, bool bSelectNewNode)
UEdGraphNode * PerformAction(UEdGraph *ParentGraph, UEdGraphPin *FromPin, const FVector2D Location, bool bSelectNewNode=true) override