refactor: hide backend Adapter handles
- static methods for backend adapters
This commit is contained in:
parent
84968d201d
commit
95c9f47e63
@ -33,10 +33,9 @@ import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
public final class AndroidGraphics extends CanvasAdapter {
|
||||
public static final AndroidGraphics INSTANCE = new AndroidGraphics();
|
||||
|
||||
public static void init() {
|
||||
g = INSTANCE;
|
||||
CanvasAdapter.init(new AndroidGraphics());
|
||||
}
|
||||
|
||||
// public static android.graphics.Bitmap getAndroidBitmap(Bitmap bitmap) {
|
||||
@ -52,22 +51,22 @@ public final class AndroidGraphics extends CanvasAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap decodeBitmap(InputStream inputStream) {
|
||||
public Bitmap decodeBitmapImpl(InputStream inputStream) {
|
||||
return new AndroidBitmap(inputStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Paint getPaint() {
|
||||
public Paint newPaintImpl() {
|
||||
return new AndroidPaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap getBitmap(int width, int height, int format) {
|
||||
public Bitmap newBitmapImpl(int width, int height, int format) {
|
||||
return new AndroidBitmap(width, height, format);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Canvas getCanvas() {
|
||||
public Canvas newCanvasImpl() {
|
||||
return new AndroidCanvas();
|
||||
}
|
||||
|
||||
@ -100,7 +99,7 @@ public final class AndroidGraphics extends CanvasAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap loadBitmapAsset(String fileName) {
|
||||
public Bitmap loadBitmapAssetImpl(String fileName) {
|
||||
try {
|
||||
return createBitmap(fileName);
|
||||
} catch (IOException e) {
|
||||
|
@ -27,7 +27,7 @@ public class AndroidAssets extends AssetAdapter {
|
||||
Context mContext;
|
||||
|
||||
public static void init(Context ctx) {
|
||||
g = new AndroidAssets(ctx);
|
||||
AssetAdapter.init(new AndroidAssets(ctx));
|
||||
}
|
||||
|
||||
private AndroidAssets(Context ctx) {
|
||||
|
@ -34,7 +34,7 @@ import android.graphics.drawable.Drawable;
|
||||
public final class AndroidGraphics extends CanvasAdapter {
|
||||
|
||||
public static void init() {
|
||||
g = new AndroidGraphics();
|
||||
CanvasAdapter.init(new AndroidGraphics());
|
||||
}
|
||||
|
||||
public static android.graphics.Paint getAndroidPaint(Paint paint) {
|
||||
@ -46,12 +46,12 @@ public final class AndroidGraphics extends CanvasAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap decodeBitmap(InputStream inputStream) {
|
||||
public Bitmap decodeBitmapImpl(InputStream inputStream) {
|
||||
return new AndroidBitmap(inputStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap loadBitmapAsset(String fileName) {
|
||||
public Bitmap loadBitmapAssetImpl(String fileName) {
|
||||
try {
|
||||
return createBitmap(fileName);
|
||||
} catch (IOException e) {
|
||||
@ -61,17 +61,17 @@ public final class AndroidGraphics extends CanvasAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Paint getPaint() {
|
||||
public Paint newPaintImpl() {
|
||||
return new AndroidPaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap getBitmap(int width, int height, int format) {
|
||||
public Bitmap newBitmapImpl(int width, int height, int format) {
|
||||
return new AndroidBitmap(width, height, format);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Canvas getCanvas() {
|
||||
public Canvas newCanvasImpl() {
|
||||
return new AndroidCanvas();
|
||||
}
|
||||
|
||||
|
@ -30,10 +30,9 @@ import org.oscim.backend.canvas.Canvas;
|
||||
import org.oscim.backend.canvas.Paint;
|
||||
|
||||
public class AwtGraphics extends CanvasAdapter {
|
||||
private static final AwtGraphics INSTANCE = new AwtGraphics();
|
||||
|
||||
public static void init() {
|
||||
g = INSTANCE;
|
||||
CanvasAdapter.init(new AwtGraphics());
|
||||
}
|
||||
|
||||
private AwtGraphics() {
|
||||
@ -41,17 +40,17 @@ public class AwtGraphics extends CanvasAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Paint getPaint() {
|
||||
public Paint newPaintImpl() {
|
||||
return new AwtPaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap getBitmap(int width, int height, int format) {
|
||||
public Bitmap newBitmapImpl(int width, int height, int format) {
|
||||
return new AwtBitmap(width, height, format);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Canvas getCanvas() {
|
||||
public Canvas newCanvasImpl() {
|
||||
return new AwtCanvas();
|
||||
}
|
||||
|
||||
@ -87,7 +86,7 @@ public class AwtGraphics extends CanvasAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap decodeBitmap(InputStream inputStream) {
|
||||
public Bitmap decodeBitmapImpl(InputStream inputStream) {
|
||||
try {
|
||||
return new AwtBitmap(inputStream);
|
||||
} catch (IOException e) {
|
||||
@ -97,7 +96,7 @@ public class AwtGraphics extends CanvasAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap loadBitmapAsset(String fileName) {
|
||||
public Bitmap loadBitmapAssetImpl(String fileName) {
|
||||
try {
|
||||
return createBitmap(fileName);
|
||||
} catch (IOException e) {
|
||||
|
@ -46,6 +46,6 @@ public class GdxAssets extends AssetAdapter {
|
||||
}
|
||||
|
||||
public static void init(String path) {
|
||||
g = new GdxAssets(path);
|
||||
AssetAdapter.init(new GdxAssets(path));
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ public class JeoTileSource extends TileSource {
|
||||
sink.completed(TILE_NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
Bitmap b = CanvasAdapter.g.decodeBitmap(new ByteArrayInputStream(t.getData()));
|
||||
Bitmap b = CanvasAdapter.decodeBitmap(new ByteArrayInputStream(t.getData()));
|
||||
sink.setTileImage(b);
|
||||
log.debug("success {}", tile);
|
||||
sink.completed(SUCCESS);
|
||||
|
@ -48,7 +48,7 @@ public abstract class SpriteManager<T> {
|
||||
|
||||
Sprite items;
|
||||
|
||||
protected final Canvas mCanvas = CanvasAdapter.g.getCanvas();
|
||||
protected final Canvas mCanvas = CanvasAdapter.newCanvas();
|
||||
protected TextureItem mTexture;
|
||||
|
||||
public SpriteManager() {
|
||||
|
@ -18,7 +18,7 @@ public class ThemeTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
AwtGraphics.init();
|
||||
AssetAdapter.g = new AssetAdapter() {
|
||||
AssetAdapter.init(new AssetAdapter() {
|
||||
@Override
|
||||
public InputStream openFileAsStream(String name) {
|
||||
try {
|
||||
@ -29,7 +29,7 @@ public class ThemeTest {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
IRenderTheme t = ThemeLoader.load(VtmThemes.DEFAULT);
|
||||
|
||||
|
@ -33,7 +33,7 @@ public class SymbolRenderLayer extends ElementRenderer {
|
||||
it.billboard = false;
|
||||
|
||||
try {
|
||||
it.bitmap = CanvasAdapter.g.loadBitmapAsset("jar:symbols/cafe.png");
|
||||
it.bitmap = CanvasAdapter.getBitmapAsset("jar:symbols/cafe.png");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
|
@ -39,6 +39,6 @@ public enum VtmThemes implements ThemeFile {
|
||||
|
||||
@Override
|
||||
public InputStream getRenderThemeAsStream() {
|
||||
return AssetAdapter.g.openFileAsStream(mPath);
|
||||
return AssetAdapter.readFileAsStream(mPath);
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,6 @@ public class GwtGdxGraphics extends CanvasAdapter {
|
||||
|
||||
public static boolean NO_STROKE_TEXT = false;
|
||||
|
||||
public static final GwtGdxGraphics INSTANCE = new GwtGdxGraphics();
|
||||
static final Context2d ctx;
|
||||
|
||||
static {
|
||||
@ -47,33 +46,32 @@ public class GwtGdxGraphics extends CanvasAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap decodeBitmap(InputStream in) {
|
||||
public Bitmap decodeBitmapImpl(InputStream in) {
|
||||
//ImageData data = new ImageData();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap loadBitmapAsset(String fileName) {
|
||||
public Bitmap loadBitmapAssetImpl(String fileName) {
|
||||
return new GwtBitmap(fileName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Paint getPaint() {
|
||||
public Paint newPaintImpl() {
|
||||
return new GwtPaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap getBitmap(int width, int height, int format) {
|
||||
public Bitmap newBitmapImpl(int width, int height, int format) {
|
||||
return new GwtBitmap(width, height, format);
|
||||
}
|
||||
|
||||
@Override
|
||||
public org.oscim.backend.canvas.Canvas getCanvas() {
|
||||
public org.oscim.backend.canvas.Canvas newCanvasImpl() {
|
||||
return new GwtCanvas();
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
g = INSTANCE;
|
||||
CanvasAdapter.init(new GwtGdxGraphics());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,20 +27,21 @@ import java.io.InputStreamReader;
|
||||
public abstract class AssetAdapter {
|
||||
|
||||
/** The instance provided by backend */
|
||||
public static AssetAdapter g;
|
||||
static AssetAdapter g;
|
||||
|
||||
/**
|
||||
* Open file from asset path as stream.
|
||||
*
|
||||
* @param name the name
|
||||
* @return the input stream
|
||||
*/
|
||||
public abstract InputStream openFileAsStream(String name);
|
||||
protected abstract InputStream openFileAsStream(String file);
|
||||
|
||||
public String openTextFile(String name) {
|
||||
public static InputStream readFileAsStream(String file) {
|
||||
return g.openFileAsStream(file);
|
||||
}
|
||||
|
||||
public static String readTextFile(String file) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
InputStream is = g.openFileAsStream(name);
|
||||
InputStream is = g.openFileAsStream(file);
|
||||
if (is == null)
|
||||
return null;
|
||||
|
||||
@ -55,5 +56,10 @@ public abstract class AssetAdapter {
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
|
||||
}
|
||||
|
||||
public static void init(AssetAdapter adapter) {
|
||||
g = adapter;
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ import org.oscim.backend.canvas.Paint;
|
||||
public abstract class CanvasAdapter {
|
||||
|
||||
/** The instance provided by backend */
|
||||
public static CanvasAdapter g;
|
||||
static CanvasAdapter g;
|
||||
|
||||
/** The dpi. */
|
||||
public static float dpi = 240;
|
||||
@ -42,14 +42,22 @@ public abstract class CanvasAdapter {
|
||||
*
|
||||
* @return the canvas
|
||||
*/
|
||||
public abstract Canvas getCanvas();
|
||||
protected abstract Canvas newCanvasImpl();
|
||||
|
||||
public static Canvas newCanvas() {
|
||||
return g.newCanvasImpl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Paint.
|
||||
*
|
||||
* @return the paint
|
||||
*/
|
||||
public abstract Paint getPaint();
|
||||
protected abstract Paint newPaintImpl();
|
||||
|
||||
public static Paint newPaint() {
|
||||
return g.newPaintImpl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create {@link Bitmap} with given dimensions.
|
||||
@ -59,7 +67,11 @@ public abstract class CanvasAdapter {
|
||||
* @param format the format
|
||||
* @return the bitmap
|
||||
*/
|
||||
public abstract Bitmap getBitmap(int width, int height, int format);
|
||||
protected abstract Bitmap newBitmapImpl(int width, int height, int format);
|
||||
|
||||
public static Bitmap newBitmap(int width, int height, int format) {
|
||||
return g.newBitmapImpl(width, height, format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create {@link Bitmap} from InputStream.
|
||||
@ -67,7 +79,11 @@ public abstract class CanvasAdapter {
|
||||
* @param inputStream the input stream
|
||||
* @return the bitmap
|
||||
*/
|
||||
public abstract Bitmap decodeBitmap(InputStream inputStream);
|
||||
protected abstract Bitmap decodeBitmapImpl(InputStream inputStream);
|
||||
|
||||
public static Bitmap decodeBitmap(InputStream inputStream) {
|
||||
return g.decodeBitmapImpl(inputStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create {@link Bitmap} from bundled assets.
|
||||
@ -75,7 +91,11 @@ public abstract class CanvasAdapter {
|
||||
* @param fileName the file name
|
||||
* @return the bitmap
|
||||
*/
|
||||
public abstract Bitmap loadBitmapAsset(String fileName);
|
||||
protected abstract Bitmap loadBitmapAssetImpl(String fileName);
|
||||
|
||||
public static Bitmap getBitmapAsset(String fileName) {
|
||||
return g.loadBitmapAssetImpl(fileName);
|
||||
}
|
||||
|
||||
protected static Bitmap createBitmap(String src) throws IOException {
|
||||
if (src == null || src.length() == 0) {
|
||||
@ -89,8 +109,12 @@ public abstract class CanvasAdapter {
|
||||
return null;
|
||||
}
|
||||
|
||||
Bitmap bitmap = CanvasAdapter.g.decodeBitmap(inputStream);
|
||||
Bitmap bitmap = decodeBitmap(inputStream);
|
||||
inputStream.close();
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
protected static void init(CanvasAdapter adapter) {
|
||||
g = adapter;
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ public abstract class GLShader {
|
||||
|
||||
public static int loadShader(String file) {
|
||||
String path = "shaders/" + file + ".glsl";
|
||||
String vs = AssetAdapter.g.openTextFile(path);
|
||||
String vs = AssetAdapter.readTextFile(path);
|
||||
|
||||
if (vs == null)
|
||||
throw new IllegalArgumentException("shader file not found: " + path);
|
||||
|
@ -42,7 +42,7 @@ public class TextLayer extends TextureLayer {
|
||||
|
||||
public TextLayer() {
|
||||
super(RenderElement.SYMBOL);
|
||||
mCanvas = CanvasAdapter.g.getCanvas();
|
||||
mCanvas = CanvasAdapter.newCanvas();
|
||||
fixed = true;
|
||||
}
|
||||
|
||||
|
@ -186,7 +186,7 @@ public class TextureItem extends Inlist<TextureItem> {
|
||||
synchronized (mBitmaps) {
|
||||
int size = mBitmaps.size();
|
||||
if (size == 0)
|
||||
t.bitmap = CanvasAdapter.g.getBitmap(mWidth, mHeight, 0);
|
||||
t.bitmap = CanvasAdapter.newBitmap(mWidth, mHeight, 0);
|
||||
else {
|
||||
t.bitmap = mBitmaps.remove(size - 1);
|
||||
t.bitmap.eraseColor(Color.TRANSPARENT);
|
||||
|
@ -531,7 +531,7 @@ public class XmlThemeBuilder extends DefaultHandler {
|
||||
|
||||
if (src != null) {
|
||||
try {
|
||||
Bitmap bitmap = CanvasAdapter.g.loadBitmapAsset(src);
|
||||
Bitmap bitmap = CanvasAdapter.getBitmapAsset(src);
|
||||
if (bitmap != null)
|
||||
b.texture = new TextureItem(bitmap, true);
|
||||
} catch (Exception e) {
|
||||
@ -566,7 +566,7 @@ public class XmlThemeBuilder extends DefaultHandler {
|
||||
}
|
||||
validateExists("img", img, elementName);
|
||||
|
||||
Bitmap bitmap = CanvasAdapter.g.loadBitmapAsset(IMG_PATH + img);
|
||||
Bitmap bitmap = CanvasAdapter.getBitmapAsset(IMG_PATH + img);
|
||||
mTextureAtlas = new TextureAtlas(bitmap);
|
||||
}
|
||||
|
||||
|
@ -165,7 +165,7 @@ public final class TextStyle extends RenderStyle {
|
||||
this.priority = tb.priority;
|
||||
this.texture = tb.texture;
|
||||
|
||||
paint = CanvasAdapter.g.getPaint();
|
||||
paint = CanvasAdapter.newPaint();
|
||||
paint.setTextAlign(Align.CENTER);
|
||||
paint.setTypeface(tb.fontFamily, tb.fontStyle);
|
||||
|
||||
@ -173,7 +173,7 @@ public final class TextStyle extends RenderStyle {
|
||||
paint.setTextSize(tb.fontSize);
|
||||
|
||||
if (tb.strokeWidth > 0) {
|
||||
stroke = CanvasAdapter.g.getPaint();
|
||||
stroke = CanvasAdapter.newPaint();
|
||||
stroke.setStyle(Paint.Style.STROKE);
|
||||
stroke.setTextAlign(Align.CENTER);
|
||||
stroke.setTypeface(tb.fontFamily, tb.fontStyle);
|
||||
|
@ -72,7 +72,7 @@ public class BitmapTileSource extends UrlTileSource {
|
||||
public boolean decode(Tile tile, ITileDataSink sink, InputStream is)
|
||||
throws IOException {
|
||||
|
||||
Bitmap bitmap = CanvasAdapter.g.decodeBitmap(is);
|
||||
Bitmap bitmap = CanvasAdapter.decodeBitmap(is);
|
||||
if (!bitmap.isValid()) {
|
||||
log.debug("{} invalid bitmap", tile);
|
||||
return false;
|
||||
|
Loading…
x
Reference in New Issue
Block a user