public bool HasConnectionTo(Node node) { foreach(Connection connection in connections) { if(connection.to == node) return true; } return false; }
void ActivateNode(Node node) { Debug.Log("Activating: " + brain.name + "/" + node.name); if(activeNode != null) { foreach(BrainState state in activeNode.states) { state.OnNodeEnd(); state.enabled = false; } } activeNode = node; foreach(BrainState state in activeNode.states) { state.enabled = true; state.OnNodeBegin(); } }
void CheckConditions(Node node) { for(int connectionIndex = 0; connectionIndex < node.connections.Count; connectionIndex++) { Connection connection = node.connections[connectionIndex]; int fulfilledConditions = 0; for(int conditionIndex = 0; conditionIndex < connection.conditions.Count; conditionIndex++) { Condition condition = connection.conditions[conditionIndex]; Parameter parameter = condition.parameter; switch(parameter.type) { case ParameterType.Bool: bool boolA = parameter.boolValue; bool boolB = condition.boolValue; if( (boolA == boolB) && (condition.equalityCondition == Condition.EqualityCondition.Equal) || (boolA != boolB) && (condition.equalityCondition == Condition.EqualityCondition.NotEqual)) { fulfilledConditions++; } break; case ParameterType.Int: int intA = parameter.intValue; int intB = condition.intValue; if( (intA == intB) && (condition.sizeCondition == Condition.SizeCondition.Equal) || (intA > intB) && (condition.sizeCondition == Condition.SizeCondition.Greater) || (intA < intB) && (condition.sizeCondition == Condition.SizeCondition.Less)) { fulfilledConditions++; } break; case ParameterType.Float: float floatA = parameter.floatValue; float floatB = condition.floatValue; if( (floatA == floatB) && (condition.sizeCondition == Condition.SizeCondition.Equal) || (floatA > floatB) && (condition.sizeCondition == Condition.SizeCondition.Greater) || (floatA < floatB) && (condition.sizeCondition == Condition.SizeCondition.Less)) { fulfilledConditions++; } break; case ParameterType.String: string stringA = parameter.stringValue; string stringB = condition.stringValue; if( (stringA == stringB) && (condition.equalityCondition == Condition.EqualityCondition.Equal) || (stringA != stringB) && (condition.equalityCondition == Condition.EqualityCondition.NotEqual)) { fulfilledConditions++; } break; case ParameterType.Random: float rFloatA = Random.value; float rFloatB = condition.floatValue; if( (rFloatA == rFloatB) && (condition.sizeCondition == Condition.SizeCondition.Equal) || (rFloatA > rFloatB) && (condition.sizeCondition == Condition.SizeCondition.Greater) || (rFloatA < rFloatB) && (condition.sizeCondition == Condition.SizeCondition.Less)) { fulfilledConditions++; } break; } } if(connection.conditions.Count == fulfilledConditions && activeNode != connection.to) { ActivateNode(connection.to); } } }
public void DrawNodeInspector(Node node) { Color originalColor = GUI.color; GUI.color = Color.green; if(!(showNodeInspector = EditorGUILayout.Foldout(showNodeInspector, string.Format("Node [{0}]", node.name)))) { GUI.color = originalColor; return; } if(node == brain.anyNode) { GUILayout.Label("The Ω node has no settings."); } else { node.name = EditorGUILayout.TextField("Name", node.name); if(GUI.changed) { RefreshAsset(); } if(GUILayout.Button("Make Default")) { brain.defaultNode = node; RefreshAsset(); } if(GUILayout.Button("Remove Node")) { brain.RemoveNode(node); BrainWindow.window.selectedNode = null; BrainWindow.window.selectedConnection = null; RefreshAsset(); } } 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 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 OnFocus() { dragNode = null; linkNode = null; selectedConnection = null; }
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(); } }