Пример #1
0
        public static bool DrawButton(Rect contentRect, Layout hMode, Layout vMode)
        {
            const float padding = 4f;
            const float size    = 40f;

            var inner = contentRect;

            inner.x      += padding;
            inner.width  -= padding * 2f;
            inner.y      += padding;
            inner.height -= padding * 2f;

            var style = new GUIStyle(EditorStyles.miniButton);

            style.stretchHeight = true;
            style.fixedHeight   = 0f;
            style.normal.scaledBackgrounds[0]   = null;
            style.onNormal.scaledBackgrounds[0] = Texture2D.whiteTexture;
            var oldColor = GUI.color;

            GUI.color = new Color(1f, 1f, 1f, contentRect.Contains(Event.current.mousePosition) ? 0.3f : 0f);
            if (GUI.Button(contentRect, string.Empty, style) == true)
            {
                return(true);
            }
            GUI.color = oldColor;

            GUILayoutExt.DrawBoxNotFilled(contentRect, 1f, new Color(1f, 1f, 1f, 0.3f), padding);
            GUILayoutExt.DrawBoxNotFilled(contentRect, 1f, new Color(1f, 1f, 1f, 0.5f), padding * 3f);

            var vLine = inner;

            vLine.y     += (size - padding * 2f) * kPivotsForModes[(int)vMode] - 0.5f;
            vLine.height = 1f;
            vLine.x     += 1f;
            vLine.width -= 2f;
            GUILayoutExt.DrawRect(vLine, new Color(0.7f, 0.3f, 0.3f, 1));

            var hLine = inner;

            hLine.x      += (size - padding * 2f) * kPivotsForModes[(int)hMode] - 0.5f;
            hLine.width   = 1f;
            hLine.y      += 1f;
            hLine.height -= 2f;
            GUILayoutExt.DrawRect(hLine, new Color(0.7f, 0.3f, 0.3f, 1));

            var pivot = new Vector2(
                Mathf.Lerp(inner.xMin, inner.xMax, kPivotsForModes[(int)hMode]),
                Mathf.Lerp(inner.yMin, inner.yMax, kPivotsForModes[(int)vMode])
                );

            GUILayoutExt.DrawRect(new Rect(pivot.x - 1f, pivot.y - 1f, 3f, 3f), new Color(0.8f, 0.6f, 0.0f, 1));

            return(false);
        }
Пример #2
0
        public void OnEnable()
        {
            try {
                #pragma warning disable
                var _ = this.serializedObject;
                #pragma warning restore
            } catch (System.Exception) {
                return;
            }

            this.createPool = this.serializedObject.FindProperty("createPool");

            this.objectState = this.serializedObject.FindProperty("objectState");
            this.focusState  = this.serializedObject.FindProperty("focusState");

            this.preferences = this.serializedObject.FindProperty("preferences");
            this.modules     = this.serializedObject.FindProperty("modules");
            this.layouts     = this.serializedObject.FindProperty("layouts");

            var moduleItems = this.modules.FindPropertyRelative("modules");
            this.listModules = new UnityEditorInternal.ReorderableList(this.serializedObject, moduleItems, false, false, true, true);
            const float elementHeight = 64f;
            this.listModules.headerHeight         = 1f;
            this.listModules.elementHeight        = elementHeight;
            this.listModules.drawElementCallback += (rect, index, active, focus) => {
                var item      = moduleItems.GetArrayElementAtIndex(index);
                var prevValue = item.FindPropertyRelative("module").FindPropertyRelative("guid");
                rect.y     += 2f;
                rect.height = 18f;
                EditorGUI.BeginChangeCheck();
                GUILayoutExt.DrawProperty(rect, item, 20f);
                GUILayoutExt.DrawRect(new Rect(rect.x, rect.y + elementHeight - 3f, rect.width, 1f), new Color(1f, 1f, 1f, 0.1f));
                if (EditorGUI.EndChangeCheck() == true)
                {
                    var newValue = item.FindPropertyRelative("module").FindPropertyRelative("guid");
                    if (prevValue != newValue && string.IsNullOrEmpty(newValue.stringValue) == false)
                    {
                        var guid      = newValue.stringValue;
                        var assetPath = AssetDatabase.GUIDToAssetPath(guid);
                        var obj       = AssetDatabase.LoadAssetAtPath <WindowModule>(assetPath);

                        var module = obj;
                        item.FindPropertyRelative("order").intValue = module.defaultOrder;
                    }
                }
                //EditorGUI.PropertyField(rect, item);
            };

            EditorHelpers.SetFirstSibling(this.targets);
        }
