@@ -24,16 +24,20 @@ public abstract class Tree<TNode, TData>: TreeBase<TNode, TData>
24
24
[ SerializeField ] public string pathSeparator = "/" ;
25
25
[ SerializeField ] public bool displayRootNode = true ;
26
26
[ SerializeField ] public bool isCheckable = false ;
27
+
27
28
[ NonSerialized ] public GUIStyle FolderStyle ;
28
29
[ NonSerialized ] public GUIStyle TreeNodeStyle ;
29
30
[ NonSerialized ] public GUIStyle ActiveTreeNodeStyle ;
31
+ [ NonSerialized ] public GUIStyle FocusedTreeNodeStyle ;
32
+ [ NonSerialized ] public GUIStyle FocusedActiveTreeNodeStyle ;
30
33
31
34
[ SerializeField ] private List < TNode > nodes = new List < TNode > ( ) ;
32
35
[ SerializeField ] private TNode selectedNode = null ;
33
36
34
37
[ NonSerialized ] private Stack < bool > indents = new Stack < bool > ( ) ;
35
38
[ NonSerialized ] private Action < TNode > rightClickNextRender ;
36
39
[ NonSerialized ] private TNode rightClickNextRenderNode ;
40
+ [ NonSerialized ] private int controlId ;
37
41
38
42
public bool IsInitialized { get { return Nodes != null && Nodes . Count > 0 && ! String . IsNullOrEmpty ( Nodes [ 0 ] . Path ) ; } }
39
43
public bool RequiresRepaint { get ; private set ; }
@@ -83,6 +87,18 @@ protected override List<TNode> Nodes
83
87
84
88
public Rect Render ( Rect containingRect , Rect rect , Vector2 scroll , Action < TNode > singleClick = null , Action < TNode > doubleClick = null , Action < TNode > rightClick = null )
85
89
{
90
+ controlId = GUIUtility . GetControlID ( FocusType . Keyboard ) ;
91
+
92
+ var treeNodeStyle = TreeNodeStyle ;
93
+ var activeTreeNodeStyle = ActiveTreeNodeStyle ;
94
+
95
+ var treeHasFocus = GUIUtility . keyboardControl == controlId ;
96
+ if ( treeHasFocus )
97
+ {
98
+ treeNodeStyle = FocusedTreeNodeStyle ;
99
+ activeTreeNodeStyle = FocusedActiveTreeNodeStyle ;
100
+ }
101
+
86
102
if ( Event . current . type != EventType . Repaint )
87
103
{
88
104
if ( rightClickNextRender != null )
@@ -109,7 +125,8 @@ public Rect Render(Rect containingRect, Rect rect, Vector2 scroll, Action<TNode>
109
125
var titleDisplay = ! ( rect . y > endDisplay || rect . yMax < startDisplay ) ;
110
126
if ( titleDisplay )
111
127
{
112
- renderResult = titleNode . Render ( rect , Styles . TreeIndentation , selectedNode == titleNode , FolderStyle , TreeNodeStyle , ActiveTreeNodeStyle ) ;
128
+ var isSelected = selectedNode == titleNode ;
129
+ renderResult = titleNode . Render ( rect , Styles . TreeIndentation , isSelected , FolderStyle , treeNodeStyle , activeTreeNodeStyle ) ;
113
130
}
114
131
115
132
if ( renderResult == TreeNodeRenderResult . VisibilityChange )
@@ -142,7 +159,7 @@ public Rect Render(Rect containingRect, Rect rect, Vector2 scroll, Action<TNode>
142
159
var display = ! ( rect . y > endDisplay || rect . yMax < startDisplay ) ;
143
160
if ( display )
144
161
{
145
- renderResult = node . Render ( rect , Styles . TreeIndentation , selectedNode == node , FolderStyle , TreeNodeStyle , ActiveTreeNodeStyle ) ;
162
+ renderResult = node . Render ( rect , Styles . TreeIndentation , selectedNode == node , FolderStyle , treeNodeStyle , activeTreeNodeStyle ) ;
146
163
}
147
164
148
165
if ( renderResult == TreeNodeRenderResult . VisibilityChange )
@@ -213,6 +230,8 @@ private bool HandleInput(Rect rect, TNode currentNode, int index, Action<TNode>
213
230
if ( Event . current . type == EventType . MouseDown && clickRect . Contains ( Event . current . mousePosition ) )
214
231
{
215
232
Event . current . Use ( ) ;
233
+ GUIUtility . keyboardControl = controlId ;
234
+
216
235
SelectedNode = currentNode ;
217
236
requiresRepaint = true ;
218
237
var clickCount = Event . current . clickCount ;
@@ -234,7 +253,7 @@ private bool HandleInput(Rect rect, TNode currentNode, int index, Action<TNode>
234
253
}
235
254
236
255
// Keyboard navigation if this child is the current selection
237
- if ( currentNode == selectedNode && Event . current . type == EventType . KeyDown )
256
+ if ( GUIUtility . keyboardControl == controlId && currentNode == selectedNode && Event . current . type == EventType . KeyDown )
238
257
{
239
258
int directionY = Event . current . keyCode == KeyCode . UpArrow ? - 1 : Event . current . keyCode == KeyCode . DownArrow ? 1 : 0 ;
240
259
int directionX = Event . current . keyCode == KeyCode . LeftArrow ? - 1 : Event . current . keyCode == KeyCode . RightArrow ? 1 : 0 ;
@@ -465,34 +484,36 @@ public TreeNodeRenderResult Render(Rect rect, float indentation, bool isSelected
465
484
var iconRect = new Rect ( nodeStartX , nodeRect . y , fillRect . width - nodeStartX , nodeRect . height ) ;
466
485
var statusRect = new Rect ( iconRect . x + 6 , iconRect . yMax - 9 , 9 , 9 ) ;
467
486
468
- // if (Event.current.type == EventType.repaint)
469
- // {
470
- // if (blackStyle == null)
471
- // blackStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.black) } };
472
- //
473
- // if (greenStyle == null)
474
- // greenStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.green) } };
475
- //
476
- // if (blueStyle == null)
477
- // blueStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.blue) } };
478
- //
479
- // if (yellowStyle == null)
480
- // yellowStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.yellow) } };
481
- //
482
- // if (magentaStyle == null)
483
- // magentaStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.magenta) } };
484
- //
485
- // GUI.Box(nodeRect, GUIContent.none, blackStyle);
486
- //
487
- // GUI.Box(toggleRect, GUIContent.none, isFolder ? greenStyle : blueStyle);
488
- //
489
- // GUI.Box(checkRect, GUIContent.none, yellowStyle);
490
- // GUI.Box(iconRect, GUIContent.none, magentaStyle);
491
- // }
487
+ //if (Event.current.type == EventType.repaint)
488
+ //{
489
+ // if (blackStyle == null)
490
+ // blackStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.black) } };
491
+ //
492
+ // if (greenStyle == null)
493
+ // greenStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.green) } };
494
+ //
495
+ // if (blueStyle == null)
496
+ // blueStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.blue) } };
497
+ //
498
+ // if (yellowStyle == null)
499
+ // yellowStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.yellow) } };
500
+ //
501
+ // if (magentaStyle == null)
502
+ // magentaStyle = new GUIStyle { normal = { background = Utility.GetTextureFromColor(Color.magenta) } };
503
+ //
504
+ // GUI.Box(nodeRect, GUIContent.none, blackStyle);
505
+ //
506
+ // GUI.Box(toggleRect, GUIContent.none, isFolder ? greenStyle : blueStyle);
507
+ //
508
+ // GUI.Box(checkRect, GUIContent.none, yellowStyle);
509
+ // GUI.Box(iconRect, GUIContent.none, magentaStyle);
510
+ //}
511
+
512
+ var contentStyle = IsActive ? activeNodeStyle : nodeStyle ;
492
513
493
514
if ( Event . current . type == EventType . repaint )
494
515
{
495
- nodeStyle . Draw ( fillRect , GUIContent . none , false , false , false , isSelected ) ;
516
+ contentStyle . Draw ( fillRect , GUIContent . none , false , false , false , isSelected ) ;
496
517
}
497
518
498
519
var styleOn = false ;
@@ -539,11 +560,9 @@ public TreeNodeRenderResult Render(Rect rect, float indentation, bool isSelected
539
560
}
540
561
}
541
562
542
- var contentStyle = IsActive ? activeNodeStyle : nodeStyle ;
543
-
544
563
if ( Event . current . type == EventType . repaint )
545
564
{
546
- contentStyle . Draw ( iconRect , content , false , false , styleOn , isSelected ) ;
565
+ contentStyle . Draw ( iconRect , content , false , false , false , isSelected ) ;
547
566
}
548
567
549
568
if ( IconBadge != null )
0 commit comments