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.
}
public void moveMapByY(int amt) {
getCurrentMap().shift(0, amt);
redraw();
}
public void moveMapByX(int amt) {
getCurrentMap().shift(amt, 0);
redraw();
}
@Override
public void buildEditorUI(Pane tileEditorAnchorPane) {
anchorPane = tileEditorAnchorPane;

View File

@ -38,30 +38,30 @@ import org.badvision.outlaweditor.ui.UIAction;
* @author brobert
*/
public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable {
public static final long serialVersionUID = 6486309334559843742L;
Map backingMap;
boolean backingMapStale;
int width;
int height;
public TileMap(Map m) {
backingMapStale = false;
width = 0;
height = 0;
loadFromMap(m);
}
public static final double SATURATION = 0.70;
public static final double VALUE = 1.0;
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) {
@ -70,15 +70,18 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
return Collections.EMPTY_LIST;
}
}
public void putLocationScript(int x, int y, Script s) {
if (x < 0 || y < 0) {
return;
}
LocationTrigger trigger = new Script.LocationTrigger();
trigger.setX(x);
trigger.setY(y);
s.getLocationTrigger().add(trigger);
registerLocationScript(x, y, s);
}
public void removeLocationScripts(int x, int y) {
int loc = getMortonNumber(x, y);
List<Script> scripts = locationScripts.get(loc);
@ -92,7 +95,7 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
locationScripts.remove(loc);
ApplicationState.getInstance().getController().redrawScripts();
}
private void registerLocationScript(int x, int y, Script s) {
if (!scriptColors.containsKey(s)) {
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);
ApplicationState.getInstance().getController().redrawScripts();
}
private int getMortonNumber(int x, int y) {
int morton = 0;
for (int i = 0; i < 16; i++) {
@ -117,7 +120,7 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
}
return morton;
}
public Tile get(int x, int y) {
if (size() <= y || get(y) == null) {
return null;
@ -127,8 +130,11 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
}
return get(y).get(x);
}
public void put(int x, int y, Tile t) {
if (x < 0 || y < 0) {
return;
}
width = Math.max(x + 1, width);
height = Math.max(y + 1, height);
for (int i = size(); i <= y; i++) {
@ -144,11 +150,11 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
row.set(x, t);
backingMapStale = true;
}
public Map getBackingMap() {
return backingMap;
}
public void updateBackingMap() {
ObjectFactory f = new ObjectFactory();
backingMap.getChunk().clear();
@ -170,9 +176,10 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
backingMap.getChunk().add(c);
backingMapStale = false;
}
private void loadFromMap(Map m) {
clear();
locationScripts.clear();
width = 0;
height = 0;
Set<Tile> unknownTiles = new HashSet<>();
@ -220,20 +227,40 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
backingMapStale = false;
}
public static String NULL_TILE_ID = "_";
public static boolean isNullTile(String tileId) {
return tileId.equalsIgnoreCase(NULL_TILE_ID);
}
public void clearScriptTriggersFromMap(Script script) {
script.getLocationTrigger().clear();
locationScripts.values().stream().filter((scripts) -> !(scripts == null)).forEach((scripts) -> {
scripts.remove(script);
});
}
public void removeScriptFromMap(Script script) {
clearScriptTriggersFromMap(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
@FXML
abstract public void scrollMapUp(ActionEvent event);
@FXML
abstract public void showShiftUI(ActionEvent event);
public void initalize() {
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.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.event.ActionEvent;
import javafx.scene.control.*;
import javafx.scene.control.cell.ComboBoxListCell;
@ -22,6 +20,7 @@ import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javax.xml.bind.JAXBException;
@ -330,7 +329,30 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
}
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
public void rebuildMapSelectors() {
Map m = mapSelect.getSelectionModel().getSelectedItem();

View File

@ -16,7 +16,7 @@
<?import javafx.scene.layout.HBox?>
<?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>
<VBox prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
@ -44,6 +44,7 @@
</items>
</Menu>
<MenuItem mnemonicParsing="false" onAction="#mapTogglePanZoom" text="Toggle pan/zoom controls" />
<MenuItem mnemonicParsing="false" onAction="#showShiftUI" text="Shft Map" />
</items>
</MenuButton>
<Label fx:id="cursorInfo" text="CursorInfo" />