Browse Source

made tiles configurable

lookup-tables
Stephan Richter 5 years ago
parent
commit
9ac2801522
  1. 7
      pom.xml
  2. 22
      src/main/java/de/srsoftware/web4rail/Plan.java
  3. 15
      src/main/java/de/srsoftware/web4rail/tiles/StretchableTile.java
  4. 7
      src/main/java/de/srsoftware/web4rail/tiles/Tile.java

7
pom.xml

@ -33,7 +33,7 @@
<dependency> <dependency>
<groupId>de.srsoftware</groupId> <groupId>de.srsoftware</groupId>
<artifactId>tools</artifactId> <artifactId>tools</artifactId>
<version>1.1.6</version> <version>1.1.7</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
@ -48,6 +48,11 @@
<version>1.1.2</version> <version>1.1.2</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20200518</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

22
src/main/java/de/srsoftware/web4rail/Plan.java

@ -11,6 +11,7 @@ import java.util.HashMap;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Vector; import java.util.Vector;
import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -67,13 +68,14 @@ public class Plan {
return new Tag("div").clazz("list").content(tiles.toString()).addTo(tileMenu); return new Tag("div").clazz("list").content(tiles.toString()).addTo(tileMenu);
} }
private Tile addTile(String clazz, String xs, String ys) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { private Tile addTile(String clazz, String xs, String ys, String configJson) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
int x = Integer.parseInt(xs); int x = Integer.parseInt(xs);
int y = Integer.parseInt(ys); int y = Integer.parseInt(ys);
if (clazz == null) throw new NullPointerException(TILE+" must not be null!"); if (clazz == null) throw new NullPointerException(TILE+" must not be null!");
Class<Tile> tc = Tile.class; Class<Tile> tc = Tile.class;
clazz = tc.getName().replace(".Tile", "."+clazz); clazz = tc.getName().replace(".Tile", "."+clazz);
Tile tile = (Tile) tc.getClassLoader().loadClass(clazz).getDeclaredConstructor().newInstance(); Tile tile = (Tile) tc.getClassLoader().loadClass(clazz).getDeclaredConstructor().newInstance();
if (configJson != null) tile.configure(new JSONObject(configJson));
set(x, y, tile); set(x, y, tile);
return tile; return tile;
@ -103,9 +105,12 @@ public class Plan {
BufferedReader br = new BufferedReader(new FileReader(file)); BufferedReader br = new BufferedReader(new FileReader(file));
while (br.ready()) { while (br.ready()) {
String line = br.readLine().trim(); String line = br.readLine().trim();
String[] parts = line.split(":"); String[] parts = line.split(":",4);
try { try {
result.addTile(parts[2].trim(), parts[0].trim(), parts[1].trim()); String x = parts[0];
String y = parts[1];
String clazz = parts[2];
result.addTile(clazz, x, y, parts.length>3 ? parts[3] : null);
} catch (Exception e) { } catch (Exception e) {
LOG.warn("Was not able to load \"{}\":",line,e); LOG.warn("Was not able to load \"{}\":",line,e);
} }
@ -141,7 +146,7 @@ public class Plan {
} }
private String moveTile(String direction, int x, int y) throws IOException { private String moveTile(String direction, int x, int y) throws IOException {
LOG.debug("moveTile({},{},{})",direction,x,y); //LOG.debug("moveTile({},{},{})",direction,x,y);
Vector<Tile> moved = null; Vector<Tile> moved = null;
switch (direction) { switch (direction) {
case EAST: case EAST:
@ -186,7 +191,7 @@ public class Plan {
if (action == null) throw new NullPointerException(ACTION+" should not be null!"); if (action == null) throw new NullPointerException(ACTION+" should not be null!");
switch (action) { switch (action) {
case ACTION_ADD: case ACTION_ADD:
Tile tile = addTile(params.get(TILE),params.get(X),params.get(Y)); Tile tile = addTile(params.get(TILE),params.get(X),params.get(Y),null);
return t("Added {}",tile.getClass().getSimpleName()); return t("Added {}",tile.getClass().getSimpleName());
case ACTION_MOVE: case ACTION_MOVE:
return moveTile(params.get(DIRECTION),params.get(X),params.get(Y)); return moveTile(params.get(DIRECTION),params.get(X),params.get(Y));
@ -235,7 +240,12 @@ public class Plan {
for (Entry<Integer, Tile> row : column.getValue().entrySet()) { for (Entry<Integer, Tile> row : column.getValue().entrySet()) {
int y = row.getKey(); int y = row.getKey();
Tile tile = row.getValue().position(x, y); Tile tile = row.getValue().position(x, y);
if (tile != null && !(tile instanceof Shadow)) br.append(x+":"+y+":"+tile.getClass().getSimpleName()+"\n"); if (tile != null && !(tile instanceof Shadow)) {
br.append(x+":"+y+":"+tile.getClass().getSimpleName());
JSONObject config = tile.config();
if (!config.isEmpty()) br.append(":"+config);
br.append("\n");
}
} }
} }
br.close(); br.close();

15
src/main/java/de/srsoftware/web4rail/tiles/StretchableTile.java

@ -3,6 +3,8 @@ package de.srsoftware.web4rail.tiles;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.json.JSONObject;
import de.srsoftware.tools.Tag; import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Window; import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.tags.Form; import de.srsoftware.web4rail.tags.Form;
@ -11,6 +13,19 @@ public abstract class StretchableTile extends Tile {
private static final String LENGTH = "length"; private static final String LENGTH = "length";
public int length = 1; public int length = 1;
@Override
public JSONObject config() {
JSONObject config = super.config();
if (length != 1) config.put(LENGTH, length);
return config;
}
@Override
public void configure(JSONObject config) {
super.configure(config);
if (config.has(LENGTH)) setLength(config.getInt(LENGTH));
}
public Tag propMenu() { public Tag propMenu() {
Window menu = new Window("tile-properties",t("Properties of {} @ ({},{})",getClass().getSimpleName(),x,y)); Window menu = new Window("tile-properties",t("Properties of {} @ ({},{})",getClass().getSimpleName(),x,y));
Form form = new Form(); Form form = new Form();

7
src/main/java/de/srsoftware/web4rail/tiles/Tile.java

@ -7,6 +7,7 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Scanner; import java.util.Scanner;
import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -26,6 +27,12 @@ public abstract class Tile {
classes.add(getClass().getSimpleName()); classes.add(getClass().getSimpleName());
} }
public JSONObject config() {
return new JSONObject();
}
public void configure(JSONObject config) {}
public int height() { public int height() {
return 1; return 1;
} }

Loading…
Cancel
Save