Пример #3
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);
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var aspectFromProp = property.FindPropertyRelative("aspectFrom");
            var aspectToProp   = property.FindPropertyRelative("aspectTo");
            var aspectFrom     = aspectFromProp.vector2Value;
            var aspectTo       = aspectToProp.vector2Value;

            var offset      = EditorGUI.indentLevel * 24f;
            var defPosition = position;
            var aspectRect  = new Rect(position.x + offset, position.y, EditorGUIUtility.labelWidth, position.height);

            var fillColor   = new Color(0.5f, 0.3f, 0.3f, 0.2f);
            var borderColor = fillColor;

            borderColor.a = 0.5f;

            position.x     += EditorGUIUtility.labelWidth;
            position.width -= EditorGUIUtility.labelWidth;

            GUILayoutExt.DrawRect(position, fillColor);
            GUILayoutExt.DrawBoxNotFilled(position, 1f, borderColor);

            var padding = 4f;
            var size    = position.height - padding * 2f;
            var rect    = new Rect(position.x + position.width * 0.5f - size * 0.5f, position.y + padding, size, size);

            GUILayoutExt.DrawRect(rect, fillColor);

            var fromRect = new Rect(0f, 0f, aspectFrom.x, aspectFrom.y);
            var toRect   = new Rect(0f, 0f, aspectTo.x, aspectTo.y);

            fromRect = EditorHelpers.FitRect(fromRect, rect);
            toRect   = EditorHelpers.FitRect(toRect, rect);
            GUILayoutExt.DrawBoxNotFilled(fromRect, 2f, new Color(0.4f, 0.4f, 1f, 0.6f));
            GUILayoutExt.DrawBoxNotFilled(toRect, 2f, new Color(1f, 0.4f, 1f, 0.6f));

            {
                GUILayout.BeginArea(aspectRect);
                //GUILayoutExt.Box(4f, 4f, () => {

                GUILayout.BeginHorizontal();
                {
                    GUILayout.Space(defPosition.x + 10f);

                    GUILayout.BeginVertical();
                    {
                        GUILayout.Label("From");
                        GUILayout.BeginHorizontal();
                        aspectFrom.x = EditorGUILayout.FloatField(aspectFrom.x);
                        GUILayout.Label(":", GUILayout.Width(10f));
                        aspectFrom.y = EditorGUILayout.FloatField(aspectFrom.y);
                        aspectFromProp.vector2Value = aspectFrom;
                        GUILayout.EndHorizontal();

                        //}, GUIStyle.none);
                        //GUILayoutExt.Box(4f, 4f, () => {

                        GUILayout.Label("To");
                        GUILayout.BeginHorizontal();
                        aspectTo.x = EditorGUILayout.FloatField(aspectTo.x);
                        GUILayout.Label(":", GUILayout.Width(10f));
                        aspectTo.y = EditorGUILayout.FloatField(aspectTo.y);
                        aspectToProp.vector2Value = aspectTo;
                        GUILayout.EndHorizontal();
                    }
                    GUILayout.EndVertical();
                }
                GUILayout.EndHorizontal();

                //}, GUIStyle.none);
                GUILayout.EndArea();
            }
        }
        public virtual void OnEnable()
        {
            try {
                #pragma warning disable
                var _ = this.serializedObject;
                #pragma warning restore
            } catch (System.Exception) {
                return;
            }

            this.createPool = this.serializedObject.FindProperty("createPool");

            this.objectState             = this.serializedObject.FindProperty("objectState");
            this.animationParameters     = this.serializedObject.FindProperty("animationParameters");
            this.renderBehaviourOnHidden = this.serializedObject.FindProperty("renderBehaviourOnHidden");

            this.subObjects       = this.serializedObject.FindProperty("subObjects");
            this.componentModules = this.serializedObject.FindProperty("componentModules");

            this.allowRegisterInRoot    = this.serializedObject.FindProperty("allowRegisterInRoot");
            this.autoRegisterSubObjects = this.serializedObject.FindProperty("autoRegisterSubObjects");
            this.hiddenByDefault        = this.serializedObject.FindProperty("hiddenByDefault");

            if (this.listModules == null)
            {
                var componentsProp = this.componentModules.FindPropertyRelative("modules");
                //if (componentsProp.arraySize != 0) {

                this.serializedObject.Update();
                this.componentModules.FindPropertyRelative("windowComponent").objectReferenceValue = this.componentModules.serializedObject.targetObject;
                this.serializedObject.ApplyModifiedProperties();
                this.listModules = new UnityEditorInternal.ReorderableList(componentsProp.serializedObject, componentsProp, true, true, true, true);
                this.listModules.elementHeight = 40f;
                this.listModules.onAddCallback = (rList) => {
                    if (rList.serializedProperty != null)
                    {
                        ++rList.serializedProperty.arraySize;
                        rList.index = rList.serializedProperty.arraySize - 1;
                        var idx  = rList.index;
                        var prop = componentsProp.GetArrayElementAtIndex(idx);
                        prop.objectReferenceValue = null;
                    }
                };
                this.listModules.onRemoveCallback = (rList) => {
                    var idx  = this.listModules.index;
                    var prop = componentsProp.GetArrayElementAtIndex(idx);
                    if (prop.objectReferenceValue != null)
                    {
                        Object.DestroyImmediate(prop.objectReferenceValue, true);
                    }
                    componentsProp.DeleteArrayElementAtIndex(idx);
                };
                this.listModules.drawElementBackgroundCallback = (rect, index, active, focused) => {
                    if (focused == true || active == true)
                    {
                        GUILayoutExt.DrawRect(rect, new Color(0.1f, 0.4f, 0.7f, 1f));
                    }
                    else
                    {
                        GUILayoutExt.DrawRect(rect, new Color(1f, 1f, 1f, index % 2 == 0 ? 0.05f : 0f));
                    }
                };
                this.listModules.elementHeightCallback = (index) => {
                    var prop = componentsProp.GetArrayElementAtIndex(index);
                    if (prop.objectReferenceValue != null)
                    {
                        var height   = 0f;
                        var so       = new SerializedObject(prop.objectReferenceValue);
                        var iterator = so.GetIterator();
                        iterator.NextVisible(true);

                        while (true)
                        {
                            if (EditorHelpers.IsFieldOfTypeBeneath(prop.objectReferenceValue.GetType(), typeof(WindowComponentModule), iterator.propertyPath) == true)
                            {
                                height += EditorGUI.GetPropertyHeight(iterator, new GUIContent(iterator.displayName), false);
                            }

                            if (!iterator.NextVisible(iterator.isExpanded))
                            {
                                break;
                            }
                        }

                        return(40f + height);
                    }

                    return(40f);
                };
                this.listModules.drawElementCallback = (rect, index, active, focused) => {
                    var padding = 10f;
                    rect.x     += padding;
                    rect.y     += padding;
                    rect.width -= padding * 2f;
                    rect.height = 18f;
                    EditorGUI.PropertyField(rect, componentsProp.GetArrayElementAtIndex(index), new GUIContent("Module"));
                    rect.y += 18f;
                    rect.y += padding;

                    var prop = componentsProp.GetArrayElementAtIndex(index);
                    if (prop.objectReferenceValue != null)
                    {
                        var so = new SerializedObject(prop.objectReferenceValue);
                        so.Update();

                        so.FindProperty("windowComponent").objectReferenceValue = this.serializedObject.targetObject;

                        var iterator = so.GetIterator();
                        iterator.NextVisible(true);

                        EditorGUI.indentLevel += 1;
                        int indent = EditorGUI.indentLevel;
                        while (true)
                        {
                            if (EditorHelpers.IsFieldOfTypeBeneath(prop.objectReferenceValue.GetType(), typeof(WindowComponentModule), iterator.propertyPath) == true)
                            {
                                rect.height = EditorGUI.GetPropertyHeight(iterator, new GUIContent(iterator.displayName), false);

                                //totalHeight += rect.height;
                                EditorGUI.indentLevel = indent + iterator.depth;
                                EditorGUI.PropertyField(rect, iterator);
                                rect.y += rect.height;
                            }

                            if (!iterator.NextVisible(iterator.isExpanded))
                            {
                                break;
                            }
                        }

                        EditorGUI.indentLevel  = indent;
                        EditorGUI.indentLevel -= 1;

                        /*
                         * var iter = so.GetIterator();
                         * while (iter.NextVisible(true) == true) {
                         *
                         *  if (iter.hasVisibleChildren == true) {
                         *
                         *      iter.isExpanded = EditorGUI.Foldout(rect, iter.isExpanded, iter.displayName);
                         *
                         *      if (iter.isExpanded == false) continue;
                         *
                         *  }
                         *
                         *  if (EditorHelpers.IsFieldOfTypeBeneath(prop.objectReferenceValue.GetType(), typeof(WindowComponentModule), iter.propertyPath) == true) {
                         *
                         *      rect.height = EditorGUI.GetPropertyHeight(iter);
                         *      EditorGUI.PropertyField(rect, iter);
                         *      rect.y += rect.height;
                         *
                         *  }
                         *
                         * }*/

                        so.ApplyModifiedProperties();
                    }
                };
                this.listModules.drawHeaderCallback = (rect) => { GUI.Label(rect, "Modules"); };

                //}
            }

            EditorHelpers.SetFirstSibling(this.targets);
        }
