Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

Commit

Permalink
refactor: hide backend Adapter handles
Browse files Browse the repository at this point in the history
- static methods for backend adapters
  • Loading branch information
hjanetzek committed Sep 11, 2014
1 parent 84968d2 commit 95c9f47
Show file tree
Hide file tree
Showing 19 changed files with 84 additions and 58 deletions.
Expand Up @@ -33,10 +33,9 @@
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) {
Expand All @@ -52,22 +51,22 @@ private AndroidGraphics() {
}

@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();
}

Expand Down Expand Up @@ -100,7 +99,7 @@ public static MarkerSymbol makeMarker(Resources res, int id, HotspotPlace place)
}

@Override
public Bitmap loadBitmapAsset(String fileName) {
public Bitmap loadBitmapAssetImpl(String fileName) {
try {
return createBitmap(fileName);
} catch (IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion vtm-android/src/org/oscim/android/AndroidAssets.java
Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions vtm-android/src/org/oscim/android/canvas/AndroidGraphics.java
Expand Up @@ -34,7 +34,7 @@
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) {
Expand All @@ -46,12 +46,12 @@ private AndroidGraphics() {
}

@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) {
Expand All @@ -61,17 +61,17 @@ public Bitmap loadBitmapAsset(String fileName) {
}

@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();
}

Expand Down
13 changes: 6 additions & 7 deletions vtm-desktop/src/org/oscim/awt/AwtGraphics.java
Expand Up @@ -30,28 +30,27 @@
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() {
// do nothing
}

@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();
}

Expand Down Expand Up @@ -87,7 +86,7 @@ static synchronized float getTextWidth(FontMetrics fm, String text) {
}

@Override
public Bitmap decodeBitmap(InputStream inputStream) {
public Bitmap decodeBitmapImpl(InputStream inputStream) {
try {
return new AwtBitmap(inputStream);
} catch (IOException e) {
Expand All @@ -97,7 +96,7 @@ public Bitmap decodeBitmap(InputStream inputStream) {
}

@Override
public Bitmap loadBitmapAsset(String fileName) {
public Bitmap loadBitmapAssetImpl(String fileName) {
try {
return createBitmap(fileName);
} catch (IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion vtm-gdx/src/org/oscim/gdx/GdxAssets.java
Expand Up @@ -46,6 +46,6 @@ public InputStream openFileAsStream(String fileName) {
}

public static void init(String path) {
g = new GdxAssets(path);
AssetAdapter.init(new GdxAssets(path));
}
}
2 changes: 1 addition & 1 deletion vtm-jeo/src/org/oscim/layers/JeoTileSource.java
Expand Up @@ -47,7 +47,7 @@ public void query(MapTile tile, ITileDataSink sink) {
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);
Expand Down
2 changes: 1 addition & 1 deletion vtm-playground/src/org/oscim/test/SpriteManager.java
Expand Up @@ -48,7 +48,7 @@ public Sprite(T i, TextureAtlas a, Rect r) {

Sprite items;

protected final Canvas mCanvas = CanvasAdapter.g.getCanvas();
protected final Canvas mCanvas = CanvasAdapter.newCanvas();
protected TextureItem mTexture;

public SpriteManager() {
Expand Down
4 changes: 2 additions & 2 deletions vtm-playground/src/org/oscim/test/ThemeTest.java
Expand Up @@ -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 {
Expand All @@ -29,7 +29,7 @@ public InputStream openFileAsStream(String name) {
return null;
}
}
};
});

IRenderTheme t = ThemeLoader.load(VtmThemes.DEFAULT);

Expand Down
Expand Up @@ -33,7 +33,7 @@ public SymbolRenderLayer() {
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();

Expand Down
2 changes: 1 addition & 1 deletion vtm-themes/src/org/oscim/theme/VtmThemes.java
Expand Up @@ -39,6 +39,6 @@ private VtmThemes(String path) {

@Override
public InputStream getRenderThemeAsStream() {
return AssetAdapter.g.openFileAsStream(mPath);
return AssetAdapter.readFileAsStream(mPath);
}
}
14 changes: 6 additions & 8 deletions vtm-web/src/org/oscim/gdx/client/GwtGdxGraphics.java
Expand Up @@ -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 {
Expand All @@ -47,33 +46,32 @@ public static synchronized float getTextWidth(String text, String font) {
}

@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());
}

}
20 changes: 13 additions & 7 deletions vtm/src/org/oscim/backend/AssetAdapter.java
Expand Up @@ -27,20 +27,21 @@
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;

Expand All @@ -55,5 +56,10 @@ public String openTextFile(String name) {
}

return sb.toString();

}

public static void init(AssetAdapter adapter) {
g = adapter;
}
}
38 changes: 31 additions & 7 deletions vtm/src/org/oscim/backend/CanvasAdapter.java
Expand Up @@ -29,7 +29,7 @@
public abstract class CanvasAdapter {

/** The instance provided by backend */
public static CanvasAdapter g;
static CanvasAdapter g;

/** The dpi. */
public static float dpi = 240;
Expand All @@ -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.
Expand All @@ -59,23 +67,35 @@ 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.
*
* @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.
*
* @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) {
Expand All @@ -89,8 +109,12 @@ protected static Bitmap createBitmap(String src) throws IOException {
return null;
}

Bitmap bitmap = CanvasAdapter.g.decodeBitmap(inputStream);
Bitmap bitmap = decodeBitmap(inputStream);
inputStream.close();
return bitmap;
}

protected static void init(CanvasAdapter adapter) {
g = adapter;
}
}
2 changes: 1 addition & 1 deletion vtm/src/org/oscim/renderer/GLShader.java
Expand Up @@ -45,7 +45,7 @@ public boolean useProgram() {

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);
Expand Down

0 comments on commit 95c9f47

Please sign in to comment.