/// <summary> /// This is basically a constructor. Since ScriptableObjects cannot have constructors, /// this one is called from a factory method. /// </summary> /// <param name="selectionTree">Tree that contains the dropdown items to show.</param> /// <param name="windowHeight">Height of the window. If set to 0, it will be auto-adjusted.</param> /// <param name="windowPosition">Position of the window to set.</param> private void OnCreate(SelectionTree selectionTree, float windowHeight, Vector2 windowPosition, DropdownWindowType windowType) { ResetControl(); wantsMouseMove = true; _selectionTree = selectionTree; _selectionTree.SelectionChanged += Close; _optimalWidth = CalculateOptimalWidth(_selectionTree.SelectionPaths); _preventExpandingHeight = new PreventExpandingHeight(windowHeight == 0f); _positionOnCreation = GetWindowRect(windowPosition, windowHeight); if (windowType == DropdownWindowType.Dropdown) { // ShowAsDropDown usually shows the window under a button, but since we don't need to align the window to // any button, we set buttonRect.height to 0f. Rect buttonRect = new Rect(_positionOnCreation) { height = 0f }; ShowAsDropDown(buttonRect, _positionOnCreation.size); } else if (windowType == DropdownWindowType.Popup) { position = _positionOnCreation; ShowPopup(); } else { throw new Exception("Unknown window type"); } }
/// <summary> /// This is basically a constructor. Since ScriptableObjects cannot have constructors, /// this one is called from a factory method. /// </summary> /// <param name="selectionTree">Tree that contains the dropdown items to show.</param> /// <param name="windowHeight">Height of the window. If set to 0, it will be auto-adjusted.</param> private void OnCreate(SelectionTree selectionTree, float windowHeight) { ResetControl(); wantsMouseMove = true; _selectionTree = selectionTree; _selectionTree.SelectionChanged += Close; float windowWidth = CalculateOptimalWidth(); _preventExpandingHeight = new PreventExpandingHeight(windowHeight == 0f); Vector2 windowPosition = GUIUtility.GUIToScreenPoint(Event.current.mousePosition); var windowSize = new Vector2(windowWidth, windowHeight); // ShowAsDropDown usually shows the window under a button, but since we don't need to align the window to // any button, we set buttonRect.height to 0f. var buttonRect = new Rect(windowPosition, new Vector2(windowSize.x, 0f)); ShowAsDropDown(buttonRect, windowSize); }
/// <summary> /// This is basically a constructor. Since ScriptableObjects cannot have constructors, /// this one is called from a factory method. /// </summary> /// <param name="selectionTree">Tree that contains the dropdown items to show.</param> /// <param name="windowHeight">Height of the window. If set to 0, it will be auto-adjusted.</param> private void OnCreate(SelectionTree selectionTree, float windowHeight) { ResetControl(); wantsMouseMove = true; _selectionTree = selectionTree; _selectionTree.SelectionChanged += Close; _optimalWidth = CalculateOptimalWidth(); _preventExpandingHeight = new PreventExpandingHeight(windowHeight == 0f); Vector2 windowPosition = GUIUtility.GUIToScreenPoint(Event.current.mousePosition); // If the window width is smaller than the distance from cursor to the right border of the window, the // window will not appear because the cursor is outside of the window and OnGUI will never be called. float distanceToRightBorder = Screen.currentResolution.width - windowPosition.x + 8f; var windowSize = new Vector2(Mathf.Max(distanceToRightBorder, _optimalWidth), windowHeight); // ShowAsDropDown usually shows the window under a button, but since we don't need to align the window to // any button, we set buttonRect.height to 0f. var buttonRect = new Rect(windowPosition, new Vector2(distanceToRightBorder, 0f)); ShowAsDropDown(buttonRect, windowSize); }