|
@@ -107,7 +107,8 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
|
|
|
tempCps.getPosition().y + controller.getScaleDiv2(), x, y);
|
|
|
|
|
|
for (CpsEdge con : model.getEdgesOnCanvas()) {
|
|
|
- if (con.getA().getID() != model.getSelectedObjectID() && con.getB().getID() != model.getSelectedObjectID())
|
|
|
+ if (con.getA().getID() != model.getSelectedObjectID() && con.getB().getID() != model.getSelectedObjectID()
|
|
|
+ && con != edgeHighlight)
|
|
|
g2.drawLine(con.getA().getPosition().x + controller.getScaleDiv2(),
|
|
|
con.getA().getPosition().y + controller.getScaleDiv2(),
|
|
|
con.getB().getPosition().x + controller.getScaleDiv2(),
|
|
@@ -202,15 +203,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
|
|
|
}
|
|
|
// Edge Selection
|
|
|
if (tempCps == null) {
|
|
|
- Line2D l;
|
|
|
- for (CpsEdge p : model.getEdgesOnCanvas()) {
|
|
|
- l = new Line2D(p.getA().getPosition().x, p.getA().getPosition().y, p.getB().getPosition().x,
|
|
|
- p.getB().getPosition().y);
|
|
|
- // distance from a point to a line
|
|
|
- if (l.ptLineDist(x - model.getScaleDiv2(), y - model.getScaleDiv2()) < 10) {
|
|
|
- edgeHighlight = p;
|
|
|
- }
|
|
|
- }
|
|
|
+ edgeHighlight = mousePositionOnEdge(x, y);
|
|
|
}
|
|
|
// Object Selection Highlighting (selectRect)
|
|
|
objectSelectionHighlighting();
|
|
@@ -326,7 +319,6 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
|
|
|
private void drawDeleteEdge() {
|
|
|
boolean node = true;
|
|
|
boolean newEdge = true;
|
|
|
- boolean addEdge = false;
|
|
|
boolean onEdge = true;
|
|
|
CpsEdge e = null;
|
|
|
|
|
@@ -355,44 +347,38 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
|
|
|
}
|
|
|
}
|
|
|
if (onEdge) {
|
|
|
- CpsEdge temp = null;
|
|
|
- CpsEdge e1 = null;
|
|
|
- CpsEdge e2 = null;
|
|
|
- for (CpsEdge p : model.getEdgesOnCanvas()) {
|
|
|
- Line2D l = new Line2D(p.getA().getPosition().x, p.getA().getPosition().y, p.getB().getPosition().x,
|
|
|
- p.getB().getPosition().y);
|
|
|
- // distance from a point to a line
|
|
|
- if (l.ptLineDist(x - model.getScaleDiv2(), y - model.getScaleDiv2()) < 10) {
|
|
|
- node = false;
|
|
|
- addEdge = true;
|
|
|
-
|
|
|
- CpsNode n = new CpsNode("Node");
|
|
|
- n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
|
|
|
- controller.addObjectCanvas(n);
|
|
|
-
|
|
|
- CpsObject r, k;
|
|
|
- r = p.getA();
|
|
|
- k = p.getB();
|
|
|
-
|
|
|
- e = new CpsEdge(n, tempCps);
|
|
|
- n.AddConnection(e);
|
|
|
- tempCps.AddConnection(e);
|
|
|
+ CpsEdge p = mousePositionOnEdge(x, y);
|
|
|
+ if (p != null) {
|
|
|
+ CpsEdge temp = null;
|
|
|
+ CpsEdge e1 = null;
|
|
|
+ CpsEdge e2 = null;
|
|
|
|
|
|
- e1 = new CpsEdge(n, r);
|
|
|
- n.AddConnection(e1);
|
|
|
- r.AddConnection(e1);
|
|
|
+ node = false;
|
|
|
|
|
|
- e2 = new CpsEdge(n, k);
|
|
|
- n.AddConnection(e2);
|
|
|
- k.AddConnection(e2);
|
|
|
+ CpsNode n = new CpsNode("Node");
|
|
|
+ n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
|
|
|
+ controller.addObjectCanvas(n);
|
|
|
|
|
|
- p.getA().getConnections().remove(p);
|
|
|
- p.getB().getConnections().remove(p);
|
|
|
-
|
|
|
- temp = p;
|
|
|
- }
|
|
|
- }
|
|
|
- if (addEdge) {
|
|
|
+ CpsObject r, k;
|
|
|
+ r = p.getA();
|
|
|
+ k = p.getB();
|
|
|
+
|
|
|
+ e = new CpsEdge(n, tempCps);
|
|
|
+ n.AddConnection(e);
|
|
|
+ tempCps.AddConnection(e);
|
|
|
+
|
|
|
+ e1 = new CpsEdge(n, r);
|
|
|
+ n.AddConnection(e1);
|
|
|
+ r.AddConnection(e1);
|
|
|
+
|
|
|
+ e2 = new CpsEdge(n, k);
|
|
|
+ n.AddConnection(e2);
|
|
|
+ k.AddConnection(e2);
|
|
|
+
|
|
|
+ p.getA().getConnections().remove(p);
|
|
|
+ p.getB().getConnections().remove(p);
|
|
|
+
|
|
|
+ temp = p;
|
|
|
controller.removeEdgesOnCanvas(temp);
|
|
|
controller.AddEdgeOnCanvas(e);
|
|
|
controller.AddEdgeOnCanvas(e1);
|
|
@@ -414,6 +400,46 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Checks if the mouse is on an Edge
|
|
|
+ *
|
|
|
+ * @param x
|
|
|
+ * Position of the Mouse
|
|
|
+ * @param y
|
|
|
+ * Position of the Mouse
|
|
|
+ *
|
|
|
+ * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
|
|
|
+ */
|
|
|
+ public CpsEdge mousePositionOnEdge(int x, int y) {
|
|
|
+ int lx, ly, hx, hy;
|
|
|
+ for (CpsEdge p : model.getEdgesOnCanvas()) {
|
|
|
+ Line2D l = new Line2D(p.getA().getPosition().x, p.getA().getPosition().y, p.getB().getPosition().x,
|
|
|
+ p.getB().getPosition().y);
|
|
|
+ if (p.getA().getPosition().x > p.getB().getPosition().x) {
|
|
|
+ hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
|
|
|
+ lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
|
|
|
+ } else {
|
|
|
+ lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
|
|
|
+ hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
|
|
|
+ }
|
|
|
+ if (p.getA().getPosition().y > p.getB().getPosition().y) {
|
|
|
+ hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
|
|
|
+ ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
|
|
|
+ } else {
|
|
|
+ ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
|
|
|
+ hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
|
|
|
+ }
|
|
|
+
|
|
|
+ // distance from a point to a line and between both Objects
|
|
|
+ System.out.println("" + x + " " + y + " , " + hx + " " + lx + " " + hy + " " + ly);
|
|
|
+ if (l.ptLineDistSq(x - model.getScaleDiv2(), y - model.getScaleDiv2()) < 14 && x > lx && x < hx && y > ly
|
|
|
+ && y < hy) {
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Checks if a double click was made
|
|
|
*
|