浏览代码

edge selecting not working yet

MW 8 年之前
父节点
当前提交
fdddad7870

+ 20 - 3
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/debug/Debug.java

@@ -1,5 +1,7 @@
 package de.tu_darmstadt.informatik.tk.scopviz.debug;
 
+import org.graphstream.graph.Edge;
+
 /**
  * Debug class to allow easy, static access to console output.
  * 
@@ -22,10 +24,25 @@ public class Debug {
 	/**
 	 * Short form for System.out.println().
 	 * 
-	 * @param s
+	 * @param i
 	 *            Integer to be printed on the console
 	 */
-	public static void out(int s) {
-		System.out.println(s);
+	public static void out(int i) {
+		System.out.println(i);
+	}
+
+	/**
+	 * Short form for System.out.println().
+	 * 
+	 * @param e
+	 *            Edge to be printed on the console
+	 */
+	public static void out(Edge e) {
+		if (e != null) {
+			System.out.println("Edge: " + e.toString());
+		} else {
+
+			System.out.println("Edge: null");
+		}
 	}
 }

+ 35 - 1
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/ButtonManager.java

@@ -1,5 +1,8 @@
 package de.tu_darmstadt.informatik.tk.scopviz.ui;
 
+import java.util.Iterator;
+
+import org.graphstream.graph.Edge;
 import org.graphstream.graph.Graph;
 import org.graphstream.graph.Node;
 import org.graphstream.ui.geom.Point3;
@@ -9,6 +12,7 @@ import de.tu_darmstadt.informatik.tk.scopviz.main.CreationMode;
 import de.tu_darmstadt.informatik.tk.scopviz.main.GraphManager;
 import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
 import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
+import de.tu_darmstadt.informatik.tk.scopviz.main.SelectionMode;
 import javafx.event.ActionEvent;
 import javafx.event.EventHandler;
 import javafx.scene.input.MouseEvent;
@@ -62,7 +66,10 @@ public class ButtonManager {
 			GraphManager graphManager = Main.getInstance().getGraphManager();
 			Graph graph = graphManager.getGraph();
 			Point3 cursorPos = graphManager.getView().getCamera().transformPxToGu(event.getX(), event.getY());
-
+			if (Main.getInstance().getCreationMode() == CreationMode.CREATE_NONE)
+			// Debug.out("(" + cursorPos.x + ", " + cursorPos.y + ")");
+			// Debug.out(getClosestEdge(cursorPos));
+			getClosestEdge(cursorPos);
 			Node n;
 
 			// Create node based on creation Mode
@@ -111,6 +118,33 @@ public class ButtonManager {
 		}
 	};
 
+	public static Edge getClosestEdge(Point3 pos) {
+		return getClosestEdge(pos, Double.MAX_VALUE);
+	}
+
+	public static Edge getClosestEdge(Point3 pos, double maxDistance) {
+		double x0 = pos.x;
+		double y0 = pos.y;
+		GraphManager gm = Main.getInstance().getGraphManager();
+		double dist = maxDistance;
+		Edge result = null;
+		for (Iterator<Edge> iterator = gm.getGraph().getEdgeIterator(); iterator.hasNext();) {
+			Edge edge = (Edge) iterator.next();
+			Debug.out(""+edge.getNode1().getAttribute("x"));
+			double x1 = edge.getNode0().getAttribute("x");
+			double y1 = edge.getNode0().getAttribute("y");
+			double x2 = edge.getNode1().getAttribute("x");
+			double y2 = edge.getNode1().getAttribute("y");
+			double cdist = Math.abs((y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1)
+					/ Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));
+			if (cdist < dist) {
+				dist = cdist;
+				result = edge;
+			}
+		}
+		return result;
+	}
+
 	public static final EventHandler<ActionEvent> underlayHandler = new EventHandler<ActionEvent>() {
 
 		@Override