internal void OnGUI() { var offset = GraphEditor.Skin.tooltipPadding; var popupRect = new Rect(); popupRect.xMin = Event.current.mousePosition.x + 10; popupRect.yMin = Event.current.mousePosition.y + 10; popupRect.size = GUI.skin.label.CalcSize(new GUIContent(Text)); popupRect.size += new Vector2(offset.horizontal, offset.vertical); XGUI.ResetToStyle(null); XGUI.Color = GraphEditor.Skin.tooltipColor; XGUI.Border = GraphEditor.Skin.tooltipTextureOffset; XGUI.Normal.background = GraphEditor.Skin.tooltipBackground; XGUI.Box(popupRect, XGUI.None); popupRect.xMin += offset.left; popupRect.yMin += offset.right; XGUI.ResetToStyle(null); XGUI.RichText = true; XGUI.Alignment = TextAnchor.UpperLeft; XGUI.Normal.textColor = GraphEditor.Skin.tooltipTextColor; XGUI.Label(popupRect, Text); }
public static void DrawNode(GraphEditor editor, Node node) { GUI.enabled = !editor.search.IsOpen; EditorGUI.BeginChangeCheck(); if (Util.IsNull(node)) { return; } var serializedObject = new SerializedObject(node); var inputs = node.GetInputSockets(); var outputs = node.GetOutputSockets(); var inputWidth = GetContentWidth(node, true, inputs); var inputHeight = GetPropertyHeights(serializedObject, node, inputs, editor); var outputWidth = GetContentWidth(node, false, outputs); var outputHeight = GetPropertyHeights(serializedObject, node, outputs, editor); var height = Mathf.Max(inputHeight, outputHeight) + LINE_PADDING + EditorGUIUtility.singleLineHeight; // Draw box var rect = new Rect(); rect.width = inputWidth + outputWidth + BOX_PADDING * 2 + SOCKET_RADIUS * 4 + SOCKET_PADDING * 2 + MIDDLE_PADDING; if (inputWidth == 0 || outputWidth == 0) { rect.width -= MIDDLE_PADDING + SOCKET_RADIUS * 2 + SOCKET_PADDING; } rect.height = height + BOX_PADDING * 2; rect.center = new Vector2(node.XPos, -node.YPos) + editor.Offset; var fullRect = new Rect(rect); XGUI.ResetToStyle(null); XGUI.Color = GraphEditor.Skin.nodeColor; if (editor.Target == null || !editor.Target.Equals(node)) { XGUI.Color = GraphEditor.Skin.TintColor (XGUI.Color, GraphEditor.Skin.offNodeTint); } XGUI.Border = GraphEditor.Skin.nodeTextureOffset; XGUI.Normal.background = GraphEditor.Skin.nodeBackgroundTexture; XGUI.Box(rect); if (editor.Target != null && editor.Target.Equals(node)) { XGUI.ResetToStyle(null); XGUI.Color = GraphEditor.Skin.nodeFrameColor; XGUI.Border = GraphEditor.Skin.nodeTextureOffset; XGUI.Normal.background = GraphEditor.Skin.nodeFrameTexture; XGUI.Box(rect); } var labelRect = new Rect(rect); labelRect.x += BOX_PADDING; labelRect.y += BOX_PADDING; labelRect.height = EditorGUIUtility.singleLineHeight; XGUI.ResetToStyle(null); XGUI.FontStyle = FontStyle.Bold; XGUI.Normal.textColor = GraphEditor.Skin.nodeTextHeaderColor; if (editor.search.IsOpen) { XGUI.Normal.textColor = GraphEditor.Skin.TintColor( XGUI.Normal.textColor, GraphEditor.Skin.disabledNodeTextTint); } XGUI.Label(labelRect, node.DisplayName); rect.x += BOX_PADDING; if (inputWidth != 0) { rect.x += SOCKET_RADIUS * 2 + SOCKET_PADDING; } rect.y = labelRect.y + labelRect.height + LINE_PADDING; rect.width = inputWidth; var leftRect = new Rect(rect); if (inputWidth != 0) { rect.x += inputWidth + MIDDLE_PADDING; } rect.width = outputWidth; var rightRect = new Rect(rect); DrawMembers(true, editor, node, leftRect, serializedObject, inputs); DrawMembers(false, editor, node, rightRect, serializedObject, outputs); rect = fullRect; if (!editor.search.IsOpen) { switch (Event.current.type) { case EventType.MouseDown: if (rect.Contains(Event.current.mousePosition)) { editor.Target = node; Event.current.Use(); } break; case EventType.MouseDrag: if (ReferenceEquals(editor.Target, node)) { Undo.RecordObject(node, "Move " + node.GetType().Name); if (editor.Snap <= 0) { node.XPos += (int)Event.current.delta.x; node.YPos -= (int)Event.current.delta.y; } else { var graphPos = editor.GraphPosition; node.XPos = Mathf.RoundToInt(graphPos.x / editor.Snap) * editor.Snap; node.YPos = Mathf.RoundToInt(graphPos.y / editor.Snap) * editor.Snap; } Event.current.Use(); } break; case EventType.MouseUp: if (ReferenceEquals(editor.Target, node)) { Event.current.Use(); } break; case EventType.KeyDown: switch (Event.current.keyCode) { #if UNITY_EDITOR_OSX case KeyCode.Backspace: var masked = Event.current.modifiers & EventModifiers.Command; if (masked == EventModifiers.Command) { Delete(editor, node); } break; #endif case KeyCode.Delete: Delete(editor, node); break; } break; } } if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); } GUI.enabled = true; }
public void OnGUI(GraphEditor editor) { if (editor.Target != this) { Close(); return; } if (!isOpen) { return; } GUI.enabled = true; var rect = new Rect(); rect.size = size; rect.center = position + new Vector2(0, rect.size.y / 2); XGUI.ResetToStyle(GUI.skin.box); XGUI.Normal.background = GraphEditor.boxTexture; XGUI.Box(rect); rect.position += Vector2.one * SEARCH_PADDING; rect.size -= Vector2.one * SEARCH_PADDING * 2; XGUI.ResetToStyle(null); XGUI.BeginArea(rect); XGUI.BeginVertical(); if (XEvent.IsKeyDown(KeyCode.Escape)) { Close(); XEvent.Use(); } // Search bar XGUI.BeginHorizontal(); XGUI.ResetToStyle(GUI.skin.button); var buttonString = searchNodes ? "Nodes" : "Variables"; if (XGUI.Button(buttonString, XGUI.Width(70))) { searchNodes = !searchNodes; } bool keysUsed = false; var newSearchStr = searchStr; if (searchNodes) { // Detect key events before the text field switch (Event.current.type) { case EventType.KeyDown: switch (Event.current.keyCode) { case KeyCode.UpArrow: selected--; Event.current.Use(); break; case KeyCode.DownArrow: selected++; Event.current.Use(); break; case KeyCode.Home: selected = 0; Event.current.Use(); break; case KeyCode.End: selected = int.MaxValue; Event.current.Use(); break; case KeyCode.PageUp: selected -= 11; Event.current.Use(); break; case KeyCode.PageDown: selected += 11; Event.current.Use(); break; } keysUsed = Event.current.type == EventType.Used; break; } XGUI.ResetToStyle(GUI.skin.textField); GUI.SetNextControlName("search_field"); newSearchStr = XGUI.TextField(searchStr); GUI.FocusControl("search_field"); var countStr = "" + resultCount; if (searchJob != null && searchJob.IsRunning) { countStr += "..."; } GUILayout.Label(countStr, GUILayout.ExpandWidth(false)); } GUILayout.EndHorizontal(); var hoveringOnResult = false; if (searchNodes) { hoveringOnResult = DrawNodeResults(editor, newSearchStr, keysUsed); } else { DrawVariableResults(editor); } GUILayout.EndVertical(); GUILayout.EndArea(); switch (Event.current.type) { case EventType.MouseDown: if (hoveringOnResult) { SelectResult(editor, selected); Close(); } else if (rect.Contains(Event.current.mousePosition)) { editor.Target = this; Event.current.Use(); } break; case EventType.MouseUp: if (rect.Contains(Event.current.mousePosition)) { editor.Target = this; Event.current.Use(); } break; } }
void OnGUI() { Tooltip = null; // Make the box texture opaque if (boxTexture == null) { if (EditorGUIUtility.isProSkin) { // Make a copy of the old texture var oldTex = GUI.skin.box.normal.background; var tmp = RenderTexture.GetTemporary(oldTex.width, oldTex.height); Graphics.Blit(oldTex, tmp); RenderTexture previous = RenderTexture.active; RenderTexture.active = tmp; boxTexture = new Texture2D(oldTex.width, oldTex.height); boxTexture.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0); RenderTexture.active = previous; RenderTexture.ReleaseTemporary(tmp); // Remove alpha var colors = boxTexture.GetPixels(); for (int i = 0; i < colors.Length; ++i) { // Pro background color is RGB(64, 64, 64) colors[i].r = 0.2196f + (colors[i].r * colors[i].a); colors[i].g = 0.2196f + (colors[i].g * colors[i].a); colors[i].b = 0.2196f + (colors[i].b * colors[i].a); colors[i].a = 1; } boxTexture.SetPixels(colors); boxTexture.Apply(); } else { boxTexture = GUI.skin.box.normal.background; } } if (flatTexture == null) { flatTexture = new Texture2D(1, 1); flatTexture.filterMode = FilterMode.Point; flatTexture.SetPixels(new Color[1] { Color.white }); flatTexture.Apply(); } rectCache = rectCache ?? new Dictionary <Socket, Rect>(); rectCache.Clear(); autoRepaintOnSceneChange = true; wantsMouseMove = true; Offset = new Vector2( Mathf.RoundToInt(Offset.x), Mathf.RoundToInt(Offset.y) ); var graphRect = GetGraphRect(); var mousePos = Event.current.mousePosition; GraphPosition = new Vector2( mousePos.x - graphRect.xMin - Offset.x, -mousePos.y + graphRect.yMin + Offset.y ); // Top Toolbar var toolbarHeight = Vector2.up * TOOLBAR_HEIGHT; var topToolbarRect = new Rect(position); topToolbarRect.position = Vector2.zero; topToolbarRect.size -= new Vector2(10, 0); topToolbarRect.height = toolbarHeight.y; XGUI.ResetToStyle(null); XGUI.BeginArea(new Rect(0, 0, position.width - 10, TOOLBAR_HEIGHT)); XGUI.BeginHorizontal(); var oldGraph = Graph; XGUI.ResetToStyle(null); XGUI.BeginVertical(); XGUI.FlexibleSpace(); Graph = XGUI.ObjectField(Graph, false); if (Graph != oldGraph) { CenterView(); } XGUI.FlexibleSpace(); XGUI.EndVertical(); XGUI.FlexibleSpace(); XGUI.ResetToStyle(GUI.skin.button); if (XGUI.Button("Variables")) { VariablesEditor.Launch(this); } XGUI.EndHorizontal(); XGUI.EndArea(); // Bottom Toolbar XGUI.ResetToStyle(null); XGUI.BeginArea(new Rect(0, position.size.y - TOOLBAR_HEIGHT, position.width - 10, TOOLBAR_HEIGHT)); XGUI.BeginHorizontal(); XGUI.ResetToStyle(GUI.skin.button); XGUI.Enabled = Graph != null; snap = XGUI.ToggleButton(snap, "Snap"); XGUI.ResetToStyle(GUI.skin.button); XGUI.Enabled = Graph != null; if (XGUI.Button("Center View")) { CenterView(); } XGUI.FlexibleSpace(); if (graphRect.Contains(mousePos)) { XGUI.ResetToStyle(GUI.skin.label); XGUI.Alignment = TextAnchor.MiddleRight; XGUI.Label( string.Format("{0}, {1}", GraphPosition.x, GraphPosition.y), XGUI.ExpandHeight(true)); } XGUI.EndHorizontal(); XGUI.EndArea(); // Draw the graph { XGUI.ResetToStyle(null); XGUI.BackgroundColor = Skin.canvasColor; XGUI.Normal.background = flatTexture; XGUI.Box(graphRect); // Make the clipping window for the graph graphRect.position += Vector2.one * GRAPH_PADDING; graphRect.size -= Vector2.one * GRAPH_PADDING * 2; XGUI.ResetToStyle(null); XGUI.BeginClip(graphRect); // Draw the graph var gridRect = new Rect(Vector2.zero, graphRect.size); DrawGrid(gridRect); if (Graph != null && Graph.Nodes != null) { // Make a copy since nodes may reorder the list when drawn var nodes = new List <Node>(Graph.Nodes); // Draw nodes foreach (var node in nodes) { NodeEditor.DrawNode(this, node); } // Draw links if (Target is Socket) { var socket = (Socket)Target; if (rectCache.ContainsKey(socket)) { DrawConnection(rectCache[socket].center, Event.current.mousePosition, socket.IsInput(Graph), true, Skin.tempLinkColor); } } foreach (var link in Graph.Links) { if (!rectCache.ContainsKey(link.FromSocket) || !rectCache.ContainsKey(link.ToSocket)) { continue; } var from = rectCache[link.FromSocket].center; var to = rectCache[link.ToSocket].center; var socketType = link.FromSocket.GetSocketType(Graph); var color = Skin.objectSocketColor; if (socketType == typeof(ExecType)) { color = Skin.execSocketColor; } if (socketType.IsPrimitive) { color = Skin.primitiveSocketColor; } if (search.IsOpen) { color = Skin.TintColor(color, Skin.disabledSocketTint); } DrawConnection(from, to, false, false, color); } } // Draw Tooltip if (Tooltip != null) { Tooltip.OnGUI(); } // Search box search.OnGUI(this); GUI.EndClip(); } if (Graph == null) { CenterViewOn(Vector2.zero); var menuRect = new Rect(); menuRect.size = new Vector2(300, 200); menuRect.center = graphRect.center; GUI.Box(menuRect, GUIContent.none); GUILayout.BeginArea(menuRect); GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(GUILayout.Height(150)); GUILayout.FlexibleSpace(); GUILayout.BeginVertical(GUILayout.Width(250)); GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(); var size = GUI.skin.label.fontSize; var alignment = GUI.skin.label.alignment; GUI.skin.label.fontSize = 20; GUI.skin.label.alignment = TextAnchor.MiddleCenter; GUILayout.Label("Leylines"); GUI.skin.label.fontSize = size; GUI.skin.label.alignment = alignment; GUILayout.EndHorizontal(); GUILayout.FlexibleSpace(); GUILayout.BeginVertical(GUILayout.Height(130)); var rich = GUI.skin.label.richText; GUI.skin.label.richText = true; GUI.skin.label.alignment = TextAnchor.MiddleCenter; GUILayout.Label("<b>No Graph Loaded</b>"); GUI.skin.label.richText = rich; GUI.skin.label.alignment = alignment; GUILayout.EndVertical(); GUILayout.EndVertical(); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.FlexibleSpace(); GUILayout.EndArea(); } switch (Event.current.type) { case EventType.MouseDown: Target = this; GUI.FocusControl(null); Event.current.Use(); break; case EventType.MouseDrag: if (Graph == null) { break; } if (ReferenceEquals(Target, this)) { Offset += Event.current.delta; Event.current.Use(); } break; case EventType.MouseUp: if (ReferenceEquals(Target, this)) { Target = null; GUI.FocusControl(null); } if (Target != null && Target.GetType() == typeof(Socket)) { var clipPos = Event.current.mousePosition; clipPos.y -= topToolbarRect.size.y + GRAPH_PADDING; var socket = (Socket)Target; if (socket.IsInput(Graph)) { search.SetWantedOutputContext(this, socket); } else { search.SetWantedInputContext(this, socket); } search.Open(clipPos, GraphPosition, Graph.Policy, true); Target = search; GUI.FocusControl("search_field"); Event.current.Use(); } if (Event.current.button == 1 && Graph != null) { if (graphRect.Contains(Event.current.mousePosition)) { var clipPos = Event.current.mousePosition; clipPos.y -= topToolbarRect.size.y + GRAPH_PADDING; search.UnsetContext(); search.Open(clipPos, GraphPosition, Graph.Policy); Target = search; GUI.FocusControl("search_field"); Event.current.Use(); } } break; case EventType.DragUpdated: if (DragAndDrop.objectReferences.Length == 1) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; } else { DragAndDrop.visualMode = DragAndDropVisualMode.Rejected; } Event.current.Use(); break; case EventType.DragPerform: if (DragAndDrop.objectReferences.Length == 1) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; DragAndDrop.AcceptDrag(); var obj = DragAndDrop.objectReferences[0]; var type = obj.GetType(); var node = CreateInstance <DynamicNode>(); var socket = new DynamicSocket(type.Name, type, type.Name, SocketFlags.AllowMultipleLinks | SocketFlags.Editable); socket.SocketValue = obj; node.DisplayName = obj.GetType().Name; node.AddOutputSocket(socket); AddNode(node, GraphPosition); } else { DragAndDrop.visualMode = DragAndDropVisualMode.Rejected; } Event.current.Use(); break; } Repaint(); if (GUI.changed && Graph != null && !Graph.Equals(null)) { EditorUtility.SetDirty(Graph); } }
public static void DrawSocket (GraphEditor editor, Node node, Socket socket, Rect rect) { editor.rectCache[socket] = rect; var socketType = socket.GetSocketType(editor.Graph); var color = GraphEditor.Skin.objectSocketColor; var style = GraphEditor.Skin.paramSocketStyle; if (socketType == typeof(ExecType)) { color = GraphEditor.Skin.execSocketColor; style = GraphEditor.Skin.execSocketStyle; } else if (socketType.IsPrimitive) { color = GraphEditor.Skin.primitiveSocketColor; } var on = editor.Graph.Links.IsSocketLinkedTo(socket) || editor.Graph.Links.IsSocketLinkedFrom(socket); XGUI.ResetToStyle(null); XGUI.Normal.background = on ? style.onSocketTexture : style.offSocketTexture; XGUI.Color = on ? color : GraphEditor.Skin.TintColor(color, GraphEditor.Skin.offSocketTint); XGUI.Color = !editor.search.IsOpen ? XGUI.Color : GraphEditor.Skin.TintColor(XGUI.Color, GraphEditor.Skin.disabledSocketTint); XGUI.Box(rect); if (!GUI.enabled) { return; } switch (Event.current.type) { case EventType.MouseDown: if (rect.Contains(Event.current.mousePosition)) { if (Event.current.control) { editor.Graph.Links.RemoveAllWith(socket); } else { editor.Target = socket; } Event.current.Use(); } break; case EventType.MouseUp: if (!(editor.Target is Socket)) { break; } if (!rect.Contains(Event.current.mousePosition)) { break; } var otherSocket = (Socket)editor.Target; if (socket.Equals(otherSocket)) { editor.Target = null; break; } if (socket.NodeID == otherSocket.NodeID) { editor.Target = null; break; } var graph = editor.Graph; if (socket.IsInput(graph) == otherSocket.IsInput(graph)) { editor.Target = null; break; } if (socket.GetSocketType(graph) != otherSocket.GetSocketType(graph)) { if (!socket.GetSocketType(graph) .IsAssignableFrom(otherSocket.GetSocketType(graph))) { editor.Target = null; break; } } // Record to graph Undo.RegisterCompleteObjectUndo(graph, string.Format("Link {0} and {1}", socket, otherSocket ) ); if (!socket.GetFlags(graph).AllowMultipleLinks()) { graph.Links.RemoveAllWith(socket); } if (!otherSocket.GetFlags(graph).AllowMultipleLinks()) { graph.Links.RemoveAllWith(otherSocket); } graph.Links.Add(graph, socket, otherSocket); editor.Target = null; GUI.changed = true; Event.current.Use(); break; } }