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); }
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); } } } }
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(); } }