Java 8 syntax overhaul for Map Editor

This commit is contained in:
Brendan Robert 2014-06-08 10:14:00 -05:00
parent 53ba7ac8d4
commit 3c4966bb77

View File

@ -1,19 +1,13 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.badvision.outlaweditor; package org.badvision.outlaweditor;
import java.util.HashMap; import java.util.HashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.scene.Group; import javafx.scene.Group;
import javafx.scene.ImageCursor; import javafx.scene.ImageCursor;
import javafx.scene.Node;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.canvas.Canvas; import javafx.scene.canvas.Canvas;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
@ -27,7 +21,6 @@ import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
import javafx.stage.Stage; import javafx.stage.Stage;
import static org.badvision.outlaweditor.Application.currentPlatform; import static org.badvision.outlaweditor.Application.currentPlatform;
import org.badvision.outlaweditor.TransferHelper.DropEventHandler;
import org.badvision.outlaweditor.data.TileMap; import org.badvision.outlaweditor.data.TileMap;
import org.badvision.outlaweditor.data.TileUtils; import org.badvision.outlaweditor.data.TileUtils;
import org.badvision.outlaweditor.data.xml.Map; import org.badvision.outlaweditor.data.xml.Map;
@ -62,16 +55,13 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
return currentMap; return currentMap;
} }
EventHandler<ScrollEvent> scrollHandler = new EventHandler<ScrollEvent>() { EventHandler<ScrollEvent> scrollHandler = (ScrollEvent t) -> {
@Override if (t.isShiftDown()) {
public void handle(ScrollEvent t) { t.consume();
if (t.isShiftDown()) { if (t.getDeltaY() > 0) {
t.consume(); zoomIn();
if (t.getDeltaY() > 0) { } else {
zoomIn(); zoomOut();
} else {
zoomOut();
}
} }
} }
}; };
@ -101,28 +91,19 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
drawCanvas.heightProperty().bind(Application.getPrimaryStage().heightProperty().subtract(120)); drawCanvas.heightProperty().bind(Application.getPrimaryStage().heightProperty().subtract(120));
drawCanvas.widthProperty().bind(Application.getPrimaryStage().widthProperty().subtract(200)); drawCanvas.widthProperty().bind(Application.getPrimaryStage().widthProperty().subtract(200));
// drawCanvas.widthProperty().bind(anchorPane.widthProperty()); // drawCanvas.widthProperty().bind(anchorPane.widthProperty());
drawCanvas.widthProperty().addListener(new ChangeListener<Number>() { drawCanvas.widthProperty().addListener((ObservableValue<? extends Number> ov, Number t, Number t1) -> {
@Override redraw();
public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
redraw();
}
}); });
drawCanvas.heightProperty().addListener(new ChangeListener<Number>() { drawCanvas.heightProperty().addListener((ObservableValue<? extends Number> ov, Number t, Number t1) -> {
@Override redraw();
public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
redraw();
}
}); });
drawCanvas.addEventFilter(ScrollEvent.ANY, scrollHandler); drawCanvas.addEventFilter(ScrollEvent.ANY, scrollHandler);
drawCanvas.setOnMousePressed(this); drawCanvas.setOnMousePressed(this);
drawCanvas.setOnMouseDragged(this); drawCanvas.setOnMouseDragged(this);
drawCanvas.setOnMouseDragReleased(this); drawCanvas.setOnMouseDragReleased(this);
drawCanvas.setOnMouseReleased(this); drawCanvas.setOnMouseReleased(this);
scriptDragDrop.registerDropSupport(drawCanvas, new DropEventHandler<Script>() { scriptDragDrop.registerDropSupport(drawCanvas, (Script script, double x, double y) -> {
@Override assignScript(script, x, y);
public void handle(Script script, double x, double y) {
assignScript(script, x, y);
}
}); });
anchorPane.getChildren().add(0, drawCanvas); anchorPane.getChildren().add(0, drawCanvas);
} }
@ -140,12 +121,9 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
} }
public void togglePanZoom() { public void togglePanZoom() {
for (Node n : anchorPane.getChildren()) { anchorPane.getChildren().stream().filter((n) -> !(n == drawCanvas)).forEach((n) -> {
if (n == drawCanvas) {
continue;
}
n.setVisible(!n.isVisible()); n.setVisible(!n.isVisible());
} });
} }
public void scrollBy(int deltaX, int deltaY) { public void scrollBy(int deltaX, int deltaY) {
@ -198,34 +176,29 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
private long redrawRequested; private long redrawRequested;
private Thread redrawThread; private Thread redrawThread;
@Override
public void redraw() { public void redraw() {
redrawRequested = System.nanoTime(); redrawRequested = System.nanoTime();
if (redrawThread == null || redrawThread.isAlive()) { if (redrawThread == null || redrawThread.isAlive()) {
redrawThread = new Thread(new Runnable() { redrawThread = new Thread(() -> {
@Override long test = redrawRequested;
public void run() { try {
long test = redrawRequested; Thread.sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(MapEditor.class.getName()).log(Level.SEVERE, null, ex);
}
while (test != redrawRequested) {
test = redrawRequested;
try { try {
Thread.sleep(10); Thread.sleep(10);
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
Logger.getLogger(MapEditor.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(MapEditor.class.getName()).log(Level.SEVERE, null, ex);
} }
while (test != redrawRequested) {
test = redrawRequested;
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(MapEditor.class.getName()).log(Level.SEVERE, null, ex);
}
}
Platform.runLater(new Runnable() {
@Override
public void run() {
doRedraw();
}
});
redrawThread = null;
} }
Platform.runLater(() -> {
doRedraw();
});
redrawThread = null;
}); });
redrawThread.start(); redrawThread.start();
} }