Hi Stoyan,
I have the following code for the connection logic:
This is in the diagram class:
private void OperatorNodeAdded(object sender, ContainerChildEventArgs e)
{
//throw new NotImplementedException();
var addedNode = e.Node;
var nearestNode = e.Container;
if (nearestNode != null)
{
if (nearestNode is BROperator && addedNode is BROperator)
{
double minDistance = Double.MaxValue;
BROperator parentOperator = nearestNode as BROperator;
BROperator addedOperator = addedNode as BROperator;
Point[] targetPoints = new Point[2]
{
parentOperator.rightVariablePosition,
parentOperator.leftVariablePosition
};
Point pointToSnap = addedOperator.connectionPoint;
Point targetPoint = new Point();
// Check the distance of each targetpoint and the pointToSnap for the closest point
foreach (var s in targetPoints)
{
// Calculate the distance between the points
double distance_squared = (pointToSnap.X - s.X) * (pointToSnap.X - s.X) + (pointToSnap.Y - s.Y) * (pointToSnap.Y - s.Y);
double distance = Math.Sqrt(distance_squared); //Utilities.Distance(pointToSnap, s);
if (distance < minDistance)
{
minDistance = distance;
targetPoint = s;
}
}
// Move this node to position
addedOperator.Move(targetPoint.X, targetPoint.Y);
parentOperator.resizeNode();
}
}
}
And this is the BROperator class:
class BROperator : ContainerNode
{
public string operatorSign { get; private set; }
public int Priority { get; set; }
object leftVariable;
object rightVariable;
public Point leftVariablePosition { get; private set; }
public Point rightVariablePosition { get; private set; }
// The connection point of this node to connect to another node
public Point connectionPoint { get; private set; }
public BROperator()
{
initialize();
}
public BROperator(string operatorSign)
{
initialize();
this.operatorSign = operatorSign;
}
public BROperator(BROperator prototype)
: base(prototype)
{
this.Priority = prototype.Priority;
operatorSign = prototype.operatorSign;
initialize();
}
private void initialize()
{
leftVariable = null;
rightVariable = null;
this.MinimumSize = new Size(Bounds.Width, Bounds.Height);
this.CaptionHeight = 0;
this.Margin = 2;
this.AutoShrink = true;
this.AllowAddChildren = true;
this.Brush = new LinearGradientBrush(Colors.LawnGreen, Colors.LawnGreen, 20);
Rect nodeSize = new Rect();
nodeSize.Width = 70;
nodeSize.Height = 30;
this.Bounds = nodeSize;
}
public override void Draw(System.Windows.Media.DrawingContext graphics, MindFusion.Diagramming.Wpf.RenderOptions options)
{
//base.Draw(graphics, options);
double height = Bounds.Height;
double width = Bounds.Width;
double midHeight = height / 2;
double midWidth = Bounds.Width / 2;
connectionPoint = new Point(Bounds.X + midWidth, Bounds.Y);
var shape = new PathGeometry();
// Main outline of the shape
shape.Figures.Add(new PathFigure(new Point(0, midHeight), new[]
{
new LineSegment(new Point(5, 0), true),
new LineSegment(new Point(width - 5, 0), true),
new LineSegment(new Point(width, midHeight), true),
new LineSegment(new Point(width - 5, height), true),
new LineSegment(new Point(5, height), true),
new LineSegment(new Point(0, midHeight), true)
}, true));
// Scaled down outline for the leftVariable
shape.Figures.Add(new PathFigure(new Point(5, midHeight), new[]
{
new LineSegment(new Point(10, 10), true),
new LineSegment(new Point(midWidth - 15, 10), true),
new LineSegment(new Point(midWidth - 10, midHeight), true),
new LineSegment(new Point(midWidth - 15, height - 10), true),
new LineSegment(new Point(10, height-10), true)
}, true));
leftVariablePosition = new Point(Bounds.X + midWidth - 25, Bounds.Y + 10);
// Scaled down outline for the rightVariable
shape.Figures.Add(new PathFigure(new Point(midWidth + 10, midHeight), new[]
{
new LineSegment(new Point(midWidth + 15, 10), true),
new LineSegment(new Point(width - 10, 10), true),
new LineSegment(new Point(width - 5, midHeight), true),
new LineSegment(new Point(width - 10, height - 10), true),
new LineSegment(new Point(midWidth + 15, height - 10), true)
}, true));
rightVariablePosition = new Point(Bounds.X + midWidth + 25, Bounds.Y + 10);
// Add the operatorSign as text
FormattedText txt = new FormattedText(operatorSign, CultureInfo.GetCultureInfo("en-us"), FlowDirection, new Typeface("Verdana"), 15, Brushes.Black);
Point txtPosition = new Point(Bounds.Width / 2 - 5.5, Bounds.Height / 2 - 10);
graphics.DrawGeometry(Brush, new Pen(Stroke, StrokeThickness), shape);
graphics.DrawText(txt, txtPosition);
}
public void resizeNode()
{
// Sample resizing
this.MinimumSize = new Size(Bounds.Width * 2, Bounds.Height);
}
But it doesn't seem to be connecting to the right position. Help?
Thanks in advance!
Vincent