Преглед изворни кода

Merge remote-tracking branch 'origin/Jan'

Jan Enders пре 8 година
родитељ
комит
8c95b52478

+ 12 - 2
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/main/GraphManager.java

@@ -10,6 +10,7 @@ import org.graphstream.graph.Graph;
 import org.graphstream.graph.Node;
 import org.graphstream.ui.geom.Point3;
 import org.graphstream.ui.swingViewer.ViewPanel;
+import org.graphstream.ui.swingViewer.util.DefaultCamera;
 import org.graphstream.ui.view.Viewer;
 import org.graphstream.ui.view.ViewerPipe;
 
@@ -284,14 +285,23 @@ public class GraphManager {
 	 * Zooms in the view of the graph by 5 percent.
 	 */
 	public void zoomIn() {
-		view.getCamera().setViewPercent(view.getCamera().getViewPercent() * 0.95);
+		zoom(-0.05);
 	}
 
 	/**
 	 * Zooms out the view of the graph by 5 percent.
 	 */
 	public void zoomOut() {
-		view.getCamera().setViewPercent(view.getCamera().getViewPercent() * 1.05);
+		zoom(0.05);
+	}
+	
+	/**
+	 * Zooms the view by the given Amount, positive values zoom out, negative values zoom in.
+	 * 
+	 * @param amount the amount of zoom, should usually be between -0.2 and 0.2 for reasonable zoom.
+	 */
+	public void zoom(double amount) {
+		view.getCamera().setViewPercent( view.getCamera().getViewPercent() * (1 + amount));
 	}
 
 	public ViewerPipe getFromViewer() {

+ 4 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/GUIController.java

@@ -243,7 +243,11 @@ public class GUIController implements Initializable {
 	private void initializeDisplayPane() {
 		pane.heightProperty().addListener(new ResizeListener(swingNode, pane));
 		pane.widthProperty().addListener(new ResizeListener(swingNode, pane));
+		pane.setOnScroll(GraphDisplayManager.scrollHandler);
 		swingNode.setContent((JPanel) Main.getInstance().getGraphManager().getView());
+		swingNode.setOnMousePressed(GraphDisplayManager.rememberLastClickedPosHandler);
+		swingNode.setOnMouseDragged(GraphDisplayManager.mouseDraggedHandler);
+		
 		pane.setMinSize(200, 200);
 	}
 

+ 46 - 12
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/GraphDisplayManager.java

@@ -7,13 +7,19 @@ import java.util.ArrayList;
 import org.apache.commons.math3.exception.NullArgumentException;
 import org.graphstream.graph.Graph;
 import org.graphstream.graph.implementations.SingleGraph;
+import org.graphstream.ui.geom.Point3;
+import org.graphstream.ui.view.Camera;
 
+import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
 import de.tu_darmstadt.informatik.tk.scopviz.io.GraphMLImporter;
 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.MyGraph;
+import javafx.event.EventHandler;
+import javafx.scene.input.MouseEvent;
+import javafx.scene.input.ScrollEvent;
 import javafx.scene.layout.Pane;
 import javafx.stage.Stage;
 
@@ -27,7 +33,10 @@ import javafx.stage.Stage;
  */
 public class GraphDisplayManager {
 	private static final String GRAPH_STRING_ID_PREFIX = "graph";
-
+	
+	private static Point3 oldMousePos;
+	private static Point3 oldViewCenter;
+	
 	private static ArrayList<GraphManager> vList = new ArrayList<GraphManager>();
 	private static int count = 0;
 	private static GUIController guiController;
@@ -154,17 +163,6 @@ public class GraphDisplayManager {
 		}
 	}
 
-	/**
-	 * Returns the Visualizer for the given graph id
-	 * 
-	 * @param id
-	 *            of the graph
-	 * @return visualizer for the graph
-	 */
-	/*
-	 * public static Visualizer getVisualizer(int id) { return vList.get(id); }
-	 */
-
 	private static String getGraphStringID(int id) {
 		return GRAPH_STRING_ID_PREFIX + id;
 	}
@@ -212,4 +210,40 @@ public class GraphDisplayManager {
 	public static void setCurrentLayer(Layer currentLayer) {
 		GraphDisplayManager.currentLayer = currentLayer;
 	}
+
+	public static final EventHandler<ScrollEvent> scrollHandler  = new EventHandler<ScrollEvent>() {
+
+		@Override
+		public void handle(ScrollEvent event) {
+			double deltaY = event.getDeltaY();
+			getCurrentGraphManager().zoom(deltaY/-100);
+		}
+		
+	};
+	
+	public static final EventHandler<MouseEvent> rememberLastClickedPosHandler = new EventHandler<MouseEvent>(){
+		
+		@Override
+		public void handle(MouseEvent event){
+			Camera cam = getCurrentGraphManager().getView().getCamera();
+			oldMousePos  = cam.transformPxToGu(event.getSceneX(), event.getSceneY());
+			oldViewCenter = getCurrentGraphManager().getView().getCamera().getViewCenter();
+			Debug.out("Last mouse click position remembered: "+ oldMousePos.x +"/"+oldMousePos.y);
+		}
+	};
+	
+	public static final EventHandler<MouseEvent> mouseDraggedHandler =  new EventHandler<MouseEvent>(){
+		
+		@Override
+		public void handle(MouseEvent event){
+			Camera cam = getCurrentGraphManager().getView().getCamera();
+			Point3 newMousePos = cam.transformPxToGu(event.getSceneX(), event.getSceneY());
+			double offsetX = oldMousePos.x - newMousePos.x;
+			double offsetY = oldMousePos.y - newMousePos.y;
+			double newX = oldViewCenter.x + offsetX;
+			double newY = oldViewCenter.y + offsetY;
+			Debug.out("Pan by "+offsetX+"/"+offsetY+": Center moved from "+oldViewCenter.x+"/"+oldViewCenter.y+" to "+newX+"/"+newY);
+			cam.setViewCenter(newX, newY, oldViewCenter.z);
+		}
+	};
 }