/** emit event from node-GUI. */ private void EmitNodeEvent(OnNodeEvent e) { switch (modifyMode) { case ModifyMode.CONNECT_STARTED: { switch (e.eventType) { /* handling */ case OnNodeEvent.EventType.EVENT_NODE_HANDLING: { /* animate connectionPoint under mouse if this connectionPoint is able to accept this kind of connection. */ if (false) { var candidateNodes = NodesUnderPosition(e.globalMousePosition); if (!candidateNodes.Any()) break; var nodeUnderMouse = candidateNodes.Last(); // ignore if target node is source itself. if (nodeUnderMouse == e.eventSourceNode) break; var candidatePoints = nodeUnderMouse.ConnectionPointUnderGlobalPos(e.globalMousePosition); var sourcePoint = currentEventSource.eventSourceConnectionPoint; // limit by connectable or not. var connectableCandidates = candidatePoints.Where(point => IsConnectablePointFromTo(sourcePoint, point)).ToList(); if (!connectableCandidates.Any()) break; // connectable point is exist. change line color. // or, do something.. Debug.Log("connectable!"); } break; } /* drop detected. */ case OnNodeEvent.EventType.EVENT_NODE_DROPPED: { // finish connecting mode. modifyMode = ModifyMode.CONNECT_ENDED; /* connect when dropped target is connectable from start connectionPoint. */ { var candidateNodes = NodesUnderPosition(e.globalMousePosition); if (!candidateNodes.Any()) break; var nodeUnderMouse = candidateNodes.Last(); // ignore if target node is source itself. if (nodeUnderMouse == e.eventSourceNode) break; var candidatePoints = nodeUnderMouse.ConnectionPointUnderGlobalPos(e.globalMousePosition); var sourcePoint = currentEventSource.eventSourceConnectionPoint; // limit by connectable or not. var connectableCandidates = candidatePoints.Where(point => IsConnectablePointFromTo(sourcePoint, point)).ToList(); if (!connectableCandidates.Any()) break; // target point is determined. var connectablePoint = connectableCandidates.First(); var startNode = e.eventSourceNode; var startConnectionPoint = currentEventSource.eventSourceConnectionPoint; var endNode = nodeUnderMouse; var endConnectionPoint = connectablePoint; // reverse if connected from input to output. if (startConnectionPoint.isInput) { startNode = nodeUnderMouse; startConnectionPoint = connectablePoint; endNode = e.eventSourceNode; endConnectionPoint = currentEventSource.eventSourceConnectionPoint; } var label = startConnectionPoint.label; AddConnection(label, startNode, startConnectionPoint, endNode, endConnectionPoint); SaveGraphWithReload(); } break; } default: { // Debug.Log("unconsumed or ignored event:" + e.eventType); modifyMode = ModifyMode.CONNECT_ENDED; break; } } break; } case ModifyMode.CONNECT_ENDED: { switch (e.eventType) { case OnNodeEvent.EventType.EVENT_NODE_TAPPED: { Undo.RecordObject(this, "Select Node"); var tappedNodeId = e.eventSourceNode.nodeId; foreach (var node in nodes) { if (node.nodeId == tappedNodeId) { node.SetActive(); activeObject = new ActiveObject(AssetGraphSettings.ObjectKind.NODE, node.nodeId, node.GetPos()); } else node.SetInactive(); } foreach (var con in connections) { con.SetInactive(); } break; } /* start connection handling. */ case OnNodeEvent.EventType.EVENT_NODE_HANDLE_STARTED: { modifyMode = ModifyMode.CONNECT_STARTED; currentEventSource = e; break; } /* connectionPoint tapped. */ case OnNodeEvent.EventType.EVENT_CONNECTIONPOINT_RECEIVE_TAPPED: { var sourcePoint = e.eventSourceConnectionPoint; var relatedConnections = connections .Where( con => con.IsStartAtConnectionPoint(sourcePoint) || con.IsEndAtConnectionPoint(sourcePoint) ) .ToList(); /* show menuContext for control these connections. */ var menu = new GenericMenu(); menu.AddItem( new GUIContent("delete all connections"), false, () => { Undo.RecordObject(this, "Delete All Connections"); foreach (var con in relatedConnections) { var conId = con.connectionId; DeleteConnectionById(conId); } SaveGraphWithReload(); } ); menu.ShowAsContext(); break; } case OnNodeEvent.EventType.EVENT_CLOSE_TAPPED: { var deletingNodeId = e.eventSourceNode.nodeId; DeleteNode(deletingNodeId); SaveGraphWithReload(); InitializeGraph(); break; } case OnNodeEvent.EventType.EVENT_DUPLICATE_TAPPED: { var duplicateNodeId = e.eventSourceNode.nodeId; var duplicatePoint = e.globalMousePosition; DuplicateNode(duplicateNodeId, duplicatePoint.x, duplicatePoint.y); break; } /* releasse detected. node moved. node tapped. */ case OnNodeEvent.EventType.EVENT_NODE_RELEASED: { var movedNode = e.eventSourceNode; var beforePosition = activeObject.pos; var afterPosition = movedNode.GetPos(); movedNode.SetPos(beforePosition); Undo.RecordObject(this, "Move Node"); movedNode.SetPos(afterPosition); UpdateSpacerRect(); SaveGraph(); break; } default: { // Debug.Log("unconsumed or ignored event:" + e.eventType); break; } } break; } } switch (e.eventType) { case OnNodeEvent.EventType.EVENT_CONNECTIONPOINT_UPDATED: { Debug.LogError("あーポイント追加時に線が消えちゃうのが困るよな"); var targetNode = e.eventSourceNode; var connectionsFromThisNode = connections.Where(con => con.startNodeId == targetNode.nodeId).ToList(); var connectionsToThisNode = connections.Where(con => con.endNodeId == targetNode.nodeId).ToList(); // remove connections from this node. foreach (var con in connectionsFromThisNode) { connections.Remove(con); } // remove connections to this node. foreach (var con in connectionsToThisNode) { connections.Remove(con); } break; } case OnNodeEvent.EventType.EVENT_SAVE: { SaveGraphWithReload(); Repaint(); break; } } }
/** emit event from node-GUI. */ private void EmitNodeEvent (OnNodeEvent e) { switch (modifyMode) { case ModifyMode.CONNECT_STARTED: { switch (e.eventType) { /* handling */ case OnNodeEvent.EventType.EVENT_NODE_MOVING: { // do nothing. break; } /* connection drop detected from toward node. */ case OnNodeEvent.EventType.EVENT_NODE_CONNECTION_RAISED: { // finish connecting mode. modifyMode = ModifyMode.CONNECT_ENDED; if (currentEventSource == null) break; var sourceNode = currentEventSource.eventSourceNode; var sourceConnectionPoint = currentEventSource.eventSourceConnectionPoint; var targetNode = e.eventSourceNode; var targetConnectionPoint = e.eventSourceConnectionPoint; if (sourceNode.nodeId == targetNode.nodeId) break; if (!IsConnectablePointFromTo(sourceConnectionPoint, targetConnectionPoint)) break; var startNode = sourceNode; var startConnectionPoint = sourceConnectionPoint; var endNode = targetNode; var endConnectionPoint = targetConnectionPoint; // reverse if connected from input to output. if (sourceConnectionPoint.isInput) { startNode = targetNode; startConnectionPoint = targetConnectionPoint; endNode = sourceNode; endConnectionPoint = sourceConnectionPoint; } var label = startConnectionPoint.label; AddConnection(label, startNode, startConnectionPoint, endNode, endConnectionPoint); SaveGraphWithReload(); break; } /* connection drop detected by started node. */ case OnNodeEvent.EventType.EVENT_NODE_CONNECTION_OVERED: { // finish connecting mode. modifyMode = ModifyMode.CONNECT_ENDED; /* connect when dropped target is connectable from start connectionPoint. */ var candidateNodes = NodesUnderPosition(e.globalMousePosition); if (!candidateNodes.Any()) break; var nodeUnderMouse = candidateNodes[0]; // ignore if target node is source itself. if (nodeUnderMouse.nodeId == e.eventSourceNode.nodeId) break; var candidatePoints = nodeUnderMouse.ConnectionPointUnderGlobalPos(e.globalMousePosition); if (!candidatePoints.Any()) break; var sourcePoint = currentEventSource.eventSourceConnectionPoint; // limit by connectable or not. var connectableCandidates = candidatePoints.Where(point => IsConnectablePointFromTo(sourcePoint, point)).ToList(); if (!connectableCandidates.Any()) break; // target point is determined. var connectablePoint = connectableCandidates.First(); var startNode = e.eventSourceNode; var startConnectionPoint = currentEventSource.eventSourceConnectionPoint; var endNode = nodeUnderMouse; var endConnectionPoint = connectablePoint; // reverse if connected from input to output. if (startConnectionPoint.isInput) { startNode = nodeUnderMouse; startConnectionPoint = connectablePoint; endNode = e.eventSourceNode; endConnectionPoint = currentEventSource.eventSourceConnectionPoint; } var label = startConnectionPoint.label; AddConnection(label, startNode, startConnectionPoint, endNode, endConnectionPoint); SaveGraphWithReload(); break; } default: { // Debug.Log("unconsumed or ignored event:" + e.eventType); modifyMode = ModifyMode.CONNECT_ENDED; break; } } break; } case ModifyMode.CONNECT_ENDED: { switch (e.eventType) { /* node move detected. */ case OnNodeEvent.EventType.EVENT_NODE_MOVING: { var tappedNode = e.eventSourceNode; var tappedNodeId = tappedNode.nodeId; if (activeObject.idPosDict.ContainsKey(tappedNodeId)) { // already active, do nothing for this node. var distancePos = tappedNode.GetPos() - activeObject.idPosDict.ReadonlyDict()[tappedNodeId]; foreach (var node in nodes) { if (node.nodeId == tappedNodeId) continue; if (!activeObject.idPosDict.ContainsKey(node.nodeId)) continue; var relativePos = activeObject.idPosDict.ReadonlyDict()[node.nodeId] + distancePos; node.SetPos(relativePos); } break; } if (Event.current.shift) { Undo.RecordObject(this, "Select Objects"); var additiveIds = new List<string>(activeObject.idPosDict.ReadonlyDict().Keys); additiveIds.Add(tappedNodeId); activeObject = RenewActiveObject(additiveIds); UpdateActivationOfObjects(activeObject); UpdateSpacerRect(); break; } Undo.RecordObject(this, "Select Node"); activeObject = RenewActiveObject(new List<string>{tappedNodeId}); UpdateActivationOfObjects(activeObject); break; } /* start connection handling. */ case OnNodeEvent.EventType.EVENT_NODE_CONNECT_STARTED: { modifyMode = ModifyMode.CONNECT_STARTED; currentEventSource = e; break; } case OnNodeEvent.EventType.EVENT_CLOSE_TAPPED: { Undo.RecordObject(this, "Delete Node"); var deletingNodeId = e.eventSourceNode.nodeId; DeleteNode(deletingNodeId); SaveGraphWithReload(); InitializeGraph(); break; } /* releasse detected. node move over. node tapped. */ case OnNodeEvent.EventType.EVENT_NODE_TOUCHED: { var movedNode = e.eventSourceNode; var movedNodeId = movedNode.nodeId; // already active, node(s) are just tapped or moved. if (activeObject.idPosDict.ContainsKey(movedNodeId)) { /* active nodes(contains tap released node) are possibly moved. */ var movedIdPosDict = new Dictionary<string, Vector2>(); foreach (var node in nodes) { if (!activeObject.idPosDict.ContainsKey(node.nodeId)) continue; var startPos = activeObject.idPosDict.ReadonlyDict()[node.nodeId]; if (node.GetPos() != startPos) { // moved. movedIdPosDict[node.nodeId] = node.GetPos(); } } if (movedIdPosDict.Any()) { foreach (var node in nodes) { if (activeObject.idPosDict.ReadonlyDict().Keys.Contains(node.nodeId)) { var startPos = activeObject.idPosDict.ReadonlyDict()[node.nodeId]; node.SetPos(startPos); } } Undo.RecordObject(this, "Move Node"); foreach (var node in nodes) { if (movedIdPosDict.Keys.Contains(node.nodeId)) { var endPos = movedIdPosDict[node.nodeId]; node.SetPos(endPos); } } var activeObjectIds = activeObject.idPosDict.ReadonlyDict().Keys.ToList(); activeObject = RenewActiveObject(activeObjectIds); } else { // nothing moved, should cancel selecting this node. var cancelledActivatedIds = new List<string>(activeObject.idPosDict.ReadonlyDict().Keys); cancelledActivatedIds.Remove(movedNodeId); Undo.RecordObject(this, "Select Objects"); activeObject = RenewActiveObject(cancelledActivatedIds); } UpdateActivationOfObjects(activeObject); UpdateSpacerRect(); SaveGraph(); break; } if (Event.current.shift) { Undo.RecordObject(this, "Select Objects"); var additiveIds = new List<string>(activeObject.idPosDict.ReadonlyDict().Keys); // already contained, cancel. if (additiveIds.Contains(movedNodeId)) { additiveIds.Remove(movedNodeId); } else { additiveIds.Add(movedNodeId); } activeObject = RenewActiveObject(additiveIds); UpdateActivationOfObjects(activeObject); UpdateSpacerRect(); SaveGraph(); break; } Undo.RecordObject(this, "Select Node"); activeObject = RenewActiveObject(new List<string>{movedNodeId}); UpdateActivationOfObjects(activeObject); UpdateSpacerRect(); SaveGraph(); break; } default: { // Debug.Log("unconsumed or ignored event:" + e.eventType); break; } } break; } } switch (e.eventType) { case OnNodeEvent.EventType.EVENT_CONNECTIONPOINT_DELETED: { var deletedConnectionPoint = e.eventSourceConnectionPoint; var deletedOutputPointConnections = connections.Where(con => con.outputPoint.pointId == deletedConnectionPoint.pointId).ToList(); if (!deletedOutputPointConnections.Any()) break; connections.Remove(deletedOutputPointConnections[0]); break; } case OnNodeEvent.EventType.EVENT_CONNECTIONPOINT_LABELCHANGED: { var labelChangedConnectionPoint = e.eventSourceConnectionPoint; var changedLabel = labelChangedConnectionPoint.label; var labelChangedOutputPointConnections = connections.Where(con => con.outputPoint.pointId == labelChangedConnectionPoint.pointId).ToList(); if (!labelChangedOutputPointConnections.Any()) break; labelChangedOutputPointConnections[0].label = changedLabel; break; } case OnNodeEvent.EventType.EVENT_BEFORESAVE: { Undo.RecordObject(this, "Update Node Setting"); break; } case OnNodeEvent.EventType.EVENT_SAVE: { SaveGraphWithReloadSilent(); Repaint(); break; } case OnNodeEvent.EventType.EVENT_SETUPWITHPACKAGE: { package = e.eventSourceNode.currentPackage; Setup(package); Repaint(); break; } case OnNodeEvent.EventType.EVENT_UPDATEPACKAGE: { foreach (var node in nodes) { /* if package is deleted & node's current package is that, should change current package to None. */ if (!Node.NodeSharedPackages.Contains(node.currentPackage)) node.currentPackage = string.Empty; } break; } } }
public void DrawConnectionOutputPointMark (OnNodeEvent eventSource, bool justConnecting, Event current) { if (scaleFactor != SCALE_MAX) return; var defaultPointTex = outputPointMarkConnectedTex; if (justConnecting && eventSource != null) { if (eventSource.eventSourceNode.nodeId != this.nodeId) { if (eventSource.eventSourceConnectionPoint.isInput) { defaultPointTex = enablePointMarkTex; } } } var globalMousePosition = current.mousePosition; foreach (var point in connectionPoints) { if (point.isOutput) { var outputPointRect = OutputRect(point); GUI.DrawTexture( outputPointRect, defaultPointTex ); // eventPosition is contained by outputPointRect. if (outputPointRect.Contains(globalMousePosition)) { if (current.type == EventType.MouseDown) { Emit(new OnNodeEvent(OnNodeEvent.EventType.EVENT_NODE_CONNECT_STARTED, this, current.mousePosition, point)); } } } } }
private void DrawStraightLineFromCurrentEventSourcePointTo (Vector2 to, OnNodeEvent eventSource) { if (eventSource == null) return; var p = eventSource.eventSourceNode.GlobalConnectionPointPosition(eventSource.eventSourceConnectionPoint); Handles.DrawLine(new Vector3(p.x, p.y, 0f), new Vector3(to.x, to.y, 0f)); }
public void DrawConnectionInputPointMark (OnNodeEvent eventSource, bool justConnecting) { if (scaleFactor != SCALE_MAX) return; var defaultPointTex = inputPointMarkTex; if (justConnecting && eventSource != null) { if (eventSource.eventSourceNode.nodeId != this.nodeId) { if (eventSource.eventSourceConnectionPoint.isOutput) { defaultPointTex = enablePointMarkTex; } } } foreach (var point in connectionPoints) { if (point.isInput) { GUI.DrawTexture( new Rect( baseRect.x - 2f, baseRect.y + (baseRect.height - AssetGraphGUISettings.CONNECTION_POINT_MARK_SIZE)/2f, AssetGraphGUISettings.CONNECTION_POINT_MARK_SIZE, AssetGraphGUISettings.CONNECTION_POINT_MARK_SIZE ), defaultPointTex ); } } }