diff --git a/pom.xml b/pom.xml index 9ad7c57..ef59396 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 de.srsoftware web4rail - 1.2.35 + 1.2.36 Web4Rail jar Java Model Railway Control diff --git a/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java b/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java index bb199dc..c21b79b 100644 --- a/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java +++ b/src/main/java/de/srsoftware/web4rail/moving/Locomotive.java @@ -142,8 +142,8 @@ public class Locomotive extends Car implements Constants,Device{ } direction.addTo(fieldset); - Tag functions = new Tag("p"); - Map.of("F1",ACTION_TOGGLE_F1,"F2",ACTION_TOGGLE_F2,"F3",ACTION_TOGGLE_F3,"F4",ACTION_TOGGLE_F4).entrySet().forEach(e -> { + Tag functions = new Tag("p"); + Map.of("F1",ACTION_TOGGLE_F1,"F2",ACTION_TOGGLE_F2,"F3",ACTION_TOGGLE_F3,"F4",ACTION_TOGGLE_F4).entrySet().stream().sorted((e1,e2)->(e1.getKey().compareTo(e2.getKey()))).forEach(e -> { params.put(ACTION, e.getValue()); new Button(t(e.getKey()),params).addTo(functions); }); @@ -262,6 +262,7 @@ public class Locomotive extends Car implements Constants,Device{ private void queue() { int step = proto.steps * speed / (maxSpeedForward == 0 ? 100 : maxSpeedForward); + init(); plan.queue(new Command("SET {} GL "+address+" "+(orientation == FORWARD ? 0 : 1)+" "+step+" "+proto.steps+" "+(f1?1:0)+" "+(f2?1:0)+" "+(f3?1:0)+" "+(f4?1:0)) { @Override @@ -271,6 +272,27 @@ public class Locomotive extends Car implements Constants,Device{ } }); } + + public String setFunction(int num, boolean active) { + switch (num) { + case 1: + f1 = active; + break; + case 2: + f2 = active; + break; + case 3: + f3 = active; + break; + case 4: + f4 = active; + break; + default: + return t("Unknown function: {}",num); + } + queue(); + return t("{} F{}",t(active?"Activated":"Deavtivated"),num); + } /** * Sets the speed of the locomotive to the given velocity in [plan.speedUnit]s @@ -279,7 +301,6 @@ public class Locomotive extends Car implements Constants,Device{ */ public String setSpeed(int newSpeed) { LOG.debug(this.detail()+".setSpeed({})",newSpeed); - init(); speed = newSpeed; if (speed > maxSpeedForward && maxSpeedForward > 0) speed = maxSpeed(); if (speed < 0) speed = 0; @@ -293,30 +314,18 @@ public class Locomotive extends Car implements Constants,Device{ return properties(); } - private Object toggleFunction(int f) { - boolean active; + Object toggleFunction(int f) { switch (f) { case 1: - f1 =! f1; - active = f1; - break; + return setFunction(1, !f1); case 2: - f2 =! f2; - active = f2; - break; + return setFunction(2, !f2); case 3: - f3 =! f3; - active = f3; - break; + return setFunction(3, !f3); case 4: - f4 =! f4; - active = f4; - break; - default: - return t("Unknown function: {}",f); + return setFunction(4, !f4); } - queue(); - return t("{} F{}",t(active?"Activated":"Deavtivated"),f); + return t("Unknown function: {}",f); } public String turn() { @@ -329,7 +338,13 @@ public class Locomotive extends Car implements Constants,Device{ protected Window update(HashMap params) { super.update(params); if (params.containsKey(PROTOCOL)) proto = Protocol.valueOf(params.get(PROTOCOL)); - if (params.containsKey(ADDRESS)) address = Integer.parseInt(params.get(ADDRESS)); + if (params.containsKey(ADDRESS)) { + int newAddress = Integer.parseInt(params.get(ADDRESS)); + if (newAddress != address) { + init = false; + address = newAddress; + } + } return properties(); } } diff --git a/src/main/java/de/srsoftware/web4rail/moving/Train.java b/src/main/java/de/srsoftware/web4rail/moving/Train.java index cd697c1..3ebfa12 100644 --- a/src/main/java/de/srsoftware/web4rail/moving/Train.java +++ b/src/main/java/de/srsoftware/web4rail/moving/Train.java @@ -74,6 +74,7 @@ public class Train extends BaseClass implements Comparable { private static final String DESTINATION = "destination"; private HashSet tags = new HashSet(); + private boolean f1,f2,f3,f4; private Block currentBlock,destination = null; LinkedList trace = new LinkedList(); @@ -141,6 +142,14 @@ public class Train extends BaseClass implements Comparable { return train.automatic(); case ACTION_DROP: return train.dropCar(params); + case ACTION_TOGGLE_F1: + return train.toggleFunction(1); + case ACTION_TOGGLE_F2: + return train.toggleFunction(2); + case ACTION_TOGGLE_F3: + return train.toggleFunction(3); + case ACTION_TOGGLE_F4: + return train.toggleFunction(4); case ACTION_FASTER10: return train.faster(10); case ACTION_MOVE: @@ -163,6 +172,37 @@ public class Train extends BaseClass implements Comparable { return t("Unknown action: {}",params.get(ACTION)); } + Object toggleFunction(int f) { + boolean active; + switch (f) { + case 1: + f1 =! f1; + active = f1; + break; + case 2: + f2 =! f2; + active = f2; + break; + case 3: + f3 =! f3; + active = f3; + break; + case 4: + f4 =! f4; + active = f4; + break; + default: + return t("Unknown function: {}",f); + } + for (Car car : cars) { + if (car instanceof Locomotive) { + Locomotive loco = (Locomotive) car; + loco.setFunction(f,active); + } + } + return properties(); + } + public void addToTrace(Vector newTiles) { boolean active = trace.isEmpty(); for (Tile tile : newTiles) { @@ -222,7 +262,7 @@ public class Train extends BaseClass implements Comparable { car.link(car.name()+(car.stockId.isEmpty() ? "" : " ("+car.stockId+")")).addTo(li).content(NBSP); car.button(t("turn within train"),Map.of(ACTION,ACTION_TURN)).addTo(li); car.button("↑",Map.of(ACTION,ACTION_MOVE)).addTo(li); - button(t("delete"),Map.of(ACTION,ACTION_DROP,LOCO_ID,car.id().toString())).addTo(li); + button(t("delete"),Map.of(ACTION,ACTION_DROP,CAR_ID,car.id().toString())).addTo(li); li.addTo(carList); } @@ -234,7 +274,7 @@ public class Train extends BaseClass implements Comparable { new Input(ACTION, ACTION_ADD).hideIn(addLocoForm); new Input(ID,id).hideIn(addLocoForm); Select select = new Select(CAR_ID); - for (Car loco : locos) select.addOption(loco.id(), loco); + for (Car loco : locos) select.addOption(loco.id(), loco+(loco.stockId.isEmpty()?"":" ("+loco.stockId+")")); select.addTo(addLocoForm); new Button(t("add"),addLocoForm).addTo(addLocoForm); addLocoForm.addTo(new Tag("li")).addTo(carList); @@ -797,7 +837,6 @@ public class Train extends BaseClass implements Comparable { if (!tag.isEmpty()) tags.add(tag); } } - return this; }