tiles.rc: impl TileSpec class for reading tile specification

and rewrite MakeTiles to use the TileSpec class

git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@727 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
jason@long.name 2013-07-17 19:58:02 +00:00
parent 8793847415
commit 153e0e48f5
2 changed files with 176 additions and 5 deletions

View file

@ -35,10 +35,11 @@ public class MakeTiles
Graphics2D gr = buf.createGraphics();
for (int i = 0; i < NTILES; i++) {
String tileImage = recipe.getProperty(Integer.toString(i));
assert tileImage != null;
String rawSpec = recipe.getProperty(Integer.toString(i));
assert rawSpec != null;
ImageSpec ref = parseImageSpec(tileImage);
TileSpec tileSpec = TileSpec.parse(rawSpec);
ImageSpec ref = parseImageSpec(tileSpec);
drawTo(ref, gr, 0, TILE_SIZE*i);
}
@ -81,12 +82,13 @@ public class MakeTiles
int height;
}
static ImageSpec parseImageSpec(String chain)
static ImageSpec parseImageSpec(TileSpec spec)
{
ImageSpec result = null;
for (String layerStr : chain.split("\\s*\\|")) {
for (String layerStr : spec.getValues("image")) {
System.err.println("parsing "+layerStr);
ImageSpec rv = new ImageSpec();
rv.background = result;
result = rv;

View file

@ -0,0 +1,169 @@
package micropolisj.build_tool;
import java.util.*;
public class TileSpec
{
Map<String, List<String> > multiValues;
protected TileSpec()
{
this.multiValues = new HashMap<String, List<String> >();
}
public static TileSpec parse(String inStr)
{
TileSpec ts = new TileSpec();
ts.load(inStr);
return ts;
}
public String getValue(String key)
{
List<String> v = multiValues.get(key);
if (v != null && v.size() >= 1) {
return v.get(0);
}
else {
return null;
}
}
public String [] getValues(String key)
{
List<String> v = multiValues.get(key);
if (v != null) {
return v.toArray(new String[0]);
}
else {
return new String[0];
}
}
protected void load(String inStr)
{
Scanner in = new Scanner(inStr);
String k;
while ( (k=in.nextKey()) != null ) {
String v = in.nextValue();
if (v == null) {
if (k.startsWith("no")) {
k = k.substring(2);
v = "0";
}
else {
v = "1";
}
}
if (multiValues.containsKey(k)) {
multiValues.get(k).add(v);
}
else {
ArrayList<String> a = new ArrayList<String>();
a.add(v);
multiValues.put(k, a);
}
}
}
static class Scanner
{
String str;
int off = 0;
int st = ST_VALUE;
static int ST_KEY = 0;
static int ST_VALUE = 1;
static int ST_COMPAT = 2;
static int ST_EOF = 3;
Scanner(String str)
{
this.str = str;
if (str.indexOf('(') == -1) {
st = ST_COMPAT;
}
}
private void skipWhitespace()
{
while (off < str.length() && (Character.isWhitespace(str.charAt(off)) || str.charAt(off) == ',')) {
off++;
}
}
public String nextKey()
{
if (st == ST_EOF || off >= str.length()) {
return null;
}
if (st == ST_COMPAT) {
st = ST_VALUE;
return "image";
}
assert st == ST_KEY;
if (str.charAt(off) == '|') {
st = ST_VALUE;
off++;
skipWhitespace();
return "image";
}
else {
int start = off;
while (off < str.length() && (str.charAt(off) == '-' || Character.isLetterOrDigit(str.charAt(off)))) {
off++;
}
st = ST_VALUE;
return str.substring(start, off);
}
}
public String nextValue()
{
assert st == ST_VALUE;
if (off == str.length()) {
st = ST_EOF;
return null;
}
int c = str.charAt(off);
if (Character.isWhitespace(c) || c == ',') {
skipWhitespace();
st = ST_KEY;
return null;
}
int endQuote = 0;
if (c == '(') {
off++;
endQuote = ')';
}
int start = off;
while (off < str.length()) {
c = str.charAt(off);
if (c == endQuote) {
int end = off;
off++;
skipWhitespace();
st = ST_KEY;
return str.substring(start,end);
}
else if (endQuote == 0 && (Character.isWhitespace(c) || c == '|')) {
int end = off;
skipWhitespace();
st = ST_KEY;
return str.substring(start, end);
}
off++;
}
st = ST_EOF;
return str.substring(start);
}
}
}