Пример #6
0
        internal static void DrawLayoutMode(Rect position, Layout hMode, Layout vMode, Vector2 pivot, bool doPivot, bool doPosition)
        {
            if (s_Styles == null)
            {
                s_Styles = new Styles();
            }

            GUILayoutExt.DrawRect(position, new Color(1f, 1f, 1f, 0.02f));

            Color oldColor = GUI.color;

            // Make parent size the largest possible square, but enforce it's an uneven number.
            int parentWidth = (int)Mathf.Min(position.width, position.height);

            if (parentWidth % 2 == 0)
            {
                parentWidth--;
            }

            int selfWidth = parentWidth / 2;

            if (selfWidth % 2 == 0)
            {
                selfWidth++;
            }

            Vector2 parentSize = new Vector2(position.width, position.height);               //parentWidth * Vector2.one;
            Vector2 selfSize   = new Vector2(position.width * 0.5f, position.height * 0.5f); //selfWidth * Vector2.one;
            Vector2 padding    = (position.size - parentSize) / 2;

            padding.x = Mathf.Floor(padding.x);
            padding.y = Mathf.Floor(padding.y);
            Vector2 padding2 = (position.size - selfSize) / 2;

            padding2.x = Mathf.Floor(padding2.x);
            padding2.y = Mathf.Floor(padding2.y);

            Rect outer = new Rect(position.x + padding.x, position.y + padding.y, parentSize.x, parentSize.y);
            Rect inner = new Rect(position.x + padding2.x, position.y + padding2.y, selfSize.x, selfSize.y);

            if (doPosition)
            {
                for (int axis = 0; axis < 2; axis++)
                {
                    var mode = (axis == 0 ? hMode : vMode);

                    if (mode == Layout.Min)
                    {
                        Vector2 center = inner.center;
                        center[axis] += outer.min[axis] - inner.min[axis];
                        inner.center  = center;
                    }
                    if (mode == Layout.Middle)
                    {
                        // TODO
                    }
                    if (mode == Layout.Max)
                    {
                        Vector2 center = inner.center;
                        center[axis] += outer.max[axis] - inner.max[axis];
                        inner.center  = center;
                    }
                    if (mode == Layout.Stretch)
                    {
                        Vector2 innerMin = inner.min;
                        Vector2 innerMax = inner.max;
                        innerMin[axis] = outer.min[axis];
                        innerMax[axis] = outer.max[axis];
                        inner.min      = innerMin;
                        inner.max      = innerMax;
                    }
                }
            }

            Rect    anchor = new Rect();
            Vector2 min    = Vector2.zero;
            Vector2 max    = Vector2.zero;

            for (int axis = 0; axis < 2; axis++)
            {
                var mode = (axis == 0 ? hMode : vMode);

                if (mode == Layout.Min)
                {
                    min[axis] = outer.min[axis] + 0.5f;
                    max[axis] = outer.min[axis] + 0.5f;
                }
                if (mode == Layout.Middle)
                {
                    min[axis] = outer.center[axis];
                    max[axis] = outer.center[axis];
                }
                if (mode == Layout.Max)
                {
                    min[axis] = outer.max[axis] - 0.5f;
                    max[axis] = outer.max[axis] - 0.5f;
                }
                if (mode == Layout.Stretch)
                {
                    min[axis] = outer.min[axis] + 0.5f;
                    max[axis] = outer.max[axis] - 0.5f;
                }
            }
            anchor.min = min;
            anchor.max = max;

            // Draw parent rect
            if (Event.current.type == EventType.Repaint)
            {
                GUI.color = s_Styles.parentColor * oldColor;
                s_Styles.frame.Draw(outer, false, false, false, false);
            }

            // Draw anchor lines
            if (hMode != Layout.Undefined && hMode != Layout.Stretch)
            {
                GUI.color = s_Styles.simpleAnchorColor * oldColor;
                GUI.DrawTexture(new Rect(anchor.xMin - 0.5f, outer.y + 1, 1, outer.height - 2), EditorGUIUtility.whiteTexture);
                GUI.DrawTexture(new Rect(anchor.xMax - 0.5f, outer.y + 1, 1, outer.height - 2), EditorGUIUtility.whiteTexture);
            }
            if (vMode != Layout.Undefined && vMode != Layout.Stretch)
            {
                GUI.color = s_Styles.simpleAnchorColor * oldColor;
                GUI.DrawTexture(new Rect(outer.x + 1, anchor.yMin - 0.5f, outer.width - 2, 1), EditorGUIUtility.whiteTexture);
                GUI.DrawTexture(new Rect(outer.x + 1, anchor.yMax - 0.5f, outer.width - 2, 1), EditorGUIUtility.whiteTexture);
            }

            // Draw stretch mode arrows
            if (hMode == Layout.Stretch)
            {
                GUI.color = s_Styles.stretchAnchorColor * oldColor;
                DrawArrow(new Rect(inner.x + 1, inner.center.y - 0.5f, inner.width - 2, 1));
            }
            if (vMode == Layout.Stretch)
            {
                GUI.color = s_Styles.stretchAnchorColor * oldColor;
                DrawArrow(new Rect(inner.center.x - 0.5f, inner.y + 1, 1, inner.height - 2));
            }

            // Draw self rect
            if (Event.current.type == EventType.Repaint)
            {
                GUI.color = s_Styles.selfColor * oldColor;
                s_Styles.frame.Draw(inner, false, false, false, false);
            }

            // Draw pivot
            if (doPivot && hMode != Layout.Undefined && vMode != Layout.Undefined)
            {
                Vector2 pivotInner = new Vector2(
                    Mathf.Lerp(inner.xMin + 0.5f, inner.xMax - 0.5f, pivot.x),
                    Mathf.Lerp(inner.yMax + 0.5f, inner.yMin - 0.5f, pivot.y)
                    );

                GUI.color = s_Styles.pivotColor * oldColor;
                GUI.DrawTexture(new Rect(pivotInner.x - 2.5f, pivotInner.y - 1.5f, 5, 3), EditorGUIUtility.whiteTexture);
                GUI.DrawTexture(new Rect(pivotInner.x - 1.5f, pivotInner.y - 2.5f, 3, 5), EditorGUIUtility.whiteTexture);
            }

            // Draw anchor corners
            if (hMode != Layout.Undefined && vMode != Layout.Undefined)
            {
                GUI.color = s_Styles.anchorCornerColor * oldColor;
                GUI.DrawTexture(new Rect(anchor.xMin - 1.5f, anchor.yMin - 1.5f, 2, 2), EditorGUIUtility.whiteTexture);
                GUI.DrawTexture(new Rect(anchor.xMax - 0.5f, anchor.yMin - 1.5f, 2, 2), EditorGUIUtility.whiteTexture);
                GUI.DrawTexture(new Rect(anchor.xMin - 1.5f, anchor.yMax - 0.5f, 2, 2), EditorGUIUtility.whiteTexture);
                GUI.DrawTexture(new Rect(anchor.xMax - 0.5f, anchor.yMax - 0.5f, 2, 2), EditorGUIUtility.whiteTexture);
            }

            GUI.color = oldColor;
        }
