protected override TreeViewItem BuildRoot()
        {
            var idForHiddenRoot    = -1;
            var depthForHiddenRoot = -1;
            var root = new TreeViewItem(idForHiddenRoot, depthForHiddenRoot, "root");

            var depth = 0;

            var top = new SelectionWindowTreeViewItem(-1, depth, m_AllIdentifier.name, m_AllIdentifier);

            root.AddChild(top);

            var expandList = new List <int> {
                -1
            };
            var lastName = "";
            var node     = root;

            for (var index = 0; index < m_Names.Length; ++index)
            {
                var nameWithIndex = m_Names[index];
                if (nameWithIndex == m_AllIdentifier.nameWithIndex)
                {
                    continue;
                }

                var identifier = new TreeItemIdentifier(nameWithIndex);
                var item       = new SelectionWindowTreeViewItem(index, depth, m_Names[index], identifier);

                if (identifier.name != lastName)
                {
                    // New items at root
                    node  = top;
                    depth = 0;
                }

                node.AddChild(item);

                if (identifier.name != lastName)
                {
                    // Extra instances hang of the parent
                    lastName = identifier.name;
                    node     = item;
                    depth    = 1;
                }
            }

            SetExpanded(expandList);

            SetupDepthsFromParentsAndChildren(root);

            return(root);
        }
        protected int GetChildCount(TreeItemIdentifier selectedIdentifier, out int selected)
        {
            var count         = 0;
            var selectedCount = 0;

            if (selectedIdentifier.index == TreeItemIdentifier.kAll)
            {
                if (selectedIdentifier.name == "All")
                {
                    for (var index = 0; index < m_Names.Length; ++index)
                    {
                        var nameWithIndex = m_Names[index];
                        var identifier    = new TreeItemIdentifier(nameWithIndex);

                        if (identifier.index != TreeItemIdentifier.kAll)
                        {
                            count++;
                            if (m_Selection.selection.Contains(nameWithIndex))
                            {
                                selectedCount++;
                            }
                        }
                    }
                }
                else
                {
                    for (var index = 0; index < m_Names.Length; ++index)
                    {
                        var nameWithIndex = m_Names[index];
                        var identifier    = new TreeItemIdentifier(nameWithIndex);

                        if (selectedIdentifier.name == identifier.name &&
                            identifier.index != TreeItemIdentifier.kAll)
                        {
                            count++;
                            if (m_Selection.selection.Contains(nameWithIndex))
                            {
                                selectedCount++;
                            }
                        }
                    }
                }
            }

            selected = selectedCount;
            return(count);
        }
        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(Column)).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();
        }
Example #4
0
 public TreeItemIdentifier(TreeItemIdentifier treeItemIdentifier)
 {
     name = treeItemIdentifier.name;
     index = treeItemIdentifier.index;
     nameWithIndex = treeItemIdentifier.nameWithIndex;
 }
Example #5
0
 public SelectionWindowTreeViewItem(int id, int depth, string displayName, TreeItemIdentifier treeItemIdentifier)
     : base(id, depth, displayName)
 {
     TreeItemIdentifier = treeItemIdentifier;
 }
        void CellGUI(Rect cellRect, SelectionWindowTreeViewItem item, Column 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 Column.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 Column.GroupName:
            {
                var groupName = GetItemGroupName(item);
                var content   = new GUIContent(groupName);
                EditorGUI.LabelField(cellRect, content);
            }
            break;

            case Column.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;
            }
        }
        bool TreeItemSelected(TreeItemIdentifier selectedIdentifier)
        {
            if (m_Selection.selection != null &&
                m_Selection.selection.Count > 0 &&
                m_Selection.selection.Contains(selectedIdentifier.nameWithIndex))
            {
                return(true);
            }

            // If querying the 'All' filter then check if all selected
            if (selectedIdentifier.nameWithIndex == m_AllIdentifier.nameWithIndex)
            {
                // Check all items without All in the name are selected
                foreach (var nameWithIndex in m_Names)
                {
                    var identifier = new TreeItemIdentifier(nameWithIndex);
                    if (identifier.index ==
                        TreeItemIdentifier.kAll /* || identifier.index == TreeItemIdentifier.kSingle*/)
                    {
                        continue;
                    }

                    if (!m_Selection.selection.Contains(nameWithIndex))
                    {
                        return(false);
                    }
                }

                return(true);
            }

            // Need to check 'all' and item group all.
            if (selectedIdentifier.index == TreeItemIdentifier.kAll)
            {
                // Count all items that match this item group
                var count = 0;
                foreach (var nameWithIndex in m_Names)
                {
                    var identifier = new TreeItemIdentifier(nameWithIndex);
                    if (identifier.index == TreeItemIdentifier.kAll || identifier.index == TreeItemIdentifier.kSingle)
                    {
                        continue;
                    }

                    if (selectedIdentifier.name != identifier.name)
                    {
                        continue;
                    }

                    count++;
                }

                // Count all the items we have selected that match this item group
                var selectedCount = 0;
                foreach (var nameWithIndex in m_Selection.selection)
                {
                    var identifier = new TreeItemIdentifier(nameWithIndex);
                    if (selectedIdentifier.name != identifier.name)
                    {
                        continue;
                    }
                    if (identifier.index > count)
                    {
                        continue;
                    }

                    selectedCount++;
                }

                if (count == selectedCount)
                {
                    return(true);
                }
            }

            return(false);
        }