Browse Source

implementing train actions

lookup-tables
Stephan Richter 5 years ago
parent
commit
cf4df1882e
  1. 1
      resources/css/style.css
  2. 27
      resources/js/plan.js
  3. 28
      src/main/java/de/srsoftware/web4rail/Plan.java
  4. 15
      src/main/java/de/srsoftware/web4rail/moving/Train.java
  5. 16
      src/main/java/de/srsoftware/web4rail/tiles/Block.java
  6. 30
      src/main/java/de/srsoftware/web4rail/tiles/Tile.java

1
resources/css/style.css

@ -113,6 +113,7 @@ svg circle{ @@ -113,6 +113,7 @@ svg circle{
right: 10px;
background: yellow;
padding: 5px;
overflow: scroll;
}
h2{

27
resources/js/plan.js

@ -15,12 +15,7 @@ function addMessage(txt){ @@ -15,12 +15,7 @@ function addMessage(txt){
function addTile(x,y){
console.log("addTile:",selected.id,x,y);
$.ajax({
url : PLAN,
method: POST,
data : {action:mode,tile:selected.id,x:x,y:y},
success: addMessage
});
return request({action:mode,tile:selected.id,x:x,y:y});
}
function bodyClick(ev){
@ -97,12 +92,7 @@ function heartbeat(data){ @@ -97,12 +92,7 @@ function heartbeat(data){
function moveTile(x,y){
console.log("moveTile:",selected.id,x,y);
$.ajax({
url : PLAN,
method: POST,
data : {action:mode,direction:selected.id,x:x,y:y},
success: addMessage
});
return request({action:mode,direction:selected.id,x:x,y:y});
}
function openRoute(id){
@ -136,17 +126,16 @@ function request(data){ @@ -136,17 +126,16 @@ function request(data){
}
}
});
return false;
}
function runAction(ev){
console.log("runAction: ",ev.target.id);
$.ajax({
url : PLAN,
method : POST,
data : {action:ev.target.id,file:'default'}, // TODO: ask for name
success: function(resp){ addMessage(resp);}
});
return false;
return request({action:ev.target.id,file:'default'}); // TODO: ask for name
}
function train(x,y,action){
return request({action:action+"Train",x:x,y:y});
}
function stream(ev){

28
src/main/java/de/srsoftware/web4rail/Plan.java

@ -25,6 +25,7 @@ import org.slf4j.LoggerFactory; @@ -25,6 +25,7 @@ import org.slf4j.LoggerFactory;
import de.keawe.tools.translations.Translation;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.moving.Train;
import de.srsoftware.web4rail.tiles.Block;
import de.srsoftware.web4rail.tiles.BlockH;
import de.srsoftware.web4rail.tiles.BlockV;
@ -98,6 +99,7 @@ public class Plan { @@ -98,6 +99,7 @@ public class Plan {
private static final String ID = "id";
private static final String ROUTE = "route";
private static final HashMap<OutputStreamWriter,Integer> clients = new HashMap<OutputStreamWriter, Integer>();
private static final String ACTION_SHOW_TRAIN = "showTrain";
private HashMap<Integer,HashMap<Integer,Tile>> tiles = new HashMap<Integer,HashMap<Integer,Tile>>();
private HashSet<Block> blocks = new HashSet<Block>();
@ -183,6 +185,10 @@ public class Plan { @@ -183,6 +185,10 @@ public class Plan {
return results;
}
private Tile get(String x, String y,boolean resolveShadows) {
return get(Integer.parseInt(x),Integer.parseInt(y),resolveShadows);
}
public Tile get(int x, int y,boolean resolveShadows) {
HashMap<Integer, Tile> column = tiles.get(x);
Tile tile = (column == null) ? null : column.get(y);
@ -366,11 +372,13 @@ public class Plan { @@ -366,11 +372,13 @@ public class Plan {
case ACTION_MOVE:
return moveTile(params.get(DIRECTION),params.get(X),params.get(Y));
case ACTION_PROPS:
return propMenu(params.get(X),params.get(Y));
return propMenu(get(params.get(X),params.get(Y),true));
case ACTION_ROUTE:
return routeProperties(params.get(ID));
case ACTION_SAVE:
return saveTo(params.get(FILE));
case ACTION_SHOW_TRAIN:
return showTrain(get(params.get(X),params.get(Y),true));
case ACTION_UPDATE:
return update(params);
default:
@ -390,12 +398,7 @@ public class Plan { @@ -390,12 +398,7 @@ public class Plan {
return route.properties();
}
private Tag propMenu(String x, String y) {
return propMenu(Integer.parseInt(x),Integer.parseInt(y));
}
private Tag propMenu(int x, int y) {
Tile tile = get(x, y,true);
private Tag propMenu(Tile tile) {
if (tile == null) return null;
return tile.propMenu();
}
@ -405,8 +408,6 @@ public class Plan { @@ -405,8 +408,6 @@ public class Plan {
routes.put(route.id(), route);
}
private void remove(Tile tile) {
remove_intern(tile.x,tile.y);
if (tile instanceof Block) blocks.remove(tile);
@ -465,6 +466,15 @@ public class Plan { @@ -465,6 +466,15 @@ public class Plan {
column.put(y,tile.position(x, y));
}
private Object showTrain(Tile tile) {
if (tile instanceof Block) {
Block block = (Block) tile;
Train train = block.train();
if (train != null) return train.props();
}
return null;
}
private synchronized void stream(String data) {
data = data.replaceAll("\n", "").replaceAll("\r", "");
LOG.debug("streaming: {}",data);

15
src/main/java/de/srsoftware/web4rail/moving/Train.java

@ -2,6 +2,11 @@ package de.srsoftware.web4rail.moving; @@ -2,6 +2,11 @@ package de.srsoftware.web4rail.moving;
import java.util.Vector;
import de.keawe.tools.translations.Translation;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Application;
import de.srsoftware.web4rail.Window;
public class Train {
private Vector<Locomotive> locos = new Vector<Locomotive>();
private Vector<Car> cars = new Vector<Car>();
@ -28,4 +33,14 @@ public class Train { @@ -28,4 +33,14 @@ public class Train {
public String name() {
return name != null ? name : locos.firstElement().name();
}
public Tag props() {
Window window = new Window("train-properties",t("Properties of {}",getClass().getSimpleName()));
return window;
}
private String t(String message, Object...fills) {
return Translation.get(Application.class, message, fills);
}
}

16
src/main/java/de/srsoftware/web4rail/tiles/Block.java

@ -42,6 +42,18 @@ public abstract class Block extends StretchableTile{ @@ -42,6 +42,18 @@ public abstract class Block extends StretchableTile{
return form;
}
@Override
public Tag propMenu() {
Tag window = super.propMenu();
if (train != null) {
new Tag("h4").content(t("Train:")).addTo(window);
new Tag("span").clazz("link").attr("onclick","train("+x+","+y+",'show')").content(train.name()).addTo(window);
new Tag("span").clazz("link").attr("onclick","train("+x+","+y+",'start')").content(" - "+t("start")).addTo(window);
}
return window;
}
@Override
public Tag tag(Map<String, Object> replacements) throws IOException {
if (replacements == null) replacements = new HashMap<String, Object>();
@ -64,4 +76,8 @@ public abstract class Block extends StretchableTile{ @@ -64,4 +76,8 @@ public abstract class Block extends StretchableTile{
public void setTrain(Train train) {
this.train = train;
}
public Train train() {
return train;
}
}

30
src/main/java/de/srsoftware/web4rail/tiles/Tile.java

@ -70,29 +70,29 @@ public abstract class Tile { @@ -70,29 +70,29 @@ public abstract class Tile {
new Tag("input").attr("type", "hidden").attr("name","action").attr("value", "update").addTo(form);
new Tag("input").attr("type", "hidden").attr("name","x").attr("value", x).addTo(form);
new Tag("input").attr("type", "hidden").attr("name","y").attr("value", y).addTo(form);
if (!routes.isEmpty()) {
new Tag("h4").content(t("Routes using this tile:")).addTo(form);
Tag routeList = new Tag("ul");
for (Route route : routes) {
new Tag("li").clazz("link").attr("onclick","openRoute('"+route.id()+"')").content(route.name()).addTo(routeList);
}
routeList.addTo(form);
}
return form;
}
public Tag propMenu() {
Window menu = new Window("tile-properties",t("Properties of {} @ ({},{})",getClass().getSimpleName(),x,y));
Window window = new Window("tile-properties",t("Properties of {} @ ({},{})",getClass().getSimpleName(),x,y));
Tag form = propForm();
if (form!=null) {
if (form!=null && form.children().size()>3) {
new Tag("button").attr("type", "submit").content(t("save")).addTo(form);
form.addTo(menu);
form.addTo(window);
} else {
menu.content(t("This tile ({}) has no properties",getClass().getSimpleName()));
window.content(t("This tile ({}) has no properties",getClass().getSimpleName()));
}
return menu;
if (!routes.isEmpty()) {
new Tag("h4").content(t("Routes using this tile:")).addTo(window);
Tag routeList = new Tag("ul");
for (Route route : routes) {
new Tag("li").clazz("link").attr("onclick","openRoute('"+route.id()+"')").content(route.name()).addTo(routeList);
}
routeList.addTo(window);
}
return window;
}
public Tag tag(Map<String,Object> replacements) throws IOException {

Loading…
Cancel
Save