public MultiSelectionTable(TreeViewState state, MultiColumnHeader multicolumnHeader, string[] names, TreeViewSelection selection) : base(state, multicolumnHeader) { m_AllIdentifier = new TreeItemIdentifier(); m_AllIdentifier.SetName("All"); m_AllIdentifier.SetAll(); Assert.AreEqual(m_SortOptions.Length, Enum.GetValues(typeof(MyColumns)).Length, "Ensure number of sort options are in sync with number of MyColumns enum values"); // Custom setup rowHeight = kRowHeights; showAlternatingRowBackgrounds = true; showBorder = true; customFoldoutYOffset = (kRowHeights - EditorGUIUtility.singleLineHeight) * 0.5f; // center foldout in the row since we also center content. See RowGUI // extraSpaceBeforeIconAndLabel = 0; multicolumnHeader.sortingChanged += OnSortingChanged; m_Names = names; m_Selection = new TreeViewSelection(selection); Reload(); }
private void CellGUI(Rect cellRect, SelectionWindowTreeViewItem item, MyColumns column, ref RowGUIArgs args) { // Center cell rect vertically (makes it easier to place controls, icons etc in the cells) CenterRectUsingSingleLineHeight(ref cellRect); switch (column) { case MyColumns.ItemName: { args.rowRect = cellRect; // base.RowGUI(args); // Required to show tree indenting // Draw manually to keep indenting by add a tooltip var rect = cellRect; if (Event.current.rawType == EventType.Repaint) { int selectedChildren; var childCount = GetChildCount(item.TreeItemIdentifier, out selectedChildren); string text; string tooltip; var fullName = item.TreeItemIdentifier.name; var groupName = GetItemGroupName(item); if (childCount <= 1) { text = item.displayName; tooltip = groupName == "" ? text : string.Format("{0}\n{1}", text, groupName); } else if (selectedChildren != childCount) { text = string.Format("{0} ({1} of {2})", fullName, selectedChildren, childCount); tooltip = groupName == "" ? text : string.Format("{0}\n{1}", text, groupName); } else { text = string.Format("{0} (All)", fullName); tooltip = groupName == "" ? text : string.Format("{0}\n{1}", text, groupName); } var content = new GUIContent(text, tooltip); if (m_ActiveLineStyle == null) { m_ActiveLineStyle = new GUIStyle(DefaultStyles.label); m_ActiveLineStyle.normal.textColor = DefaultStyles.boldLabel.onActive.textColor; } // The rect is assumed indented and sized after the content when pinging var indent = GetContentIndent(item) + extraSpaceBeforeIconAndLabel; rect.xMin += indent; var iconRectWidth = 16; var kSpaceBetweenIconAndText = 2; // Draw icon var iconRect = rect; iconRect.width = iconRectWidth; // iconRect.x += 7f; Texture icon = args.item.icon; if (icon != null) { GUI.DrawTexture(iconRect, icon, ScaleMode.ScaleToFit); } rect.xMin += icon == null ? 0 : iconRectWidth + kSpaceBetweenIconAndText; //bool mouseOver = rect.Contains(Event.current.mousePosition); //DefaultStyles.label.Draw(rect, content, mouseOver, false, args.selected, args.focused); // Must use this call to draw tooltip EditorGUI.LabelField(rect, content, args.selected ? m_ActiveLineStyle : DefaultStyles.label); } } break; case MyColumns.GroupName: { var groupName = GetItemGroupName(item); var content = new GUIContent(groupName, groupName); EditorGUI.LabelField(cellRect, content); } break; case MyColumns.State: var oldState = TreeItemSelected(item.TreeItemIdentifier); var newState = EditorGUI.Toggle(cellRect, oldState); if (newState != oldState) { if (item.TreeItemIdentifier.nameWithIndex == m_AllIdentifier.nameWithIndex) { // Record active groups m_Selection.groups.Clear(); if (newState) { if (!m_Selection.groups.Contains(item.TreeItemIdentifier.nameWithIndex)) { m_Selection.groups.Add(item.TreeItemIdentifier.nameWithIndex); } } // Update selection m_Selection.selection.Clear(); if (newState) { foreach (var nameWithIndex in m_Names) { if (nameWithIndex != m_AllIdentifier.nameWithIndex) { var identifier = new TreeItemIdentifier(nameWithIndex); if (identifier.index != TreeItemIdentifier.kAll) { m_Selection.selection.Add(nameWithIndex); } } } } } else if (item.TreeItemIdentifier.index == TreeItemIdentifier.kAll) { // Record active groups if (newState) { if (!m_Selection.groups.Contains(item.TreeItemIdentifier.nameWithIndex)) { m_Selection.groups.Add(item.TreeItemIdentifier.nameWithIndex); } } else { m_Selection.groups.Remove(item.TreeItemIdentifier.nameWithIndex); // When turning off a sub group, turn of the 'all' group too m_Selection.groups.Remove(m_AllIdentifier.nameWithIndex); } // Update selection if (newState) { foreach (var nameWithIndex in m_Names) { var identifier = new TreeItemIdentifier(nameWithIndex); if (identifier.name == item.TreeItemIdentifier.name && identifier.index != TreeItemIdentifier.kAll) { if (!m_Selection.selection.Contains(nameWithIndex)) { m_Selection.selection.Add(nameWithIndex); } } } } else { var removeSelection = new List <string>(); foreach (var nameWithIndex in m_Selection.selection) { var identifier = new TreeItemIdentifier(nameWithIndex); if (identifier.name == item.TreeItemIdentifier.name && identifier.index != TreeItemIdentifier.kAll) { removeSelection.Add(nameWithIndex); } } foreach (var nameWithIndex in removeSelection) { m_Selection.selection.Remove(nameWithIndex); } } } else { if (newState) { m_Selection.selection.Add(item.TreeItemIdentifier.nameWithIndex); } else { m_Selection.selection.Remove(item.TreeItemIdentifier.nameWithIndex); // Turn off any group its in too var groupIdentifier = new TreeItemIdentifier(item.TreeItemIdentifier); groupIdentifier.SetAll(); m_Selection.groups.Remove(groupIdentifier.nameWithIndex); // Turn of the 'all' group too m_Selection.groups.Remove(m_AllIdentifier.nameWithIndex); } } } break; } }