added CoupleTrain action,
improved Destination functions, fixed bug in AddRemoveTag, improved CarOrientation functions
This commit is contained in:
@@ -47,6 +47,7 @@ public abstract class Action extends BaseClass {
|
||||
BrakeCancel.class,
|
||||
BrakeStart.class,
|
||||
ConditionalAction.class,
|
||||
CoupleTrain.class,
|
||||
DelayedAction.class,
|
||||
DetermineTrainInBlock.class,
|
||||
DisableEnableBlock.class,
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.json.JSONObject;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
@@ -20,8 +21,10 @@ import de.srsoftware.web4rail.tiles.Tile;
|
||||
public class AddDestination extends Action {
|
||||
|
||||
private static final String TURN = "turn";
|
||||
private static final String SHUNTING = "shunting";
|
||||
private Block destination;
|
||||
private boolean turnAtDestination;
|
||||
private boolean shunting;
|
||||
|
||||
public AddDestination(BaseClass parent) {
|
||||
super(parent);
|
||||
@@ -39,9 +42,12 @@ public class AddDestination extends Action {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
String dest = "@"+destination.id()+(turnAtDestination?"+turn":"");
|
||||
String flags = "+";
|
||||
if (turnAtDestination) flags += Route.TURN_FLAG;
|
||||
if (shunting) flags += Route.SHUNTING_FLAG;
|
||||
String dest = Route.DESTINATION_PREFIX+destination.id() + (flags.length()>1 ? flags : "");
|
||||
for (String tag: train.tags()) {
|
||||
if (tag.startsWith("@")) {
|
||||
if (tag.startsWith(Route.DESTINATION_PREFIX)) {
|
||||
train.removeTag(tag);
|
||||
dest = tag+dest;
|
||||
break;
|
||||
@@ -56,12 +62,14 @@ public class AddDestination extends Action {
|
||||
JSONObject json = super.json();
|
||||
if (isSet(destination)) json.put(Train.DESTINATION,destination.id().toString());
|
||||
if (turnAtDestination) json.put(TURN,true);
|
||||
if (shunting) json.put(SHUNTING, true);
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
if (json.has(TURN)) turnAtDestination = json.getBoolean(TURN);
|
||||
if (json.has(SHUNTING)) shunting = json.getBoolean(SHUNTING);
|
||||
if (json.has(Train.DESTINATION)) {
|
||||
Id blockId = new Id(json.getString(Train.DESTINATION));
|
||||
destination = BaseClass.get(blockId);
|
||||
@@ -89,6 +97,7 @@ public class AddDestination extends Action {
|
||||
button(t("Clear destinations"),Map.of(ACTION,ACTION_UPDATE,Train.DESTINATION,"0")).addTo(span);
|
||||
formInputs.add(t("Destination")+": "+(isNull(destination) ? t("Clear destinations") : destination),span);
|
||||
formInputs.add(t("Turn at destination"),new Checkbox(TURN, t("Turn"), turnAtDestination));
|
||||
formInputs.add(t("Shunting"),new Checkbox(SHUNTING, t("Shunting"), shunting));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@@ -113,6 +122,7 @@ public class AddDestination extends Action {
|
||||
}
|
||||
}
|
||||
turnAtDestination = "on".equals(params.get(TURN));
|
||||
shunting = "on".equals(params.get(SHUNTING));
|
||||
return context().properties();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,15 +21,15 @@ public class AddRemoveTag extends Action{
|
||||
}
|
||||
|
||||
private String tag = "test";
|
||||
private boolean add = true;
|
||||
private boolean remove = false;
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
if (isNull(context.train())) return false;
|
||||
if (add) {
|
||||
context.train().tags().add(tag);
|
||||
if (remove) {
|
||||
context.train().removeTag(tag);
|
||||
} else {
|
||||
context.train().tags().remove(tag);
|
||||
context.train().addTag(tag);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -38,13 +38,15 @@ public class AddRemoveTag extends Action{
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
json.put(TAG, tag);
|
||||
if (remove) json.put(ACTION_DROP, true);
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
super.load(json);
|
||||
tag = json.getString(TAG);
|
||||
if (json.has(TAG)) tag = json.getString(TAG);
|
||||
if (json.has(ACTION_DROP)) remove = json.getBoolean(ACTION_DROP);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -52,21 +54,21 @@ public class AddRemoveTag extends Action{
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(t("Tag"),new Input(TAG, tag));
|
||||
Tag div = new Tag("div");
|
||||
new Radio(TYPE, ACTION_ADD, t("add"), add).addTo(div);
|
||||
new Radio(TYPE, ACTION_DROP, t("delete"), !add).addTo(div);
|
||||
new Radio(TYPE, ACTION_ADD, t("add"), !remove).addTo(div);
|
||||
new Radio(TYPE, ACTION_DROP, t("delete"), remove).addTo(div);
|
||||
formInputs.add(t("Action"),div);
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return add ? t("Add tag \"{}\" to train",tag) : t("Remove tag \"{}\" from train",tag);
|
||||
return remove ? t("Remove tag \"{}\" from train",tag) : t("Add tag \"{}\" to train",tag);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
tag = params.get(TAG);
|
||||
add = ACTION_ADD.equals(params.get(TYPE));
|
||||
remove = ACTION_DROP.equals(params.get(TYPE));
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
|
||||
public class CoupleTrain extends Action {
|
||||
|
||||
private static final String LAST = "last";
|
||||
private static final String SWAP = "swap";
|
||||
private boolean last = false;
|
||||
private boolean swap = false;
|
||||
|
||||
public CoupleTrain(BaseClass parent) {
|
||||
super(parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
Train train = context.train();
|
||||
if (isNull(train)) return false;
|
||||
Block block = train.currentBlock();
|
||||
if (isNull(block)) return false;
|
||||
Train parkingTrain = block.parkedTrains(last);
|
||||
if (isNull(parkingTrain)) return false;
|
||||
train.coupleWith(parkingTrain,swap);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
if (last) json.put(LAST, last);
|
||||
if (swap) json.put(SWAP, swap);
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action load(JSONObject json) {
|
||||
if (json.has(LAST)) last = json.getBoolean(LAST);
|
||||
if (json.has(SWAP)) swap = json.getBoolean(SWAP);
|
||||
return super.load(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return last ? t("Couple last parked train") : t("Couple first parked train");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm) {
|
||||
formInputs.add(t("Couple"),new Checkbox(LAST, t("last parked train"), last));
|
||||
formInputs.add(t("Swap order"),new Checkbox(SWAP, t("Swap order of trains"), swap));
|
||||
return super.properties(preForm, formInputs, postForm);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
last = "on".equals(params.get(LAST));
|
||||
swap = "on".equals(params.get(SWAP));
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user