Пример #7
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var evenStyle = new GUIStyle(GUIStyle.none);

            evenStyle.normal.background = Texture2D.whiteTexture;
            var tagStyle = new GUIStyle(EditorStyles.label);

            tagStyle.alignment = TextAnchor.MiddleRight;
            var innerLayoutStyle = new GUIStyle(EditorStyles.miniLabel);

            innerLayoutStyle.alignment     = TextAnchor.UpperLeft;
            innerLayoutStyle.stretchHeight = false;

            var items = property.FindPropertyRelative("items");

            if (items.arraySize == 0)
            {
                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("Add Layout", GUILayout.Height(30f), GUILayout.Width(120f)) == true)
                {
                    ++items.arraySize;
                    this.selectedTab = 0;
                }
                GUILayout.FlexibleSpace();
                GUILayout.EndHorizontal();
                GUILayout.Space(20f);
                return;
            }

            var arr = new GUITab[items.arraySize + 1];

            this.selectedTab = Mathf.Clamp(this.selectedTab, 0, arr.Length - 2);
            var i = 0;

            for (i = 0; i < items.arraySize; ++i)
            {
                var idx     = i;
                var prop    = items.GetArrayElementAtIndex(i);
                var objRef  = prop.FindPropertyRelative("windowLayout").objectReferenceValue;
                var caption = (objRef != null ? EditorHelpers.StringToCaption(objRef.name) : "Layout (Empty)");
                arr[i] = new GUITab(caption, () => {
                    GUILayout.BeginHorizontal();
                    {
                        GUILayout.FlexibleSpace();
                        if (GUILayout.Button("Remove", GUILayout.Width(60f)) == true)
                        {
                            if (EditorUtility.DisplayDialog("Delete Layout Reference", "Are you sure?", "Yes", "No") == true)
                            {
                                items.DeleteArrayElementAtIndex(idx);
                                return;
                            }
                        }
                    }
                    GUILayout.EndHorizontal();

                    if (idx > 0)
                    {
                        GUILayout.Space(6f);

                        EditorGUI.BeginChangeCheck();
                        var targets = prop.FindPropertyRelative("targets");
                        EditorGUILayout.PropertyField(targets);
                        if (EditorGUI.EndChangeCheck() == true)
                        {
                            EditorHelpers.SetDirtyAndValidate(property);
                        }
                    }
                    else
                    {
                        EditorGUILayout.HelpBox("This is default layout. Target Filter couldn't been attach here.", MessageType.Info);
                    }

                    GUILayout.Space(6f);

                    EditorGUI.BeginChangeCheck();
                    var windowLayout = prop.FindPropertyRelative("windowLayout");
                    EditorGUILayout.PropertyField(windowLayout);
                    if (EditorGUI.EndChangeCheck() == true)
                    {
                        EditorHelpers.SetDirtyAndValidate(property);
                    }

                    var layout = windowLayout.objectReferenceValue as WindowLayout;
                    if (layout == null)
                    {
                        return;
                    }

                    EditorGUI.BeginChangeCheck();
                    var layoutPreferences = prop.FindPropertyRelative("layoutPreferences");
                    EditorGUILayout.PropertyField(layoutPreferences);
                    if (layoutPreferences.objectReferenceValue == null)
                    {
                        EditorGUILayout.HelpBox("Layout Preferences are CanvasScaler override parameters. It's highly recommended to use override here.", MessageType.Info);
                    }

                    if (layoutPreferences.objectReferenceValue is WindowLayoutPreferences windowLayoutPreferences)
                    {
                        windowLayoutPreferences.Apply(layout.canvasScaler);

                        try {
                            EditorGUI.BeginDisabledGroup(true);
                            var editorCanvasScaler = Editor.CreateEditor(layout.canvasScaler);
                            editorCanvasScaler.OnInspectorGUI();
                            EditorGUI.EndDisabledGroup();
                        } catch (System.Exception) {}
                    }

                    if (EditorGUI.EndChangeCheck() == true)
                    {
                        EditorHelpers.SetDirtyAndValidate(property);
                    }

                    GUILayout.Space(2f);
                    GUILayoutExt.Separator();
                    GUILayout.Space(2f);

                    if (this.list == null)
                    {
                        var componentsProp      = prop.FindPropertyRelative("components");
                        this.list               = new UnityEditorInternal.ReorderableList(property.serializedObject, componentsProp, true, true, false, false);
                        this.list.elementHeight = 40f;
                        this.list.onAddCallback = (rList) => {
                            if (rList.serializedProperty != null)
                            {
                                ++rList.serializedProperty.arraySize;
                                rList.index = rList.serializedProperty.arraySize - 1;
                            }
                        };
                        this.list.drawElementBackgroundCallback = (rect, index, active, focused) => {
                            if (focused == true)
                            {
                                GUILayoutExt.DrawRect(rect, new Color(0.1f, 0.4f, 0.7f, 1f));
                            }
                            else
                            {
                                GUILayoutExt.DrawRect(rect, new Color(1f, 1f, 1f, index % 2 == 0 ? 0.05f : 0f));
                            }
                        };
                        this.list.drawElementCallback = (rect, index, active, focused) => {
                            //EditorGUI.PropertyField(rect, componentsProp.GetArrayElementAtIndex(index));

                            EditorGUI.BeginChangeCheck();
                            {
                                var captionRect = new Rect(rect.x, rect.y, rect.width, 18f);
                                var tagRect     = captionRect;
                                var layoutRect  = new Rect(tagRect.x + 140f, tagRect.y, tagRect.width, tagRect.height);
                                var objectRect  = new Rect(captionRect.x, captionRect.y + 18f, captionRect.width, captionRect.height);

                                var compProp  = componentsProp.GetArrayElementAtIndex(index);
                                var component = compProp.FindPropertyRelative("component");

                                var localRagId        = compProp.FindPropertyRelative("localTag").intValue;
                                var windowLayoutInner = (WindowLayout)compProp.FindPropertyRelative("windowLayout").objectReferenceValue;
                                string layoutName     = string.Empty;
                                if (windowLayoutInner != null)
                                {
                                    var tagId         = compProp.FindPropertyRelative("tag").intValue;
                                    var layoutElement = windowLayoutInner.GetLayoutElementByTagId(tagId);
                                    if (layoutElement != null)
                                    {
                                        layoutName = layoutElement.name;
                                    }
                                }

                                using (GUILayoutExt.GUIColor(new Color(1f, 1f, 1f, 0.4f))) {
                                    if (windowLayoutInner != null && windowLayoutInner != layout)
                                    {
                                        GUI.Label(layoutRect, "(" + EditorHelpers.StringToCaption(windowLayoutInner.name) + ")", innerLayoutStyle);
                                    }
                                }

                                GUI.Label(captionRect, EditorHelpers.StringToCaption(layoutName), EditorStyles.boldLabel);
                                GUI.Label(tagRect, "Tag: " + localRagId.ToString(), tagStyle);
                                EditorGUI.PropertyField(objectRect, component, new GUIContent(string.Empty));
                            }

                            if (EditorGUI.EndChangeCheck() == true)
                            {
                                EditorHelpers.SetDirtyAndValidate(property);
                            }
                        };
                        this.list.drawHeaderCallback = (rect) => {
                            GUI.Label(rect, "Components");
                            var buttonRect   = rect;
                            var width        = 80f;
                            buttonRect.x     = rect.width - width;
                            buttonRect.width = width;
                            if (GUI.Button(buttonRect, "Refresh") == true)
                            {
                                (componentsProp.serializedObject.targetObject as WindowObject).ValidateEditor();
                            }
                        };
                    }

                    this.list.DoLayoutList();

                    /*
                     * EditorGUI.BeginChangeCheck();
                     * var components = prop.FindPropertyRelative("components");
                     * for (int j = 0; j < components.arraySize; ++j) {
                     *
                     *  var compProp = components.GetArrayElementAtIndex(j);
                     *  var component = compProp.FindPropertyRelative("component");
                     *
                     *  var c = GUI.color;
                     *  GUI.color = new Color(1f, 1f, 1f, 0.1f);
                     *  GUILayout.BeginVertical(j % 2 == 0 ? evenStyle : oddStyle);
                     *  GUI.color = c;
                     *  {
                     *
                     *      GUILayout.Space(4f);
                     *      GUILayout.BeginHorizontal();
                     *      {
                     *          var localRagId = compProp.FindPropertyRelative("localTag").intValue;
                     *          var windowLayoutInner = (WindowLayout)compProp.FindPropertyRelative("windowLayout").objectReferenceValue;
                     *          string layoutName = string.Empty;
                     *          if (windowLayoutInner != null) {
                     *
                     *              var tagId = compProp.FindPropertyRelative("tag").intValue;
                     *              var layoutElement = windowLayoutInner.GetLayoutElementByTagId(tagId);
                     *              if (layoutElement != null) layoutName = layoutElement.name;
                     *
                     *          }
                     *          GUILayout.Label(EditorHelpers.StringToCaption(layoutName), EditorStyles.boldLabel, GUILayout.ExpandWidth(false));
                     *          using (GUILayoutExt.GUIColor(new Color(1f, 1f, 1f, 0.4f))) {
                     *              if (windowLayoutInner != null && windowLayoutInner != layout) GUILayout.Label("(" + EditorHelpers.StringToCaption(windowLayoutInner.name) + ")", innerLayoutStyle, GUILayout.ExpandWidth(false));
                     *          }
                     *
                     *          GUILayout.FlexibleSpace();
                     *          GUILayout.Label("Tag: " + localRagId.ToString(), tagStyle, GUILayout.ExpandWidth(false));
                     *      }
                     *      GUILayout.EndHorizontal();
                     *      EditorGUILayout.PropertyField(component);
                     *
                     *      GUILayout.Space(4f);
                     *      GUILayoutExt.Separator();
                     *
                     *  }
                     *  GUILayout.EndVertical();
                     *
                     * }
                     * if (EditorGUI.EndChangeCheck() == true) {
                     *
                     *  EditorHelpers.SetDirtyAndValidate(property);
                     *
                     * }*/
                });
            }

            arr[i] = new GUITab("+", () => {
            }, 40f);

            var scroll = this.tabScrollPosition;
            var newTab = GUILayoutExt.DrawTabs(
                this.selectedTab,
                ref scroll,
                arr
                );

            this.tabScrollPosition = scroll;

            if (newTab != this.selectedTab)
            {
                if (newTab == i)
                {
                    // Add item
                    ++items.arraySize;
                    this.selectedTab = i;
                }
                else
                {
                    this.selectedTab = newTab;
                }
            }
        }
        public void OnEnable()
        {
            try {
                #pragma warning disable
                var _ = this.serializedObject;
                #pragma warning restore
            } catch (System.Exception) {
                return;
            }

            this.createPool = this.serializedObject.FindProperty("createPool");

            this.objectState = this.serializedObject.FindProperty("objectState");
            this.focusState  = this.serializedObject.FindProperty("focusState");

            this.preferences = this.serializedObject.FindProperty("preferences");
            this.modules     = this.serializedObject.FindProperty("modules");
            this.layouts     = this.serializedObject.FindProperty("layouts");

            this.audioEvents = this.serializedObject.FindProperty("audioEvents");

            var moduleItems = this.modules.FindPropertyRelative("modules");
            this.listModules = new UnityEditorInternal.ReorderableList(this.serializedObject, moduleItems, false, false, true, true);
            this.listModules.headerHeight           = 1f;
            this.listModules.elementHeightCallback += (index) => {
                var item = moduleItems.GetArrayElementAtIndex(index);
                var h    = 0f;
                h += EditorGUI.GetPropertyHeight(item.FindPropertyRelative("targets"));
                h += 2f;
                h += EditorGUI.GetPropertyHeight(item.FindPropertyRelative("module"));
                h += 2f;
                h += 20f;
                var parameters = item.FindPropertyRelative("parameters");
                if (parameters.hasVisibleChildren == true)
                {
                    var px = parameters.Copy();
                    px.NextVisible(true);
                    var depth = px.depth;
                    while (px.depth >= depth)
                    {
                        h += EditorGUI.GetPropertyHeight(px, true);
                        if (px.NextVisible(false) == false)
                        {
                            break;
                        }
                    }
                    h += 4f;
                }
                h += 4f;
                return(h);
            };
            this.listModules.drawElementCallback += (rect, index, active, focus) => {
                var item      = moduleItems.GetArrayElementAtIndex(index);
                var prevValue = item.FindPropertyRelative("module").FindPropertyRelative("guid").stringValue;
                rect.y     += 2f;
                rect.height = 18f;
                EditorGUI.BeginChangeCheck();
                //GUILayoutExt.DrawProperty(rect, item, 20f);
                EditorGUI.PropertyField(rect, item.FindPropertyRelative("targets"));
                rect.y += EditorGUI.GetPropertyHeight(item.FindPropertyRelative("targets"), true);
                rect.y += 2f;
                EditorGUI.PropertyField(rect, item.FindPropertyRelative("module"));
                rect.y += EditorGUI.GetPropertyHeight(item.FindPropertyRelative("module"), true);
                rect.y += 2f;
                EditorGUI.LabelField(rect, "Parameters", EditorStyles.centeredGreyMiniLabel);
                rect.y += 20f;

                var parameters = item.FindPropertyRelative("parameters");
                {
                    if (string.IsNullOrEmpty(parameters.managedReferenceFullTypename) == true)
                    {
                        parameters.managedReferenceValue = new WindowModule.Parameters();
                    }

                    if (parameters.hasVisibleChildren == true)
                    {
                        var px = parameters.Copy();
                        px.NextVisible(true);
                        var depth = px.depth;
                        while (px.depth >= depth)
                        {
                            var h = EditorGUI.GetPropertyHeight(px, true);
                            EditorGUI.PropertyField(rect, px, true);
                            rect.y += h;
                            rect.y += 2f;
                            if (px.NextVisible(false) == false)
                            {
                                break;
                            }
                        }
                    }

                    rect.y += 4f;

                    /*
                     * var p = parameters.Copy();
                     * if (p.CountInProperty() > 0) {
                     *  var px = parameters.Copy();
                     *  px.NextVisible(true);
                     *  var h = EditorGUI.GetPropertyHeight(px, true);
                     *  EditorGUI.PropertyField(rect, px, true);
                     *  rect.y += h;
                     *  rect.y += 2f;
                     * }*/
                }

                GUILayoutExt.DrawRect(new Rect(rect.x, rect.y - 3f, rect.width, 1f), new Color(1f, 1f, 1f, 0.1f));
                rect.y += 4f;
                if (EditorGUI.EndChangeCheck() == true)
                {
                    var newValue = item.FindPropertyRelative("module").FindPropertyRelative("guid").stringValue;
                    if (prevValue != newValue && string.IsNullOrEmpty(newValue) == false)
                    {
                        var guid             = newValue;
                        var assetPath        = AssetDatabase.GUIDToAssetPath(guid);
                        var module           = AssetDatabase.LoadAssetAtPath <WindowModule>(assetPath);
                        var sourceParameters = module.parameters;
                        parameters.managedReferenceValue = sourceParameters;
                    }
                }
            };

            EditorHelpers.SetFirstSibling(this.targets);
        }