Beispiel #1
0
 private AdvancedDropdownItemState GetStateForItem(AdvancedDropdownItem item)
 {
     if (m_LastSelectedState != null && m_LastSelectedState.itemId == item.id)
     {
         return(m_LastSelectedState);
     }
     for (int i = 0; i < states.Length; i++)
     {
         if (states[i].itemId == item.id)
         {
             m_LastSelectedState = states[i];
             return(m_LastSelectedState);
         }
     }
     Array.Resize(ref states, states.Length + 1);
     states[states.Length - 1] = new AdvancedDropdownItemState(item);
     m_LastSelectedState       = states[states.Length - 1];
     return(states[states.Length - 1]);
 }
        void SetSelectionFromState()
        {
            var selectedIndex = m_State.GetSelectedIndex(m_CurrentlyRenderedTree);

            while (selectedIndex >= 0)
            {
                var child = m_State.GetSelectedChild(m_CurrentlyRenderedTree);
                if (child == null)
                {
                    break;
                }
                selectedIndex = m_State.GetSelectedIndex(child);
                if (selectedIndex < 0)
                {
                    break;
                }
                m_ViewsStack.Push(m_CurrentlyRenderedTree);
                m_CurrentlyRenderedTree = child;
            }
        }
Beispiel #3
0
        internal void MoveUpSelection(AdvancedDropdownItem item)
        {
            var state         = GetStateForItem(item);
            var selectedIndex = state.selectedIndex;

            do
            {
                --selectedIndex;
            }while (selectedIndex >= 0 && item.children.ElementAt(selectedIndex).IsSeparator());

            if (selectedIndex < 0)
            {
                selectedIndex = item.children.Count() - 1;
            }

            if (selectedIndex >= 0)
            {
                SetSelectionOnItem(item, selectedIndex);
            }
        }
        private void DrawDropdown(float anim, AdvancedDropdownItem group)
        {
            // Start of animated area (the part that moves left and right)
            var areaPosition = new Rect(0, 0, position.width, position.height);

            // Adjust to the frame
            areaPosition.x      += kBorderThickness;
            areaPosition.y      += kBorderThickness;
            areaPosition.height -= kBorderThickness * 2;
            areaPosition.width  -= kBorderThickness * 2;

            GUILayout.BeginArea(m_Gui.GetAnimRect(areaPosition, anim));
            // Header
            if (showHeader)
            {
                m_Gui.DrawHeader(group, GoToParent, m_ViewsStack.Count > 0);
            }

            DrawList(group);
            GUILayout.EndArea();
        }
Beispiel #5
0
        protected bool AddMatchItem(AdvancedDropdownItem e, string name, string[] searchWords, List <AdvancedDropdownItem> matchesStart, List <AdvancedDropdownItem> matchesWithin)
        {
            var didMatchAll   = true;
            var didMatchStart = false;

            // See if we match ALL the seaarch words.
            for (var w = 0; w < searchWords.Length; w++)
            {
                var search = searchWords[w];
                if (name.Contains(search))
                {
                    // If the start of the item matches the first search word, make a note of that.
                    if (w == 0 && name.StartsWith(search))
                    {
                        didMatchStart = true;
                    }
                }
                else
                {
                    // As soon as any word is not matched, we disregard this item.
                    didMatchAll = false;
                    break;
                }
            }
            // We always need to match all search words.
            // If we ALSO matched the start, this item gets priority.
            if (didMatchAll)
            {
                if (didMatchStart)
                {
                    matchesStart.Add(e);
                }
                else
                {
                    matchesWithin.Add(e);
                }
            }
            return(didMatchAll);
        }
Beispiel #6
0
        protected virtual AdvancedDropdownItem Search(string searchString)
        {
            if (m_SearchableElements == null)
            {
                BuildSearchableElements();
            }
            if (string.IsNullOrEmpty(searchString))
            {
                return(null);
            }

            // Support multiple search words separated by spaces.
            var searchWords = searchString.ToLower().Split(' ');

            // We keep two lists. Matches that matches the start of an item always get first priority.
            var matchesStart  = new List <AdvancedDropdownItem>();
            var matchesWithin = new List <AdvancedDropdownItem>();

            foreach (var e in m_SearchableElements)
            {
                var name = e.name.ToLower().Replace(" ", "");
                AddMatchItem(e, name, searchWords, matchesStart, matchesWithin);
            }

            var searchTree = new AdvancedDropdownItem(kSearchHeader);

            matchesStart.Sort();
            foreach (var element in matchesStart)
            {
                searchTree.AddChild(element);
            }
            matchesWithin.Sort();
            foreach (var element in matchesWithin)
            {
                searchTree.AddChild(element);
            }
            return(searchTree);
        }
Beispiel #7
0
        void SetSelectionFromState()
        {
            var selectedIndex = m_State.GetSelectedIndex(m_CurrentlyRenderedTree);

            while (selectedIndex >= 0)
            {
                var child = m_State.GetSelectedChild(m_CurrentlyRenderedTree);
                if (child == null)
                {
                    break;
                }
                if (child.id == m_CurrentlyRenderedTree.id)
                {
                    Debug.LogWarning($"Same id: {child.id} given to both {child.displayName} and {m_CurrentlyRenderedTree.displayName}. Selection may be wrong.");
                }
                selectedIndex = m_State.GetSelectedIndex(child);
                if (selectedIndex < 0)
                {
                    break;
                }
                m_ViewsStack.Push(m_CurrentlyRenderedTree);
                m_CurrentlyRenderedTree = child;
            }
        }
