Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 6115784

Browse files
Adding functionality to make tree node elements selectable
1 parent 851ae97 commit 6115784

File tree

1 file changed

+57
-22
lines changed

1 file changed

+57
-22
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public abstract class Tree
2323
[SerializeField] public string PathIgnoreRoot;
2424
[SerializeField] public string PathSeparator = "/";
2525
[SerializeField] public bool DisplayRootNode = true;
26+
[SerializeField] public bool Selectable = false;
2627
[SerializeField] public GUIStyle FolderStyle;
2728
[SerializeField] public GUIStyle TreeNodeStyle;
2829
[SerializeField] public GUIStyle ActiveTreeNodeStyle;
@@ -69,7 +70,8 @@ public void Load(IEnumerable<ITreeData> data, string title)
6970
Name = title,
7071
Label = title,
7172
Level = -1 + displayRootLevel,
72-
IsFolder = true
73+
IsFolder = true,
74+
Selectable = Selectable
7375
};
7476
SetNodeIcon(titleNode);
7577
nodes.Add(titleNode);
@@ -105,7 +107,8 @@ public void Load(IEnumerable<ITreeData> data, string title)
105107
IsActive = d.IsActive,
106108
Label = label,
107109
Level = i + displayRootLevel,
108-
IsFolder = isFolder
110+
IsFolder = isFolder,
111+
Selectable = Selectable
109112
};
110113

111114
if (node.IsActive)
@@ -162,7 +165,7 @@ public Rect Render(Rect rect, Vector2 scroll, Action<TreeNode> singleClick = nul
162165
if (DisplayRootNode)
163166
{
164167
var titleNode = nodes[0];
165-
var selectionChanged = titleNode.Render(rect, 0f, selectedNode == titleNode, FolderStyle, TreeNodeStyle, ActiveTreeNodeStyle);
168+
var selectionChanged = titleNode.Render(rect, Styles.TreeIndentation, selectedNode == titleNode, FolderStyle, TreeNodeStyle, ActiveTreeNodeStyle);
166169

167170
if (selectionChanged)
168171
{
@@ -428,53 +431,85 @@ public class TreeNode
428431
public bool IsActive;
429432
public GUIContent content;
430433
[NonSerialized] public Texture2D Icon;
434+
public bool Selectable;
431435

432436
public void Load()
433437
{
434438
content = new GUIContent(Label, Icon);
435439
}
436440

437-
public bool Render(Rect rect, float indentation, bool isSelected, GUIStyle folderStyle, GUIStyle nodeStyle, GUIStyle activeNodeStyle)
441+
public bool Render(Rect rect, float indentation, bool isSelected, GUIStyle toggleStyle, GUIStyle nodeStyle, GUIStyle activeNodeStyle)
438442
{
439443
if (IsHidden)
440444
return false;
441445

442-
GUIStyle style;
443-
if (IsFolder)
444-
{
445-
style = folderStyle;
446-
}
447-
else
446+
var changed = false;
447+
var fillRect = rect;
448+
var nodeStartX = Level * indentation * (Selectable ? 2 : 1);
449+
450+
if (Selectable && Level > 0)
448451
{
449-
style = IsActive ? activeNodeStyle : nodeStyle;
452+
nodeStartX += 2 * Level;
450453
}
451454

452-
bool changed = false;
453-
var fillRect = rect;
454-
var nodeRect = new Rect(Level * indentation, rect.y, rect.width, rect.height);
455+
var nodeRect = new Rect(nodeStartX, rect.y, rect.width, rect.height);
456+
457+
var data = string.Format("Label: {0} ", Label);
458+
data += string.Format("Start: {0} ", nodeStartX);
455459

456460
if (Event.current.type == EventType.repaint)
457461
{
458462
nodeStyle.Draw(fillRect, GUIContent.none, false, false, false, isSelected);
459-
if (IsFolder)
463+
}
464+
465+
var styleOn = false;
466+
if (IsFolder)
467+
{
468+
data += string.Format("FolderStart: {0} ", nodeStartX);
469+
470+
var toggleRect = new Rect(nodeStartX, nodeRect.y, indentation, nodeRect.height);
471+
nodeStartX += toggleRect.width;
472+
473+
styleOn = !IsCollapsed;
474+
475+
if (Event.current.type == EventType.repaint)
460476
{
461-
style.Draw(nodeRect, content, false, false, !IsCollapsed, isSelected);
477+
toggleStyle.Draw(toggleRect, GUIContent.none, false, false, styleOn, isSelected);
462478
}
463-
else
479+
480+
EditorGUI.BeginChangeCheck();
464481
{
465-
style.Draw(nodeRect, content, false, false, false, isSelected);
482+
GUI.Toggle(toggleRect, !IsCollapsed, GUIContent.none, GUIStyle.none);
466483
}
484+
changed = EditorGUI.EndChangeCheck();
467485
}
468486

469-
if (IsFolder)
487+
if (Selectable)
470488
{
471-
var toggleRect = new Rect(nodeRect.x, nodeRect.y, style.border.horizontal, nodeRect.height);
489+
data += string.Format("SelectStart: {0} ", nodeStartX);
490+
491+
var selectRect = new Rect(nodeStartX, nodeRect.y, indentation, nodeRect.height);
492+
493+
nodeStartX += selectRect.width + 2;
472494

473495
EditorGUI.BeginChangeCheck();
474-
GUI.Toggle(toggleRect, !IsCollapsed, GUIContent.none, GUIStyle.none);
475-
changed = EditorGUI.EndChangeCheck();
496+
{
497+
GUI.Toggle(selectRect, false, GUIContent.none, Styles.ToggleMixedStyle);
498+
}
499+
EditorGUI.EndChangeCheck();
476500
}
477501

502+
data += string.Format("ContentStart: {0} ", nodeStartX);
503+
var contentStyle = IsActive ? activeNodeStyle : nodeStyle;
504+
505+
var contentRect = new Rect(nodeStartX, rect.y, rect.width, rect.height);
506+
if (Event.current.type == EventType.repaint)
507+
{
508+
contentStyle.Draw(contentRect, content, false, false, styleOn, isSelected);
509+
}
510+
511+
Debug.Log(data);
512+
478513
return changed;
479514
}
480515

0 commit comments

Comments
 (0)