extendend destination directions to be usable with actions
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>web4rail</artifactId>
|
||||
<version>1.5.29</version>
|
||||
<version>1.5.30</version>
|
||||
<name>Web4Rail</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Java Model Railway Control</description>
|
||||
|
||||
@@ -80,7 +80,7 @@ function assign(context){
|
||||
pendingAssignment = context;
|
||||
closeWindows();
|
||||
$(PLAN).css('cursor','help');
|
||||
if (context.realm == 'train' && context.action == 'move')addBlockDirections();
|
||||
if (context.assign == 'destination') addBlockDirections();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -50,11 +50,14 @@ public class Destination {
|
||||
return Train.DESTINATION_PREFIX+parts[2];
|
||||
}
|
||||
|
||||
private Destination enterFrom(Direction enterFrom) {
|
||||
public Destination enterFrom(Direction enterFrom) {
|
||||
this.enterDirection = enterFrom;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Direction enterFrom() {
|
||||
return enterDirection;
|
||||
}
|
||||
|
||||
public static Destination from(String tag) {
|
||||
if (BaseClass.isNull(tag)) return null;
|
||||
@@ -91,7 +94,7 @@ public class Destination {
|
||||
}
|
||||
|
||||
|
||||
private Destination shunting(boolean enable) {
|
||||
public Destination shunting(boolean enable) {
|
||||
this.shunting = enable;
|
||||
return this;
|
||||
}
|
||||
@@ -100,31 +103,36 @@ public class Destination {
|
||||
return shunting;
|
||||
}
|
||||
|
||||
public String tag() {
|
||||
StringBuilder flags = new StringBuilder();
|
||||
if (turn) flags.append(Train.TURN_FLAG);
|
||||
if (shunting) flags.append(Train.SHUNTING_FLAG);
|
||||
if (enterDirection == Direction.EAST) flags.append('←');
|
||||
if (enterDirection == Direction.WEST) flags.append('→');
|
||||
if (enterDirection == Direction.NORTH)flags.append('↓');
|
||||
if (enterDirection == Direction.SOUTH) flags.append('↑');
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append('@').append(block());
|
||||
if (flags.length()>0) sb.append('+').append(flags);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (enterDirection == null) {
|
||||
sb.append(block);
|
||||
} else {
|
||||
sb.append(BaseClass.t("{} from {}",block,enterDirection.inverse()));
|
||||
}
|
||||
if (shunting || turn) {
|
||||
sb.append("(");
|
||||
if (shunting) sb.append("shunting ");
|
||||
if (turn) sb.append("turn ");
|
||||
sb.append(")");
|
||||
sb.append(BaseClass.t("{} from {}",block,enterDirection));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private void turn(boolean enable) {
|
||||
public void turn(boolean enable) {
|
||||
this.turn = enable;
|
||||
}
|
||||
|
||||
public boolean turn() {
|
||||
return turn;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -395,7 +395,7 @@ public class Route extends BaseClass {
|
||||
}
|
||||
|
||||
public boolean endsAt(Destination destination) {
|
||||
return isSet(destination) && endBlock == destination.block && destination.accepts(endDirection);
|
||||
return isSet(destination) && endBlock == destination.block && destination.accepts(endDirection.inverse());
|
||||
}
|
||||
|
||||
public void finish(Train train) {
|
||||
|
||||
@@ -8,8 +8,10 @@ import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.Destination;
|
||||
import de.srsoftware.web4rail.LoadCallback;
|
||||
import de.srsoftware.web4rail.Params;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Checkbox;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
@@ -21,12 +23,11 @@ import de.srsoftware.web4rail.tiles.Tile;
|
||||
|
||||
public class AddRemoveDestination extends Action {
|
||||
|
||||
private static final String TRIGGER = "destination_trigger";
|
||||
private static final String TURN = "turn";
|
||||
private static final String SHUNTING = "shunting";
|
||||
private static final String TRIGGER = "destination_trigger";
|
||||
private Block destination;
|
||||
private boolean turnAtDestination;
|
||||
private boolean shunting;
|
||||
private static final String FROM = "from";
|
||||
private Destination destination;
|
||||
private Tile destinationTrigger = null;
|
||||
|
||||
public AddRemoveDestination(BaseClass parent) {
|
||||
@@ -45,10 +46,7 @@ public class AddRemoveDestination extends Action {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
String flags = "+";
|
||||
if (turnAtDestination) flags += Train.TURN_FLAG;
|
||||
if (shunting) flags += Train.SHUNTING_FLAG;
|
||||
String dest = Train.DESTINATION_PREFIX+destination.id() + (flags.length()>1 ? flags : "");
|
||||
String dest = destination.tag();
|
||||
for (String tag: train.tags()) {
|
||||
if (tag.startsWith(Train.DESTINATION_PREFIX)) {
|
||||
train.removeTag(tag);
|
||||
@@ -63,28 +61,40 @@ public class AddRemoveDestination extends Action {
|
||||
|
||||
@Override
|
||||
protected String highlightId() {
|
||||
return isSet(destination) ? destination.id().toString() : null;
|
||||
return isSet(destination) ? destination.block() : null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
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);
|
||||
if (isSet(destination)) {
|
||||
json.put(Train.DESTINATION,destination.block());
|
||||
if (destination.turn()) json.put(TURN,true);
|
||||
if (destination.shunting()) json.put(SHUNTING, true);
|
||||
if (isSet(destination.enterFrom())) json.put(FROM, destination.enterFrom());
|
||||
}
|
||||
if (isSet(destinationTrigger)) json.put(TRIGGER, destinationTrigger.id());
|
||||
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)) new LoadCallback() {
|
||||
@Override
|
||||
public void afterLoad() {
|
||||
destination = BaseClass.get(Id.from(json, Train.DESTINATION));
|
||||
Id id = Id.from(json, Train.DESTINATION);
|
||||
Block block = Block.get(id);
|
||||
if (isNull(block)) {
|
||||
LOG.warn("Unknown block id \"{}\" encountered during AddRemoveDestination.load(json)",id);
|
||||
return;
|
||||
}
|
||||
destination = new Destination(block);
|
||||
|
||||
if (json.has(TURN)) destination.turn(json.getBoolean(TURN));
|
||||
if (json.has(SHUNTING)) destination.shunting(json.getBoolean(SHUNTING));
|
||||
if (json.has(FROM)) destination.enterFrom(Direction.valueOf(json.getString(FROM)));
|
||||
|
||||
}
|
||||
};
|
||||
if (json.has(TRIGGER)) new LoadCallback() {
|
||||
@@ -103,8 +113,10 @@ public class AddRemoveDestination extends Action {
|
||||
button(t("Select from plan"),Map.of(ACTION,ACTION_UPDATE,ASSIGN,Train.DESTINATION)).addTo(span);
|
||||
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));
|
||||
if (isSet(destination)) {
|
||||
formInputs.add(t("Turn at destination"),new Checkbox(TURN, t("Turn"), destination.turn()));
|
||||
formInputs.add(t("Shunting"),new Checkbox(SHUNTING, t("Shunting"), destination.shunting()));
|
||||
}
|
||||
formInputs.add(t("Trigger Contact/Switch at destination")+": "+(isNull(destinationTrigger) ? t("unset") : destinationTrigger),button(t("Select from plan"),Map.of(ACTION,ACTION_UPDATE,ASSIGN,CONTACT)));
|
||||
return super.properties(preForm, formInputs, postForm,errors);
|
||||
}
|
||||
@@ -112,8 +124,8 @@ public class AddRemoveDestination extends Action {
|
||||
@Override
|
||||
public String toString() {
|
||||
if (isNull(destination)) return t("Clear destinations of train");
|
||||
String suffix = turnAtDestination ? t("Turn") : null;
|
||||
if (shunting) suffix = (isSet(suffix) ? suffix+" + " : "")+t("Shunting");
|
||||
String suffix = destination.turn() ? t("Turn") : null;
|
||||
if (destination.shunting()) suffix = (isSet(suffix) ? suffix+" + " : "")+t("Shunting");
|
||||
return t("Add {} to destinations of train",destination)+(isSet(suffix) ? " ("+suffix+")" : "");
|
||||
}
|
||||
|
||||
@@ -126,7 +138,8 @@ public class AddRemoveDestination extends Action {
|
||||
} else {
|
||||
Tile tile = plan.get(new Id(destId), true);
|
||||
if (tile instanceof Block) {
|
||||
destination = (Block) tile;
|
||||
Block block = (Block) tile;
|
||||
destination = new Destination(block,block.enterDirection(destId));
|
||||
} else {
|
||||
return t("Clicked tile is not a {}!",t("block"));
|
||||
}
|
||||
@@ -136,8 +149,10 @@ public class AddRemoveDestination extends Action {
|
||||
Tile tile = Tile.get(Id.from(params,CONTACT));
|
||||
if (tile instanceof Contact || tile instanceof Switch) destinationTrigger = tile;
|
||||
}
|
||||
turnAtDestination = "on".equals(params.getString(TURN));
|
||||
shunting = "on".equals(params.getString(SHUNTING));
|
||||
if (isSet(destination)) {
|
||||
destination.turn("on".equals(params.getString(TURN)));
|
||||
destination.shunting("on".equals(params.getString(SHUNTING)));
|
||||
}
|
||||
return context().properties();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -921,7 +921,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
if (shunting) {
|
||||
boolean connection = currentBlock.routes().stream().anyMatch(route -> route.startBlock() == currentBlock && route.endBlock() == tile);
|
||||
if (!connection) return t("No direct route from {} to {}",currentBlock,tile);
|
||||
} else enterDirection = block.determineDirection(dest);
|
||||
} else enterDirection = block.enterDirection(dest);
|
||||
|
||||
destination = new Destination(block,enterDirection);
|
||||
|
||||
|
||||
@@ -248,7 +248,7 @@ public abstract class Block extends StretchableTile{
|
||||
return internalContacts;
|
||||
}
|
||||
|
||||
public abstract Direction determineDirection(String dest);
|
||||
public abstract Direction enterDirection(String dest);
|
||||
|
||||
public abstract Direction directionA();
|
||||
public abstract Direction directionB();
|
||||
|
||||
@@ -28,12 +28,12 @@ public class BlockH extends Block{
|
||||
}
|
||||
|
||||
@Override
|
||||
public Direction determineDirection(String id) {
|
||||
public Direction enterDirection(String id) {
|
||||
Set<Direction> endDirections = arrivingRoutes().stream().map(Route::endDirection).collect(Collectors.toSet());
|
||||
if (endDirections.size()<2) return endDirections.stream().findAny().get();
|
||||
if (endDirections.size()<2) return endDirections.stream().findAny().get().inverse();
|
||||
if (stretch()<2) return null;
|
||||
if (id().equals(id)) return directionB();
|
||||
if (((x+stretch()-1)+"-"+y).equals(id)) return directionA();
|
||||
if (id().equals(id)) return directionA();
|
||||
if (((x+stretch()-1)+"-"+y).equals(id)) return directionB();
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,12 +28,12 @@ public class BlockV extends Block{
|
||||
}
|
||||
|
||||
@Override
|
||||
public Direction determineDirection(String id) {
|
||||
public Direction enterDirection(String id) {
|
||||
Set<Direction> endDirections = arrivingRoutes().stream().map(Route::endDirection).collect(Collectors.toSet());
|
||||
if (endDirections.size()<2) return endDirections.stream().findAny().get();
|
||||
if (endDirections.size()<2) return endDirections.stream().findAny().get().inverse();
|
||||
if (stretch()<2) return null;
|
||||
if (id().equals(id)) return directionB();
|
||||
if ((x+"-"+(y+stretch()-1)).equals(id)) return directionA();
|
||||
if (id().equals(id)) return directionA();
|
||||
if ((x+"-"+(y+stretch()-1)).equals(id)) return directionB();
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user