11using System . Collections . Generic ;
22using UnityEditor . Experimental . GraphView ;
33using UnityEngine ;
4+ using UEdge = UnityEditor . Experimental . GraphView . Edge ;
45using UGraphView = UnityEditor . Experimental . GraphView . GraphView ;
56using UNode = UnityEditor . Experimental . GraphView . Node ;
67
78namespace GBG . PlayableGraphMonitor . Editor . Node
89{
9- public abstract class GraphViewNode : UNode
10+ public enum NodeFlag : uint
11+ {
12+ None = 0 ,
13+ Active = 1 << 0 ,
14+ Dirty = 1 << 1 ,
15+ }
16+
17+ public readonly struct NodeInput
1018 {
11- public int Depth { get ; }
19+ public UEdge Edge { get ; }
1220
21+ public GraphViewNode Node { get ; }
22+
23+
24+ public NodeInput ( UEdge edge , GraphViewNode node )
25+ {
26+ Edge = edge ;
27+ Node = node ;
28+ }
29+ }
30+
31+ public abstract class GraphViewNode : UNode
32+ {
1333 public IReadOnlyList < Port > InputPorts => InternalInputPorts ;
1434
1535 protected List < Port > InternalInputPorts { get ; } = new List < Port > ( ) ;
@@ -18,30 +38,57 @@ public abstract class GraphViewNode : UNode
1838
1939 protected List < Port > InternalOutputPorts { get ; } = new List < Port > ( ) ;
2040
21- // public IReadOnlyList<Edge> InputEdges => InternalInputEdges;
41+ protected UGraphView Container { get ; set ; }
2242
23- protected List < Edge > InternalInputEdges { get ; } = new List < Edge > ( ) ;
43+ public IReadOnlyList < NodeInput > Inputs => InternalInputs ;
2444
25- protected UGraphView Container { get ; set ; }
45+ protected List < NodeInput > InternalInputs { get ; } = new List < NodeInput > ( ) ;
2646
2747 protected GraphViewNode Parent { get ; private set ; }
2848
29- protected new List < GraphViewNode > Children { get ; } = new List < GraphViewNode > ( ) ;
3049
50+ public virtual void Update ( ) { }
3151
32- private Vector2 ? _hierarchySize ;
3352
53+ #region Hierarchy
3454
35- protected GraphViewNode ( int depth )
55+ public virtual void AddToContainer ( UGraphView container )
56+ {
57+ Container = container ;
58+ Container . AddElement ( this ) ;
59+ }
60+
61+ public virtual void RemoveFromContainer ( )
3662 {
37- Depth = depth ;
63+ // self
64+ Container . RemoveElement ( this ) ;
65+
66+ // children
67+ for ( int i = 0 ; i < InternalInputs . Count ; i ++ )
68+ {
69+ var input = InternalInputs [ i ] ;
70+ Container . RemoveElement ( input . Edge ) ;
71+ input . Node . RemoveFromContainer ( ) ;
72+ }
73+
74+ InternalInputs . Clear ( ) ;
75+
76+ Container = null ;
3877 }
3978
79+
4080 protected Port InstantiatePort < TPort > ( Direction direction )
4181 {
4282 return InstantiatePort ( Orientation . Horizontal , direction , Port . Capacity . Single , typeof ( TPort ) ) ;
4383 }
4484
85+ #endregion
86+
87+
88+ #region Layout
89+
90+ private Vector2 ? _hierarchySize ;
91+
4592
4693 public Vector2 GetNodeSize ( )
4794 {
@@ -56,20 +103,20 @@ public Vector2 GetHierarchySize()
56103 return _hierarchySize . Value ;
57104 }
58105
59- if ( Children . Count == 0 )
106+ if ( Inputs . Count == 0 )
60107 {
61108 _hierarchySize = GetNodeSize ( ) ;
62109 return _hierarchySize . Value ;
63110 }
64111
65112 var subHierarchySize = Vector2 . zero ;
66- for ( int i = 0 ; i < Children . Count ; i ++ )
113+ for ( int i = 0 ; i < Inputs . Count ; i ++ )
67114 {
68- var childSize = Children [ i ] . GetHierarchySize ( ) ;
115+ var childSize = Inputs [ i ] . Node . GetHierarchySize ( ) ;
69116 subHierarchySize . x = Mathf . Max ( subHierarchySize . x , childSize . x ) ;
70117 subHierarchySize . y += childSize . y ;
71118 }
72- subHierarchySize . y += ( Children . Count - 1 ) * NodeLayoutInfo . VerticalSpace ;
119+ subHierarchySize . y += ( Inputs . Count - 1 ) * NodeLayoutInfo . VerticalSpace ;
73120
74121 var hierarchySize = GetNodeSize ( ) + new Vector2 ( NodeLayoutInfo . HorizontalSpace , 0 ) ;
75122 hierarchySize . y = Mathf . Max ( hierarchySize . y , subHierarchySize . y ) ;
@@ -85,11 +132,11 @@ public void CalculateLayout(Vector2 origin)
85132 SetPosition ( new Rect ( nodePos , Vector2 . zero ) ) ;
86133
87134 origin . x -= GetNodeSize ( ) . x - NodeLayoutInfo . HorizontalSpace ;
88- for ( int i = 0 ; i < Children . Count ; i ++ )
135+ for ( int i = 0 ; i < Inputs . Count ; i ++ )
89136 {
90- var childNode = Children [ i ] ;
91- var childHierarchySize = childNode . GetHierarchySize ( ) ;
92- childNode . CalculateLayout ( origin ) ;
137+ var childNode = Inputs [ i ] ;
138+ var childHierarchySize = childNode . Node . GetHierarchySize ( ) ;
139+ childNode . Node . CalculateLayout ( origin ) ;
93140
94141 origin . y += childHierarchySize . y ;
95142 }
@@ -102,36 +149,29 @@ public static Vector2 CalculateSubTreeRootNodePosition(Vector2 subTreeSize, Vect
102149 return subTreePos ;
103150 }
104151
105- public virtual void AddToContainer ( UGraphView container )
106- {
107- Container = container ;
108- Container . AddElement ( this ) ;
109- }
152+ #endregion
110153
111- public virtual void RemoveFromContainer ( )
112- {
113- // self
114- Container . RemoveElement ( this ) ;
115154
116- // input edges
117- for ( int i = 0 ; i < InternalInputEdges . Count ; i ++ )
118- {
119- Container . RemoveElement ( InternalInputEdges [ i ] ) ;
120- }
155+ #region Flags
121156
122- InternalInputEdges . Clear ( ) ;
157+ private uint _flags = 0 ;
123158
124- // children
125- foreach ( var childNode in Children )
126- {
127- childNode . RemoveFromContainer ( ) ;
128- }
129159
130- Children . Clear ( ) ;
160+ public bool CheckFlag ( NodeFlag flag )
161+ {
162+ return ( _flags & ( uint ) flag ) != 0 ;
163+ }
131164
132- Container = null ;
165+ public void AddFlag ( NodeFlag flag )
166+ {
167+ _flags |= ( uint ) flag ;
168+ }
169+
170+ public void RemoveFlag ( NodeFlag flag )
171+ {
172+ _flags &= ~ ( uint ) flag ;
133173 }
134174
135- public abstract void CreateAndConnectInputNodes ( ) ;
175+ #endregion
136176 }
137177}
0 commit comments