@@ -24,16 +24,20 @@ public abstract class Tree<TNode, TData>: TreeBase<TNode, TData>
2424 [ SerializeField ] public string pathSeparator = "/" ;
2525 [ SerializeField ] public bool displayRootNode = true ;
2626 [ SerializeField ] public bool isCheckable = false ;
27+
2728 [ NonSerialized ] public GUIStyle FolderStyle ;
2829 [ NonSerialized ] public GUIStyle TreeNodeStyle ;
2930 [ NonSerialized ] public GUIStyle ActiveTreeNodeStyle ;
31+ [ NonSerialized ] public GUIStyle FocusedTreeNodeStyle ;
32+ [ NonSerialized ] public GUIStyle FocusedActiveTreeNodeStyle ;
3033
3134 [ SerializeField ] private List < TNode > nodes = new List < TNode > ( ) ;
3235 [ SerializeField ] private TNode selectedNode = null ;
3336
3437 [ NonSerialized ] private Stack < bool > indents = new Stack < bool > ( ) ;
3538 [ NonSerialized ] private Action < TNode > rightClickNextRender ;
3639 [ NonSerialized ] private TNode rightClickNextRenderNode ;
40+ [ NonSerialized ] private int controlId ;
3741
3842 public bool IsInitialized { get { return Nodes != null && Nodes . Count > 0 && ! String . IsNullOrEmpty ( Nodes [ 0 ] . Path ) ; } }
3943 public bool RequiresRepaint { get ; private set ; }
@@ -83,6 +87,18 @@ protected override List<TNode> Nodes
8387
8488 public Rect Render ( Rect containingRect , Rect rect , Vector2 scroll , Action < TNode > singleClick = null , Action < TNode > doubleClick = null , Action < TNode > rightClick = null )
8589 {
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+
86102 if ( Event . current . type != EventType . Repaint )
87103 {
88104 if ( rightClickNextRender != null )
@@ -109,7 +125,8 @@ public Rect Render(Rect containingRect, Rect rect, Vector2 scroll, Action<TNode>
109125 var titleDisplay = ! ( rect . y > endDisplay || rect . yMax < startDisplay ) ;
110126 if ( titleDisplay )
111127 {
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 ) ;
113130 }
114131
115132 if ( renderResult == TreeNodeRenderResult . VisibilityChange )
@@ -142,7 +159,7 @@ public Rect Render(Rect containingRect, Rect rect, Vector2 scroll, Action<TNode>
142159 var display = ! ( rect . y > endDisplay || rect . yMax < startDisplay ) ;
143160 if ( display )
144161 {
145- renderResult = node . Render ( rect , Styles . TreeIndentation , selectedNode == node , FolderStyle , TreeNodeStyle , ActiveTreeNodeStyle ) ;
162+ renderResult = node . Render ( rect , Styles . TreeIndentation , selectedNode == node , FolderStyle , treeNodeStyle , activeTreeNodeStyle ) ;
146163 }
147164
148165 if ( renderResult == TreeNodeRenderResult . VisibilityChange )
@@ -213,6 +230,8 @@ private bool HandleInput(Rect rect, TNode currentNode, int index, Action<TNode>
213230 if ( Event . current . type == EventType . MouseDown && clickRect . Contains ( Event . current . mousePosition ) )
214231 {
215232 Event . current . Use ( ) ;
233+ GUIUtility . keyboardControl = controlId ;
234+
216235 SelectedNode = currentNode ;
217236 requiresRepaint = true ;
218237 var clickCount = Event . current . clickCount ;
@@ -234,7 +253,7 @@ private bool HandleInput(Rect rect, TNode currentNode, int index, Action<TNode>
234253 }
235254
236255 // 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 )
238257 {
239258 int directionY = Event . current . keyCode == KeyCode . UpArrow ? - 1 : Event . current . keyCode == KeyCode . DownArrow ? 1 : 0 ;
240259 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
465484 var iconRect = new Rect ( nodeStartX , nodeRect . y , fillRect . width - nodeStartX , nodeRect . height ) ;
466485 var statusRect = new Rect ( iconRect . x + 6 , iconRect . yMax - 9 , 9 , 9 ) ;
467486
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 ;
492513
493514 if ( Event . current . type == EventType . repaint )
494515 {
495- nodeStyle . Draw ( fillRect , GUIContent . none , false , false , false , isSelected ) ;
516+ contentStyle . Draw ( fillRect , GUIContent . none , false , false , false , isSelected ) ;
496517 }
497518
498519 var styleOn = false ;
@@ -539,11 +560,9 @@ public TreeNodeRenderResult Render(Rect rect, float indentation, bool isSelected
539560 }
540561 }
541562
542- var contentStyle = IsActive ? activeNodeStyle : nodeStyle ;
543-
544563 if ( Event . current . type == EventType . repaint )
545564 {
546- contentStyle . Draw ( iconRect , content , false , false , styleOn , isSelected ) ;
565+ contentStyle . Draw ( iconRect , content , false , false , false , isSelected ) ;
547566 }
548567
549568 if ( IconBadge != null )
0 commit comments