public static Region GetConnectionRegion(NodeConnection connection) { var to = connection.To; var from = connection.From; RectangleF toBounds; RectangleF fromBounds; if (to.Node.Collapsed) toBounds = to.Node.inputBounds; else toBounds = to.bounds; if (from.Node.Collapsed) fromBounds = from.Node.outputBounds; else fromBounds = from.bounds; var x1 = (fromBounds.Left + fromBounds.Right) / 2.0f; var y1 = (fromBounds.Top + fromBounds.Bottom) / 2.0f; var x2 = (toBounds.Left + toBounds.Right) / 2.0f; var y2 = (toBounds.Top + toBounds.Bottom) / 2.0f; Region region; float centerX; float centerY; using (var linePath = GetArrowLinePath( x1, y1, x2, y2, out centerX, out centerY, true, 5.0f)) { region = new Region(linePath); } return region; }
public void HandleConnectionAdded(NodeConnection connection, bool input) { NodeConnector connector; Node node; NodeItem colorFromItem = null; NodeImageItem imageFromItem = null; NodeImageItem imageOutputItem = null; if (input) node = connection.To.Node; else node = connection.From.Node; NodeItem colorToItem = (NodeItem)node.Items.FirstOrDefault(item => item.Tag.Equals(1)); NodeItem imageToItem = (NodeItem)node.Items.FirstOrDefault(item => item.Tag.Equals(2)); imageOutputItem = (NodeImageItem)node.Items.FirstOrDefault(item => item.Tag.Equals(3)); var outArgs = new NodeItemEventArgs(imageOutputItem); foreach (NodeConnection conn in node.Connections) { if (conn.To.Item == null) continue; if (conn.To.Item == imageToItem) imageFromItem = conn.From.Item as NodeImageItem; if (conn.To.Item == colorToItem) colorFromItem = conn.From.Item; } if (!colorHandlerDict.ContainsKey(node) && colorFromItem != null && imageFromItem != null) { colorHandlerDict[node] = (sender, args) => { if (imageFromItem != null && colorFromItem != null) { imageOutputItem.Image = AddColor(HelperMethods.ColorFromFloatStruct(colorFromItem.OutputData), imageFromItem.Image); node.UpdateOutput(outArgs); } }; colorFromItem.Node.OutputChanged += colorHandlerDict[node]; } if (!imageHandlerDict.ContainsKey(node) && imageFromItem != null && colorFromItem != null) { imageHandlerDict[node] = (sender, args) => { if (imageFromItem != null && colorFromItem != null) { imageOutputItem.Image = AddColor(HelperMethods.ColorFromFloatStruct(colorFromItem.OutputData), imageFromItem.Image); node.UpdateOutput(outArgs); } }; imageFromItem.Node.OutputChanged += imageHandlerDict[node]; } if (imageFromItem != null && colorFromItem != null) { imageOutputItem.Image = AddColor(HelperMethods.ColorFromFloatStruct(colorFromItem.OutputData), imageFromItem.Image); node.UpdateOutput(outArgs); } }
public static Region GetConnectionRegion(NodeConnection connection) { var to = connection.To; var from = connection.From; RectangleF toBounds; RectangleF fromBounds; if (to.Node.Collapsed) { toBounds = to.Node.inputBounds; } else { toBounds = to.bounds; } if (from.Node.Collapsed) { fromBounds = from.Node.outputBounds; } else { fromBounds = from.bounds; } var x1 = (fromBounds.Left + fromBounds.Right) / 2.0f; var y1 = (fromBounds.Top + fromBounds.Bottom) / 2.0f; var x2 = (toBounds.Left + toBounds.Right) / 2.0f; var y2 = (toBounds.Top + toBounds.Bottom) / 2.0f; Region region; float centerX; float centerY; if ((connection.state & RenderState.Hidden) != 0) { region = new Region(connection.textBounds); } else { using (var linePath = GetArrowLinePath(x1, y1, x2, y2, out centerX, out centerY, true, 5.0f)) { region = new Region(linePath); } } return(region); }
public void RenderConnection(Graphics graphics, NodeConnection connection) { var to = connection.To; var from = connection.From; RectangleF toBounds = to.bounds; RectangleF fromBounds = @from.bounds; var x1 = (fromBounds.Left + fromBounds.Right) / 2.0f; var y1 = (fromBounds.Top + fromBounds.Bottom) / 2.0f; var x2 = (toBounds.Left + toBounds.Right) / 2.0f; var y2 = (toBounds.Top + toBounds.Bottom) / 2.0f; using (var path = GraphUtils.GetArrowLinePath(x1, y1, x2, y2, false)) { using (var brush = new SolidBrush(GetArrowLineColor(connection.state | RenderState.Connected))) { graphics.DrawPath(new Pen(brush, 4), path); } connection.bounds = path.GetBounds(); } }
private NodeConnection DeserializeConnection(string s, ref List<NodeConnection> connections, ref Dictionary<int, Node> nodeIdReverseDict) { string[] splitData = s.Split('|'); if (splitData.Length != 4) return null; int fromID = int.Parse(splitData[0]); int toID = int.Parse(splitData[2]); Node fromNode = nodeIdReverseDict[fromID]; Node toNode = nodeIdReverseDict[toID]; NodeItem fromItem = null, toItem = null; foreach (NodeItem item in fromNode.Items) { if (item.Tag != null && item.Tag.ToString().Equals(splitData[1])) fromItem = item; } foreach (NodeItem item in toNode.Items) { if (item.Tag.ToString().Equals(splitData[3])) toItem = item; } NodeConnector fromConnector = new NodeOutputConnector(fromItem, true); NodeConnector toConnector = new NodeInputConnector(toItem, true); NodeConnection NC = new NodeConnection() { From = fromConnector, To = toConnector }; connections.Add(NC); return NC; }
public NodeConnectionEventArgs(NodeConnection connection) { Connection = connection; From = connection.From; To = connection.To; }
static void RenderLabel(Graphics graphics, NodeConnection connection, PointF center, RenderState state, bool isFromLabel = false) { using (var path = new GraphicsPath(FillMode.Winding)) { int cornerSize = (int)GraphConstants.CornerSize * 2; int connectorSize = (int)GraphConstants.ConnectorSize; int halfConnectorSize = (int)Math.Ceiling(connectorSize / 2.0f); int xOffset = GraphConstants.HiddenConnectionLabelOffset; bool isConnectionHidden = (connection.state & RenderState.Hidden) != 0; SizeF size; PointF position; PointF textPosition = center; var text = isConnectionHidden && isFromLabel ? "⋯" : connection.Name; if (connection.textBounds.IsEmpty || connection.textBounds.Location != center) { size = graphics.MeasureString(text, SystemFonts.StatusFont, center, GraphConstants.CenterTextStringFormat); if (isConnectionHidden) { if (isFromLabel) { textPosition = new PointF(center.X + xOffset + size.Width / 2.0f, center.Y); position = new PointF(center.X + xOffset - halfConnectorSize, center.Y - (size.Height / 2.0f)); } else { textPosition = new PointF(center.X - xOffset - size.Width / 2.0f, center.Y); position = new PointF(center.X - xOffset - size.Width - halfConnectorSize, center.Y - (size.Height / 2.0f)); } } else { position = new PointF(center.X - (size.Width / 2.0f) - halfConnectorSize, center.Y - (size.Height / 2.0f)); } size.Width += connectorSize; connection.textBounds = new RectangleF(position, size); } else { size = connection.textBounds.Size; position = connection.textBounds.Location; } var halfWidth = size.Width / 2.0f; var halfHeight = size.Height / 2.0f; var connectorOffset = (int)Math.Floor((GraphConstants.MinimumItemHeight - GraphConstants.ConnectorSize) / 2.0f); var left = position.X; var top = position.Y; var right = position.X + size.Width; var bottom = position.Y + size.Height; path.AddArc(left, top, cornerSize, cornerSize, 180, 90); path.AddArc(right - cornerSize, top, cornerSize, cornerSize, 270, 90); path.AddArc(right - cornerSize, bottom - cornerSize, cornerSize, cornerSize, 0, 90); path.AddArc(left, bottom - cornerSize, cornerSize, cornerSize, 90, 90); path.CloseFigure(); using (var brush = new SolidBrush(GetArrowLineColor(state))) { graphics.FillPath(brush, path); } graphics.DrawString(text, SystemFonts.StatusFont, Brushes.Black, textPosition, GraphConstants.CenterTextStringFormat); //draw outline for all conn. labels when not dragged focused or hovered if ((state & ~RenderState.Hidden & ~RenderState.Backward) == RenderState.None) { graphics.DrawPath(new Pen(GetArrowLineColor(state | RenderState.Connected)), path); } //graphics.DrawRectangle(Pens.Red, connection.textBounds.Left, connection.textBounds.Top, connection.textBounds.Width, connection.textBounds.Height); } }
static void RenderLabel(Graphics graphics, NodeConnection connection, PointF center, RenderState state, bool isFromLabel = false) { using (var path = new GraphicsPath(FillMode.Winding)) { int cornerSize = (int)GraphConstants.CornerSize * 2; int connectorSize = (int)GraphConstants.ConnectorSize; int halfConnectorSize = (int)Math.Ceiling(connectorSize / 2.0f); int xOffset = GraphConstants.HiddenConnectionLabelOffset; bool isConnectionHidden = (connection.state & RenderState.Hidden) != 0; SizeF size; PointF position; PointF textPosition = center; var text = isConnectionHidden && isFromLabel ? "⋯" : connection.Name; if (connection.textBounds.IsEmpty || connection.textBounds.Location != center) { size = graphics.MeasureString(text, StatusFont, center, GraphConstants.CenterTextStringFormat); if (isConnectionHidden) { if (isFromLabel) { textPosition = new PointF(center.X + xOffset + size.Width / 2.0f, center.Y); position = new PointF(center.X + xOffset - halfConnectorSize, center.Y - (size.Height / 2.0f)); } else { textPosition = new PointF(center.X - xOffset - size.Width / 2.0f, center.Y); position = new PointF(center.X - xOffset - size.Width - halfConnectorSize, center.Y - (size.Height / 2.0f)); } } else { position = new PointF(center.X - (size.Width / 2.0f) - halfConnectorSize, center.Y - (size.Height / 2.0f)); } size.Width += connectorSize; connection.textBounds = new RectangleF(position, size); } else { size = connection.textBounds.Size; position = connection.textBounds.Location; } var halfWidth = size.Width / 2.0f; var halfHeight = size.Height / 2.0f; var connectorOffset = (int)Math.Floor((GraphConstants.MinimumItemHeight - GraphConstants.ConnectorSize) / 2.0f); var left = position.X; var top = position.Y; var right = position.X + size.Width; var bottom = position.Y + size.Height; path.AddArc(left, top, cornerSize, cornerSize, 180, 90); path.AddArc(right - cornerSize, top, cornerSize, cornerSize, 270, 90); path.AddArc(right - cornerSize, bottom - cornerSize, cornerSize, cornerSize, 0, 90); path.AddArc(left, bottom - cornerSize, cornerSize, cornerSize, 90, 90); path.CloseFigure(); using (var brush = new SolidBrush(GetArrowLineColor(state))) { graphics.FillPath(brush, path); } graphics.DrawString(text, StatusFont, Brushes.Black, textPosition, GraphConstants.CenterTextStringFormat); //draw outline for all conn. labels when not dragged focused or hovered if ((state & ~RenderState.Hidden & ~RenderState.Backward) == RenderState.None) { graphics.DrawPath(new Pen(GetArrowLineColor(state | RenderState.Connected)), path); } //graphics.DrawRectangle(Pens.Red, connection.textBounds.Left, connection.textBounds.Top, connection.textBounds.Width, connection.textBounds.Height); } }
public NodeConnection Connect(NodeConnector from, NodeConnector to) { if (from == null || to == null || from.Node == null || to.Node == null || !from.Enabled || !to.Enabled) return null; foreach (var other in from.Node.connections) { if (other.From == from && other.To == to) return null; } foreach (var other in to.Node.connections) { if (other.From == from && other.To == to) return null; } var connection = new NodeConnection(); connection.From = from; connection.To = to; from.Node.connections.Add(connection); to.Node.connections.Add(connection); if (ConnectionAdded != null) { var eventArgs = new AcceptNodeConnectionEventArgs(connection); ConnectionAdded(this, eventArgs); if (eventArgs.Cancel) { Disconnect(connection); return null; } } return connection; }
public bool Disconnect(NodeConnection connection) { if (connection == null) return false; if (ConnectionRemoving != null) { var eventArgs = new AcceptNodeConnectionEventArgs(connection); ConnectionRemoving(this, eventArgs); if (eventArgs.Cancel) return false; } if (HasFocus(connection)) FocusElement = null; var from = connection.From; var to = connection.To; if (from != null && from.Node != null) from.Node.connections.Remove(connection); if (to != null && to.Node != null) to.Node.connections.Remove(connection); // Just in case somebody stored it somewhere .. connection.From = null; connection.To = null; if (ConnectionRemoved != null) ConnectionRemoved(this, new NodeConnectionEventArgs(from, to, connection)); this.Invalidate(); return true; }
/// <summary> /// Checks whether the connection between two connectors is allowed. /// This is achieved through event propagation. /// </summary> /// <returns></returns> private bool ConnectionIsAllowed(NodeConnector from, NodeConnector to) { if (HighlightCompatible && null != CompatibilityStrategy) { if (!CompatibilityStrategy.CanConnect(from, to)) return false; } // If someone has subscribed to the ConnectionAdding event, // give them a chance to interrupt this connection attempt. if (null != ConnectionAdding) { // Populate a temporary NodeConnection instance. var connection = new NodeConnection(); connection.From = from; connection.To = to; // Fire the event and see if someone cancels it. var eventArgs = new AcceptNodeConnectionEventArgs(connection); ConnectionAdding(this, eventArgs); if (eventArgs.Cancel) return false; } return true; }
public void HandleConnectionAdded(Graph.NodeConnection connection, bool input) { }
public void HandleConnectionAdded(NodeConnection connection, bool input) { }
public NodeConnectionEventArgs(NodeConnector from, NodeConnector to, NodeConnection connection) { Connection = connection; From = from; To = to; FromItem = from.Item.Name; ToItem = to.Item.Name; }
private void rebuildConnections(GraphControl graph, Dictionary<string, CConnectionFields> inputs, Dictionary<string, CConnectionFields> outputs) { foreach(String key in inputs.Keys) { CConnectionFields inputFields = inputs[key]; NodeConnection conn = new NodeConnection(); conn.Tag = TagFactory.GetTagObject(inputFields.Tag); conn.Name = inputFields.name; String[] fromNameParts = inputFields.From.Split(':'); String fromNodeName = fromNameParts[0]; String fromItemName = fromNameParts[1]; String[] toNameParts = inputFields.To.Split(':'); String toNodeName = toNameParts[0]; String toItemName = toNameParts[1]; Node fromNode = findNode(graph, fromNodeName); NodeItem fromItem = findItem(fromNode, fromItemName); Node toNode = findNode(graph, toNodeName); NodeItem toItem = findItem(toNode, toItemName); graph.Connect(fromItem.Output, toItem.Input); conn.FromItem = fromItemName; conn.ToItem = toItemName; } foreach (String key in outputs.Keys) { CConnectionFields outputFields = outputs[key]; NodeConnection conn = new NodeConnection(); conn.Tag = TagFactory.GetTagObject(outputFields.Tag); conn.Name = outputFields.name; String[] fromNameParts = outputFields.From.Split(':'); String fromNodeName = fromNameParts[0]; String fromItemName = fromNameParts[1]; String[] toNameParts = outputFields.To.Split(':'); String toNodeName = toNameParts[0]; String toItemName = toNameParts[1]; Node fromNode = findNode(graph, fromNodeName); NodeItem fromItem = findItem(fromNode, fromItemName); Node toNode = findNode(graph, toNodeName); NodeItem toItem = findItem(toNode, toItemName); graph.Connect(fromItem.Output, toItem.Input); conn.FromItem = fromItemName; conn.ToItem = toItemName; } graph.Refresh(); }
private string SerializeConnection(NodeConnection NC, GraphModel model) { string retStr = ""; retStr += model.NodeIdentifierDict[NC.From.Node] + "|"; retStr += NC.From.Item.Tag + "|"; retStr += model.NodeIdentifierDict[NC.To.Node] + "|"; retStr += NC.To.Item.Tag; return retStr; }
public void HandleConnectionAdded(NodeConnection connection, bool input) { Node node; if (input) node = connection.To.Node; else node = connection.From.Node; var nodeItem1 = (NodeColorItem)node.Items.FirstOrDefault(item => item.Tag.Equals(1)); var nodeItem2 = (NodeColorItem)node.Items.FirstOrDefault(item => item.Tag.Equals(2)); if (connection.To.Item == nodeItem1) { var outItem = connection.From.Item; nodeItem1.Color = HelperMethods.ColorFromFloatStruct(outItem.OutputData); var outModule = (IModule)outItem.Node.ParentModule; //TODO: Should this be here? node.OutputChanged += color1Handler; if (color1Handler == null) { color1Handler = (sender, args) => { nodeItem1.Color = HelperMethods.ColorFromFloatStruct(outItem.OutputData); SetNodeOutputColor(node, HelperMethods.MixColors(nodeItem1.Color, nodeItem2.Color)); }; if(input) connection.From.Node.OutputChanged += color1Handler; } } if (connection.To.Item == nodeItem2) { var outItem = connection.From.Item; nodeItem2.Color = HelperMethods.ColorFromFloatStruct(outItem.OutputData); var outModule = (IModule)outItem.Node.ParentModule; node.OutputChanged += color2Handler; if (color2Handler == null) { color2Handler = (sender, args) => { nodeItem2.Color = HelperMethods.ColorFromFloatStruct(outItem.OutputData); SetNodeOutputColor(node, HelperMethods.MixColors(nodeItem1.Color, nodeItem2.Color)); }; if (input) connection.From.Node.OutputChanged += color2Handler; } } if(nodeItem1.Color != null && nodeItem2.Color != null) SetNodeOutputColor(node, HelperMethods.MixColors(nodeItem1.Color, nodeItem2.Color)); }
public AcceptNodeConnectionEventArgs(NodeConnection connection, bool cancel) : base(cancel) { Connection = connection; }
public AcceptNodeConnectionEventArgs(NodeConnection connection) { Connection = connection; }
public NodeConnectionEventArgs(NodeConnector from, NodeConnector to, NodeConnection connection) { Connection = connection; From = from; To = to; }
private static void RefreshConnectionView(NodeConnection connection) { var from = (connection.From.Node as MyNodeView).Node; var to = (connection.To.Node as MyNodeView).Node; var connectionView = (connection as MyNodeViewConnection); // If order == 0, the node is likely an output. connectionView.Backward = to.TopologicalOrder != 0 && @from.TopologicalOrder >= to.TopologicalOrder; // If order == 0, the node is likely an output. MyAbstractMemoryBlock output = from.GetAbstractOutput((int) connection.From.Item.Tag); if (output != null) connectionView.Dynamic = output.IsDynamic; else connectionView.Dynamic = false; }
static void RenderLabel(Graphics graphics, NodeConnection connection, PointF center, RenderState state) { using (var path = new GraphicsPath(FillMode.Winding)) { int cornerSize = (int)GraphConstants.CornerSize * 2; int connectorSize = (int)GraphConstants.ConnectorSize; int halfConnectorSize = (int)Math.Ceiling(connectorSize / 2.0f); SizeF size; PointF position; var text = connection.Name; if (connection.textBounds.IsEmpty || connection.textBounds.Location != center) { size = graphics.MeasureString(text, SystemFonts.StatusFont, center, GraphConstants.CenterTextStringFormat); position = new PointF(center.X - (size.Width / 2.0f) - halfConnectorSize, center.Y - (size.Height / 2.0f)); size.Width += connectorSize; connection.textBounds = new RectangleF(position, size); } else { size = connection.textBounds.Size; position = connection.textBounds.Location; } var halfWidth = size.Width / 2.0f; var halfHeight = size.Height / 2.0f; var connectorOffset = (int)Math.Floor((GraphConstants.MinimumItemHeight - GraphConstants.ConnectorSize) / 2.0f); var left = position.X; var top = position.Y; var right = position.X + size.Width; var bottom = position.Y + size.Height; path.AddArc(left, top, cornerSize, cornerSize, 180, 90); path.AddArc(right - cornerSize, top, cornerSize, cornerSize, 270, 90); path.AddArc(right - cornerSize, bottom - cornerSize, cornerSize, cornerSize, 0, 90); path.AddArc(left, bottom - cornerSize, cornerSize, cornerSize, 90, 90); path.CloseFigure(); using (var brush = new SolidBrush(GetArrowLineColor(state))) { graphics.FillPath(brush, path); } graphics.DrawString(text, SystemFonts.StatusFont, Brushes.Black, center, GraphConstants.CenterTextStringFormat); if (state == RenderState.None) graphics.DrawPath(Pens.Black, path); //graphics.DrawRectangle(Pens.Black, connection.textBounds.Left, connection.textBounds.Top, connection.textBounds.Width, connection.textBounds.Height); } }
public NodeConnectionEventArgs(NodeConnection connection) { Connection = connection; From = connection.From; To = connection.To; FromItem = connection.From.Item.Name; ToItem = connection.To.Item.Name; }