internal float GetSelectionHeight(AdvancedDropdownDataSource dataSource, Rect buttonRect) { if (State.GetSelectedIndex(dataSource.MainTree) == -1) { return(0); } float heigth = 0; for (int i = 0; i < dataSource.MainTree.Children.Count(); i++) { AdvancedDropdownItem child = dataSource.MainTree.Children.ElementAt(i); GUIContent content = new GUIContent(child.Name, child.Icon); if (State.GetSelectedIndex(dataSource.MainTree) == i) { float diff = (LineStyle.CalcHeight(content, 0) - buttonRect.height) / 2f; return(heigth + diff); } if (child.IsSeparator()) { heigth += Styles.lineSeparator.CalcHeight(content, 0) + Styles.lineSeparator.margin.vertical; } else { heigth += LineStyle.CalcHeight(content, 0); } } return(heigth); }
public void Show(Rect rect) { if (windowInstance != null) { windowInstance.Close(); windowInstance = null; } if (dataSource == null) { dataSource = new CallbackDataSource(BuildRoot); } if (gui == null) { gui = new AdvancedDropdownGUI(); } windowInstance = ScriptableObject.CreateInstance <AdvancedDropdownWindow> (); if (minimumSize != Vector2.zero) { windowInstance.minSize = minimumSize; } if (maximumSize != Vector2.zero) { windowInstance.maxSize = maximumSize; } windowInstance.State = state; windowInstance.DataSource = dataSource; windowInstance.Gui = gui; windowInstance.WindowClosed += (w) => ItemSelected(w.GetSelectedItem()); windowInstance.Init(rect); }
public void Init(Rect buttonRect) { Vector2 screenPoint = GUIUtility.GUIToScreenPoint(new Vector2(buttonRect.x, buttonRect.y)); buttonRectScreenPos.x = screenPoint.x; buttonRectScreenPos.y = screenPoint.y; if (state == null) { state = new AdvancedDropdownState(); } if (dataSource == null) { dataSource = new MultiLevelDataSource(); } if (gui == null) { gui = new AdvancedDropdownGUI(); } gui.State = state; 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(); currentlyRenderedTree = HasSearch ? dataSource.SearchTree : dataSource.MainTree; ShowAsDropDown(buttonRect, CalculateWindowSize(buttonRectScreenPos, out requiredDropdownSize)); // If the dropdown is as height as the screen height, give it some margin if (position.height < requiredDropdownSize.y) { Rect pos = position; pos.y += 5; pos.height -= 10; position = pos; } if (SetInitialSelectionPosition) { initialSelectionPosition = gui.GetSelectionHeight(dataSource, buttonRect); } wantsMouseMove = true; SetSelectionFromState(); }
internal Vector2 CalculateContentSize(AdvancedDropdownDataSource dataSource) { float maxWidth = 0; float maxHeight = 0; bool includeArrow = false; float arrowWidth = 0; foreach (AdvancedDropdownItem child in dataSource.MainTree.Children) { GUIContent content = new GUIContent(child.Name, child.Icon); Vector2 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 += Styles.lineSeparator.CalcHeight(content, maxWidth) + 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)); }