forked from MothCocoon/FlowGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowNode_ExecutionMultiGate.cpp
More file actions
150 lines (125 loc) · 2.96 KB
/
Copy pathFlowNode_ExecutionMultiGate.cpp
File metadata and controls
150 lines (125 loc) · 2.96 KB
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
// Copyright https://github.com/MothCocoon/FlowGraph/graphs/contributors
#include "Nodes/Route/FlowNode_ExecutionMultiGate.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(FlowNode_ExecutionMultiGate)
UFlowNode_ExecutionMultiGate::UFlowNode_ExecutionMultiGate(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
, StartIndex(INDEX_NONE)
{
#if WITH_EDITOR
Category = TEXT("Route");
NodeDisplayStyle = FlowNodeStyle::Logic;
#endif
FString ResetPinTooltip = TEXT("Finish work of this node.");
ResetPinTooltip += LINE_TERMINATOR;
ResetPinTooltip += TEXT("Calling In input will start triggering output pins once again.");
InputPins.Add(FFlowPin(TEXT("Reset"), ResetPinTooltip));
SetNumberedOutputPins(0, 1);
AllowedSignalModes = {EFlowSignalMode::Enabled, EFlowSignalMode::Disabled};
}
#if WITH_EDITOR
bool UFlowNode_ExecutionMultiGate::CanUserRemoveOutput(const FName& PinName) const
{
return OutputPins.Num() > 2;
}
#endif
void UFlowNode_ExecutionMultiGate::ExecuteInput(const FName& PinName)
{
if (PinName == DefaultInputPin.PinName)
{
if (Completed.Num() == 0)
{
Completed.Init(false, OutputPins.Num());
}
if (!Completed.Contains(false))
{
return;
}
const bool bUseStartIndex = !Completed.Contains(true) && Completed.IsValidIndex(StartIndex);
if (bRandom)
{
int32 Index;
if (bUseStartIndex)
{
Index = StartIndex;
}
else
{
TArray<int32> AvailableIndexes;
AvailableIndexes.Reserve(Completed.Num()); // todo
for (int32 i = 0; i < Completed.Num(); i++)
{
if (Completed[i] == false)
{
AvailableIndexes.Emplace(i);
}
}
const int32 Random = FMath::RandRange(0, AvailableIndexes.Num() - 1);
Index = AvailableIndexes[Random];
}
Completed[Index] = true;
TriggerOutput(OutputPins[Index].PinName, false);
}
else
{
if (bUseStartIndex)
{
NextOutput = StartIndex;
}
const int32 CurrentOutput = NextOutput;
// We have to calculate NextOutput before TriggerOutput(..)
// TriggerOutput may call Reset and Cleanup
NextOutput = ++NextOutput % OutputPins.Num();
Completed[CurrentOutput] = true;
TriggerOutput(OutputPins[CurrentOutput].PinName, false);
}
if (!Completed.Contains(false) && bLoop)
{
Finish();
}
}
else if (PinName == TEXT("Reset"))
{
Finish();
}
}
void UFlowNode_ExecutionMultiGate::Cleanup()
{
NextOutput = 0;
Completed.Reset();
Super::Cleanup();
}
#if WITH_EDITOR
FString UFlowNode_ExecutionMultiGate::GetNodeDescription() const
{
FString Result;
Result.Reserve(128);
if (bRandom)
{
Result.Append(TEXT("Random"));
}
if (bRandom && bLoop)
{
Result.Append(TEXT(", "));
}
if (bLoop)
{
Result.Append(TEXT("Loop"));
}
if (StartIndex != INDEX_NONE)
{
if (bRandom || bLoop)
{
Result.Append(TEXT(", "));
}
if (OutputPins.IsValidIndex(StartIndex))
{
Result.Appendf(TEXT("Start Index: %d"), StartIndex);
}
else
{
Result.Append(TEXT("StartIndex: Invalid"));
}
}
return Result;
}
#endif