File size: 6,656 Bytes
18a519f | 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 | using System;
using System.ComponentModel;
namespace Unity.VisualScripting
{
[TypeIcon(typeof(FlowGraph))]
[UnitCategory("Nesting")]
[UnitTitle("Subgraph")]
[RenamedFrom("Bolt.SuperUnit")]
[RenamedFrom("Unity.VisualScripting.SuperUnit")]
[DisplayName("Subgraph Node")]
public sealed class SubgraphUnit : NesterUnit<FlowGraph, ScriptGraphAsset>, IGraphEventListener, IGraphElementWithData
{
public sealed class Data : IGraphElementData
{
public bool isListening;
}
public IGraphElementData CreateData()
{
return new Data();
}
public SubgraphUnit() : base() { }
public SubgraphUnit(ScriptGraphAsset macro) : base(macro) { }
public static SubgraphUnit WithInputOutput()
{
var superUnit = new SubgraphUnit();
superUnit.nest.source = GraphSource.Embed;
superUnit.nest.embed = FlowGraph.WithInputOutput();
return superUnit;
}
public static SubgraphUnit WithStartUpdate()
{
var superUnit = new SubgraphUnit();
superUnit.nest.source = GraphSource.Embed;
superUnit.nest.embed = FlowGraph.WithStartUpdate();
return superUnit;
}
public override FlowGraph DefaultGraph()
{
return FlowGraph.WithInputOutput();
}
protected override void Definition()
{
isControlRoot = true; // TODO: Infer relations instead
// Using portDefinitions and type checks instead of specific definition collections
// to avoid duplicates. Iterating only once for speed.
foreach (var definition in nest.graph.validPortDefinitions)
{
if (definition is ControlInputDefinition)
{
var controlInputDefinition = (ControlInputDefinition)definition;
var key = controlInputDefinition.key;
ControlInput(key, (flow) =>
{
foreach (var unit in nest.graph.units)
{
if (unit is GraphInput)
{
var inputUnit = (GraphInput)unit;
flow.stack.EnterParentElement(this);
return inputUnit.controlOutputs[key];
}
}
return null;
});
}
else if (definition is ValueInputDefinition)
{
var valueInputDefinition = (ValueInputDefinition)definition;
var key = valueInputDefinition.key;
var type = valueInputDefinition.type;
var hasDefaultValue = valueInputDefinition.hasDefaultValue;
var defaultValue = valueInputDefinition.defaultValue;
var port = ValueInput(type, key);
if (hasDefaultValue)
{
port.SetDefaultValue(defaultValue);
}
}
else if (definition is ControlOutputDefinition)
{
var controlOutputDefinition = (ControlOutputDefinition)definition;
var key = controlOutputDefinition.key;
ControlOutput(key);
}
else if (definition is ValueOutputDefinition)
{
var valueOutputDefinition = (ValueOutputDefinition)definition;
var key = valueOutputDefinition.key;
var type = valueOutputDefinition.type;
ValueOutput(type, key, (flow) =>
{
flow.stack.EnterParentElement(this);
// Manual looping to avoid LINQ allocation
// Also removing check for multiple output nodes for speed
// (The first output node will be used without any error)
foreach (var unit in nest.graph.units)
{
if (unit is GraphOutput)
{
var outputUnit = (GraphOutput)unit;
var value = flow.GetValue(outputUnit.valueInputs[key]);
flow.stack.ExitParentElement();
return value;
}
}
flow.stack.ExitParentElement();
throw new InvalidOperationException("Missing output node when to get value.");
});
}
}
}
public void StartListening(GraphStack stack)
{
if (stack.TryEnterParentElement(this))
{
nest.graph.StartListening(stack);
stack.ExitParentElement();
}
stack.GetElementData<Data>(this).isListening = true;
}
public void StopListening(GraphStack stack)
{
stack.GetElementData<Data>(this).isListening = false;
if (stack.TryEnterParentElement(this))
{
nest.graph.StopListening(stack);
stack.ExitParentElement();
}
}
public bool IsListening(GraphPointer pointer)
{
return pointer.GetElementData<Data>(this).isListening;
}
#region Editing
public override void AfterAdd()
{
base.AfterAdd();
nest.beforeGraphChange += StopWatchingPortDefinitions;
nest.afterGraphChange += StartWatchingPortDefinitions;
StartWatchingPortDefinitions();
}
public override void BeforeRemove()
{
base.BeforeRemove();
StopWatchingPortDefinitions();
nest.beforeGraphChange -= StopWatchingPortDefinitions;
nest.afterGraphChange -= StartWatchingPortDefinitions;
}
private void StopWatchingPortDefinitions()
{
if (nest.graph != null)
{
nest.graph.onPortDefinitionsChanged -= Define;
}
}
private void StartWatchingPortDefinitions()
{
if (nest.graph != null)
{
nest.graph.onPortDefinitionsChanged += Define;
}
}
#endregion
}
}
|