using System;
using System.Drawing;
using System.Windows.Forms;
namespace Simulation {
///
/// Draws directed edges between the SimNode's in the simulation model.
///
public class SimConnector {
/// The beginning SimNode for the connector.
private SimNode startNode;
/// The ending SimNode for the connector.
private SimNode stopNode ;
/// boolean, true if edge direction is left to right.
private bool leftRight;
/// space between SimNode edge and any vertical line segments.
private int gap = 25;
/// half space of the gap, used to draw the arrow ends.
private int halfGap;
/// Point [] holds 4 line segments of any edge.
private Point[] line = new Point[4];
/// Point [] holds locations use to draw arrow ends.
private Point[] end = new Point[3];
///
/// Create a connector between two SimNodes.
///
/// edge is directed from start SimNode.
/// edge is directed to end SimNode.
/// direction of edge, true for left to right.
public SimConnector (SimNode start, SimNode end, bool leftToRight) {
startNode = start;
stopNode = end;
leftRight = leftToRight;
halfGap = gap / 4;
}
///
/// Show the connector on the screen
///
/// drawing context.
public void draw(PaintEventArgs pea) {
Graphics grfx = pea.Graphics; // get the graphics context for display.
bool startDirection = startNode.LeftRight; // direction of start SimNode.
bool stopDirection = stopNode.LeftRight; // direction of stop SimNode.
int x1, y1, y2, back; // locations of line seqments of directed edge.
back = line[3].X - halfGap;
if ( startDirection && stopDirection && leftRight) {
// startNode output and connection are left to right
line[0] = startNode.RightConnector;
line[3] = stopNode.LeftConnector;
x1 = line[0].X + gap;
y1 = line[0].Y;
y2 = line[3].Y;
line[1] = new Point(x1, y1);
line[2] = new Point(x1, y2);
back = line[3].X - halfGap; }
if (startDirection && (! stopDirection) && (! leftRight)) {
// startNode leftToRight, but connection and stopNode are right to left
line[0] = startNode.RightConnector;
line[3] = stopNode.RightConnector;
x1 = line[0].X + gap;
y1 = line[0].Y;
y2 = line[3].Y;
line[1] = new Point(x1, y1);
line[2] = new Point(x1, y2);
back = line[3].X + halfGap; }
if ( (! startDirection) && (! stopDirection) && (! leftRight)) {
// startNode, stopNode, and connection all right to left
line[0] = startNode.LeftConnector;
line[3] = stopNode.RightConnector;
x1 = line[0].X - gap;
y1 = line[0].Y;
y2 = line[3].Y;
line[1] = new Point(x1, y1);
line[2] = new Point(x1, y2);
back = line[3].X + halfGap; }
if ((! startDirection) && stopDirection && (! leftRight)) {
//startNode right to left, stopNode left to right, and connection right to left
line[0] = startNode.LeftConnector;
line[3] = stopNode.LeftConnector;
x1 = line[0].X - gap;
y1 = line[0].Y;
y2 = line[3].Y;
line[1] = new Point(x1, y1);
line[2] = new Point(x1, y2);
back = line[3].X - halfGap; }
grfx.DrawLines(Pens.Black, line);
// set points for arrow end
end[0] = line[3];
end[1] = new Point(back, line[3].Y - halfGap);
end[2] = new Point(back, line[3].Y + halfGap);
grfx.FillPolygon(Brushes.Black, end);
}
}
}