private static void DrawNavigationArrow(bool selected, Vector2 directionFrom, Vector2 directionTo, Rect fromRect, Rect toRect) { UnityEditor.Handles.color = new Color(1.0f, 0.9f, 0.1f, selected == true ? 1f : 0.3f); Vector2 sideDirFrom = new Vector2(directionFrom.y, -directionFrom.x); Vector2 sideDirTo = new Vector2(directionTo.y, -directionTo.x); var fromPoint = WindowLayoutEditor.GetPointOnRectEdge(fromRect, directionFrom); var toPoint = WindowLayoutEditor.GetPointOnRectEdge(toRect, directionTo); fromPoint.y += kTargetOffset; toPoint.y -= kTargetOffset; float fromSize = UnityEditor.HandleUtility.GetHandleSize(fromPoint) * 0.05f; float toSize = UnityEditor.HandleUtility.GetHandleSize(toPoint) * 0.05f; fromPoint += sideDirFrom * fromSize; toPoint += sideDirTo * toSize; float length = Vector2.Distance(fromPoint, toPoint); var force = Mathf.Lerp(kMaxSplineForce, kMinSplineForce, length / maxLength); Vector2 fromTangent = directionFrom * length * force; Vector2 toTangent = directionTo * length * force; UnityEditor.Handles.DrawBezier(fromPoint, toPoint, fromPoint + fromTangent, toPoint + toTangent, UnityEditor.Handles.color, null, kArrowThickness); var color = UnityEditor.Handles.color; color.a = selected == true ? 1f : 0.5f; UnityEditor.Handles.color = color; UnityEditor.Handles.DrawAAPolyLine(kArrowThickness, toPoint, toPoint + (directionTo - sideDirTo) * toSize * kArrowHeadSize); UnityEditor.Handles.DrawAAPolyLine(kArrowThickness, toPoint, toPoint + (directionTo + sideDirTo) * toSize * kArrowHeadSize); }
public virtual void OnPreviewGUI(Color color, Rect r, GUIStyle background, bool drawInfo, bool selectable) { var _target = this.target as LayoutWindowType; var layout = _target.layout.layout; var layoutElements = _target.layout.components; if (layout != null) { if (this.layoutEditor == null) { this.layoutEditor = Editor.CreateEditor(layout) as WindowLayoutEditor; } if (this.layoutEditor != null) { //var emptyStyle = GUIStyle.none; if (Event.current.type != EventType.Layout) { this.layoutEditor.OnPreviewGUI(color, r, background, drawInfo, true, (element, elementRect, isClicked) => { if (isClicked == true) { var tag = element.tag; var comp = layoutElements.FirstOrDefault((e) => e.tag == tag); if (comp != null) { this.selectedComponent = comp; } } /*var tag = element.tag; * var comp = layoutElements.FirstOrDefault((e) => e.tag == tag); * if (comp != null) { * * comp.OnPreviewGUI(elementRect, emptyStyle); * * }*/ }); } } } }
public void OnPreviewGUI(Color color, Rect r, GUIStyle background, bool drawInfo, bool selectable, bool hovered, WindowLayoutElement selectedElement, System.Action <WindowLayoutElement> onSelection, System.Action <WindowLayoutElement, Rect, bool> onElementGUI, List <WindowLayoutElement> highlighted) { GUI.BeginClip(r); var oldColor = GUI.color; var c = Color.white; c.a = 0.3f; GUI.color = c; GUI.Box(r, string.Empty, background); GUI.color = oldColor; var _target = this.target as UnityEngine.UI.Windows.WindowLayout; if (_target == null) { return; } color.a = 0.4f; var selectedColor = color; var notSelectedColor = color; selectedColor.a = 0.8f; notSelectedColor.a = 0.4f; var elements = _target.elements; if (ME.EditorUtilities.IsPrefab(_target.gameObject) == false && _target.gameObject.activeInHierarchy == true) { this.CalculateRects(_target); } var scaleFactor = 0f; if (elements.Count > 0) { scaleFactor = this.GetFactor(new Vector2(_target.root.editorRectLocal.width, _target.root.editorRectLocal.height), new Vector2(r.width, r.height)); } if (ME.EditorUtilities.IsPrefab(_target.gameObject) == true) { if (this.processReconnection == false && scaleFactor == 0f) { this.processReconnection = true; // We must re-attach prefab var tempGo = UnityEditor.PrefabUtility.InstantiatePrefab(_target.gameObject) as GameObject; Selection.activeGameObject = tempGo; foreach (var component in tempGo.GetComponents <Component>()) { var editor = Editor.CreateEditor(component) as WindowLayoutEditor; if (editor != null) { editor.CalculateRects(component as UnityEngine.UI.Windows.WindowLayout); EditorUtility.SetDirty(component); } } var i = 0; EditorApplication.update += () => { ++i; if (i == 30) { UnityEditor.PrefabUtility.ReplacePrefab(tempGo, PrefabUtility.GetPrefabParent(tempGo), ReplacePrefabOptions.ConnectToPrefab); GameObject.DestroyImmediate(tempGo); this.processReconnection = false; } }; } } var scaleFactorCanvas = _target.editorScale > 0f ? 1f / _target.editorScale : 1f; scaleFactor *= scaleFactorCanvas; var selected = WindowLayoutStyles.styles.boxSelected; var styles = WindowLayoutStyles.styles.boxes; var stylesSelected = WindowLayoutStyles.styles.boxesSelected; var elementSelectedStyle = WindowLayoutStyles.styles.layoutElementSelected; var elementHighlightStyle = WindowLayoutStyles.styles.layoutElementHighlighted; var horArrowsStyle = ME.Utilities.CacheStyle("WindowLayout.GetEditorStyle.horArrowsStyle", "ColorPickerHorizThumb", (style) => new GUIStyle(style)); var vertArrowsStyle = ME.Utilities.CacheStyle("WindowLayout.GetEditorStyle.vertArrowsStyle", "ColorPickerVertThumb", (style) => new GUIStyle(style)); var horStyle = ME.Utilities.CacheStyle("WindowLayout.GetEditorStyle.horStyle", "box", (style) => new GUIStyle(style)); var vertStyle = ME.Utilities.CacheStyle("WindowLayout.GetEditorStyle.vertStyle", "box", (style) => new GUIStyle(style)); var tagSelectedRectBool = false; var tagSelectedRect = new Rect(); var selectedRect = new Rect(); foreach (var element in elements) { if (element == null) { continue; } element.editorDrawDepth = element.GetComponentsInParent <WindowLayoutElement>(true).Length - 1; var rect = element.editorRect; rect.x *= scaleFactor; rect.y *= scaleFactor; rect.x += r.x + r.width * 0.5f; rect.y += r.y + r.height * 0.5f; rect.x -= r.x; rect.y -= r.y; rect.width *= scaleFactor; rect.height *= scaleFactor; var styleDepth = Mathf.Clamp(element.editorDrawDepth, 0, WindowLayoutStyles.MAX_DEPTH - 1); var style = styles[styleDepth]; if (rect.Contains(Event.current.mousePosition) == true) { style = stylesSelected[styleDepth]; element.editorHovered = true; this.Repaint(); } else { element.editorHovered = false; this.Repaint(); } if (/*WindowLayoutElement.waitForComponentConnectionElementTemp == element ||*/ Selection.gameObjects.Contains(element.gameObject) == true) { style = selected; selectedRect = rect; } if (selectedElement != null) { GUI.color = (selectedElement == element) ? selectedColor : notSelectedColor; if (selectedElement == element) { style = selected; selectedRect = rect; } } else { GUI.color = color; } GUI.Label(rect, string.Empty, style); GUI.color = oldColor; var boxColor = EditorGUIUtility.isProSkin ? oldColor : new Color(1f, 1f, 1f, 0.2f); var marginX = 4f; var marginY = 2f; if (element.autoStretchX == true) { var vRect = new Rect(rect); vRect.x += marginX; vRect.y = vRect.y + vRect.height * 0.5f - vertArrowsStyle.fixedHeight * 0.5f; vRect.width -= marginX * 2f; vRect.height = vertArrowsStyle.fixedHeight; GUI.color = oldColor; GUI.Label(vRect, string.Empty, vertArrowsStyle); GUI.color = boxColor; GUI.Label(vRect, string.Empty, vertStyle); } if (element.autoStretchY == true) { var vRect = new Rect(rect); vRect.x = vRect.x + vRect.width * 0.5f - horArrowsStyle.fixedWidth * 0.5f; vRect.y += marginY; vRect.width = horArrowsStyle.fixedWidth; vRect.height -= marginY * 2f; GUI.color = oldColor; GUI.Label(vRect, string.Empty, horArrowsStyle); GUI.color = boxColor; GUI.Label(vRect, string.Empty, horStyle); } if (Event.current.type == EventType.Repaint) { element.tempEditorRect = rect; } if (UnityEditor.UI.Windows.Plugins.Flow.Layout.LayoutSettingsEditor.selectedTagIndex == (int)element.tag) { tagSelectedRectBool = true; tagSelectedRect = rect; } } if (highlighted != null) { foreach (var element in elements) { if (highlighted.Contains(element) == true) { GUI.Box(element.tempEditorRect, string.Empty, elementHighlightStyle); } } } GUI.Box(selectedRect, string.Empty, elementSelectedStyle); if (drawInfo == true || onElementGUI != null || selectable == true) { WindowLayoutElement maxDepthElement = null; var _maxDepth = -1; foreach (var element in elements) { if (element.showInComponentsList == false) { continue; } if (element.editorHovered == false) { continue; } if (_maxDepth < element.editorDrawDepth) { _maxDepth = element.editorDrawDepth; maxDepthElement = element; } } foreach (var element in elements) { if (element.showInComponentsList == false) { continue; } if (element.editorHovered == false || maxDepthElement != element) { continue; } if (GUI.Button(element.tempEditorRect, string.Empty, GUIStyle.none) == true) { if (selectable == true) { //WindowLayoutElement.waitForComponentConnectionElementTemp = element; //WindowLayoutElement.waitForComponentConnectionTemp = true; if (onSelection != null) { onSelection(element); } if (onElementGUI != null) { onElementGUI(element, element.tempEditorRect, true); } } } else { if (onElementGUI != null) { onElementGUI(element, element.tempEditorRect, false); } } } } GUI.color = oldColor; GUI.EndClip(); if (tagSelectedRectBool == true) { var scrollValue = Vector2.zero; var inspectorType = Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.InspectorWindow"); if (EditorWindow.mouseOverWindow != null) { var scrollType = inspectorType.GetField("m_ScrollPosition", BindingFlags.Instance | BindingFlags.Public); if (scrollType != null) { scrollValue = (Vector2)scrollType.GetValue(EditorWindow.mouseOverWindow); } } var tagSelectedRectOffset = tagSelectedRect; tagSelectedRectOffset.center += r.center; tagSelectedRectOffset.center -= new Vector2(r.width * 0.5f, r.height * 0.5f); var rc = UnityEditor.UI.Windows.Plugins.Flow.Layout.LayoutSettingsEditor.selectedTagRect; rc.center -= scrollValue; GUI.Box(tagSelectedRectOffset, string.Empty, elementHighlightStyle); GUI.Box(rc, string.Empty, elementHighlightStyle); WindowLayoutEditor.DrawNavigationArrow(true, Vector2.up, tagSelectedRectOffset.xMin <= 20f ? Vector2.down : Vector2.left, rc, tagSelectedRectOffset); } }