Hi Stoyan,
I have a Node that has the following shape:
String outline = @"
h = Height;
w = Width;
midH = Height / 2;
LineTo(5, 0);
LineTo(10, -7);
LineTo(15, 0);
LineTo(w, 0);
LineTo(w, 15);
LineTo(35, 15);
LineTo(30, 8);
LineTo(25, 15);
LineTo(20, 15);
LineTo(20, midH-7.5);
LineTo(w, midH-7.5);
LineTo(w, midH+7.5);
LineTo(35, midH+7.5);
LineTo(30, midH+0.5);
LineTo(25, midH+7.5);
LineTo(20, midH+7.5);
LineTo(20, h-15);
LineTo(w, h-15);
LineTo(w, h);
LineTo(15, h);
LineTo(10, h-7);
LineTo(5, h);
LineTo(0, h);
LineTo(0, 0);";
That produces the following shape for the node:

I can snap the same node to any of the three marked positions of a previously added node with the same shape.
Here is the code that handles the snapping:
protected void AlignToNearestPoint(Point current, InteractionState ist)
{
if (ist.AdjustmentHandle == 8) // If the node is moved
{
var nearbyNode = Parent.GetNearestNode(current, 30, this);
if (nearbyNode != null)
{
/* If the nearest node has multiple points to connect to, the connection Points
* will be stored here and used later to calculate to which point to snap to.
*/
Point[] targetPoints;
var bounds_nearby_node = nearbyNode.Bounds;
double x_coord = 0;
double y_coord = 0;
var bounds_this_node = this.Bounds;
double x_coord2 = 0;
double y_coord2 = 0;
double dx = 0;
double dy = 0;
if (nearbyNode is If_Shape)
{
// Calculate the point of the nearby node to connect to, default is bottom
x_coord = bounds_nearby_node.X + bounds_nearby_node.Width / 2;
y_coord = bounds_nearby_node.Bottom - bounds_nearby_node.Height / 90;
var targetPoint = new Point(x_coord, y_coord);
// Calculate the point of this node to connect to the nearby node
x_coord2 = bounds_this_node.X + bounds_this_node.Width / 2;
y_coord2 = bounds_this_node.Y;
var pointToSnap = new Point(x_coord2, y_coord2);
double then_x = bounds_nearby_node.X + bounds_nearby_node.Width / 1.43;
double then_y = bounds_nearby_node.Bottom - bounds_nearby_node.Height / 1.18;
Point then_statement = new Point(then_x, then_y);
double else_x = bounds_nearby_node.X + bounds_nearby_node.Width / 1.43;
double else_y = bounds_nearby_node.Bottom - bounds_nearby_node.Height / 2.4;
Point else_statement = new Point(else_x, else_y);
Point bottom = new Point(bounds_nearby_node.X + bounds_nearby_node.Width / 2,
bounds_nearby_node.Bottom - bounds_nearby_node.Height / 90);
double minDistance = double.MaxValue;
targetPoints = new Point[3] { then_statement, else_statement, bottom };
// Check the distance of each targetpoint and the pointToSnap for the closest point
foreach (var s in targetPoints)
{
// Calculate the distancen 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;
}
}
// Calculate the distance of the two points
dx = targetPoint.X - pointToSnap.X;
dy = targetPoint.Y - pointToSnap.Y;
// Move this node by the calculated distance
bounds_this_node.Offset(dx, dy);
SetBounds(bounds_this_node, true, true);
// Lastly, set the connectednode of the nearest node to this node
If_Shape nearby_Node = nearbyNode as If_Shape;
nearby_Node.connected_node = this;
//nearby_Node.resizeAfterSnapping(this, then_statement, else_statement, bottom, targetPoint);
if (lastInflatedNode != nearby_Node)
{
if (lastInflatedNode != null)
lastInflatedNode.Bounds = lastBounds;
Rect r = nearby_Node.Bounds;
lastBounds = r;
nearby_Node.Bounds = new Rect(r.X - 20, r.Y - 20, r.Width, r.Height + 40);
lastInflatedNode = nearby_Node;
}
}
}
}
For example, if I want to snap a node to the 1st marked spot the distance between the upper part and the middle part should become the height of the added node (100 in this case because I set the Bounds to (100, 100)). And if I want to snap it to the 2nd marked spot only the distance between the middle and lower part should increase. The snapping works but I'm still a little stuck on the re-sizing. At first I tried assigning a new shape with the calculated differences but the Bounds stay the same. Any ideas?
Thanks in advance!
Vincent