Added shift to map editor

This commit is contained in:
Brendan Robert 2018-01-24 01:19:45 -06:00
parent 9692f89145
commit 0ddc073a3d
5 changed files with 84 additions and 21 deletions

View File

@ -125,6 +125,16 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
public void moveMapByY(int amt) {
getCurrentMap().shift(0, amt);
redraw();
}
public void moveMapByX(int amt) {
getCurrentMap().shift(amt, 0);
redraw();
}
@Override @Override
public void buildEditorUI(Pane tileEditorAnchorPane) { public void buildEditorUI(Pane tileEditorAnchorPane) {
anchorPane = tileEditorAnchorPane; anchorPane = tileEditorAnchorPane;

View File

@ -38,30 +38,30 @@ import org.badvision.outlaweditor.ui.UIAction;
* @author brobert * @author brobert
*/ */
public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable { public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable {
public static final long serialVersionUID = 6486309334559843742L; public static final long serialVersionUID = 6486309334559843742L;
Map backingMap; Map backingMap;
boolean backingMapStale; boolean backingMapStale;
int width; int width;
int height; int height;
public TileMap(Map m) { public TileMap(Map m) {
backingMapStale = false; backingMapStale = false;
width = 0; width = 0;
height = 0; height = 0;
loadFromMap(m); loadFromMap(m);
} }
public static final double SATURATION = 0.70; public static final double SATURATION = 0.70;
public static final double VALUE = 1.0; public static final double VALUE = 1.0;
public static double HUE = 180; public static double HUE = 180;
private final java.util.Map<Integer, List<Script>> locationScripts = new HashMap<>(); private final java.util.Map<Integer, List<Script>> locationScripts = new HashMap<>();
private final java.util.Map<Script, Color> scriptColors = new HashMap<>(); private final java.util.Map<Script, Color> scriptColors = new HashMap<>();
public Optional<Color> getScriptColor(Script s) { public Optional<Color> getScriptColor(Script s) {
return Optional.ofNullable(scriptColors.get(s)); return Optional.ofNullable(scriptColors.get(s));
} }
public List<Script> getLocationScripts(int x, int y) { public List<Script> getLocationScripts(int x, int y) {
List<Script> list = locationScripts.get(getMortonNumber(x, y)); List<Script> list = locationScripts.get(getMortonNumber(x, y));
if (list != null) { if (list != null) {
@ -70,15 +70,18 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
} }
} }
public void putLocationScript(int x, int y, Script s) { public void putLocationScript(int x, int y, Script s) {
if (x < 0 || y < 0) {
return;
}
LocationTrigger trigger = new Script.LocationTrigger(); LocationTrigger trigger = new Script.LocationTrigger();
trigger.setX(x); trigger.setX(x);
trigger.setY(y); trigger.setY(y);
s.getLocationTrigger().add(trigger); s.getLocationTrigger().add(trigger);
registerLocationScript(x, y, s); registerLocationScript(x, y, s);
} }
public void removeLocationScripts(int x, int y) { public void removeLocationScripts(int x, int y) {
int loc = getMortonNumber(x, y); int loc = getMortonNumber(x, y);
List<Script> scripts = locationScripts.get(loc); List<Script> scripts = locationScripts.get(loc);
@ -92,7 +95,7 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
locationScripts.remove(loc); locationScripts.remove(loc);
ApplicationState.getInstance().getController().redrawScripts(); ApplicationState.getInstance().getController().redrawScripts();
} }
private void registerLocationScript(int x, int y, Script s) { private void registerLocationScript(int x, int y, Script s) {
if (!scriptColors.containsKey(s)) { if (!scriptColors.containsKey(s)) {
scriptColors.put(s, Color.hsb(HUE, SATURATION, 0.75 + Math.cos(HUE / Math.PI / 2.0) / 8.0)); scriptColors.put(s, Color.hsb(HUE, SATURATION, 0.75 + Math.cos(HUE / Math.PI / 2.0) / 8.0));
@ -107,7 +110,7 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
list.add(s); list.add(s);
ApplicationState.getInstance().getController().redrawScripts(); ApplicationState.getInstance().getController().redrawScripts();
} }
private int getMortonNumber(int x, int y) { private int getMortonNumber(int x, int y) {
int morton = 0; int morton = 0;
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
@ -117,7 +120,7 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
} }
return morton; return morton;
} }
public Tile get(int x, int y) { public Tile get(int x, int y) {
if (size() <= y || get(y) == null) { if (size() <= y || get(y) == null) {
return null; return null;
@ -127,8 +130,11 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
} }
return get(y).get(x); return get(y).get(x);
} }
public void put(int x, int y, Tile t) { public void put(int x, int y, Tile t) {
if (x < 0 || y < 0) {
return;
}
width = Math.max(x + 1, width); width = Math.max(x + 1, width);
height = Math.max(y + 1, height); height = Math.max(y + 1, height);
for (int i = size(); i <= y; i++) { for (int i = size(); i <= y; i++) {
@ -144,11 +150,11 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
row.set(x, t); row.set(x, t);
backingMapStale = true; backingMapStale = true;
} }
public Map getBackingMap() { public Map getBackingMap() {
return backingMap; return backingMap;
} }
public void updateBackingMap() { public void updateBackingMap() {
ObjectFactory f = new ObjectFactory(); ObjectFactory f = new ObjectFactory();
backingMap.getChunk().clear(); backingMap.getChunk().clear();
@ -170,9 +176,10 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
backingMap.getChunk().add(c); backingMap.getChunk().add(c);
backingMapStale = false; backingMapStale = false;
} }
private void loadFromMap(Map m) { private void loadFromMap(Map m) {
clear(); clear();
locationScripts.clear();
width = 0; width = 0;
height = 0; height = 0;
Set<Tile> unknownTiles = new HashSet<>(); Set<Tile> unknownTiles = new HashSet<>();
@ -220,20 +227,40 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
backingMapStale = false; backingMapStale = false;
} }
public static String NULL_TILE_ID = "_"; public static String NULL_TILE_ID = "_";
public static boolean isNullTile(String tileId) { public static boolean isNullTile(String tileId) {
return tileId.equalsIgnoreCase(NULL_TILE_ID); return tileId.equalsIgnoreCase(NULL_TILE_ID);
} }
public void clearScriptTriggersFromMap(Script script) { public void clearScriptTriggersFromMap(Script script) {
script.getLocationTrigger().clear(); script.getLocationTrigger().clear();
locationScripts.values().stream().filter((scripts) -> !(scripts == null)).forEach((scripts) -> { locationScripts.values().stream().filter((scripts) -> !(scripts == null)).forEach((scripts) -> {
scripts.remove(script); scripts.remove(script);
}); });
} }
public void removeScriptFromMap(Script script) { public void removeScriptFromMap(Script script) {
clearScriptTriggersFromMap(script); clearScriptTriggersFromMap(script);
backingMap.getScripts().getScript().remove(script); backingMap.getScripts().getScript().remove(script);
} }
public void shift(int xAmt, int yAmt) {
Scripts scripts = backingMap.getScripts();
if (scripts != null) {
List<Script> allScripts = new ArrayList<>(scripts.getScript());
allScripts.forEach(
s -> s.getLocationTrigger().forEach(
l -> {
l.setX(l.getX() + xAmt);
l.setY(l.getY() + yAmt);
}
)
);
}
backingMap.getChunk().forEach(c -> {
c.setX(c.getX() + xAmt);
c.setY(c.getY() + yAmt);
});
loadFromMap(backingMap);
}
} }

View File

@ -149,6 +149,9 @@ public abstract class MapEditorTabController {
// Handler for Button[Button[id=null, styleClass=button moveButton]] onAction // Handler for Button[Button[id=null, styleClass=button moveButton]] onAction
@FXML @FXML
abstract public void scrollMapUp(ActionEvent event); abstract public void scrollMapUp(ActionEvent event);
@FXML
abstract public void showShiftUI(ActionEvent event);
public void initalize() { public void initalize() {
assert mapEditorAnchorPane != null : "fx:id=\"mapEditorAnchorPane\" was not injected: check your FXML file 'mapEditorTab.fxml'."; assert mapEditorAnchorPane != null : "fx:id=\"mapEditorAnchorPane\" was not injected: check your FXML file 'mapEditorTab.fxml'.";

View File

@ -13,8 +13,6 @@ import java.util.HashMap;
import java.util.function.Consumer; import java.util.function.Consumer;
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.beans.binding.Bindings;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.control.cell.ComboBoxListCell; import javafx.scene.control.cell.ComboBoxListCell;
@ -22,6 +20,7 @@ import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage; import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseEvent; import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font; import javafx.scene.text.Font;
import javafx.scene.text.FontWeight; import javafx.scene.text.FontWeight;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
@ -330,7 +329,30 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
} }
redrawMapScripts(); redrawMapScripts();
} }
@Override
public void showShiftUI(ActionEvent evt) {
Dialog dialog = new Dialog();
dialog.setTitle("Shift map");
dialog.setOnCloseRequest(v->dialog.close());
dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
GridPane pane = new GridPane();
Button moveUp = new Button("");
moveUp.setOnMouseClicked(v->getCurrentEditor().moveMapByY(-1));
pane.add(moveUp, 1, 0);
Button moveDown = new Button("");
moveDown.setOnMouseClicked(v->getCurrentEditor().moveMapByY(1));
pane.add(moveDown, 1, 2);
Button moveLeft = new Button("");
moveLeft.setOnMouseClicked(v->getCurrentEditor().moveMapByX(-1));
pane.add(moveLeft, 0, 1);
Button moveRight = new Button("");
moveRight.setOnMouseClicked(v->getCurrentEditor().moveMapByX(1));
pane.add(moveRight, 2, 1);
dialog.getDialogPane().setContent(pane);
dialog.show();
}
@Override @Override
public void rebuildMapSelectors() { public void rebuildMapSelectors() {
Map m = mapSelect.getSelectionModel().getSelectedItem(); Map m = mapSelect.getSelectionModel().getSelectedItem();

View File

@ -16,7 +16,7 @@
<?import javafx.scene.layout.HBox?> <?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<AnchorPane id="mapsTab" minHeight="0.0" minWidth="0.0" prefHeight="480.0" prefWidth="677.0" stylesheets="@styles/applicationui.css" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.badvision.outlaweditor.ui.impl.MapEditorTabControllerImpl"> <AnchorPane id="mapsTab" minHeight="0.0" minWidth="0.0" prefHeight="480.0" prefWidth="677.0" stylesheets="@styles/applicationui.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.badvision.outlaweditor.ui.impl.MapEditorTabControllerImpl">
<children> <children>
<VBox prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <VBox prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children> <children>
@ -44,6 +44,7 @@
</items> </items>
</Menu> </Menu>
<MenuItem mnemonicParsing="false" onAction="#mapTogglePanZoom" text="Toggle pan/zoom controls" /> <MenuItem mnemonicParsing="false" onAction="#mapTogglePanZoom" text="Toggle pan/zoom controls" />
<MenuItem mnemonicParsing="false" onAction="#showShiftUI" text="Shft Map" />
</items> </items>
</MenuButton> </MenuButton>
<Label fx:id="cursorInfo" text="CursorInfo" /> <Label fx:id="cursorInfo" text="CursorInfo" />