Exemplo n.º 1
0
        private float GetWindowXPosition(float requestedXPosition, float windowWidth)
        {
            // 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 screenWidth = EditorGUIUtilityHelper.GetScreenWidth();

            requestedXPosition -= 8f; // This will make the window appear so that foldout arrows are precisely below the cursor.
            float distanceToRightBorder = screenWidth - requestedXPosition;

            if (windowWidth > distanceToRightBorder)
            {
                requestedXPosition = screenWidth - windowWidth;
            }

            return(requestedXPosition);
        }
        /// <summary>Resizes the window to the needed size.</summary>
        /// <param name="window">The window to change the size of.</param>
        /// <param name="width">The width to set. If the value is -1f, the width will not be changed.</param>
        /// <param name="height">The height to set. If the value is -1f, the height will not be changed.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// Thrown if width or height are negative numbers and not -1f.
        /// </exception>
        /// <example><code>
        /// public class DummyWindow : EditorWindow
        /// {
        ///     private void OnCreate(Rect buttonRect)
        ///     {
        ///         var windowSize = new Vector2(100f, 100f);
        ///         ShowAsDropDown(buttonRect, windowSize);
        ///     }
        ///
        ///     private void OnGUI()
        ///     {
        ///         float optimalWidth = CalculateOptimalWidth();
        ///         float optimalHeight = Math.Min(_contentHeight, DropdownStyle.MaxWindowHeight);
        ///         this.Resize(optimalWidth, optimalHeight);
        ///     }
        /// }
        /// </code></example>
        [PublicAPI] public static void Resize(this EditorWindow window, float width = -1f, float height = -1f)
        {
            EnsureTheValueIsNotNegative(nameof(width), width);
            EnsureTheValueIsNotNegative(nameof(height), height);

            bool changeWidth  = width != -1f;
            bool changeHeight = height != -1f;

            Rect positionToAdjust = window.position;

            if (changeWidth)
            {
                positionToAdjust.width = width;
            }

            if (changeHeight)
            {
                positionToAdjust.height = height;
            }

            window.minSize = new Vector2(changeWidth ? width : window.minSize.x, changeHeight ? height : window.minSize.y);
            window.maxSize = new Vector2(changeWidth ? width : window.maxSize.x, changeHeight ? height : window.maxSize.y);

            if (changeWidth)
            {
                float screenWidth = EditorGUIUtilityHelper.GetScreenWidth();
                if (positionToAdjust.xMax >= screenWidth)
                {
                    positionToAdjust.x -= positionToAdjust.xMax - screenWidth;
                }
            }

            if (changeHeight)
            {
                // MainWindow is more reliable than Screen.currentResolution.height, especially for the multi-display setup.
                float mainWinYMax = EditorGUIUtilityHelper.GetMainWindowPosition().yMax;

                if (positionToAdjust.yMax >= mainWinYMax)
                {
                    positionToAdjust.y -= positionToAdjust.yMax - mainWinYMax;
                }
            }

            window.position = positionToAdjust;
        }