11using System . Collections . Generic ;
2- using GBG . PlayableGraphMonitor . Editor . GraphView ;
32using UnityEditor . Experimental . GraphView ;
4- using UnityEngine . Assertions ;
3+ using UnityEngine ;
54using UGraphView = UnityEditor . Experimental . GraphView . GraphView ;
65using UNode = UnityEditor . Experimental . GraphView . Node ;
76
87namespace GBG . PlayableGraphMonitor . Editor . Node
98{
109 public abstract class GraphViewNode : UNode
1110 {
11+ internal static NodeLayoutInfo LayoutInfo ;
12+
1213 public int Depth { get ; }
1314
1415 public IReadOnlyList < Port > InputPorts => InternalInputPorts ;
@@ -19,14 +20,90 @@ public abstract class GraphViewNode : UNode
1920
2021 protected List < Port > InternalOutputPorts { get ; } = new List < Port > ( ) ;
2122
22- protected UGraphView Container { get ; set ; }
23+ protected UGraphView Container { get ; set ; }
2324
2425 // public IReadOnlyList<Edge> InputEdges => InternalInputEdges;
2526
2627 protected List < Edge > InternalInputEdges { get ; } = new List < Edge > ( ) ;
2728
28- protected List < GraphViewNode > ChildNodes { get ; } = new List < GraphViewNode > ( ) ;
29+ protected GraphViewNode Parent { get ; private set ; }
30+
31+ protected new List < GraphViewNode > Children { get ; } = new List < GraphViewNode > ( ) ;
32+
33+
34+ private Vector2 ? _hierarchySize ;
35+
36+
37+ public Vector2 GetNodeSize ( )
38+ {
39+ return NodeLayoutInfo . StandardNodeSize ;
40+ //return worldBound.size;
41+ }
42+
43+ public Vector2 GetHierarchySize ( )
44+ {
45+ if ( _hierarchySize != null )
46+ {
47+ return _hierarchySize . Value ;
48+ }
49+
50+
51+ if ( Children . Count == 0 )
52+ {
53+ _hierarchySize = GetNodeSize ( ) ;
54+ return _hierarchySize . Value ;
55+ }
56+
57+ if ( Children . Count == 1 )
58+ {
59+ var selfSize = GetNodeSize ( ) ;
60+ var childHierarchySize = Children [ 0 ] . GetHierarchySize ( ) ;
61+ _hierarchySize = new Vector2
62+ {
63+ x = selfSize . x + childHierarchySize . x + NodeLayoutInfo . HorizontalSpace ,
64+ y = selfSize . y + childHierarchySize . y + NodeLayoutInfo . VerticalSpace
65+ } ;
66+
67+ return _hierarchySize . Value ;
68+ }
69+
70+ var hierarchySize = GetNodeSize ( ) + new Vector2 ( NodeLayoutInfo . HorizontalSpace , 0 ) ;
71+ for ( int i = 0 ; i < Children . Count ; i ++ )
72+ {
73+ var childSize = Children [ i ] . GetHierarchySize ( ) ;
74+ hierarchySize . x += childSize . x ;
75+ hierarchySize . y = Mathf . Max ( hierarchySize . y , childSize . y ) ;
76+ }
2977
78+ hierarchySize . y += ( Children . Count - 1 ) * NodeLayoutInfo . VerticalSpace ;
79+ _hierarchySize = hierarchySize ;
80+
81+ return _hierarchySize . Value ;
82+ }
83+
84+ public void CalculateLayout ( Vector2 treeSize , Vector2 origin )
85+ {
86+ var hierarchySize = GetHierarchySize ( ) ;
87+ var nodePos = CalculateSubTreeRootPosition ( treeSize , hierarchySize , origin ) ;
88+ SetPosition ( new Rect ( nodePos , Vector2 . zero ) ) ;
89+
90+ origin . x -= GetNodeSize ( ) . x - NodeLayoutInfo . HorizontalSpace ;
91+ for ( int i = 0 ; i < Children . Count ; i ++ )
92+ {
93+ var childNode = Children [ i ] ;
94+ var childHierarchySize = childNode . GetHierarchySize ( ) ;
95+ childNode . CalculateLayout ( treeSize , origin ) ;
96+
97+ origin . y += childHierarchySize . y + NodeLayoutInfo . VerticalSpace ;
98+ }
99+ }
100+
101+ public static Vector2 CalculateSubTreeRootPosition ( Vector2 treeSize , Vector2 subTreeSize , Vector2 subTreeOrigin )
102+ {
103+ var subTreePos = subTreeOrigin ;
104+ subTreePos . y += subTreeSize . y / 2 ;
105+ return subTreePos ;
106+ }
30107
31108 public virtual void AddToContainer ( UGraphView container )
32109 {
@@ -48,12 +125,12 @@ public virtual void RemoveFromContainer()
48125 InternalInputEdges . Clear ( ) ;
49126
50127 // children
51- foreach ( var childNode in ChildNodes )
128+ foreach ( var childNode in Children )
52129 {
53130 childNode . RemoveFromContainer ( ) ;
54131 }
55132
56- ChildNodes . Clear ( ) ;
133+ Children . Clear ( ) ;
57134
58135 Container = null ;
59136 }
@@ -71,4 +148,4 @@ protected Port InstantiatePort<TPort>(Direction direction)
71148 return InstantiatePort ( Orientation . Horizontal , direction , Port . Capacity . Single , typeof ( TPort ) ) ;
72149 }
73150 }
74- }
151+ }
0 commit comments