Browse Source

working on stretchable tiles

lookup-tables
Stephan Richter 5 years ago
parent
commit
10837473ce
  1. 7
      resources/css/style.css
  2. 3
      resources/js/plan.js
  3. 1
      resources/translations/Application.de.translation
  4. 2
      src/main/java/de/srsoftware/web4rail/Application.java
  5. 25
      src/main/java/de/srsoftware/web4rail/Plan.java
  6. 14
      src/main/java/de/srsoftware/web4rail/tags/Form.java
  7. 20
      src/main/java/de/srsoftware/web4rail/tiles/StraightH.java
  8. 20
      src/main/java/de/srsoftware/web4rail/tiles/StraightV.java
  9. 49
      src/main/java/de/srsoftware/web4rail/tiles/StretchableTile.java
  10. 14
      src/main/java/de/srsoftware/web4rail/tiles/Tile.java

7
resources/css/style.css

@ -7,7 +7,12 @@ html{
} }
body{ body{
min-height: 100%; min-height: 100%;
background: #c6dbd2; }
#plan{
background: #c6dbd2;
height: 100%;
width: 100%;
} }
.tile{ .tile{

3
resources/js/plan.js

@ -1,7 +1,7 @@
const ADD = 'add'; const ADD = 'add';
const MOVE = 'move'; const MOVE = 'move';
const SQUARE = 30; const SQUARE = 30;
const BODY = 'body'; const BODY = '#plan';
const DIV = 'DIV'; const DIV = 'DIV';
const SVG = 'svg'; const SVG = 'svg';
const PLAN = 'plan'; const PLAN = 'plan';
@ -137,6 +137,7 @@ function savePlan(ev){
window.onload = function () { window.onload = function () {
var isDragging = false; var isDragging = false;
console.log($(BODY).each(function(){console.log(this)}));
$('.menu > div').click(closeMenu); $('.menu > div').click(closeMenu);
$('.menu .addtile .list svg').click(enableAdding); $('.menu .addtile .list svg').click(enableAdding);
$('.menu .move .list div').click(enableMove); $('.menu .move .list div').click(enableMove);

1
resources/translations/Application.de.translation

@ -3,5 +3,6 @@ Added {} : {} hinzugefügt
Add tile : Kachel hinzufügen Add tile : Kachel hinzufügen
Plan saved as "{}". : Plan als „{}“ gespeichert. Plan saved as "{}". : Plan als „{}“ gespeichert.
Properties : Eigenschaften Properties : Eigenschaften
Properties of {} @ ({},{}) : Eigenschaften von {} @ ({},{})
Save plan : Plan speichern Save plan : Plan speichern
Unknown action\: {} : Unbekannte Aktion: {} Unknown action\: {} : Unbekannte Aktion: {}

2
src/main/java/de/srsoftware/web4rail/Application.java

@ -107,7 +107,7 @@ public class Application {
try { try {
if (!params.isEmpty()) { if (!params.isEmpty()) {
send(client,plan.process(params)); send(client,plan.process(params));
} else send(client,plan.html().style("css/style.css").js("js/jquery-3.5.1.min.js").js("js/plan.js")); } else send(client,plan.html());
} catch (Exception e) { } catch (Exception e) {
LOG.error("Error during sendPlan(): {}",e); LOG.error("Error during sendPlan(): {}",e);
send(client,new Page().append(e.getMessage())); send(client,new Page().append(e.getMessage()));

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

@ -56,6 +56,7 @@ public class Plan {
private static final String WEST = "west"; private static final String WEST = "west";
private static final String SOUTH = "south"; private static final String SOUTH = "south";
private static final String NORTH = "north"; private static final String NORTH = "north";
private static final String ACTION_UPDATE = "update";
private HashMap<Integer,HashMap<Integer,Tile>> tiles = new HashMap<Integer,HashMap<Integer,Tile>>(); private HashMap<Integer,HashMap<Integer,Tile>> tiles = new HashMap<Integer,HashMap<Integer,Tile>>();
@ -74,8 +75,6 @@ public class Plan {
clazz = tc.getName().replace(".Tile", "."+clazz); clazz = tc.getName().replace(".Tile", "."+clazz);
Tile tile = (Tile) tc.getClassLoader().loadClass(clazz).getDeclaredConstructor().newInstance(); Tile tile = (Tile) tc.getClassLoader().loadClass(clazz).getDeclaredConstructor().newInstance();
set(x, y, tile); set(x, y, tile);
for (int i=1; i<tile.len(); i++) set(x+i,y,new Shadow(tile));
for (int i=1; i<tile.height(); i++) set(x,y+1,new Shadow(tile));
return tile; return tile;
} }
@ -86,7 +85,7 @@ public class Plan {
} }
public Page html() throws IOException { public Page html() throws IOException {
Page page = new Page(); Page page = new Page().append("<div id=\"plan\">");
for (Entry<Integer, HashMap<Integer, Tile>> column : tiles.entrySet()) { for (Entry<Integer, HashMap<Integer, Tile>> column : tiles.entrySet()) {
int x = column.getKey(); int x = column.getKey();
for (Entry<Integer, Tile> row : column.getValue().entrySet()) { for (Entry<Integer, Tile> row : column.getValue().entrySet()) {
@ -95,7 +94,7 @@ public class Plan {
if (tile != null) page.append("\t\t"+tile.tag()+"\n"); if (tile != null) page.append("\t\t"+tile.tag()+"\n");
} }
} }
return page.append(menu()).append(messages()); return page.append(menu()).append(messages()).append("</div>").style("css/style.css").js("js/jquery-3.5.1.min.js").js("js/plan.js");
} }
public static Plan load(String filename) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { public static Plan load(String filename) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
@ -195,6 +194,8 @@ public class Plan {
return propMenu(params.get(X),params.get(Y)); return propMenu(params.get(X),params.get(Y));
case ACTION_SAVE: case ACTION_SAVE:
return saveTo(params.get(NAME)); return saveTo(params.get(NAME));
case ACTION_UPDATE:
return update(params);
default: default:
LOG.warn("Unknown action: {}",action); LOG.warn("Unknown action: {}",action);
} }
@ -204,6 +205,16 @@ public class Plan {
} }
} }
private Page update(HashMap<String, String> params) throws IOException {
return update(Integer.parseInt(params.get("x")),Integer.parseInt(params.get("y")),params);
}
private Page update(int x,int y, HashMap<String, String> params) throws IOException {
Tile tile = get(x,y);
if (tile != null) set(x,y,tile.update(params));
return this.html();
}
private Tag propMenu(String x, String y) { private Tag propMenu(String x, String y) {
return propMenu(Integer.parseInt(x),Integer.parseInt(y)); return propMenu(Integer.parseInt(x),Integer.parseInt(y));
} }
@ -239,7 +250,11 @@ public class Plan {
tiles.put(x, column); tiles.put(x, column);
} }
old = column.remove(y); old = column.remove(y);
if (tile != null && !(tile instanceof Eraser)) column.put(y,tile.position(x, y)); if (tile != null && !(tile instanceof Eraser)) {
column.put(y,tile.position(x, y));
for (int i=1; i<tile.len(); i++) set(x+i,y,new Shadow(tile));
for (int i=1; i<tile.height(); i++) set(x,y+i,new Shadow(tile));
}
return old; return old;
} }

14
src/main/java/de/srsoftware/web4rail/tags/Form.java

@ -0,0 +1,14 @@
package de.srsoftware.web4rail.tags;
import de.srsoftware.tools.Tag;
public class Form extends Tag {
private static final long serialVersionUID = 3518580733330482303L;
public Form() {
super("form");
attr("method","POST");
}
}

20
src/main/java/de/srsoftware/web4rail/tiles/StraightH.java

@ -1,4 +1,24 @@
package de.srsoftware.web4rail.tiles; package de.srsoftware.web4rail.tiles;
import java.io.IOException;
import de.srsoftware.tools.Tag;
public class StraightH extends StretchableTile{ public class StraightH extends StretchableTile{
@Override
public int len() {
return length;
}
@Override
public Tag tag() throws IOException {
Tag tag = super.tag();
if (length>1) {
String style = tag.get("style");
tag.style(style.trim()+" width: "+(30*length)+"px;");
tag.attr("preserveAspectRatio","none");
}
return tag;
}
} }

20
src/main/java/de/srsoftware/web4rail/tiles/StraightV.java

@ -1,5 +1,25 @@
package de.srsoftware.web4rail.tiles; package de.srsoftware.web4rail.tiles;
import java.io.IOException;
import de.srsoftware.tools.Tag;
public class StraightV extends StretchableTile{ public class StraightV extends StretchableTile{
@Override
public int height() {
return length;
}
@Override
public Tag tag() throws IOException {
Tag tag = super.tag();
if (length>1) {
LOG.debug("{}.tag: length = {}",getClass().getSimpleName(),length);
String style = tag.get("style");
tag.style(style.trim()+" height: "+(30*length)+"px;");
tag.attr("preserveAspectRatio","none");
}
return tag;
}
} }

49
src/main/java/de/srsoftware/web4rail/tiles/StretchableTile.java

@ -1,5 +1,54 @@
package de.srsoftware.web4rail.tiles; package de.srsoftware.web4rail.tiles;
import java.util.HashMap;
import java.util.Map.Entry;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.tags.Form;
public abstract class StretchableTile extends Tile { public abstract class StretchableTile extends Tile {
private static final String LENGTH = "length";
public int length = 1; public int length = 1;
public Tag propMenu() {
Window menu = new Window("tile-properties",t("Properties of {} @ ({},{})",getClass().getSimpleName(),x,y));
Form form = new Form();
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);
Tag label = new Tag("label").content(t("length:"));
new Tag("input").attr("type", "number").attr("name","length").attr("value", length).addTo(label);
label.addTo(form);
new Tag("button").attr("type", "submit").content(t("save")).addTo(form);
form.addTo(menu);
return menu;
}
private void setLength(String value) {
try {
setLength(Integer.parseInt(value));
} catch (NumberFormatException nfe) {
LOG.warn("{} is not a valid length!",value);
}
}
public void setLength(int len) {
this.length = Math.max(1, len);
}
@Override
public Tile update(HashMap<String, String> params) {
super.update(params);
for (Entry<String, String> entry : params.entrySet()) {
switch (entry.getKey()) {
case LENGTH:
setLength(entry.getValue());
break;
}
}
return this;
}
} }

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

@ -3,9 +3,13 @@ package de.srsoftware.web4rail.tiles;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Scanner; import java.util.Scanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.keawe.tools.translations.Translation; import de.keawe.tools.translations.Translation;
import de.srsoftware.tools.Tag; import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Application; import de.srsoftware.web4rail.Application;
@ -15,6 +19,7 @@ public abstract class Tile {
protected int x = -1,y = -1; protected int x = -1,y = -1;
protected HashSet<String> classes = new HashSet<String>(); protected HashSet<String> classes = new HashSet<String>();
protected static Logger LOG = LoggerFactory.getLogger(Tile.class);
public Tile() { public Tile() {
classes.add("tile"); classes.add("tile");
@ -47,7 +52,7 @@ public abstract class Tile {
.size(100,100) .size(100,100)
.attr("name", getClass().getSimpleName()) .attr("name", getClass().getSimpleName())
.attr("viewbox", "0 0 100 100"); .attr("viewbox", "0 0 100 100");
if (x>-1) svg.style("left: "+(30*x)+"px; top: "+(30*y)+"px"); if (x>-1) svg.style("left: "+(30*x)+"px; top: "+(30*y)+"px;");
File file = new File(System.getProperty("user.dir")+"/resources/svg/"+getClass().getSimpleName()+".svg"); File file = new File(System.getProperty("user.dir")+"/resources/svg/"+getClass().getSimpleName()+".svg");
if (file.exists()) { if (file.exists()) {
@ -71,7 +76,7 @@ public abstract class Tile {
return svg; return svg;
} }
private 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);
} }
@ -79,4 +84,9 @@ public abstract class Tile {
public String toString() { public String toString() {
return t("{}({},{})",getClass().getSimpleName(),x,y) ; return t("{}({},{})",getClass().getSimpleName(),x,y) ;
} }
public Tile update(HashMap<String, String> params) {
LOG.debug("{}.update({})",getClass().getSimpleName(),params);
return this;
}
} }

Loading…
Cancel
Save