-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathK2Node_NativeForEach.cpp
357 lines (273 loc) · 14.3 KB
/
K2Node_NativeForEach.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "K2Nodes/K2Node_NativeForEach.h"
#include "CoreTechK2Utilities.h"
// BlueprintGraph
#include "K2Node_AssignmentStatement.h"
#include "K2Node_CallFunction.h"
#include "K2Node_ExecutionSequence.h"
#include "K2Node_IfThenElse.h"
#include "K2Node_TemporaryVariable.h"
// Kismet
#include "Kismet/KismetArrayLibrary.h"
#include "Kismet/KismetMathLibrary.h"
// KismetCompiler
#include "KismetCompiler.h"
#define LOCTEXT_NAMESPACE "K2Node_NativeForEach"
const FName UK2Node_NativeForEach::ArrayPinName( TEXT( "ArrayPin" ) );
const FName UK2Node_NativeForEach::BreakPinName( TEXT( "BreakPin" ) );
const FName UK2Node_NativeForEach::ElementPinName( TEXT( "ElementPin" ) );
const FName UK2Node_NativeForEach::ArrayIndexPinName( TEXT( "ArrayIndexPin" ) );
const FName UK2Node_NativeForEach::CompletedPinName( TEXT( "CompletedPin" ) );
void UK2Node_NativeForEach::AllocateDefaultPins( )
{
Super::AllocateDefaultPins( );
// Execution pin
CreatePin( EGPD_Input, UEdGraphSchema_K2::PC_Exec, UEdGraphSchema_K2::PN_Execute );
const auto ArrayPin = CreatePin( EGPD_Input, UEdGraphSchema_K2::PC_Wildcard, ArrayPinName );
ArrayPin->PinType.ContainerType = EPinContainerType::Array;
ArrayPin->PinType.bIsConst = true;
ArrayPin->PinType.bIsReference = true;
ArrayPin->PinFriendlyName = LOCTEXT( "ArrayPin_FriendlyName", "Array" );
OriginalWildcardType = ArrayPin->PinType;
const auto BreakPin = CreatePin( EGPD_Input, UEdGraphSchema_K2::PC_Exec, BreakPinName );
BreakPin->PinFriendlyName = LOCTEXT( "BreakPin_FriendlyName", "Break" );
BreakPin->bAdvancedView = true;
// For Each pin
const auto ForEachPin = CreatePin( EGPD_Output, UEdGraphSchema_K2::PC_Exec, UEdGraphSchema_K2::PN_Then );
ForEachPin->PinFriendlyName = LOCTEXT( "ForEachPin_FriendlyName", "Loop Body" );
const auto ElementPin = CreatePin( EGPD_Output, UEdGraphSchema_K2::PC_Wildcard, ElementPinName );
ElementPin->PinFriendlyName = LOCTEXT( "ElementPin_FriendlyName", "Array Element" );
const auto IndexPin = CreatePin( EGPD_Output, UEdGraphSchema_K2::PC_Int, ArrayIndexPinName );
IndexPin->PinFriendlyName = LOCTEXT( "IndexPin_FriendlyName", "Array Index" );
IndexPin->PinToolTip = LOCTEXT( "IndexPin_Tooltip", "Index of Element into Array" ).ToString( );
const auto CompletedPin = CreatePin( EGPD_Output, UEdGraphSchema_K2::PC_Exec, CompletedPinName );
CompletedPin->PinFriendlyName = LOCTEXT( "CompletedPin_FriendlyName", "Completed" );
CompletedPin->PinToolTip = LOCTEXT( "CompletedPin_Tooltip", "Execution once all array elements have been visited" ).ToString( );
if (InputCurrentType.PinCategory == NAME_None)
{
InputCurrentType = OriginalWildcardType;
}
else if (InputCurrentType.PinCategory != UEdGraphSchema_K2::PC_Wildcard)
{
ArrayPin->PinType = InputCurrentType;
ElementPin->PinType = InputCurrentType;
ElementPin->PinType.ContainerType = EPinContainerType::None;
}
CoreTechK2Utilities::SetPinToolTip( ArrayPin, LOCTEXT( "ArrayPin_Tooltip", "Array to visit all elements of" ) );
CoreTechK2Utilities::SetPinToolTip( ElementPin, LOCTEXT( "ElementPin_Tooltip", "Element of the Array" ) );
if (AdvancedPinDisplay == ENodeAdvancedPins::NoPins)
AdvancedPinDisplay = ENodeAdvancedPins::Hidden;
}
void UK2Node_NativeForEach::PostPasteNode( )
{
Super::PostPasteNode( );
InputCurrentType.PinCategory = NAME_None;
if (const auto ArrayPin = GetArrayPin( ))
{
if ((InputCurrentType.PinCategory == NAME_None) && ArrayPin->LinkedTo.Num( ))
{
PinConnectionListChanged( ArrayPin );
}
}
}
void UK2Node_NativeForEach::ExpandNode( FKismetCompilerContext& CompilerContext, UEdGraph* SourceGraph )
{
Super::ExpandNode( CompilerContext, SourceGraph );
if (CheckForErrors( CompilerContext ))
{
// remove all the links to this node as they are no longer needed
BreakAllNodeLinks( );
return;
}
const auto K2Schema = GetDefault< UEdGraphSchema_K2 >( );
///////////////////////////////////////////////////////////////////////////////////
// Cache off versions of all our important pins
const auto ExecPin = GetExecPin( );
const auto ArrayPin = GetArrayPin( );
const auto ForEachPin = GetForEachPin( );
const auto ArrayElementPin = GetElementPin( );
const auto ArrayIndexPin = GetArrayIndexPin( );
const auto CompletedPin = GetCompletedPin( );
///////////////////////////////////////////////////////////////////////////////////
// Create a loop counter variable
const auto CreateTemporaryVariable = CompilerContext.SpawnIntermediateNode< UK2Node_TemporaryVariable >( this, SourceGraph );
CreateTemporaryVariable->VariableType.PinCategory = UEdGraphSchema_K2::PC_Int;
CreateTemporaryVariable->AllocateDefaultPins( );
const auto Temp_Variable = CreateTemporaryVariable->GetVariablePin( );
CompilerContext.MovePinLinksToIntermediate( *ArrayIndexPin, *Temp_Variable );
///////////////////////////////////////////////////////////////////////////////////
// Initialize the temporary to 0
const auto InitTemporaryVariable = CompilerContext.SpawnIntermediateNode< UK2Node_AssignmentStatement >( this, SourceGraph );
InitTemporaryVariable->AllocateDefaultPins( );
const auto Init_Exec = InitTemporaryVariable->GetExecPin( );
const auto Init_Variable = InitTemporaryVariable->GetVariablePin( );
const auto Init_Value = InitTemporaryVariable->GetValuePin( );
const auto Init_Then = InitTemporaryVariable->GetThenPin( );
CompilerContext.MovePinLinksToIntermediate( *ExecPin, *Init_Exec );
K2Schema->TryCreateConnection( Init_Variable, Temp_Variable );
Init_Value->DefaultValue = TEXT( "0" );
///////////////////////////////////////////////////////////////////////////////////
// Branch on comparing the loop index with the length of the array
const auto BranchOnIndex = CompilerContext.SpawnIntermediateNode< UK2Node_IfThenElse >( this, SourceGraph );
BranchOnIndex->AllocateDefaultPins( );
const auto Branch_Exec = BranchOnIndex->GetExecPin( );
const auto Branch_Input = BranchOnIndex->GetConditionPin( );
const auto Branch_Then = BranchOnIndex->GetThenPin( );
const auto Branch_Else = BranchOnIndex->GetElsePin( );
Init_Then->MakeLinkTo( Branch_Exec );
CompilerContext.MovePinLinksToIntermediate( *CompletedPin, *Branch_Else );
const auto CompareLessThan = CompilerContext.SpawnIntermediateNode< UK2Node_CallFunction >( this, SourceGraph );
CompareLessThan->FunctionReference.SetExternalMember( GET_FUNCTION_NAME_CHECKED( UKismetMathLibrary, Less_IntInt ), UKismetMathLibrary::StaticClass( ) );
CompareLessThan->AllocateDefaultPins( );
const auto Compare_A = CompareLessThan->FindPinChecked( TEXT( "A" ) );
const auto Compare_B = CompareLessThan->FindPinChecked( TEXT( "B" ) );
const auto Compare_Return = CompareLessThan->GetReturnValuePin( );
Branch_Input->MakeLinkTo( Compare_Return );
Temp_Variable->MakeLinkTo( Compare_A );
const auto GetArrayLength = CompilerContext.SpawnIntermediateNode< UK2Node_CallFunction >( this, SourceGraph );
GetArrayLength->FunctionReference.SetExternalMember( GET_FUNCTION_NAME_CHECKED( UKismetArrayLibrary, Array_Length ), UKismetArrayLibrary::StaticClass( ) );
GetArrayLength->AllocateDefaultPins( );
const auto ArrayLength_Array = GetArrayLength->FindPinChecked( TEXT( "TargetArray" ) );
const auto ArrayLength_Return = GetArrayLength->GetReturnValuePin( );
// Coerce the wildcard pin types
ArrayLength_Array->PinType = ArrayPin->PinType;
Compare_B->MakeLinkTo( ArrayLength_Return );
CompilerContext.CopyPinLinksToIntermediate( *ArrayPin, *ArrayLength_Array );
///////////////////////////////////////////////////////////////////////////////////
// Sequence the loop body and incrementing the loop counter
const auto LoopSequence = CompilerContext.SpawnIntermediateNode< UK2Node_ExecutionSequence >( this, SourceGraph );
LoopSequence->AllocateDefaultPins( );
const auto Sequence_Exec = LoopSequence->GetExecPin( );
const auto Sequence_One = LoopSequence->GetThenPinGivenIndex( 0 );
const auto Sequence_Two = LoopSequence->GetThenPinGivenIndex( 1 );
Branch_Then->MakeLinkTo( Sequence_Exec );
CompilerContext.MovePinLinksToIntermediate( *ForEachPin, *Sequence_One );
const auto GetArrayElement = CompilerContext.SpawnIntermediateNode< UK2Node_CallFunction >( this, SourceGraph );
GetArrayElement->FunctionReference.SetExternalMember( GET_FUNCTION_NAME_CHECKED( UKismetArrayLibrary, Array_Get ), UKismetArrayLibrary::StaticClass( ) );
GetArrayElement->AllocateDefaultPins( );
const auto GetElement_Array = GetArrayElement->FindPinChecked( TEXT( "TargetArray" ) );
const auto GetElement_Index = GetArrayElement->FindPinChecked( TEXT( "Index" ) );
const auto GetElement_Return = GetArrayElement->FindPinChecked( TEXT( "Item" ) );
// Coerce the wildcard pin types
GetElement_Array->PinType = ArrayPin->PinType;
GetElement_Return->PinType = ArrayElementPin->PinType;
CompilerContext.CopyPinLinksToIntermediate( *ArrayPin, *GetElement_Array );
GetElement_Index->MakeLinkTo( Temp_Variable );
CompilerContext.MovePinLinksToIntermediate( *ArrayElementPin, *GetElement_Return );
///////////////////////////////////////////////////////////////////////////////////
// Increment the loop counter by one
const auto IncrementVariable = CompilerContext.SpawnIntermediateNode< UK2Node_AssignmentStatement >( this, SourceGraph );
IncrementVariable->AllocateDefaultPins( );
const auto Inc_Exec = IncrementVariable->GetExecPin( );
const auto Inc_Variable = IncrementVariable->GetVariablePin( );
const auto Inc_Value = IncrementVariable->GetValuePin( );
const auto Inc_Then = IncrementVariable->GetThenPin( );
Sequence_Two->MakeLinkTo( Inc_Exec );
Branch_Exec->MakeLinkTo( Inc_Then );
K2Schema->TryCreateConnection( Temp_Variable, Inc_Variable );
const auto AddOne = CompilerContext.SpawnIntermediateNode< UK2Node_CallFunction >( this, SourceGraph );
AddOne->FunctionReference.SetExternalMember( GET_FUNCTION_NAME_CHECKED( UKismetMathLibrary, Add_IntInt ), UKismetMathLibrary::StaticClass( ) );
AddOne->AllocateDefaultPins( );
const auto Add_A = AddOne->FindPinChecked( TEXT( "A" ) );
const auto Add_B = AddOne->FindPinChecked( TEXT( "B" ) );
const auto Add_Return = AddOne->GetReturnValuePin( );
Temp_Variable->MakeLinkTo( Add_A );
Add_B->DefaultValue = TEXT( "1" );
Add_Return->MakeLinkTo( Inc_Value );
///////////////////////////////////////////////////////////////////////////////////
// Create a sequence from the break exec that will set the loop counter to the last array index.
// The loop will then increment the counter and terminate on the next run of SequenceTwo.
const auto BreakPin = GetBreakPin( );
const auto SetVariable = CompilerContext.SpawnIntermediateNode< UK2Node_AssignmentStatement >( this, SourceGraph );
SetVariable->AllocateDefaultPins( );
const auto Set_Exec = SetVariable->GetExecPin( );
const auto Set_Variable = SetVariable->GetVariablePin( );
const auto Set_Value = SetVariable->GetValuePin( );
CompilerContext.MovePinLinksToIntermediate( *BreakPin, *Set_Exec );
K2Schema->TryCreateConnection( Temp_Variable, Set_Variable );
const auto GetArrayLastIndex = CompilerContext.SpawnIntermediateNode< UK2Node_CallFunction >( this, SourceGraph );
GetArrayLastIndex->FunctionReference.SetExternalMember( GET_FUNCTION_NAME_CHECKED( UKismetArrayLibrary, Array_LastIndex ), UKismetArrayLibrary::StaticClass( ) );
GetArrayLastIndex->AllocateDefaultPins( );
const auto GetIndex_Array = GetArrayLastIndex->FindPinChecked( TEXT( "TargetArray" ) );
const auto GetIndex_Return = GetArrayLastIndex->GetReturnValuePin( );
// Coerce the wildcard pin types
GetIndex_Array->PinType = ArrayPin->PinType;
CompilerContext.CopyPinLinksToIntermediate( *ArrayPin, *GetIndex_Array );
GetIndex_Return->MakeLinkTo( Set_Value );
///////////////////////////////////////////////////////////////////////////////////
//
BreakAllNodeLinks( );
}
bool UK2Node_NativeForEach::CheckForErrors( const FKismetCompilerContext& CompilerContext )
{
bool bError = false;
if (GetArrayPin( )->LinkedTo.Num( ) == 0)
{
CompilerContext.MessageLog.Error( *LOCTEXT( "MissingArray_Error", "For Each (Native) node @@ must have an array to iterate." ).ToString( ), this );
bError = true;
}
return bError;
}
void UK2Node_NativeForEach::PinConnectionListChanged( UEdGraphPin* Pin )
{
Super::PinConnectionListChanged( Pin );
if (Pin == nullptr)
return;
if (Pin->PinName == ArrayPinName)
{
if (Pin->LinkedTo.Num( ) > 0)
InputCurrentType = Pin->LinkedTo[ 0 ]->PinType;
else
InputCurrentType = OriginalWildcardType;
Pin->PinType = InputCurrentType;
const auto ElementPin = GetElementPin( );
ElementPin->PinType = InputCurrentType;
ElementPin->PinType.ContainerType = EPinContainerType::None;
CoreTechK2Utilities::SetPinToolTip( Pin, LOCTEXT( "ArrayPin_Tooltip", "Array to visit all elements of" ) );
CoreTechK2Utilities::SetPinToolTip( ElementPin, LOCTEXT( "ElementPin_Tooltip", "Element of the Array" ) );
}
}
UEdGraphPin* UK2Node_NativeForEach::GetArrayPin( void ) const
{
return FindPinChecked( ArrayPinName );
}
UEdGraphPin* UK2Node_NativeForEach::GetBreakPin( void ) const
{
return FindPinChecked( BreakPinName );
}
UEdGraphPin* UK2Node_NativeForEach::GetForEachPin( void ) const
{
return FindPinChecked( UEdGraphSchema_K2::PN_Then );
}
UEdGraphPin* UK2Node_NativeForEach::GetElementPin( void ) const
{
return FindPinChecked( ElementPinName );
}
UEdGraphPin* UK2Node_NativeForEach::GetArrayIndexPin( void ) const
{
return FindPinChecked( ArrayIndexPinName );
}
UEdGraphPin* UK2Node_NativeForEach::GetCompletedPin( void ) const
{
return FindPinChecked( CompletedPinName );
}
FText UK2Node_NativeForEach::GetNodeTitle( ENodeTitleType::Type TitleType ) const
{
return LOCTEXT( "NodeTitle_NONE", "For Each Loop (Native)" );
}
FText UK2Node_NativeForEach::GetTooltipText( ) const
{
return LOCTEXT( "NodeToolTip", "Loop over each element of an array" );
}
FText UK2Node_NativeForEach::GetMenuCategory( ) const
{
return LOCTEXT( "NodeMenu", "Core Utilities" );
}
FSlateIcon UK2Node_NativeForEach::GetIconAndTint( FLinearColor& OutColor ) const
{
return FSlateIcon( "EditorStyle", "GraphEditor.Macro.ForEach_16x" );
}
void UK2Node_NativeForEach::GetMenuActions( FBlueprintActionDatabaseRegistrar& ActionRegistrar ) const
{
CoreTechK2Utilities::DefaultGetMenuActions( this, ActionRegistrar );
}
#undef LOCTEXT_NAMESPACE