public void DrawConnectionInspector(Connection connection) { Color originalColor = GUI.color; GUI.color = Color.cyan; Node nodeA = connection.from; Node nodeB = connection.to; if((nodeA == null || nodeB == null) || !(showConnectionInspector = EditorGUILayout.Foldout(showConnectionInspector, string.Format("Connection [{0} > {1}]", nodeA.name, nodeB.name)))) { GUI.color = originalColor; return; } if(GUILayout.Button("Remove Connection")) { brain.RemoveConnection(connection); BrainWindow.window.selectedConnection = null; RefreshAsset(); } if((showConditionInspector = EditorGUILayout.Foldout(showConditionInspector, "Conditions"))) { // LIST CONDITIONALS Condition removeConditional = null; foreach(Condition condition in connection.conditions) { Parameter parameter = condition.parameter; EditorGUILayout.BeginHorizontal(); switch(parameter.type) { case ParameterType.Bool: condition.equalityCondition = (Condition.EqualityCondition) EditorGUILayout.EnumPopup(condition.equalityCondition, GUILayout.ExpandWidth(true)); condition.boolValue = GUILayout.Toggle(condition.boolValue, string.Format("{0}", parameter.name)); break; case ParameterType.Int: condition.sizeCondition = (Condition.SizeCondition) EditorGUILayout.EnumPopup(condition.sizeCondition, GUILayout.ExpandWidth(true)); condition.intValue = EditorGUILayout.IntField(string.Format("{0}", parameter.name), condition.intValue); break; case ParameterType.Float: condition.sizeCondition = (Condition.SizeCondition) EditorGUILayout.EnumPopup(condition.sizeCondition, GUILayout.ExpandWidth(true)); condition.floatValue = EditorGUILayout.FloatField(string.Format("{0}", parameter.name), condition.floatValue); break; case ParameterType.String: condition.equalityCondition = (Condition.EqualityCondition) EditorGUILayout.EnumPopup(condition.equalityCondition, GUILayout.ExpandWidth(true)); condition.stringValue = EditorGUILayout.TextField(string.Format("{0}", parameter.name), condition.stringValue); break; case ParameterType.Random: condition.sizeCondition = (Condition.SizeCondition) EditorGUILayout.EnumPopup(condition.sizeCondition, GUILayout.ExpandWidth(true)); condition.floatValue = EditorGUILayout.FloatField(string.Format("{0} (Random 0..1)", parameter.name), condition.floatValue); break; default: break; } if(GUILayout.Button("Remove", GUILayout.MaxWidth(80))) { removeConditional = condition; } EditorGUILayout.EndHorizontal(); } if(removeConditional != null) { connection.conditions.Remove(removeConditional); brain.RemoveCondition(removeConditional); } EditorGUILayout.Space(); // ADD CONDITIONAL if(brain.parameters.Count > 0) { if(selectedParameterIndex >= brain.parameters.Count) selectedParameterIndex = 0; string[] parameterNames = new string[brain.parameters.Count]; for(int i = 0; i < brain.parameters.Count; i++) { parameterNames[i] = brain.parameters[i].name; } selectedParameterIndex = EditorGUILayout.Popup(selectedParameterIndex, parameterNames); Parameter selectedParameter = brain.GetParameter(parameterNames[selectedParameterIndex]); if(GUILayout.Button(string.Format("Add new {0} condition", selectedParameter.name.ToLower()))) { if(connection.HasConditionForParameter(selectedParameter)) { Debug.LogError("Cannot add the same condition twice."); } else { Condition condition = brain.CreateCondition(selectedParameter); connection.conditions.Add(condition); RefreshAsset(); AddToBrainAsset(condition); } } } EditorGUILayout.Space(); } GUI.color = originalColor; }
void DrawNode(Node node) { Color originalColor = GUI.color; if(brain.defaultNode == node) GUI.color = Color.yellow; if(brain.anyNode == node) GUI.color = Color.magenta; if(selectedNode == node) GUI.color = Color.green; Vector2 originalPosition = new Vector2(node.position.x, node.position.y); node.position.x += scrollPosition.x; node.position.y += scrollPosition.y; if(selectedNode == node) { Rect positionLabelRect = new Rect(node.position); positionLabelRect.y += node.position.height; GUI.Label(positionLabelRect, originalPosition.ToString()); } GUILayout.BeginArea(node.position); GUI.Box(new Rect(0, 0, node.position.width, node.position.height), ""); GUIStyle labelStyle = new GUIStyle(GUI.skin.label); labelStyle.fontSize = brain.anyNode == node ? 48 : 12; labelStyle.fontStyle = FontStyle.Bold; labelStyle.alignment = TextAnchor.MiddleCenter; labelStyle.wordWrap = true; GUI.Label(new Rect(0, 0, node.position.width, node.position.height), node.name, labelStyle); GUILayout.EndArea(); if(Event.current.button == 0) { switch(Event.current.type) { case EventType.MouseDown: if(node.position.Contains(Event.current.mousePosition)) { if(linkNode != null && linkNode != node) { if(node == brain.anyNode) { ShowNotification(new GUIContent("Can not connect to the Ω node.")); } else if(linkNode.HasConnectionTo(node)) { ShowNotification(new GUIContent("Can not connect to the same node twice.")); } else { selectedConnection = brain.CreateConnection(linkNode, node); AddToBrainAsset(selectedConnection); } linkNode = null; } dragNode = node; selectedNode = node; Event.current.Use(); Save(); } break; case EventType.MouseDrag: if(dragNode == node) { originalPosition.x += Event.current.delta.x; originalPosition.y += Event.current.delta.y; Event.current.Use(); Repaint(); } break; case EventType.MouseUp: if(dragNode == node) { dragNode = null; int snap = 64; originalPosition.x = Mathf.RoundToInt(originalPosition.x / snap) * snap; originalPosition.y = Mathf.RoundToInt(originalPosition.y / snap) * snap; Event.current.Use(); Repaint(); } break; } } node.position.x = originalPosition.x; node.position.y = originalPosition.y; GUI.color = originalColor; }
void OnFocus() { dragNode = null; linkNode = null; selectedConnection = null; }
void WindowEvents() { if((Event.current.button == 0) && (Event.current.type == EventType.MouseDrag)) { scrollPosition += Event.current.delta; Repaint(); } if((Event.current.button == 0) && (Event.current.type == EventType.MouseDown)) { Selection.activeObject = brain; selectedNode = null; selectedConnection = null; linkNode = null; GUI.FocusControl(""); Save(); } if((Event.current.button == 1) && (Event.current.type == EventType.MouseDown)) { Node node = brain.CreateNode(); node.position.x = Event.current.mousePosition.x - node.position.width / 2f - scrollPosition.x; node.position.y = Event.current.mousePosition.y - node.position.height / 2f - scrollPosition.y; selectedNode = node; Save(); AddToBrainAsset(node); } }
void DrawNodeConnections(Node node) { int connectionCounter = 0; Vector3 startPos; Vector3 endPos; Vector3 startTan; Vector3 endTan; Rect fromNodeRect = node.position; Vector3 connectFromPos = new Vector3(fromNodeRect.x + fromNodeRect.width, fromNodeRect.y, 0) + (Vector3) scrollPosition; Vector3 buttonSize = new Vector3(80, 20, 0); Vector3 buttonRelativePosition = Vector3.zero; Rect buttonRect; foreach(Connection connection in node.connections) { Color originalColor = GUI.color; Rect toNodeRect = connection.to.position; Color linkColor; if(selectedConnection == connection) { linkColor = Color.cyan; } else if((selectedNode != null) && (selectedNode == connection.from || selectedNode == connection.to)) { linkColor = Color.yellow; } else if(selectedNode == null) { linkColor = new Color(1, 1, 1, 0.5f); } else { linkColor = new Color(1, 1, 1, 0.1f); } GUI.color = linkColor; buttonRelativePosition = new Vector3(connectFromPos.x + 10, connectFromPos.y + connectionCounter * (buttonSize.y + 2), 0); buttonRect = new Rect(buttonRelativePosition.x, buttonRelativePosition.y, buttonSize.x, buttonSize.y); if(GUI.Button(buttonRect, connection.to.name)) { selectedConnection = connection; Save(); } linkColor.a = 0.3f; startPos = connectFromPos + new Vector3(0, 10, 0); endPos = new Vector3(buttonRect.x, buttonRect.y + buttonRect.height / 2f, 0); startTan = startPos + Vector3.right * 10; endTan = endPos + Vector3.left * 10; Handles.DrawBezier(startPos, endPos, startTan, endTan, linkColor, null, 4f); startPos = endPos + new Vector3(buttonSize.x, 0, 0); endPos = new Vector3(toNodeRect.x, toNodeRect.y + 10, 0) + (Vector3) scrollPosition; startTan = startPos + Vector3.right * 35; endTan = endPos - Vector3.right * 25; Handles.DrawBezier(startPos, endPos, startTan, endTan, linkColor, null, 4f); connectionCounter++; GUI.color = originalColor; } buttonSize.x = 48; buttonRelativePosition = new Vector3(connectFromPos.x + 10, connectFromPos.y + connectionCounter * (buttonSize.y + 2), 0); buttonRect = new Rect(buttonRelativePosition.x, buttonRelativePosition.y, buttonSize.x, buttonSize.y); if(GUI.Button(buttonRect, ">>>")) { linkNode = node; linkPosition = new Vector2(buttonRect.xMax, buttonRect.yMin + buttonRect.height / 2f); } if(linkNode != null) { endPos = Event.current.mousePosition; startTan = (Vector3)linkPosition + Vector3.right * 75; endTan = endPos + ((Vector3)linkPosition - endPos).normalized * 35 - Vector3.up * 40; Handles.DrawBezier((Vector3)linkPosition, endPos, startTan, endTan, Color.cyan, null, 3f); Repaint(); } }