Added script erasing support and fixed bizarre bug with map editor

This commit is contained in:
Brendan Robert
2014-07-06 00:52:58 -05:00
parent 486ba70778
commit bfe626eada
11 changed files with 127 additions and 56 deletions

View File

@@ -29,6 +29,7 @@ import org.badvision.outlaweditor.data.TileUtils;
import org.badvision.outlaweditor.data.xml.Map;
import org.badvision.outlaweditor.data.xml.Script;
import org.badvision.outlaweditor.data.xml.Tile;
import org.badvision.outlaweditor.ui.ToolType;
/**
*
@@ -118,11 +119,17 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
public void assignScript(Script script, double x, double y) {
int xx = (int) (x / tileWidth);
int yy = (int) (y / tileHeight);
System.out.println("Dropped " + script.getName() + " at " + xx + "," + yy);
getCurrentMap().putLocationScript(xx, yy, script);
redraw();
}
public void unassignScripts(double x, double y) {
int xx = (int) (x / tileWidth);
int yy = (int) (y / tileHeight);
getCurrentMap().removeLocationScripts(xx, yy);
redraw();
}
public void togglePanZoom() {
anchorPane.getChildren().stream().filter((n) -> !(n == drawCanvas)).forEach((n) -> {
n.setVisible(!n.isVisible());
@@ -238,8 +245,8 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
}
}
private static final int dashLength=3;
private static final int dashLength = 3;
private void highlightScripts(int x, int y, List<Script> scripts) {
if (scripts == null || scripts.isEmpty()) {
return;
@@ -249,40 +256,40 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
double xx = x * tileWidth;
double yy = y * tileHeight;
gc.setLineWidth(4);
for (int i = 0; i < tileWidth-2; i += dashLength) {
for (int i = 0; i < tileWidth - 2; i += dashLength) {
idx = (idx + 1) % scripts.size();
gc.beginPath();
gc.moveTo(xx,yy);
gc.moveTo(xx, yy);
currentMap.getScriptColor(scripts.get(idx)).ifPresent(gc::setStroke);
xx += dashLength;
gc.lineTo(xx, yy);
gc.setEffect(new DropShadow(2, Color.BLACK));
gc.stroke();
}
for (int i = 0; i < tileHeight-2; i += dashLength) {
for (int i = 0; i < tileHeight - 2; i += dashLength) {
idx = (idx + 1) % scripts.size();
gc.beginPath();
gc.moveTo(xx,yy);
gc.moveTo(xx, yy);
currentMap.getScriptColor(scripts.get(idx)).ifPresent(gc::setStroke);
yy += dashLength;
gc.lineTo(xx, yy);
gc.setEffect(new DropShadow(2, Color.BLACK));
gc.stroke();
}
for (int i = 0; i < tileWidth-2; i += dashLength) {
for (int i = 0; i < tileWidth - 2; i += dashLength) {
idx = (idx + 1) % scripts.size();
gc.beginPath();
gc.moveTo(xx,yy);
gc.moveTo(xx, yy);
currentMap.getScriptColor(scripts.get(idx)).ifPresent(gc::setStroke);
xx -= dashLength;
gc.lineTo(xx, yy);
gc.setEffect(new DropShadow(2, Color.BLACK));
gc.stroke();
}
for (int i = 0; i < tileHeight-2; i += dashLength) {
for (int i = 0; i < tileHeight - 2; i += dashLength) {
idx = (idx + 1) % scripts.size();
gc.beginPath();
gc.moveTo(xx,yy);
gc.moveTo(xx, yy);
currentMap.getScriptColor(scripts.get(idx)).ifPresent(gc::setStroke);
yy -= dashLength;
gc.lineTo(xx, yy);
@@ -291,10 +298,13 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
}
}
public void setupDragDrop(TransferHelper<Script> scriptHelper) {
public void setupDragDrop(TransferHelper<Script> scriptHelper, TransferHelper<ToolType> toolHelper) {
scriptHelper.registerDropSupport(drawCanvas, (Script script, double x, double y) -> {
assignScript(script, x, y);
});
toolHelper.registerDropSupport(drawCanvas, (ToolType tool, double x, double y) -> {
unassignScripts(x, y);
});
}

View File

@@ -3,7 +3,7 @@ package org.badvision.outlaweditor;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javafx.event.EventHandler;
import javafx.scene.ImageCursor;
import javafx.scene.Node;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
@@ -11,6 +11,7 @@ import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import org.badvision.outlaweditor.ui.ToolType;
/**
* Simplify management of drag/drop operations
@@ -27,6 +28,7 @@ public class TransferHelper<T> {
static Map<String, DataFormat> dataFormats = new HashMap<>();
public interface DropEventHandler<T> {
public void handle(T object, double x, double y);
}
@@ -50,35 +52,50 @@ public class TransferHelper<T> {
source.setOnDragDetected((MouseEvent event) -> {
registry.put(id, object);
Dragboard db = source.startDragAndDrop(TransferMode.LINK);
if (type.isAssignableFrom(ToolType.class)) {
ToolType tool = (ToolType) object;
tool.getIcon().ifPresent(db::setDragView);
}
ClipboardContent content = new ClipboardContent();
content.put(format, id);
db.setContent(content);
event.consume();
dropSupportRegisterHandler.run();
});
source.setOnDragDone((DragEvent event) -> {
registry.remove(id);
dropSupportRegisterHandler.run();
});
}
Runnable dropSupportRegisterHandler;
Runnable dropSupportUnregisterHandler;
public void registerDropSupport(final Node target, final DropEventHandler<T> handler) {
target.setOnDragOver((DragEvent event) -> {
Dragboard db = event.getDragboard();
if (db.getContentTypes().contains(format)) {
event.acceptTransferModes(TransferMode.LINK);
}
event.consume();
});
target.setOnDragDropped((DragEvent event) -> {
Dragboard db = event.getDragboard();
if (db.getContentTypes().contains(format)) {
event.setDropCompleted(true);
String id = (String) db.getContent(format);
T object = (T) registry.get(id);
handler.handle(object, event.getX(), event.getY());
} else {
event.setDropCompleted(false);
}
event.consume();
});
dropSupportUnregisterHandler = () -> {
target.setOnDragOver(null);
target.setOnDragDropped(null);
};
dropSupportRegisterHandler = () -> {
target.setOnDragOver((DragEvent event) -> {
Dragboard db = event.getDragboard();
if (db.getContentTypes().contains(format)) {
event.acceptTransferModes(TransferMode.LINK);
}
event.consume();
});
target.setOnDragDropped((DragEvent event) -> {
Dragboard db = event.getDragboard();
if (db.getContentTypes().contains(format)) {
event.setDropCompleted(true);
String id = (String) db.getContent(format);
T object = (T) registry.get(id);
handler.handle(object, event.getX(), event.getY());
} else {
event.setDropCompleted(false);
}
event.consume();
});
};
}
}

View File

@@ -46,11 +46,11 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
public static double HUE = 180;
private final java.util.Map<Integer, List<Script>> locationScripts = new HashMap<>();
private final java.util.Map<Script, Color> scriptColors = new HashMap<>();
public Optional<Color> getScriptColor(Script s) {
return Optional.ofNullable(scriptColors.get(s));
}
public List<Script> getLocationScripts(int x, int y) {
List<Script> list = locationScripts.get(getMortonNumber(x, y));
if (list != null) {
@@ -68,6 +68,20 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
registerLocationScript(x, y, s);
}
public void removeLocationScripts(int x, int y) {
int loc = getMortonNumber(x, y);
List<Script> scripts = locationScripts.get(loc);
if (scripts != null) {
scripts.forEach(s -> {
s.getLocationTrigger().removeIf(t -> {
return t.getX() == x && t.getY() == y;
});
});
}
locationScripts.remove(loc);
Application.getInstance().getController().redrawScripts();
}
private void registerLocationScript(int x, int y, Script s) {
if (!scriptColors.containsKey(s)) {
scriptColors.put(s, Color.hsb(HUE, SATURATION, VALUE));
@@ -159,7 +173,7 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
)
);
}
m.getChunk().forEach( c-> {
m.getChunk().forEach(c -> {
int y = c.getY();
for (JAXBElement<List<String>> row : c.getRow()) {
int x = c.getX();

View File

@@ -2,6 +2,7 @@ package org.badvision.outlaweditor.ui;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
@@ -52,7 +53,9 @@ public abstract class MapEditorTabController {
protected TextField mapWidthField; // Value injected by FXMLLoader
@FXML // fx:id="mapWrapAround"
protected CheckBox mapWrapAround; // Value injected by FXMLLoader
@FXML
protected Button scriptEraseTool;
@FXML
abstract public void mapDraw1(ActionEvent event);

View File

@@ -0,0 +1,20 @@
package org.badvision.outlaweditor.ui;
import java.util.Optional;
import javafx.scene.image.Image;
public enum ToolType {
ERASER("images/eraser.png"), FILL(null), SELECT(null), MOVE(null), DRAW(null);
ToolType(String iconPath) {
if (iconPath != null) {
icon = Optional.of(new Image(iconPath));
} else {
icon = Optional.empty();
}
}
Optional<Image> icon;
public Optional<Image> getIcon() {
return icon;
}
}

View File

@@ -1,6 +1,5 @@
package org.badvision.outlaweditor.ui.impl;
import org.badvision.outlaweditor.ui.EntitySelectorCell;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
@@ -12,19 +11,21 @@ import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.util.Callback;
import org.badvision.outlaweditor.Application;
import org.badvision.outlaweditor.MapEditor;
import org.badvision.outlaweditor.TransferHelper;
import static org.badvision.outlaweditor.Application.currentPlatform;
import static org.badvision.outlaweditor.Application.gameData;
import static org.badvision.outlaweditor.ui.UIAction.confirm;
import static org.badvision.outlaweditor.ui.UIAction.createAndEditScript;
import org.badvision.outlaweditor.MapEditor;
import org.badvision.outlaweditor.TransferHelper;
import static org.badvision.outlaweditor.data.PropertyHelper.bind;
import static org.badvision.outlaweditor.data.PropertyHelper.stringProp;
import org.badvision.outlaweditor.data.TileUtils;
import org.badvision.outlaweditor.data.xml.Script;
import org.badvision.outlaweditor.data.xml.Map;
import org.badvision.outlaweditor.data.xml.Script;
import org.badvision.outlaweditor.ui.EntitySelectorCell;
import org.badvision.outlaweditor.ui.MapEditorTabController;
import org.badvision.outlaweditor.ui.ToolType;
import org.badvision.outlaweditor.ui.UIAction;
import static org.badvision.outlaweditor.ui.UIAction.confirm;
import static org.badvision.outlaweditor.ui.UIAction.createAndEditScript;
/**
*
@@ -32,6 +33,7 @@ import org.badvision.outlaweditor.ui.UIAction;
*/
public class MapEditorTabControllerImpl extends MapEditorTabController {
final TransferHelper<Script> scriptDragDrop = new TransferHelper<>(Script.class);
final TransferHelper<ToolType> toolDragDrop = new TransferHelper<>(ToolType.class);
@Override
public void mapDraw1(ActionEvent event) {
@@ -234,7 +236,7 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
e.setEntity(m);
e.buildEditorUI(mapEditorAnchorPane);
setCurrentEditor(e);
e.setupDragDrop(scriptDragDrop);
e.setupDragDrop(scriptDragDrop, toolDragDrop);
}
redrawMapScripts();
}
@@ -270,6 +272,7 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
public void finishUpdate(Map item) {
}
});
toolDragDrop.registerDragSupport(scriptEraseTool, ToolType.ERASER);
}
@Override

