TileImage: teach MakeTiles module how to use LoaderContext

This commit is contained in:
Jason Long 2014-08-15 13:08:26 -07:00
parent 92776621d6
commit 860faa1081
2 changed files with 40 additions and 14 deletions

View file

@ -18,7 +18,6 @@ import static micropolisj.XML_Helper.*;
public class MakeTiles
{
static HashMap<String,String> tileData = new HashMap<String,String>();
static HashMap<String,SourceImage> loadedImages = new HashMap<String,SourceImage>();
static final Charset UTF8 = Charset.forName("UTF-8");
static int SKIP_TILES = 0;
@ -431,6 +430,30 @@ public class MakeTiles
}
}
static class MyLoaderContext implements LoaderContext
{
HashMap<String,SourceImage> loadedImages = new HashMap<String,SourceImage>();
//implements LoaderContext
public SourceImage getDefaultImage()
{
throw new UnsupportedOperationException();
}
//implements LoaderContext
public SourceImage getImage(String fileName)
throws IOException
{
if (!loadedImages.containsKey(fileName)) {
loadedImages.put(fileName,
loadImageNoCache(fileName));
}
return loadedImages.get(fileName);
}
}
static MyLoaderContext loaderContext = new MyLoaderContext();
static TileImage loadImage(String fileName)
throws IOException
{
@ -439,12 +462,7 @@ public class MakeTiles
return loadImageXml(xmlFile);
}
if (!loadedImages.containsKey(fileName)) {
loadedImages.put(fileName,
loadImageNoCache(fileName));
}
return loadedImages.get(fileName);
return loaderContext.getImage(fileName);
}
static SourceImage loadImageReal(File pngFile, int basisSize)

View file

@ -2,6 +2,7 @@ package micropolisj.graphics;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.*;
import javax.xml.stream.*;
@ -125,20 +126,27 @@ public abstract class TileImage
public interface LoaderContext
{
SourceImage getDefaultImage();
SourceImage getImage(String name);
SourceImage getDefaultImage()
throws IOException;
SourceImage getImage(String name)
throws IOException;
}
static SimpleTileImage readSimpleImage(XMLStreamReader in, LoaderContext ctx)
throws XMLStreamException
{
SimpleTileImage img = new SimpleTileImage();
String srcImageName = in.getAttributeValue(null, "src");
if (srcImageName != null) {
img.srcImage = ctx.getImage(srcImageName);
try {
String srcImageName = in.getAttributeValue(null, "src");
if (srcImageName != null) {
img.srcImage = ctx.getImage(srcImageName);
}
else {
img.srcImage = ctx.getDefaultImage();
}
}
else {
img.srcImage = ctx.getDefaultImage();
catch (IOException e) {
throw new XMLStreamException("image source not found", e);
}
String tmp = in.getAttributeValue(null, "offsetY");
img.offsetY = tmp != null ? Integer.parseInt(tmp) : 0;