Beispiel #8
0
        protected override AdvancedDropdownItem FetchData()
        {
            selectedIDs.Clear();
            var rootGroup = new AdvancedDropdownItem("");

            for (int i = 0; i < m_DisplayedOptions.Length; i++)
            {
                var element = new AdvancedDropdownItem(m_DisplayedOptions[i].text)
                {
                    icon = (Texture2D)m_DisplayedOptions[i].image
                };
                element.elementIndex = i;
                rootGroup.AddChild(element);
                if (i == m_SelectedIndex)
                {
                    selectedIDs.Add(element.id);
                    if (m_State != null)
                    {
                        m_State.SetSelectedIndex(rootGroup, i);
                    }
                }
            }
            return(rootGroup);
        }
Beispiel #9
0
 public void RebuildSearch(string search)
 {
     m_SearchTree = Search(search);
 }
Beispiel #10
0
 public void ReloadData()
 {
     m_MainTree = FetchData();
 }
        private void DrawList(AdvancedDropdownItem item)
        {
            // Start of scroll view list
            m_State.SetScrollState(item, GUILayout.BeginScrollView(m_State.GetScrollState(item), GUIStyle.none, GUI.skin.verticalScrollbar));
            EditorGUIUtility.SetIconSize(m_Gui.iconSize);
            Rect selectedRect = new Rect();

            for (var i = 0; i < item.children.Count(); i++)
            {
                var  child    = item.children.ElementAt(i);
                bool selected = m_State.GetSelectedIndex(item) == i;

                if (child.IsSeparator())
                {
                    m_Gui.DrawLineSeparator();
                }
                else
                {
                    m_Gui.DrawItem(child, child.name, child.icon, child.enabled, child.children.Any(), selected, hasSearch);
                }

                var r = GUILayoutUtility.GetLastRect();
                if (selected)
                {
                    selectedRect = r;
                }

                // Skip input handling for the tree used for animation
                if (item != m_CurrentlyRenderedTree)
                {
                    continue;
                }

                // Select the element the mouse cursor is over.
                // Only do it on mouse move - keyboard controls are allowed to overwrite this until the next time the mouse moves.
                if (Event.current.type == EventType.MouseMove || Event.current.type == EventType.MouseDrag)
                {
                    if (!selected && r.Contains(Event.current.mousePosition))
                    {
                        m_State.SetSelectedIndex(item, i);
                        Event.current.Use();
                    }
                }
                if (Event.current.type == EventType.MouseUp && r.Contains(Event.current.mousePosition))
                {
                    m_State.SetSelectedIndex(item, i);
                    var selectedChild = m_State.GetSelectedChild(item);
                    if (selectedChild.children.Any())
                    {
                        GoToChild();
                    }
                    else
                    {
                        if (!selectedChild.IsSeparator() && selectionChanged != null)
                        {
                            selectionChanged(selectedChild);
                        }
                        if (closeOnSelection)
                        {
                            CloseWindow();
                            GUIUtility.ExitGUI();
                        }
                    }
                    Event.current.Use();
                }
            }
            EditorGUIUtility.SetIconSize(Vector2.zero);
            GUILayout.EndScrollView();

            // Scroll to selected on windows creation
            if (m_ScrollToSelected && m_InitialSelectionPosition != 0)
            {
                float diffOfPopupAboveTheButton = m_ButtonRectScreenPos.y - position.y;
                diffOfPopupAboveTheButton -= m_Gui.searchHeight + m_Gui.headerHeight;
                m_State.SetScrollState(item, new Vector2(0, m_InitialSelectionPosition - diffOfPopupAboveTheButton));
                m_ScrollToSelected         = false;
                m_InitialSelectionPosition = 0;
            }
            // Scroll to show selected
            else if (m_ScrollToSelected && Event.current.type == EventType.Repaint)
            {
                m_ScrollToSelected = false;
                Rect scrollRect = GUILayoutUtility.GetLastRect();
                if (selectedRect.yMax - scrollRect.height > m_State.GetScrollState(item).y)
                {
                    m_State.SetScrollState(item, new Vector2(0, selectedRect.yMax - scrollRect.height));
                    Repaint();
                }
                if (selectedRect.y < m_State.GetScrollState(item).y)
                {
                    m_State.SetScrollState(item, new Vector2(0, selectedRect.y));
                    Repaint();
                }
            }
        }
Beispiel #12
0
 public AdvancedDropdownItemState(AdvancedDropdownItem item)
 {
     this.itemId = item.id;
 }
Beispiel #13
0
 internal void SetScrollState(AdvancedDropdownItem item, Vector2 scrollState)
 {
     GetStateForItem(item).scroll = scrollState;
 }
Beispiel #14
0
 internal Vector2 GetScrollState(AdvancedDropdownItem item)
 {
     return(GetStateForItem(item).scroll);
 }
Beispiel #15
0
 internal void SetSelectedIndex(AdvancedDropdownItem item, int index)
 {
     GetStateForItem(item).selectedIndex = index;
 }
Beispiel #16
0
 internal int GetSelectedIndex(AdvancedDropdownItem item)
 {
     return(GetStateForItem(item).selectedIndex);
 }
Beispiel #17
0
 public void AddChild(AdvancedDropdownItem child)
 {
     m_Children.Add(child);
 }
 protected virtual void ItemSelected(AdvancedDropdownItem item)
 {
 }