View File

@@ -2,7 +2,6 @@
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.collections.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
@@ -10,29 +9,28 @@
<children>
<VBox prefHeight="500.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<fx:include fx:id="menu" source="Menubar.fxml"/>
<fx:include fx:id="menu" source="Menubar.fxml" />
<TabPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="455.0" prefWidth="676.9998779296875" tabClosingPolicy="UNAVAILABLE" VBox.vgrow="ALWAYS">
<tabs>
<Tab onSelectionChanged="#tileTabActivated" text="Tiles">
<content>
<fx:include fx:id="tile" source="tileEditorTab.fxml"/>
<fx:include fx:id="tile" source="tileEditorTab.fxml" />
</content>
</Tab>
<Tab onSelectionChanged="#mapTabActivated" text="Maps">
<content>
<fx:include fx:id="map" source="mapEditorTab.fxml"/>
<fx:include fx:id="map" source="mapEditorTab.fxml" />
</content>
</Tab>
<Tab onSelectionChanged="#imageTabActivated" text="Images">
<content>
<fx:include fx:id="image" source="imageEditorTab.fxml"/>
<fx:include fx:id="image" source="imageEditorTab.fxml" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</VBox>
<HBox layoutX="300.0" layoutY="200.0" prefHeight="100.0" prefWidth="200.0" />
</children>
<stylesheets>
<URL value="@styles/applicationui.css" />

