started to refactor train head and tail settings in tiles
This commit is contained in:
@@ -131,10 +131,10 @@ public class Route implements Constants{
|
|||||||
*/
|
*/
|
||||||
public void activate() throws IOException {
|
public void activate() throws IOException {
|
||||||
for (Tile tile : path) {
|
for (Tile tile : path) {
|
||||||
if (!(tile instanceof Block)) tile.train(train);
|
if (!(tile instanceof Block)) tile.trainHead(train);
|
||||||
}
|
}
|
||||||
train.heading(endDirection.inverse());
|
train.heading(endDirection.inverse());
|
||||||
endBlock.train(train);
|
endBlock.trainHead(train);
|
||||||
startBlock.trailingTrain(train);
|
startBlock.trailingTrain(train);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,7 +312,7 @@ public class Route implements Constants{
|
|||||||
/**
|
/**
|
||||||
* Kontakt der Route aktivieren
|
* Kontakt der Route aktivieren
|
||||||
* @param contact
|
* @param contact
|
||||||
* @param train
|
* @param trainHead
|
||||||
*/
|
*/
|
||||||
public void contact(Contact contact) {
|
public void contact(Contact contact) {
|
||||||
LOG.debug("{} on {} activated {}.",train,this,contact);
|
LOG.debug("{} on {} activated {}.",train,this,contact);
|
||||||
@@ -346,7 +346,7 @@ public class Route implements Constants{
|
|||||||
|
|
||||||
public boolean free() {
|
public boolean free() {
|
||||||
for (int i=1; i<path.size(); i++) {
|
for (int i=1; i<path.size(); i++) {
|
||||||
if (!path.get(i).free()) return false;
|
if (!path.get(i).isFree()) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -489,7 +489,7 @@ public class Route implements Constants{
|
|||||||
public boolean lock() {
|
public boolean lock() {
|
||||||
ArrayList<Tile> lockedTiles = new ArrayList<Tile>();
|
ArrayList<Tile> lockedTiles = new ArrayList<Tile>();
|
||||||
try {
|
try {
|
||||||
for (Tile tile : path) lockedTiles.add(tile.lock(this));
|
for (Tile tile : path) lockedTiles.add(tile.setRoute(this));
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
for (Tile tile: lockedTiles) tile.unlock();
|
for (Tile tile: lockedTiles) tile.unlock();
|
||||||
return false;
|
return false;
|
||||||
@@ -538,8 +538,8 @@ public class Route implements Constants{
|
|||||||
for (Tile tile : path) {
|
for (Tile tile : path) {
|
||||||
if (!(tile instanceof Block)) tile.unlock();
|
if (!(tile instanceof Block)) tile.unlock();
|
||||||
}
|
}
|
||||||
if (endBlock.route() == this) endBlock.lock(null);
|
if (endBlock.route() == this) endBlock.setRoute(null);
|
||||||
if (startBlock.route() == this) startBlock.lock(null);
|
if (startBlock.route() == this) startBlock.setRoute(null);
|
||||||
if (train != null) {
|
if (train != null) {
|
||||||
train.heading(startDirection);
|
train.heading(startDirection);
|
||||||
train.block(startBlock, false);
|
train.block(startBlock, false);
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ import de.srsoftware.web4rail.tiles.Block;
|
|||||||
public class Train implements Comparable<Train>,Constants {
|
public class Train implements Comparable<Train>,Constants {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(Train.class);
|
private static final Logger LOG = LoggerFactory.getLogger(Train.class);
|
||||||
|
|
||||||
|
public static final String HEAD = "train_head";
|
||||||
|
public static final String TAIL = "train_tile";
|
||||||
private static final HashMap<Integer, Train> trains = new HashMap<>();
|
private static final HashMap<Integer, Train> trains = new HashMap<>();
|
||||||
|
|
||||||
public static final String ID = "id";
|
public static final String ID = "id";
|
||||||
@@ -236,7 +238,7 @@ public class Train implements Comparable<Train>,Constants {
|
|||||||
previousBlocks.add(this.block);
|
previousBlocks.add(this.block);
|
||||||
}
|
}
|
||||||
this.block = block;
|
this.block = block;
|
||||||
block.train(this);
|
block.trainHead(this);
|
||||||
if (resetPreviousBlocks) resetPreviousBlocks();
|
if (resetPreviousBlocks) resetPreviousBlocks();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -480,14 +482,14 @@ public class Train implements Comparable<Train>,Constants {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void removeFromBlock(Block block) {
|
public void removeFromBlock(Block block) {
|
||||||
if (block.train() == this) block.train(null);
|
if (block.trainHead() == this) block.trainHead(null);
|
||||||
if (this.block == block) this.block = null;
|
if (this.block == block) this.block = null;
|
||||||
previousBlocks.remove(block);
|
previousBlocks.remove(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetPreviousBlocks() {
|
public void resetPreviousBlocks() {
|
||||||
for (Block block : previousBlocks) {
|
for (Block block : previousBlocks) {
|
||||||
if (block.train() == this || block.trailingTrain() == this) block.unlock();
|
if (block.trainHead() == this || block.trailingTrain() == this) block.unlock();
|
||||||
}
|
}
|
||||||
previousBlocks.clear();
|
previousBlocks.clear();
|
||||||
}
|
}
|
||||||
@@ -503,7 +505,7 @@ public class Train implements Comparable<Train>,Constants {
|
|||||||
|
|
||||||
public static Select selector(Train preselected,Collection<Train> exclude) {
|
public static Select selector(Train preselected,Collection<Train> exclude) {
|
||||||
if (exclude == null) exclude = new Vector<Train>();
|
if (exclude == null) exclude = new Vector<Train>();
|
||||||
Select select = new Select(Train.class.getSimpleName());
|
Select select = new Select(Train.HEAD);
|
||||||
new Tag("option").attr("value","0").content(t("unset")).addTo(select);
|
new Tag("option").attr("value","0").content(t("unset")).addTo(select);
|
||||||
for (Train train : Train.list()) {
|
for (Train train : Train.list()) {
|
||||||
if (exclude.contains(train)) continue;
|
if (exclude.contains(train)) continue;
|
||||||
@@ -547,8 +549,8 @@ public class Train implements Comparable<Train>,Constants {
|
|||||||
setSpeed(0);
|
setSpeed(0);
|
||||||
if (route != null) try {
|
if (route != null) try {
|
||||||
route.unlock();
|
route.unlock();
|
||||||
route.endBlock().train(null);
|
route.endBlock().trainHead(null);
|
||||||
route.startBlock().train(this);
|
route.startBlock().trainHead(this);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -571,7 +573,7 @@ public class Train implements Comparable<Train>,Constants {
|
|||||||
direction = direction.inverse();
|
direction = direction.inverse();
|
||||||
for (Locomotive loco : locos) loco.turn();
|
for (Locomotive loco : locos) loco.turn();
|
||||||
}
|
}
|
||||||
if (block != null) plan.place(block.train(this));
|
if (block != null) plan.place(block.trainHead(this));
|
||||||
return t("{} turned.",this);
|
return t("{} turned.",this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,8 +24,6 @@ public abstract class Block extends StretchableTile{
|
|||||||
public boolean turnAllowed = false;
|
public boolean turnAllowed = false;
|
||||||
private Train trailingTrain = null;
|
private Train trailingTrain = null;
|
||||||
|
|
||||||
private static final String TRAIN = Train.class.getSimpleName();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSONObject config() {
|
public JSONObject config() {
|
||||||
JSONObject config = super.config();
|
JSONObject config = super.config();
|
||||||
@@ -34,8 +32,8 @@ public abstract class Block extends StretchableTile{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean free() {
|
public boolean isFree() {
|
||||||
return super.free() && trailingTrain == null;
|
return super.isFree() && trailingTrain == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -43,7 +41,6 @@ public abstract class Block extends StretchableTile{
|
|||||||
JSONObject json = super.json();
|
JSONObject json = super.json();
|
||||||
json.put(NAME, name);
|
json.put(NAME, name);
|
||||||
json.put(ALLOW_TURN, turnAllowed);
|
json.put(ALLOW_TURN, turnAllowed);
|
||||||
if (train != null) json.put(TRAIN, train.id);
|
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,10 +49,6 @@ public abstract class Block extends StretchableTile{
|
|||||||
super.load(json);
|
super.load(json);
|
||||||
name = json.has(NAME) ? json.getString(NAME) : "Block";
|
name = json.has(NAME) ? json.getString(NAME) : "Block";
|
||||||
turnAllowed = json.has(ALLOW_TURN) && json.getBoolean(ALLOW_TURN);
|
turnAllowed = json.has(ALLOW_TURN) && json.getBoolean(ALLOW_TURN);
|
||||||
if (json.has(TRAIN)) {
|
|
||||||
Train tr = Train.get(json.getInt(TRAIN));
|
|
||||||
train(tr.block(this, false));
|
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +61,7 @@ public abstract class Block extends StretchableTile{
|
|||||||
|
|
||||||
new Checkbox(ALLOW_TURN,t("Turn allowed"),turnAllowed).addTo(new Tag("p")).addTo(form);
|
new Checkbox(ALLOW_TURN,t("Turn allowed"),turnAllowed).addTo(new Tag("p")).addTo(form);
|
||||||
|
|
||||||
Select select = Train.selector(train, null);
|
Select select = Train.selector(trainHead, null);
|
||||||
select.addTo(new Label(t("Train:")+NBSP)).addTo(new Tag("p")).addTo(form);
|
select.addTo(new Label(t("Train:")+NBSP)).addTo(new Tag("p")).addTo(form);
|
||||||
|
|
||||||
return form;
|
return form;
|
||||||
@@ -78,10 +71,10 @@ public abstract class Block extends StretchableTile{
|
|||||||
public Tag propMenu() {
|
public Tag propMenu() {
|
||||||
Tag window = super.propMenu();
|
Tag window = super.propMenu();
|
||||||
|
|
||||||
if (train != null) {
|
if (trainHead != null) {
|
||||||
window.children().insertElementAt(new Button(t("stop"),"train("+train.id+",'"+ACTION_STOP+"')"), 1);
|
window.children().insertElementAt(new Button(t("stop"),"train("+trainHead.id+",'"+ACTION_STOP+"')"), 1);
|
||||||
window.children().insertElementAt(new Button(t("start"),"train("+train.id+",'"+ACTION_START+"')"), 1);
|
window.children().insertElementAt(new Button(t("start"),"train("+trainHead.id+",'"+ACTION_START+"')"), 1);
|
||||||
window.children().insertElementAt(train.link("span"), 1);
|
window.children().insertElementAt(trainHead.link("span"), 1);
|
||||||
window.children().insertElementAt(new Tag("h4").content(t("Train:")), 1);
|
window.children().insertElementAt(new Tag("h4").content(t("Train:")), 1);
|
||||||
}
|
}
|
||||||
return window;
|
return window;
|
||||||
@@ -94,9 +87,9 @@ public abstract class Block extends StretchableTile{
|
|||||||
if (replacements == null) replacements = new HashMap<String, Object>();
|
if (replacements == null) replacements = new HashMap<String, Object>();
|
||||||
replacements.put("%text%",name);
|
replacements.put("%text%",name);
|
||||||
if (trailingTrain != null) replacements.put("%text%","("+trailingTrain.name()+")");
|
if (trailingTrain != null) replacements.put("%text%","("+trailingTrain.name()+")");
|
||||||
if (train != null) replacements.put("%text%",train.directedName());
|
if (trainHead != null) replacements.put("%text%",trainHead.directedName());
|
||||||
Tag tag = super.tag(replacements);
|
Tag tag = super.tag(replacements);
|
||||||
if (train != null || trailingTrain != null) tag.clazz(tag.get("class")+" occupied");
|
if (trainHead != null || trailingTrain != null) tag.clazz(tag.get("class")+" occupied");
|
||||||
return tag;
|
return tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,7 +105,7 @@ public abstract class Block extends StretchableTile{
|
|||||||
|
|
||||||
public void trailingTrain(Train train) {
|
public void trailingTrain(Train train) {
|
||||||
trailingTrain = train;
|
trailingTrain = train;
|
||||||
this.train = null;
|
this.trainHead = null;
|
||||||
plan.place(this);
|
plan.place(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,13 +122,13 @@ public abstract class Block extends StretchableTile{
|
|||||||
@Override
|
@Override
|
||||||
public Tile update(HashMap<String, String> params) throws IOException {
|
public Tile update(HashMap<String, String> params) throws IOException {
|
||||||
if (params.containsKey(NAME)) name=params.get(NAME);
|
if (params.containsKey(NAME)) name=params.get(NAME);
|
||||||
if (params.containsKey(TRAIN)) {
|
if (params.containsKey(Train.HEAD)) {
|
||||||
int trainId = Integer.parseInt(params.get(TRAIN));
|
int trainId = Integer.parseInt(params.get(Train.HEAD));
|
||||||
if (trainId == 0) {
|
if (trainId == 0) {
|
||||||
train(null);
|
trainHead(null);
|
||||||
} else {
|
} else {
|
||||||
Train t = Train.get(trainId);
|
Train t = Train.get(trainId);
|
||||||
if (t != null) train = t.block(this,true);
|
if (t != null) trainHead = t.block(this,true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
turnAllowed = params.containsKey(ALLOW_TURN) && params.get(ALLOW_TURN).equals("on");
|
turnAllowed = params.containsKey(ALLOW_TURN) && params.get(ALLOW_TURN).equals("on");
|
||||||
|
|||||||
@@ -37,38 +37,36 @@ import de.srsoftware.web4rail.tags.Input;
|
|||||||
import de.srsoftware.web4rail.tags.Radio;
|
import de.srsoftware.web4rail.tags.Radio;
|
||||||
|
|
||||||
public abstract class Tile implements Constants{
|
public abstract class Tile implements Constants{
|
||||||
|
protected static Logger LOG = LoggerFactory.getLogger(Tile.class);
|
||||||
|
|
||||||
public static final String ID = "id";
|
public static final String ID = "id";
|
||||||
private static final String TYPE = "type";
|
|
||||||
private static final String LOCKED = "locked";
|
private static final String LOCKED = "locked";
|
||||||
|
private static final String OCCUPIED = "occupied";
|
||||||
|
private static final String ONEW_WAY = "one_way";
|
||||||
private static final String POS = "pos";
|
private static final String POS = "pos";
|
||||||
|
private static final String ROUTE = "route";
|
||||||
|
private static final String TYPE = "type";
|
||||||
private static final String X = "x";
|
private static final String X = "x";
|
||||||
private static final String Y = "y";
|
private static final String Y = "y";
|
||||||
public int x = -1,y = -1;
|
|
||||||
|
|
||||||
private static final String ROUTE = "route";
|
|
||||||
protected Route route;
|
|
||||||
|
|
||||||
private static final String OCCUPIED = "occupied";
|
|
||||||
protected Train train;
|
|
||||||
|
|
||||||
private static final String ONEW_WAY = "one_way";
|
|
||||||
protected Direction oneWay = null;
|
|
||||||
|
|
||||||
protected HashSet<Shadow> shadows = new HashSet<>();
|
|
||||||
private HashSet<Route> routes = new HashSet<>();
|
|
||||||
protected Plan plan;
|
|
||||||
private boolean disabled = false;
|
private boolean disabled = false;
|
||||||
|
protected Direction oneWay = null;
|
||||||
protected static Logger LOG = LoggerFactory.getLogger(Tile.class);
|
protected Plan plan = null;;
|
||||||
|
protected Route route = null;
|
||||||
|
private HashSet<Route> routes = new HashSet<>();
|
||||||
|
protected HashSet<Shadow> shadows = new HashSet<>();
|
||||||
|
protected Train trainHead = null;
|
||||||
|
protected Train trainTail = null;
|
||||||
|
public Integer x = null;
|
||||||
|
public Integer y = null;
|
||||||
|
|
||||||
protected Vector<String> classes(){
|
protected Vector<String> classes(){
|
||||||
Vector<String> classes = new Vector<String>();
|
Vector<String> classes = new Vector<String>();
|
||||||
classes.add("tile");
|
classes.add("tile");
|
||||||
classes.add(getClass().getSimpleName());
|
classes.add(getClass().getSimpleName());
|
||||||
if (route != null) classes.add(LOCKED);
|
if (isSet(route)) classes.add(LOCKED);
|
||||||
if (train != null) classes.add(OCCUPIED);
|
if (isSet(trainHead) || isSet(trainTail)) classes.add(OCCUPIED);
|
||||||
if (disabled) classes.add(DISABLED);
|
if (disabled) classes.add(DISABLED);
|
||||||
return classes;
|
return classes;
|
||||||
}
|
}
|
||||||
@@ -94,11 +92,8 @@ public abstract class Tile implements Constants{
|
|||||||
return new HashMap<>();
|
return new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean free() {
|
public boolean isFree() {
|
||||||
if (disabled) return false;
|
return !(disabled || isSet(route) || isSet(trainHead) || isSet(trainTail));
|
||||||
if (route != null) return false;
|
|
||||||
if (train != null) return false;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int height() {
|
public int height() {
|
||||||
@@ -121,14 +116,24 @@ public abstract class Tile implements Constants{
|
|||||||
plan.set(tile.x, tile.y, tile);
|
plan.set(tile.x, tile.y, tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static boolean isNull(Object o) {
|
||||||
|
return o==null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static boolean isSet(Object o) {
|
||||||
|
return o != null;
|
||||||
|
}
|
||||||
|
|
||||||
public JSONObject json() {
|
public JSONObject json() {
|
||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
json.put(TYPE, getClass().getSimpleName());
|
json.put(TYPE, getClass().getSimpleName());
|
||||||
JSONObject pos = new JSONObject(Map.of(X,x,Y,y));
|
JSONObject pos = new JSONObject(Map.of(X,x,Y,y));
|
||||||
json.put(POS, pos);
|
json.put(POS, pos);
|
||||||
if (route != null) json.put(ROUTE, route.id());
|
if (isSet(route)) json.put(ROUTE, route.id());
|
||||||
if (oneWay != null) json.put(ONEW_WAY, oneWay);
|
if (isSet(oneWay)) json.put(ONEW_WAY, oneWay);
|
||||||
if (disabled) json.put(DISABLED, true);
|
if (disabled) json.put(DISABLED, true);
|
||||||
|
if (isSet(trainHead)) json.put(Train.HEAD, trainHead.id);
|
||||||
|
if (isSet(trainTail)) json.put(Train.TAIL, trainTail.id);
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,7 +144,7 @@ public abstract class Tile implements Constants{
|
|||||||
public static void loadAll(String filename, Plan plan) throws IOException {
|
public static void loadAll(String filename, Plan plan) throws IOException {
|
||||||
BufferedReader file = new BufferedReader(new FileReader(filename));
|
BufferedReader file = new BufferedReader(new FileReader(filename));
|
||||||
String line = file.readLine();
|
String line = file.readLine();
|
||||||
while (line != null) {
|
while (isSet(line)) {
|
||||||
JSONObject json = new JSONObject(line);
|
JSONObject json = new JSONObject(line);
|
||||||
String clazz = json.getString(TYPE);
|
String clazz = json.getString(TYPE);
|
||||||
|
|
||||||
@@ -160,16 +165,11 @@ public abstract class Tile implements Constants{
|
|||||||
y = pos.getInt(Y);
|
y = pos.getInt(Y);
|
||||||
if (json.has(ONEW_WAY)) oneWay = Direction.valueOf(json.getString(ONEW_WAY));
|
if (json.has(ONEW_WAY)) oneWay = Direction.valueOf(json.getString(ONEW_WAY));
|
||||||
if (json.has(DISABLED)) disabled = json.getBoolean(DISABLED);
|
if (json.has(DISABLED)) disabled = json.getBoolean(DISABLED);
|
||||||
|
if (json.has(Train.HEAD)) trainHead = Train.get(json.getInt(Train.HEAD));
|
||||||
|
if (json.has(Train.TAIL)) trainTail = Train.get(json.getInt(Train.TAIL));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tile lock(Route lockingRoute) {
|
|
||||||
if (route == lockingRoute) return this;
|
|
||||||
if (route != null && lockingRoute != null) throw new IllegalStateException(this.toString());
|
|
||||||
route = lockingRoute;
|
|
||||||
return plan.place(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Plan plan() {
|
public Plan plan() {
|
||||||
return plan;
|
return plan;
|
||||||
}
|
}
|
||||||
@@ -199,7 +199,7 @@ public abstract class Tile implements Constants{
|
|||||||
List<Direction> pd = possibleDirections();
|
List<Direction> pd = possibleDirections();
|
||||||
if (!pd.isEmpty()) {
|
if (!pd.isEmpty()) {
|
||||||
new Tag("h4").content(t("One way:")).addTo(form);
|
new Tag("h4").content(t("One way:")).addTo(form);
|
||||||
new Radio("oneway","none",t("No"),oneWay == null).addTo(form);
|
new Radio("oneway","none",t("No"),isNull(oneWay)).addTo(form);
|
||||||
for (Direction d:pd) {
|
for (Direction d:pd) {
|
||||||
new Radio("oneway",d.toString(),t(d.toString()),d == oneWay).addTo(form);
|
new Radio("oneway",d.toString(),t(d.toString()),d == oneWay).addTo(form);
|
||||||
}
|
}
|
||||||
@@ -216,7 +216,7 @@ public abstract class Tile implements Constants{
|
|||||||
new Button(t("Apply"),"submitForm('"+formId+"')").addTo(form);
|
new Button(t("Apply"),"submitForm('"+formId+"')").addTo(form);
|
||||||
form.addTo(window);
|
form.addTo(window);
|
||||||
|
|
||||||
if (route != null) {
|
if (isSet(route)) {
|
||||||
new Tag("p").content(t("Locked by {}",route)).addTo(window);
|
new Tag("p").content(t("Locked by {}",route)).addTo(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -249,9 +249,7 @@ public abstract class Tile implements Constants{
|
|||||||
int end2 = line.indexOf("<",start);
|
int end2 = line.indexOf("<",start);
|
||||||
if (end2>0 && (end<0 || end2<end)) end=end2;
|
if (end2>0 && (end<0 || end2<end)) end=end2;
|
||||||
String tag = line.substring(start, end);
|
String tag = line.substring(start, end);
|
||||||
if (tag.length()>len) {
|
if (tag.length()>len) val = Integer.parseInt(tag.substring(len)) + (int) val;
|
||||||
val = Integer.parseInt(tag.substring(len)) + (int) val;
|
|
||||||
}
|
|
||||||
line = line.replace(tag, ""+val);
|
line = line.replace(tag, ""+val);
|
||||||
start = line.indexOf(key);
|
start = line.indexOf(key);
|
||||||
}
|
}
|
||||||
@@ -269,12 +267,19 @@ public abstract class Tile implements Constants{
|
|||||||
public static void saveAll(HashMap<String, Tile> tiles ,String filename) throws IOException {
|
public static void saveAll(HashMap<String, Tile> tiles ,String filename) throws IOException {
|
||||||
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
||||||
for (Tile tile : tiles.values()) {
|
for (Tile tile : tiles.values()) {
|
||||||
if (tile == null || tile instanceof Shadow) continue;
|
if (isNull(tile) || tile instanceof Shadow) continue;
|
||||||
file.append(tile.json()+"\n");
|
file.append(tile.json()+"\n");
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Tile setRoute(Route lockingRoute) {
|
||||||
|
if (route == lockingRoute) return this; // nothing changed
|
||||||
|
if (isSet(route) && isSet(lockingRoute)) throw new IllegalStateException(this.toString()); // tile already locked by other route
|
||||||
|
route = lockingRoute;
|
||||||
|
return plan.place(this);
|
||||||
|
}
|
||||||
|
|
||||||
protected static String t(String txt, Object...fills) {
|
protected static String t(String txt, Object...fills) {
|
||||||
return Translation.get(Application.class, txt, fills);
|
return Translation.get(Application.class, txt, fills);
|
||||||
}
|
}
|
||||||
@@ -282,17 +287,17 @@ public abstract class Tile implements Constants{
|
|||||||
public Tag tag(Map<String,Object> replacements) throws IOException {
|
public Tag tag(Map<String,Object> replacements) throws IOException {
|
||||||
int width = 100*len();
|
int width = 100*len();
|
||||||
int height = 100*height();
|
int height = 100*height();
|
||||||
if (replacements == null) replacements = new HashMap<String, Object>();
|
if (isNull(replacements)) replacements = new HashMap<String, Object>();
|
||||||
replacements.put("%width%",width);
|
replacements.put("%width%",width);
|
||||||
replacements.put("%height%",height);
|
replacements.put("%height%",height);
|
||||||
String style = "";
|
String style = "";
|
||||||
Tag svg = new Tag("svg")
|
Tag svg = new Tag("svg")
|
||||||
.id((x!=-1 && y!=-1)?(id()):(getClass().getSimpleName()))
|
.id(isSet(x) && isSet(y) ? id() : getClass().getSimpleName())
|
||||||
.clazz(classes())
|
.clazz(classes())
|
||||||
.size(100,100)
|
.size(100,100)
|
||||||
.attr("name", getClass().getSimpleName())
|
.attr("name", getClass().getSimpleName())
|
||||||
.attr("viewbox", "0 0 "+width+" "+height);
|
.attr("viewbox", "0 0 "+width+" "+height);
|
||||||
if (x>-1) style="left: "+(30*x)+"px; top: "+(30*y)+"px;";
|
if (isSet(x)) style="left: "+(30*x)+"px; top: "+(30*y)+"px;";
|
||||||
if (len()>1) style+=" width: "+(30*len())+"px;";
|
if (len()>1) style+=" width: "+(30*len())+"px;";
|
||||||
if (height()>1) style+=" height: "+(30*height())+"px;";
|
if (height()>1) style+=" height: "+(30*height())+"px;";
|
||||||
|
|
||||||
@@ -305,17 +310,14 @@ public abstract class Tile implements Constants{
|
|||||||
while (scanner.hasNextLine()) {
|
while (scanner.hasNextLine()) {
|
||||||
String line = scanner.nextLine();
|
String line = scanner.nextLine();
|
||||||
if (line.startsWith("<svg") || line.endsWith("svg>")) continue;
|
if (line.startsWith("<svg") || line.endsWith("svg>")) continue;
|
||||||
for (Entry<String, Object> replacement : replacements.entrySet()) {
|
for (Entry<String, Object> replacement : replacements.entrySet()) line = replace(line,replacement);
|
||||||
line = replace(line,replacement);
|
|
||||||
}
|
|
||||||
sb.append(line+"\n");
|
sb.append(line+"\n");
|
||||||
}
|
}
|
||||||
scanner.close();
|
scanner.close();
|
||||||
svg.content(sb.toString());
|
svg.content(sb.toString());
|
||||||
|
|
||||||
if (oneWay != null) {
|
if (isSet(oneWay)) {
|
||||||
switch (oneWay) {
|
switch (oneWay) {
|
||||||
|
|
||||||
case EAST:
|
case EAST:
|
||||||
new Tag("polygon").clazz("oneway").attr("points", "100,50 75,35 75,65").addTo(svg);
|
new Tag("polygon").clazz("oneway").attr("points", "100,50 75,35 75,65").addTo(svg);
|
||||||
break;
|
break;
|
||||||
@@ -339,7 +341,7 @@ public abstract class Tile implements Constants{
|
|||||||
.addTo(svg);
|
.addTo(svg);
|
||||||
}
|
}
|
||||||
String title = title();
|
String title = title();
|
||||||
if (title!=null) new Tag("title").content(title()).addTo(svg);
|
if (isSet(title)) new Tag("title").content(title()).addTo(svg);
|
||||||
|
|
||||||
return svg;
|
return svg;
|
||||||
}
|
}
|
||||||
@@ -353,26 +355,27 @@ public abstract class Tile implements Constants{
|
|||||||
return t("{}({},{})",getClass().getSimpleName(),x,y) ;
|
return t("{}({},{})",getClass().getSimpleName(),x,y) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Train train() {
|
public Train trainHead() {
|
||||||
return train;
|
return trainHead;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tile train(Train train) {
|
public Tile trainHead(Train train) {
|
||||||
if (this.train == train) return this; // nothing to update
|
if (this.trainHead == train) return this; // nothing to update
|
||||||
this.train = train;
|
this.trainHead = train;
|
||||||
return plan.place(this);
|
return plan.place(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unlock() {
|
public void unlock() {
|
||||||
route = null;
|
route = null;
|
||||||
train = null;
|
trainHead = null;
|
||||||
|
trainTail = null;
|
||||||
plan.place(this);
|
plan.place(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tile update(HashMap<String, String> params) throws IOException {
|
public Tile update(HashMap<String, String> params) throws IOException {
|
||||||
LOG.debug("{}.update({})",getClass().getSimpleName(),params);
|
LOG.debug("{}.update({})",getClass().getSimpleName(),params);
|
||||||
String oneWayDir = params.get("oneway");
|
String oneWayDir = params.get("oneway");
|
||||||
if (oneWayDir != null) {
|
if (isSet(oneWayDir)) {
|
||||||
try {
|
try {
|
||||||
oneWay = Direction.valueOf(oneWayDir);
|
oneWay = Direction.valueOf(oneWayDir);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user