Browse Source

improved stretching of tiles

lookup-tables
Stephan Richter 5 years ago
parent
commit
140414837b
  1. 6
      resources/svg/BlockH.svg
  2. 5
      resources/svg/BlockV.svg
  3. 4
      resources/svg/StraightH.svg
  4. 4
      resources/svg/StraightV.svg
  5. 2
      src/main/java/de/srsoftware/web4rail/Plan.java
  6. 15
      src/main/java/de/srsoftware/web4rail/tiles/StraightH.java
  7. 16
      src/main/java/de/srsoftware/web4rail/tiles/StraightV.java
  8. 25
      src/main/java/de/srsoftware/web4rail/tiles/Tile.java

6
resources/svg/BlockH.svg

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
<svg width="100" height="100" viewbox="0 0 100 100">
<rect x="0" y="35" width="100" height="30" />
<rect class="block" x="10" y="10" width="80" height="80" />
<text x="10" y="65" fill="red">I love SVG!</text>
<rect x="0" y="35" width="%width%" height="30" />
<rect class="block" x="20" y="10" width="%width%-40" height="80" />
<text x="25" y="65" fill="red">%text%</text>
</svg>

Before

Width:  |  Height:  |  Size: 218 B

After

Width:  |  Height:  |  Size: 225 B

5
resources/svg/BlockV.svg

@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
<svg width="100" height="100" viewbox="0 0 100 100">
<rect x="35" y="0" width="30" height="%height%" />
<rect class="block" x="10" y="20" width="80" height="%height%-40" />
<text x="25" y="65" fill="red" transform="translate(100, 0) rotate(90)">%text%</text>
</svg>

After

Width:  |  Height:  |  Size: 268 B

4
resources/svg/StraightH.svg

@ -1,3 +1,3 @@ @@ -1,3 +1,3 @@
<svg width="100" height="100" viewbox="0 0 100 100">
<rect x="0" y="35" width="100" height="30" />
<svg width="%width%" height="100" viewbox="0 0 %width% 100">
<rect x="0" y="35" width="%width%" height="30" />
</svg>

Before

Width:  |  Height:  |  Size: 106 B

After

Width:  |  Height:  |  Size: 118 B

4
resources/svg/StraightV.svg

@ -1,3 +1,3 @@ @@ -1,3 +1,3 @@
<svg width="100" height="100" viewbox="0 0 100 100">
<rect x="35" y="0" width="30" height="100" />
<svg width="100" height="" viewbox="0 0 100 100">
<rect x="35" y="0" width="30" height="%height%" />
</svg>

Before

Width:  |  Height:  |  Size: 107 B

After

Width:  |  Height:  |  Size: 109 B

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

@ -18,6 +18,7 @@ import org.slf4j.LoggerFactory; @@ -18,6 +18,7 @@ import org.slf4j.LoggerFactory;
import de.keawe.tools.translations.Translation;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.tiles.BlockH;
import de.srsoftware.web4rail.tiles.BlockV;
import de.srsoftware.web4rail.tiles.CrossH;
import de.srsoftware.web4rail.tiles.CrossV;
import de.srsoftware.web4rail.tiles.DiagES;
@ -299,6 +300,7 @@ public class Plan { @@ -299,6 +300,7 @@ public class Plan {
tiles.append(new CrossV().tag());
tiles.append(new Eraser().tag());
tiles.append(new BlockH().tag());
tiles.append(new BlockV().tag());
return new Tag("div").clazz("list").content(tiles.toString()).addTo(tileMenu);
}
}

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

@ -1,24 +1,9 @@ @@ -1,24 +1,9 @@
package de.srsoftware.web4rail.tiles;
import java.io.IOException;
import de.srsoftware.tools.Tag;
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;
}
}

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

@ -1,25 +1,9 @@ @@ -1,25 +1,9 @@
package de.srsoftware.web4rail.tiles;
import java.io.IOException;
import de.srsoftware.tools.Tag;
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;
}
}

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

@ -52,14 +52,19 @@ public abstract class Tile { @@ -52,14 +52,19 @@ public abstract class Tile {
}
public Tag tag() throws IOException {
int width = 100*len();
int height = 100*height();
String style = "";
Tag svg = new Tag("svg")
.id((x!=-1 && y!=-1)?("tile-"+x+"-"+y):(getClass().getSimpleName()))
.clazz(classes)
.size(100,100)
.attr("name", getClass().getSimpleName())
.attr("viewbox", "0 0 100 100");
if (x>-1) svg.style("left: "+(30*x)+"px; top: "+(30*y)+"px;");
.attr("viewbox", "0 0 "+width+" "+height);
if (x>-1) style="left: "+(30*x)+"px; top: "+(30*y)+"px;";
if (len()>1) style+=" width: "+(30*len())+"px;";
if (height()>1) style+=" height: "+(30*height())+"px;";
if (!style.isEmpty()) svg.style(style);
File file = new File(System.getProperty("user.dir")+"/resources/svg/"+getClass().getSimpleName()+".svg");
if (file.exists()) {
@ -68,6 +73,8 @@ public abstract class Tile { @@ -68,6 +73,8 @@ public abstract class Tile {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("<svg") || line.endsWith("svg>")) continue;
line = replace(line,"%width%",width);
line = replace(line,"%height%",height);
sb.append(line+"\n");
}
scanner.close();
@ -83,6 +90,18 @@ public abstract class Tile { @@ -83,6 +90,18 @@ public abstract class Tile {
return svg;
}
private static String replace(String line, String key, int val) {
int start = line.indexOf(key);
int len = key.length();
while (start>0) {
String tag = line.substring(start, line.indexOf("\"",start));
int summand = (tag.length()>len) ? Integer.parseInt(tag.substring(len)) : 0;
line = line.replace(tag, ""+(val+summand));
start = line.indexOf(key);
}
return line;
}
protected static String t(String txt, Object...fills) {
return Translation.get(Application.class, txt, fills);
}

Loading…
Cancel
Save