fix for #24
This commit is contained in:
@@ -5,6 +5,6 @@ import java.util.Map;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
|
||||
public abstract class Cross extends Tile {
|
||||
public abstract class Cross extends TileWithShadow {
|
||||
public abstract Map<Connector,Turnout.State> offsetConnections(Direction from);
|
||||
}
|
||||
|
||||
@@ -33,16 +33,15 @@ public class CrossH extends Cross{
|
||||
public Map<Connector,State> offsetConnections(Direction from) {
|
||||
if (isNull(from)) return new HashMap<>();
|
||||
switch (from) {
|
||||
case NORTH:
|
||||
return Map.of(new Connector(x,y+1,Direction.NORTH),State.UNDEF);
|
||||
case SOUTH:
|
||||
return Map.of(new Connector(x,y-1,Direction.SOUTH),State.UNDEF);
|
||||
default:
|
||||
return new HashMap<>();
|
||||
case NORTH:
|
||||
return Map.of(new Connector(x,y+1,Direction.NORTH),State.UNDEF);
|
||||
case SOUTH:
|
||||
return Map.of(new Connector(x,y-1,Direction.SOUTH),State.UNDEF);
|
||||
default:
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag tag(Map<String,Object> replacements) throws IOException {
|
||||
return super.tag(replacements).size(200,100).attr("viewbox", "0 0 200 100");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.srsoftware.web4rail.tiles;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
@@ -10,14 +11,35 @@ import de.srsoftware.web4rail.tiles.Turnout.State;
|
||||
|
||||
public class CrossV extends Cross{
|
||||
|
||||
@Override
|
||||
public Map<Connector, State> connections(Direction from) {
|
||||
if (isNull(from)) return new HashMap<>();
|
||||
switch (from) {
|
||||
case WEST:
|
||||
return Map.of(new Connector(x+1,y+1,Direction.WEST),State.UNDEF);
|
||||
case EAST:
|
||||
return Map.of(new Connector(x-1,y+1,Direction.EAST),State.UNDEF);
|
||||
default:
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int height() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Connector, State> offsetConnections(Direction from) {
|
||||
return null;
|
||||
public Map<Connector,State> offsetConnections(Direction from) {
|
||||
if (isNull(from)) return new HashMap<>();
|
||||
switch (from) {
|
||||
case WEST:
|
||||
return Map.of(new Connector(x+1,y,Direction.WEST),State.UNDEF);
|
||||
case EAST:
|
||||
return Map.of(new Connector(x-1,y,Direction.EAST),State.UNDEF);
|
||||
default:
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -20,7 +20,7 @@ public class Shadow extends Tile{
|
||||
return super.connections(from);
|
||||
}
|
||||
|
||||
public Shadow(StretchableTile overlay, int x, int y) {
|
||||
public Shadow(TileWithShadow overlay, int x, int y) {
|
||||
this.overlay = overlay;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
||||
@@ -3,7 +3,6 @@ package de.srsoftware.web4rail.tiles;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -13,14 +12,9 @@ import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
|
||||
public abstract class StretchableTile extends Tile {
|
||||
public abstract class StretchableTile extends TileWithShadow {
|
||||
private static final String STRETCH_LENGTH = "stretch";
|
||||
private int stretch = 1;
|
||||
private Vector<Id> shadows = new Vector<Id>();
|
||||
|
||||
public void add(Shadow shadow) {
|
||||
shadows.add(shadow.id());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject config() {
|
||||
@@ -58,17 +52,9 @@ public abstract class StretchableTile extends Tile {
|
||||
if (!tileAtDest.move(dx, dy)) return false;
|
||||
}
|
||||
|
||||
boolean moved = super.move(dx, dy);
|
||||
if (moved) placeShadows();
|
||||
return moved;
|
||||
return super.move(dx, dy);
|
||||
}
|
||||
|
||||
public void placeShadows() {
|
||||
removeShadows();
|
||||
for (int dx=1; dx<width(); dx++) plan.place(new Shadow(this, x+dx, y));
|
||||
for (int dy=1; dy<height(); dy++) plan.place(new Shadow(this, x, y+dy));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(stretchType(),new Input(STRETCH_LENGTH, stretch).numeric().addTo(new Tag("span")).content(NBSP+t("Tile(s)")));
|
||||
@@ -81,13 +67,6 @@ public abstract class StretchableTile extends Tile {
|
||||
removeShadows();
|
||||
return super.remove();
|
||||
}
|
||||
|
||||
private void removeShadows() {
|
||||
while (!shadows.isEmpty()) {
|
||||
Tile tile = BaseClass.get(shadows.remove(0));
|
||||
if (tile instanceof Shadow) tile.remove();
|
||||
}
|
||||
}
|
||||
|
||||
public int stretch() {
|
||||
return stretch;
|
||||
|
||||
@@ -109,7 +109,7 @@ public abstract class Tile extends BaseClass implements Comparable<Tile>{
|
||||
clazz = Tile.class.getName().replace(".Tile", "."+clazz);
|
||||
Tile tile = (Tile) Tile.class.getClassLoader().loadClass(clazz).getDeclaredConstructor().newInstance();
|
||||
tile.load(json).register();
|
||||
if (tile instanceof StretchableTile) ((StretchableTile)tile).placeShadows();
|
||||
if (tile instanceof TileWithShadow) ((TileWithShadow)tile).placeShadows();
|
||||
plan.place(tile);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package de.srsoftware.web4rail.tiles;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
|
||||
public class TileWithShadow extends Tile {
|
||||
private Vector<Id> shadows = new Vector<Id>();
|
||||
|
||||
public void add(Shadow shadow) {
|
||||
shadows.add(shadow.id());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean move(int dx, int dy) {
|
||||
boolean moved = super.move(dx, dy);
|
||||
if (moved) placeShadows();
|
||||
return moved;
|
||||
}
|
||||
|
||||
public void placeShadows() {
|
||||
removeShadows();
|
||||
for (int dx=1; dx<width(); dx++) plan.place(new Shadow(this, x+dx, y));
|
||||
for (int dy=1; dy<height(); dy++) plan.place(new Shadow(this, x, y+dy));
|
||||
}
|
||||
|
||||
protected void removeShadows() {
|
||||
while (!shadows.isEmpty()) {
|
||||
Tile tile = BaseClass.get(shadows.remove(0));
|
||||
if (tile instanceof Shadow) tile.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user