Beispiel #1
0
        public override void OnGUI(Rect rect)
        {
            if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Return)
            {
                this.editorWindow.Close();
            }

            const float offset = 10f;
            const float size   = 40f;

            for (int x = 0; x < 3; ++x)
            {
                for (int y = 0; y < 3; ++y)
                {
                    if (this.drawMiddle == false && x == 1 && y == 1)
                    {
                        continue;
                    }

                    var hMode = (WindowSystemSidePropertyDrawer.Layout)x;
                    var vMode = (WindowSystemSidePropertyDrawer.Layout)y;

                    var buttonRect = rect;
                    buttonRect.x      = offset + size * x;
                    buttonRect.y      = offset + size * y;
                    buttonRect.width  = size;
                    buttonRect.height = size;
                    if (WindowSystemSidePropertyDrawer.DrawButton(buttonRect, hMode, vMode) == true)
                    {
                        if (this.callback != null)
                        {
                            this.callback.Invoke(hMode, vMode);
                        }
                        this.editorWindow.Close();
                    }

                    if (hMode == this.selectedX && vMode == this.selectedY)
                    {
                        GUILayoutExt.DrawBoxNotFilled(buttonRect, 1f, Color.white);
                    }
                }
            }
        }
Beispiel #2
0
        public static bool DrawLayout(float aspect, WindowLayout windowLayout, Rect r, float offset = 20f, HashSet <WindowLayout> used = null, DeviceInfo.ScreenData screenData = default, DeviceInfo.OrientationData orientationData = default, UnityEngine.UI.Windows.WindowTypes.LayoutWindowType drawComponents = null)
        {
            if (used.Contains(windowLayout) == true)
            {
                return(false);
            }
            used.Add(windowLayout);

            var rSource = r;

            var rectOffset = r;

            if (offset > 0f)
            {
                rectOffset.x      += offset;
                rectOffset.y      += offset;
                rectOffset.height -= offset * 2f;
                rectOffset.width  -= offset * 2f;

                var tWidth = rectOffset.height * aspect;
                if (tWidth > rectOffset.width)
                {
                    rectOffset.y     += rectOffset.height * 0.5f;
                    rectOffset.height = rectOffset.width / aspect;
                    rectOffset.y     -= rectOffset.height * 0.5f;
                }
                else
                {
                    rectOffset.x    += rectOffset.width * 0.5f;
                    rectOffset.width = rectOffset.height * aspect;
                    rectOffset.x    -= rectOffset.width * 0.5f;
                }
            }
            else
            {
                GUILayoutExt.DrawRect(rectOffset, new Color(0f, 0f, 0f, 0.4f));
            }

            GUILayoutExt.DrawRect(rectOffset, new Color(0f, 0f, 0f, 0.2f));
            GUILayoutExt.DrawBoxNotFilled(rectOffset, 1f, new Color(0.7f, 0.7f, 0.3f, 0.5f));

            GUI.BeginClip(r);

            var resolution = windowLayout.canvasScaler.referenceResolution;

            /*windowLayout.rectTransform.anchoredPosition = new Vector2(r.x, r.y);
             * windowLayout.rectTransform.sizeDelta = new Vector2(r.width, r.height);
             * windowLayout.rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
             * windowLayout.rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
             * windowLayout.rectTransform.pivot = new Vector2(0.5f, 0.5f);
             * windowLayout.rectTransform.localRotation = Quaternion.identity;
             * windowLayout.rectTransform.localScale = Vector3.one;*/

            r = rectOffset;

            {
                if (r.width > 0f && r.height > 0f)
                {
                    Vector2 screenSize = new Vector2(r.width, r.height);

                    var   sizeDelta   = Vector2.zero;
                    float scaleFactor = 0;
                    switch (windowLayout.canvasScaler.screenMatchMode)
                    {
                    case UnityEngine.UI.CanvasScaler.ScreenMatchMode.MatchWidthOrHeight: {
                        const float kLogBase = 2;
                        // We take the log of the relative width and height before taking the average.
                        // Then we transform it back in the original space.
                        // the reason to transform in and out of logarithmic space is to have better behavior.
                        // If one axis has twice resolution and the other has half, it should even out if widthOrHeight value is at 0.5.
                        // In normal space the average would be (0.5 + 2) / 2 = 1.25
                        // In logarithmic space the average is (-1 + 1) / 2 = 0
                        float logWidth           = Mathf.Log(screenSize.x / windowLayout.canvasScaler.referenceResolution.x, kLogBase);
                        float logHeight          = Mathf.Log(screenSize.y / windowLayout.canvasScaler.referenceResolution.y, kLogBase);
                        float logWeightedAverage = Mathf.Lerp(logWidth, logHeight, windowLayout.canvasScaler.matchWidthOrHeight);
                        scaleFactor = Mathf.Pow(kLogBase, logWeightedAverage);
                        break;
                    }

                    case UnityEngine.UI.CanvasScaler.ScreenMatchMode.Expand: {
                        scaleFactor = Mathf.Min(screenSize.x / windowLayout.canvasScaler.referenceResolution.x, screenSize.y / windowLayout.canvasScaler.referenceResolution.y);
                        break;
                    }

                    case UnityEngine.UI.CanvasScaler.ScreenMatchMode.Shrink: {
                        scaleFactor = Mathf.Max(screenSize.x / windowLayout.canvasScaler.referenceResolution.x, screenSize.y / windowLayout.canvasScaler.referenceResolution.y);
                        break;
                    }
                    }

                    if (scaleFactor > 0f)
                    {
                        sizeDelta = new Vector2(screenSize.x / scaleFactor, screenSize.y / scaleFactor);
                        windowLayout.rectTransform.sizeDelta  = sizeDelta;
                        windowLayout.rectTransform.pivot      = new Vector2(0.5f, 0.5f);
                        windowLayout.rectTransform.localScale = Vector3.one;
                        resolution = windowLayout.rectTransform.sizeDelta;
                    }
                }
            }

            var labelStyle = new GUIStyle(EditorStyles.label);

            labelStyle.alignment = TextAnchor.LowerLeft;

            var isHighlighted    = false;
            var highlightedIndex = -1;
            var highlightedRect  = Rect.zero;

            for (int i = 0; i < windowLayout.layoutElements.Length; ++i)
            {
                var element = windowLayout.layoutElements[i];
                if (element == null)
                {
                    windowLayout.ValidateEditor();
                    return(false);
                }

                var rect = WindowLayoutUtilities.GetRect(windowLayout.rectTransform, element.rectTransform, r, resolution, offset > 0f);
                if (rect.Contains(Event.current.mousePosition) == true)
                {
                    if (highlightedIndex >= 0 && highlightedRect.width * highlightedRect.height < rect.width * rect.height)
                    {
                        continue;
                    }

                    highlightedIndex = i;
                    highlightedRect  = rect;
                    isHighlighted    = true;
                }
            }

            var hasInnerHighlight = false;

            for (int i = 0; i < windowLayout.layoutElements.Length; ++i)
            {
                var element = windowLayout.layoutElements[i];
                var rect    = WindowLayoutUtilities.GetRect(windowLayout.rectTransform, element.rectTransform, r, resolution, offset > 0f);

                using (new GUILayoutExt.GUIColorUsing(highlightedIndex < 0 || i == highlightedIndex ? Color.white : new Color(1f, 1f, 1f, 0.6f))) {
                    if (drawComponents != null)
                    {
                        drawComponents.layouts.GetActive().GetLayoutComponentItemByTagId(element.tagId, windowLayout, out var componentItem);
                        var comp = componentItem.component.GetEditorRef <WindowComponent>();
                        if (comp != null)
                        {
                            WindowLayoutUtilities.DrawComponent(rect, comp, componentItem.localTag);
                        }

                        WindowSystemSidePropertyDrawer.DrawLayoutMode(rect, element.rectTransform);
                    }
                    else
                    {
                        WindowSystemSidePropertyDrawer.DrawLayoutMode(rect, element.rectTransform);
                    }
                }

                if (element.innerLayout != null)
                {
                    hasInnerHighlight = WindowLayoutUtilities.DrawLayout(aspect, element.innerLayout, rect, offset: 0f, used: used, drawComponents: drawComponents);
                    //WindowLayoutUtilities.DrawLayoutElements(highlightedIndex, rect, resolution, element.innerLayout, used);
                }
            }

            if (highlightedIndex >= 0 && hasInnerHighlight == false)
            {
                var element = windowLayout.layoutElements[highlightedIndex];
                var rect    = highlightedRect;

                var padding = 6f;
                var color   = new Color(1f, 1f, 0f, 0.5f);
                var content = new GUIContent(element.name);
                GUI.Label(new Rect(padding, 0f, rSource.width, rSource.height - padding), content, labelStyle);
                var labelWidth = labelStyle.CalcSize(content).x + 10f;
                GUILayoutExt.DrawRect(new Rect(padding, rSource.height - 1f - padding, labelWidth, 1f), color);
                var p1 = new Vector3(labelWidth + padding, rSource.height - 1f - padding);
                var p2 = new Vector3(rect.x, rect.y);
                Handles.color = color;
                Handles.DrawLine(p1, p2);

                GUILayoutExt.DrawBoxNotFilled(rect, 1f, new Color(1f, 1f, 1f, 0.2f));
            }

            GUI.EndClip();

            if (offset > 0f)
            {
                if (orientationData.cutouts != null)
                {
                    var safeArea = new Rect(orientationData.safeArea);
                    safeArea = WindowLayoutUtilities.GetRectYSwapScaled(safeArea, new Vector2(screenData.width, screenData.height), r, rectOffset);
                    GUILayoutExt.DrawBoxNotFilled(safeArea, 1f, Color.magenta);

                    foreach (var rSafe in orientationData.cutouts)
                    {
                        var rSafeRect = WindowLayoutUtilities.GetRectYSwapScaled(rSafe, new Vector2(screenData.width, screenData.height), r, rectOffset);
                        GUI.BeginClip(rSafeRect);

                        if (rSafeRect.width < rSafeRect.height)
                        {
                            for (float step = -rSafeRect.height; step < rSafeRect.height; step += 5f)
                            {
                                var v1 = new Vector3(0f, step);
                                var v2 = new Vector3(rSafeRect.width, step + rSafeRect.width);
                                Handles.color = Color.yellow;
                                Handles.DrawAAPolyLine(2f, v1, v2);
                            }
                        }
                        else
                        {
                            for (float step = -rSafeRect.width; step < rSafeRect.width; step += 5f)
                            {
                                var v1 = new Vector3(step, 0f);
                                var v2 = new Vector3(step + rSafeRect.height, rSafeRect.height);
                                Handles.color = Color.yellow;
                                Handles.DrawAAPolyLine(2f, v1, v2);
                            }
                        }

                        GUI.EndClip();
                        GUILayoutExt.DrawBoxNotFilled(rSafeRect, 1f, Color.yellow);
                    }
                }
            }

            return(isHighlighted);
        }