A game about forced loneliness, made by TACStudios
1using System; 2using System.Collections.Generic; 3 4namespace Unity.VisualScripting 5{ 6 public sealed class GraphNest<TGraph, TMacro> : IGraphNest 7 where TGraph : class, IGraph, new() 8 where TMacro : Macro<TGraph> 9 { 10 [DoNotSerialize] 11 public IGraphNester nester { get; set; } 12 13 [DoNotSerialize] 14 private GraphSource _source = GraphSource.Macro; 15 16 [DoNotSerialize] 17 private TMacro _macro; 18 19 [DoNotSerialize] 20 private TGraph _embed; 21 22 [Serialize] 23 public GraphSource source 24 { 25 get => _source; 26 set 27 { 28 if (value == source) 29 { 30 return; 31 } 32 33 BeforeGraphChange(); 34 35 _source = value; 36 37 AfterGraphChange(); 38 } 39 } 40 41 [Serialize] 42 public TMacro macro 43 { 44 get => _macro; 45 set 46 { 47 if (value == macro) 48 { 49 return; 50 } 51 52 BeforeGraphChange(); 53 54 _macro = value; 55 56 AfterGraphChange(); 57 } 58 } 59 60 [Serialize] 61 public TGraph embed 62 { 63 get => _embed; 64 set 65 { 66 if (value == embed) 67 { 68 return; 69 } 70 71 BeforeGraphChange(); 72 73 _embed = value; 74 75 AfterGraphChange(); 76 } 77 } 78 79 [DoNotSerialize] 80 public TGraph graph 81 { 82 get 83 { 84 switch (source) 85 { 86 case GraphSource.Embed: 87 return embed; 88 89 case GraphSource.Macro: 90 return macro?.graph; 91 92 default: 93 throw new UnexpectedEnumValueException<GraphSource>(source); 94 } 95 } 96 } 97 98 IMacro IGraphNest.macro 99 { 100 get => macro; 101 set => macro = (TMacro)value; 102 } 103 104 IGraph IGraphNest.embed 105 { 106 get => embed; 107 set => embed = (TGraph)value; 108 } 109 110 IGraph IGraphNest.graph => graph; 111 112 Type IGraphNest.graphType => typeof(TGraph); 113 114 Type IGraphNest.macroType => typeof(TMacro); 115 116 // TODO: Use these in the editor when appropriate to minimize change events 117 public void SwitchToEmbed(TGraph embed) 118 { 119 if (source == GraphSource.Embed && this.embed == embed) 120 { 121 return; 122 } 123 124 BeforeGraphChange(); 125 126 _source = GraphSource.Embed; 127 _embed = embed; 128 _macro = null; 129 130 AfterGraphChange(); 131 } 132 133 public void SwitchToMacro(TMacro macro) 134 { 135 if (source == GraphSource.Macro && this.macro == macro) 136 { 137 return; 138 } 139 140 BeforeGraphChange(); 141 142 _source = GraphSource.Macro; 143 _embed = null; 144 _macro = macro; 145 146 AfterGraphChange(); 147 } 148 149 public event Action beforeGraphChange; 150 151 public event Action afterGraphChange; 152 153 private void BeforeGraphChange() 154 { 155 if (graph != null) 156 { 157 nester.UninstantiateNest(); 158 } 159 160 beforeGraphChange?.Invoke(); 161 } 162 163 private void AfterGraphChange() 164 { 165 afterGraphChange?.Invoke(); 166 167 if (graph != null) 168 { 169 nester.InstantiateNest(); 170 } 171 } 172 173 #region Serialization 174 175 public IEnumerable<ISerializationDependency> deserializationDependencies 176 { 177 get 178 { 179 if (macro != null) 180 { 181 yield return macro; 182 } 183 } 184 } 185 186 #endregion 187 188 189 #region Poutine 190 191 public IEnumerable<object> GetAotStubs(HashSet<object> visited) 192 { 193 return LinqUtility.Concat<object>(graph?.GetAotStubs(visited)); 194 } 195 196 [DoNotSerialize] 197 public bool hasBackgroundEmbed => source == GraphSource.Macro && embed != null; 198 199 #endregion 200 } 201}