static public Vector2 GetConnectorPosition(NodeValue nodeValue) { Rect nodeRect = nodeValue.node.rect; float x = nodeValue.isOutput ? nodeRect.xMax : nodeRect.xMin; float y = nodeRect.yMin + Node.nodeTopSize; if (nodeValue == nodeValue.node.outputs[0]) { y -= Node.nodeTopSize / 2; } else { NodeValue[] container = nodeValue.isOutput ? nodeValue.node.outputs : nodeValue.node.inputs; for (int i = 0; i < container.Length; i++) { if (container[i] == nodeValue) { y += i * Node.connectorSpacing; break; } } y += nodeValue.isOutput ? 0 : GetNodeInputsOffset(nodeValue.node); } return(new Vector2(x, y)); }
public object EvaluateOutput(NodeValue outputNodeValue) { int nodeValueIndex = GetOutputNodeValueIndex(outputNodeValue); if (nodeValueIndex < 0) { throw new Exception("Given output not found in Node outputs"); } if (nodeValueIndex == 0) { return(this); } object methodObject = GetMethodObjectValue(); object[] parameters = GetMethodParametersValues(); object output = methodInfo.Invoke(methodObject, parameters); if (nodeValueIndex == 1) { return(output); } else { return(parameters[MapNodeIndexToParameterIndex(nodeValueIndex)]); } }
public NodeValue[] GetAllNodeValues() { int resultLength = (m_inputs == null) ? 0 : m_inputs.Length; resultLength += (m_outputs == null) ? 0 : m_outputs.Length; NodeValue[] result = new NodeValue[resultLength]; int index = 0; if (m_inputs != null) { for (int i = 0; i < m_inputs.Length; i++) { result[i] = m_inputs[i]; } index = m_inputs.Length; } if (m_outputs != null) { for (int i = 0; i < m_outputs.Length; i++) { result[i + index] = m_outputs[i]; } } return(result); }
static private void DrawConnector(Rect nodeRect, NodeValue nodeValue, int index) { Vector2 connectorPosition = GetConnectorPosition(nodeValue); DrawCircle(connectorPosition, nodeValue.variantValue.typeColor); EditorGUIUtility.AddCursorRect(GetConnectorRect(nodeValue), MouseCursor.ArrowPlus); string nodeValueLabel = NodeValueLabel(nodeValue); float width = nodeRect.width - s_connectorDiameter * 2; if (nodeValue.isOutput) { GUIStyle outputNameStyle = new GUIStyle(EditorStyles.label); outputNameStyle.alignment = TextAnchor.MiddleRight; Rect labelRect = new Rect(connectorPosition - new Vector2(width + 4 + s_connectorDiameter / 2, Node.connectorSpacing / 2), new Vector2(width, Node.connectorSpacing)); if (index >= 1) { EditorGUI.LabelField(labelRect, nodeValueLabel, outputNameStyle); } } else { GUIStyle inputNameStyle = new GUIStyle(EditorStyles.label); inputNameStyle.alignment = TextAnchor.MiddleLeft; float nameWidth = inputNameStyle.CalcSize(new GUIContent(nodeValueLabel)).x; Rect variantArea = new Rect(connectorPosition + new Vector2(Node.connectorSpacing / 2 + 2, -Node.connectorSpacing / 2), new Vector2(width - nameWidth, Node.connectorSpacing)); Rect labelRect; if (nodeValue.connection == null) { labelRect = variantArea; labelRect.x += width - nameWidth; } else { labelRect = new Rect(connectorPosition + new Vector2(Node.connectorSpacing / 2 + 2, -Node.connectorSpacing / 2), new Vector2(width, Node.connectorSpacing)); } EditorGUI.LabelField(labelRect, nodeValueLabel, inputNameStyle); if (nodeValue.connection == null) { GUILayout.BeginArea(variantArea); EditorGUI.BeginChangeCheck(); Variant tmp = new Variant(VariantView.VariantDataField(nodeValue.variantValue.variantData), nodeValue.variantValue.typeName); if (tmp != nodeValue.variantValue) { Undo.RecordObject(nodeValue, "Changed node value"); nodeValue.variantValue = tmp; TriggerEditorWindow.SetSceneDirty(); } GUILayout.EndArea(); } } }
public void Disconnect() { if (m_isOutput) { throw new Exception("Output nodes can not be disconnected"); } m_connection = null; }
static public NodeValue CreateNodeValue(Variant defaultValue, Node node, bool isOutput) { NodeValue nodeValue = CreateInstance <NodeValue>(); nodeValue.m_value = defaultValue; nodeValue.m_node = node; nodeValue.m_isOutput = isOutput; return(nodeValue); }
static public void OnLeftMouseButton(Routine routine, Event mouseEvent) { switch (mouseEvent.type) { case EventType.MouseDown: m_selectedConnector = GetConnectorAtPosition(routine, mouseEvent.mousePosition); if (m_selectedConnector != null) { m_selectedConnectorPosition = NodeView.GetConnectorPosition(m_selectedConnector); m_selectedNode = null; } else { m_selectedNode = GetNodeAtPosition(routine, mouseEvent.mousePosition); if (m_selectedNode != null) { Undo.RegisterCompleteObjectUndo(m_selectedNode, "Drag Node"); Undo.FlushUndoRecordObjects(); } } break; case EventType.MouseDrag: if (m_selectedNode != null) { m_selectedNode.MoveBy(mouseEvent.delta); TriggerEditorWindow.SetSceneDirty(); mouseEvent.Use(); } break; case EventType.MouseUp: if (m_selectedConnector != null) { NodeValue otherConnector = GetConnectorAtPosition(routine, mouseEvent.mousePosition); if (m_selectedConnector.FakeConnect(otherConnector)) { if (m_selectedConnector.isOutput) { Undo.RegisterCompleteObjectUndo(otherConnector, "Changed node connection"); } else { Undo.RegisterCompleteObjectUndo(m_selectedConnector, "Changed node connection"); } m_selectedConnector.TryConnectWith(otherConnector); TriggerEditorWindow.SetSceneDirty(); } mouseEvent.Use(); } m_selectedNode = null; m_selectedConnector = null; break; } }
protected void LoadMethodInfo(MethodInfo methodInfo) { m_methodInfo = methodInfo; m_methodName = methodInfo.Name; m_methodDeclaringTypeName = methodInfo.DeclaringType.AssemblyQualifiedName; ParameterInfo[] methodParametersInfos = methodInfo.GetParameters(); m_methodParametersTypesNames = new string[methodParametersInfos.Length]; for (int i = 0; i < methodParametersInfos.Length; i++) { m_methodParametersTypesNames[i] = methodParametersInfos[i].ParameterType.AssemblyQualifiedName; } name = nodeMethodAttribute.name; List <NodeValue> inputs = new List <NodeValue>(); List <NodeValue> outputs = new List <NodeValue>(); if (!m_methodInfo.IsStatic) { NodeValue tmp = NodeValue.CreateNodeValue(m_methodInfo.DeclaringType, this, false); tmp.name = "Object"; inputs.Add(tmp); } outputs.Add(NodeValue.CreateNodeValue(typeof(Node), this, true)); if (m_methodInfo.ReturnType != typeof(void)) { NodeValue tmp = NodeValue.CreateNodeValue(m_methodInfo.ReturnType, this, true); tmp.name = nodeMethodAttribute.outputName; outputs.Add(tmp); } for (int i = 0; i < methodParametersInfos.Length; i++) { Type realNodeValueType = methodParametersInfos[i].IsOut ? methodParametersInfos[i].ParameterType.GetElementType() : methodParametersInfos[i].ParameterType; NodeValue nodeValue = NodeValue.CreateNodeValue(realNodeValueType, this, methodParametersInfos[i].IsOut); nodeValue.name = methodParametersInfos[i].Name; if (methodParametersInfos[i].IsOut) { outputs.Add(nodeValue); } else { inputs.Add(nodeValue); } } m_inputs = inputs.ToArray(); m_outputs = outputs.ToArray(); m_rect.height = (m_inputs.Length + m_outputs.Length - 1) * s_connectorSpacing + s_topConnectorSpacing + 10 + s_nodeTopSize; }
static private void DrawLink(NodeValue input) { if (input.connection == null) { return; } Vector2 inputPosition = NodeView.GetConnectorPosition(input); Vector2 connectionPosition = NodeView.GetConnectorPosition(input.connection); DrawBezier(inputPosition, connectionPosition, Color.red); }
public int GetInputNodeValueIndex(NodeValue nodeValue) { for (int i = 0; i < m_inputs.Length; i++) { if (m_inputs[i] == nodeValue) { return(i); } } return(-1); }
protected NodeValue[] CloneNodeValues(Node toNode, NodeValue[] originals, bool cloneInputConnection) { if (originals == null) { return(null); } NodeValue[] clones = new NodeValue[originals.Length]; for (int i = 0; i < originals.Length; i++) { clones[i] = originals[i].CloneToNode(toNode, cloneInputConnection); } return(clones); }
public void Disconnect(Node outputNode, Node inputNode) { for (int j = 0; j < inputNode.inputs.Length; j++) { NodeValue otherNodeInput = inputNode.inputs[j]; for (int k = 0; k < outputNode.outputs.Length; k++) { if (otherNodeInput.connection == outputNode.outputs[k]) { otherNodeInput.Disconnect(); } } } }
public NodeValue CloneToNode(Node newNode, bool cloneInputConnection) { NodeValue clone = CreateInstance <NodeValue>(); clone.name = name; clone.m_value = new Variant(m_value); clone.m_node = newNode; clone.m_isOutput = m_isOutput; if (!m_isOutput && cloneInputConnection) { clone.m_connection = m_connection; } return(clone); }
static public string NodeValueLabel(NodeValue nodeValue) { StringBuilder result = new StringBuilder(nodeValue.name); if (result.Length == 0) { return(string.Empty); } result[0] = char.ToUpper(result[0]); for (int i = 1; i < result.Length; i++) { if (char.IsUpper(result[i])) { result.Insert(i++, ' '); } } return(result.ToString()); }
public bool FakeConnect(NodeValue other) { if (other == null) { return(m_connection != null); } if (!CanConnectWith(other)) { return(false); } if (m_isOutput) { return(other.m_connection != this); } else { return(m_connection != other); } }
public bool CanConnectWith(NodeValue other) { //if (other == null) return false; if (other.m_isOutput == m_isOutput) { return(false); } if (other.m_node == m_node) { return(false); } Type type = m_value.GetVariantType(); Type otherType = other.m_value.GetVariantType(); if ((type == typeof(object)) || (otherType == typeof(object))) { return(true); } if (m_isOutput) { if (!type.IsAssignableFrom(otherType)) { return(false); } } else { if (!otherType.IsAssignableFrom(type)) { return(false); } } return(true); }
public bool TryConnectWith(NodeValue other) { if (other == null) { m_connection = null; return(false); } if (!CanConnectWith(other)) { return(false); } if (m_isOutput) { other.m_connection = this; } else { m_connection = other; } return(true); }
static public NodeValue GetConnectorAtPosition(Routine routine, Vector2 position) { if (routine.nodes == null) { return(null); } for (int i = routine.nodes.Length - 1; i >= 0; i--) { Node node = routine.nodes[i]; NodeValue connector = GetConnectorAtPosition(node.inputs, position); if (connector != null) { return(connector); } connector = GetConnectorAtPosition(node.outputs, position); if (connector != null) { return(connector); } } return(null); }
static public Rect GetConnectorRect(NodeValue nodeValue) { return(new Rect(GetConnectorPosition(nodeValue) - Vector2.one * s_connectorDiameter / 2, Vector2.one * s_connectorDiameter)); }