Binary file not shown.

After

Width:  |  Height:  |  Size: 774 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@@ -59,7 +59,7 @@
<items>
<Button mnemonicParsing="false" onAction="#onMapScriptAddPressed" text="+" />
<Button mnemonicParsing="false" onAction="#onMapScriptDeletePressed" text="-" />
<Button mnemonicParsing="false" onAction="#onMapScriptClonePressed" text="Clone" />
<Button mnemonicParsing="false" onAction="#onMapScriptClonePressed" text="Clone" /><Button fx:id="scriptEraseTool" mnemonicParsing="false" prefHeight="34.0" prefWidth="29.0" styleClass="eraseButton" text="E" />
</items>
</ToolBar><TextArea fx:id="scriptInfo" editable="false" layoutY="340.0" prefHeight="100.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" />
</children>

View File

@@ -7,19 +7,25 @@
}
.moveButton {
-fx-graphic: url("../images/arrowUp.png");
-fx-graphic: url("../images/arrowUp.png");
}
.zoomOutButton {
-fx-graphic: url("../images/zoomOut.png");
-fx-graphic: url("../images/zoomOut.png");
}
.zoomInButton {
-fx-graphic: url("../images/zoomIn.png");
-fx-graphic: url("../images/zoomIn.png");
}
.moveButton, .zoomInButton, .zoomOutButton{
-fx-background-color: rgba(0,0,0,0);
-fx-content-display: graphic-only;
-fx-effect: dropshadow(gaussian, navy, 10, 0.5, 0, 0);
-fx-background-color: rgba(0,0,0,0);
-fx-content-display: graphic-only;
-fx-effect: dropshadow(gaussian, navy, 10, 0.5, 0, 0);
}
.eraseButton {
-fx-graphic: url("../images/eraser-icon.png");
-fx-content-display: graphic-only;
-fx-background-color: rgba(0,0,0,0);
}