internal float GetSelectionHeight(AdvancedDropdownDataSource dataSource, Rect buttonRect)
        {
            if (state.GetSelectedIndex(dataSource.mainTree) == -1)
            {
                return(0);
            }
            var height = 0f;

            for (var i = 0; i < dataSource.mainTree.children.Count(); i++)
            {
                var child   = dataSource.mainTree.children.ElementAt(i);
                var content = new GUIContent(child.name, child.icon);
                if (state.GetSelectedIndex(dataSource.mainTree) == i)
                {
                    var diff = (lineStyle.CalcHeight(content, 0) - buttonRect.height) / 2f;
                    return(height + diff);
                }
                if (child.IsSeparator())
                {
                    height += GUIHelpers.Styles.lineSeparator.CalcHeight(content, 0) + GUIHelpers.Styles.lineSeparator.margin.vertical;
                }
                else
                {
                    height += lineStyle.CalcHeight(content, 0);
                }
            }
            return(height);
        }
Esempio n. 2
0
        public void Show(Rect rect)
        {
            if (m_WindowInstance != null)
            {
                m_WindowInstance.Close();
                m_WindowInstance = null;
            }
            if (m_DataSource == null)
            {
                m_DataSource = new CallbackDataSource(BuildRoot, BuildCustomSearch);
            }
            if (m_Gui == null)
            {
                m_Gui = new AdvancedDropdownGUI();
            }

            m_WindowInstance = ScriptableObject.CreateInstance <AdvancedDropdownWindow>();
            if (minimumSize != Vector2.zero)
            {
                m_WindowInstance.minSize = minimumSize;
            }
            if (maximumSize != Vector2.zero)
            {
                m_WindowInstance.maxSize = maximumSize;
            }
            m_WindowInstance.state         = m_State;
            m_WindowInstance.dataSource    = m_DataSource;
            m_WindowInstance.gui           = m_Gui;
            m_WindowInstance.windowClosed +=
                w => { ItemSelected(w.GetSelectedItem()); };
            m_WindowInstance.windowDestroyed += OnDestroy;
            m_WindowInstance.Init(rect);
        }
Esempio n. 3
0
        public void Init(Rect buttonRect)
        {
            var screenPoint = GUIUtility.GUIToScreenPoint(new Vector2(buttonRect.x, buttonRect.y));

            m_ButtonRectScreenPos.x = screenPoint.x;
            m_ButtonRectScreenPos.y = screenPoint.y;

            if (m_State == null)
            {
                m_State = new AdvancedDropdownState();
            }
            if (m_DataSource == null)
            {
                m_DataSource = new MultiLevelDataSource();
            }
            if (m_Gui == null)
            {
                m_Gui = new AdvancedDropdownGUI();
            }
            m_Gui.state = m_State;
            m_Gui.Init();

            // Has to be done before calling Show / ShowWithMode
            screenPoint  = GUIUtility.GUIToScreenPoint(new Vector2(buttonRect.x, buttonRect.y));
            buttonRect.x = screenPoint.x;
            buttonRect.y = screenPoint.y;

            Vector2 requiredDropdownSize;

            OnDirtyList();
            m_CurrentlyRenderedTree = hasSearch ? m_DataSource.searchTree : m_DataSource.mainTree;
            ShowAsDropDown(buttonRect, CalculateWindowSize(m_ButtonRectScreenPos, out requiredDropdownSize));

            // If the dropdown is as height as the screen height, give it some margin
            if (position.height < requiredDropdownSize.y)
            {
                var pos = position;
                pos.y      += 5;
                pos.height -= 10;
                position    = pos;
            }

            if (setInitialSelectionPosition)
            {
                m_InitialSelectionPosition = m_Gui.GetSelectionHeight(m_DataSource, buttonRect);
            }

            wantsMouseMove = true;
            SetSelectionFromState();
        }
        internal Vector2 CalculateContentSize(AdvancedDropdownDataSource dataSource)
        {
            var maxWidth     = 0f;
            var maxHeight    = 0f;
            var includeArrow = false;
            var arrowWidth   = 0f;

            foreach (var child in dataSource.mainTree.children)
            {
                var content = new GUIContent(child.name, child.icon);
                var a       = lineStyle.CalcSize(content);
                a.x += iconSize.x + 1;

                if (maxWidth < a.x)
                {
                    maxWidth      = a.x + 1;
                    includeArrow |= child.children.Any();
                }
                if (child.IsSeparator())
                {
                    maxHeight += GUIHelpers.Styles.lineSeparator.CalcHeight(content, maxWidth) + GUIHelpers.Styles.lineSeparator.margin.vertical;
                }
                else
                {
                    maxHeight += lineStyle.CalcHeight(content, maxWidth);
                }
                if (arrowWidth == 0)
                {
                    lineStyle.CalcMinMaxWidth(Styles.arrowRightContent, out arrowWidth, out arrowWidth);
                }
            }
            if (includeArrow)
            {
                maxWidth += arrowWidth;
            }
            return(new Vector2(maxWidth, maxHeight));
        }