Exemplo n.º 1
0
        public void SetGroup(string groupName)
        {
            groups.Clear();
            selection.Clear();

            var allTreeViewSelection = new TreeItemIdentifier(groupName, TreeItemIdentifier.kAll);

            groups.Add(allTreeViewSelection.nameWithIndex);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        public void SetAll(string[] names)
        {
            groups.Clear();
            var allIdentifier = new TreeItemIdentifier("All", TreeItemIdentifier.kAll);

            groups.Add(allIdentifier.nameWithIndex);

            selection.Clear();
            foreach (var nameWithIndex in names)
            {
                if (nameWithIndex != allIdentifier.nameWithIndex)
                {
                    var identifier = new TreeItemIdentifier(nameWithIndex);
                    if (identifier.index != TreeItemIdentifier.kAll)
                    {
                        selection.Add(nameWithIndex);
                    }
                }
            }
        }
Exemplo n.º 5
0
        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();
        }
Exemplo n.º 6
0
        // stephenm TODO - This seems wildly more complex than it needs to be... UNLESS assemblies can have sub-assemblies?
        // If that's the case, we need to test for that. Otherwise we need to strip a bunch of this complexity out.
        public string[] GetSelectedStrings(string[] names, bool summarize)
        {
            if (selection == null || selection.Count == 0)
            {
                if (summarize)
                {
                    return(new[] { "None" });
                }

                return(null);
            }

            // Count all items in a group
            var dict          = new Dictionary <string, int>();
            var selectionDict = new Dictionary <string, int>();

            foreach (var nameWithIndex in names)
            {
                var identifier = new TreeItemIdentifier(nameWithIndex);
                if (identifier.index == TreeItemIdentifier.kAll)
                {
                    continue;
                }

                int count;
                if (dict.TryGetValue(identifier.name, out count))
                {
                    dict[identifier.name] = count + 1;
                }
                else
                {
                    dict[identifier.name] = 1;
                }

                selectionDict[identifier.name] = 0;
            }

            // Count all the items we have 'selected' in a group
            foreach (var nameWithIndex in selection)
            {
                var identifier = new TreeItemIdentifier(nameWithIndex);

                if (dict.ContainsKey(identifier.name) &&
                    selectionDict.ContainsKey(identifier.name) &&
                    identifier.index <= dict[identifier.name])
                {
                    // Selected assembly valid and in the assembly list
                    // and also within the range of valid assemblies for this data set
                    selectionDict[identifier.name]++;
                }
            }

            // Count all groups where we have 'selected all the items'
            var selectedCount = 0;

            foreach (var name in dict.Keys)
            {
                if (selectionDict[name] != dict[name])
                {
                    continue;
                }

                selectedCount++;
            }

            // If we've just added all the item names we have everything selected
            // Note we don't compare against the names array directly as this contains the 'all' versions
            if (summarize && selectedCount == dict.Keys.Count)
            {
                return new[] { "All" }
            }
            ;

            // Add all the individual items were we haven't already added the group
            var individualItems = new List <string>();

            foreach (var name in selectionDict.Keys)
            {
                var selectionCount = selectionDict[name];

                if (selectionCount <= 0)
                {
                    continue;
                }
                var itemCount = dict[name];
                if (itemCount == 1)
                {
                    individualItems.Add(name);
                }
                else if (selectionCount != itemCount)
                {
                    individualItems.Add(string.Format("{0} ({1} of {2})", name, selectionCount, itemCount));
                }
                else
                {
                    individualItems.Add(string.Format("{0} (All)", name));
                }
            }

            // Maintain alphabetical order
            individualItems.Sort(CompareUINames);

            return(individualItems.ToArray());
        }

        int CompareUINames(string a, string b)
        {
            var aTokens = a.Split(':');
            var bTokens = b.Split(':');

            if (aTokens.Length > 1 && bTokens.Length > 1)
            {
                var firstName  = aTokens[0].Trim();
                var secondName = bTokens[0].Trim();

                if (firstName == secondName)
                {
                    var firstNameIndex  = aTokens[1].Trim();
                    var secondNameIndex = bTokens[1].Trim();

                    if (firstNameIndex == "All" && secondNameIndex != "All")
                    {
                        return(-1);
                    }
                    if (firstNameIndex != "All" && secondNameIndex == "All")
                    {
                        return(1);
                    }

                    int aGroupIndex;
                    if (int.TryParse(firstNameIndex, out aGroupIndex))
                    {
                        int bGroupIndex;
                        if (int.TryParse(secondNameIndex, out bGroupIndex))
                        {
                            return(aGroupIndex.CompareTo(bGroupIndex));
                        }
                    }
                }
            }

            return(a.CompareTo(b));
        }
    }
Exemplo n.º 7
0
        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;
            }
        }
Exemplo n.º 8
0
        private 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);
        }
 public TreeItemIdentifier(TreeItemIdentifier treeItemIdentifier)
 {
     name          = treeItemIdentifier.name;
     index         = treeItemIdentifier.index;
     nameWithIndex = treeItemIdentifier.nameWithIndex;
 }
 public SelectionWindowTreeViewItem(int id, int depth, string displayName, TreeItemIdentifier treeItemIdentifier)
     : base(id, depth, displayName)
 {
     TreeItemIdentifier = treeItemIdentifier;
 }