From faada6625fe1b84dfac7ba9668951323d76fc6ea Mon Sep 17 00:00:00 2001 From: Emux Date: Thu, 21 Jul 2016 12:13:09 +0300 Subject: [PATCH] Screen position for bitmap renderering, closes #87 --- .../oscim/android/test/SimpleMapActivity.java | 10 +++- .../src/org/oscim/android/MapScaleBar.java | 10 ++-- .../org/oscim/renderer/BitmapRenderer.java | 29 +++++------ vtm/src/org/oscim/renderer/GLViewport.java | 49 +++++++++++++++++-- 4 files changed, 73 insertions(+), 25 deletions(-) diff --git a/vtm-android-example/src/org/oscim/android/test/SimpleMapActivity.java b/vtm-android-example/src/org/oscim/android/test/SimpleMapActivity.java index be6f9199..90dac972 100644 --- a/vtm-android-example/src/org/oscim/android/test/SimpleMapActivity.java +++ b/vtm-android-example/src/org/oscim/android/test/SimpleMapActivity.java @@ -1,5 +1,6 @@ /* * Copyright 2013 Hannes Janetzek + * Copyright 2016 devemux86 * * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * @@ -19,11 +20,14 @@ package org.oscim.android.test; import android.os.Bundle; import org.oscim.android.MapScaleBar; +import org.oscim.backend.CanvasAdapter; import org.oscim.core.MapPosition; import org.oscim.core.MercatorProjection; import org.oscim.layers.tile.buildings.BuildingLayer; import org.oscim.layers.tile.vector.labeling.LabelLayer; import org.oscim.map.Layers; +import org.oscim.renderer.BitmapRenderer; +import org.oscim.renderer.GLViewport; import org.oscim.theme.IRenderTheme; import org.oscim.theme.ThemeLoader; import org.oscim.theme.VtmThemes; @@ -37,7 +41,11 @@ public class SimpleMapActivity extends BaseMapActivity { Layers layers = mMap.layers(); layers.add(new BuildingLayer(mMap, mBaseLayer)); layers.add(new LabelLayer(mMap, mBaseLayer)); - layers.add(new MapScaleBar(mMapView)); + + MapScaleBar mapScaleBar = new MapScaleBar(mMapView); + ((BitmapRenderer) mapScaleBar.getRenderer()).setPosition(GLViewport.Position.BOTTOM_LEFT); + ((BitmapRenderer) mapScaleBar.getRenderer()).setOffset(5 * CanvasAdapter.dpi / 160, 0); + layers.add(mapScaleBar); mMap.setTheme(VtmThemes.DEFAULT); } diff --git a/vtm-android/src/org/oscim/android/MapScaleBar.java b/vtm-android/src/org/oscim/android/MapScaleBar.java index 4ead3521..75e14fca 100644 --- a/vtm-android/src/org/oscim/android/MapScaleBar.java +++ b/vtm-android/src/org/oscim/android/MapScaleBar.java @@ -1,8 +1,7 @@ -package org.oscim.android; - /* * Copyright 2010, 2011, 2012 mapsforge.org * Copyright 2013 Hannes Janetzek + * Copyright 2016 devemux86 * * This program is free software: you can redistribute it and/or modify it under the * terms of the GNU Lesser General Public License as published by the Free Software @@ -15,6 +14,7 @@ package org.oscim.android; * You should have received a copy of the GNU Lesser General Public License along with * this program. If not, see . */ +package org.oscim.android; import android.graphics.Bitmap; import android.graphics.Canvas; @@ -95,11 +95,7 @@ public class MapScaleBar extends Layer implements UpdateListener { mRedrawNeeded = true; mRenderer = mBitmapLayer = new BitmapRenderer(); mLayerBitmap = new AndroidBitmap(mBitmap); - mBitmapLayer.setBitmap(mLayerBitmap, - BITMAP_WIDTH, - BITMAP_HEIGHT, - (int) (BITMAP_WIDTH * 1.2f), - (int) (BITMAP_HEIGHT * 1.2f)); + mBitmapLayer.setBitmap(mLayerBitmap, BITMAP_WIDTH, BITMAP_HEIGHT); } @Override diff --git a/vtm/src/org/oscim/renderer/BitmapRenderer.java b/vtm/src/org/oscim/renderer/BitmapRenderer.java index c2c67de5..547780ce 100644 --- a/vtm/src/org/oscim/renderer/BitmapRenderer.java +++ b/vtm/src/org/oscim/renderer/BitmapRenderer.java @@ -1,6 +1,7 @@ /* * Copyright 2013 Hannes Janetzek * Copyright 2016 Andrey Novikov + * Copyright 2016 devemux86 * * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * @@ -32,26 +33,26 @@ public class BitmapRenderer extends BucketRenderer { private int mHeight; private boolean initialized; private boolean mUpdateBitmap; - private boolean center; - private int xOffset; - private int yOffset; + private GLViewport.Position position = GLViewport.Position.TOP_LEFT; + private float xOffset, yOffset; /** - * @param bitmap with dimension being power of two - * @param srcWidth TODO width used - * @param srcHeight TODO height used + * @param bitmap with dimension being power of two + * @param width width used + * @param height height used */ - public synchronized void setBitmap(Bitmap bitmap, - int srcWidth, int srcHeight, - int targetWidth, int targetHeight) { - mWidth = targetWidth; - mHeight = targetHeight; + public synchronized void setBitmap(Bitmap bitmap, int width, int height) { mBitmap = bitmap; + mWidth = width; + mHeight = height; initialized = false; } - public synchronized void setDrawOffset(boolean center, int xOffset, int yOffset) { - this.center = center; + public synchronized void setPosition(GLViewport.Position position) { + this.position = position; + } + + public synchronized void setOffset(float xOffset, float yOffset) { this.xOffset = xOffset; this.yOffset = yOffset; } @@ -90,7 +91,7 @@ public class BitmapRenderer extends BucketRenderer { @Override public synchronized void render(GLViewport v) { - v.setScreenOffset(center, xOffset, yOffset, 8); + v.useScreenCoordinates(mWidth, mHeight, position, xOffset, yOffset, 8); BitmapBucket.Renderer.draw(buckets.get(), v, 1, 1); } } diff --git a/vtm/src/org/oscim/renderer/GLViewport.java b/vtm/src/org/oscim/renderer/GLViewport.java index 4673026b..6094d46e 100644 --- a/vtm/src/org/oscim/renderer/GLViewport.java +++ b/vtm/src/org/oscim/renderer/GLViewport.java @@ -1,6 +1,7 @@ /* * Copyright 2013 Hannes Janetzek * Copyright 2016 Andrey Novikov + * Copyright 2016 devemux86 * * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * @@ -23,6 +24,10 @@ import org.oscim.map.Viewport; public class GLViewport extends Viewport { + public enum Position { + TOP_LEFT, TOP_CENTER, TOP_RIGHT, CENTER_LEFT, CENTER, CENTER_RIGHT, BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT + } + /** * Do not modify! */ @@ -65,10 +70,48 @@ public class GLViewport extends Viewport { /** * Set MVP offset in screen pixel coordinates */ - public void setScreenOffset(boolean center, int xOffset, int yOffset, float scale) { + public void useScreenCoordinates(int width, int height, Position position, float xOffset, float yOffset, float scale) { float invScale = 1f / scale; - float x = center ? xOffset : -mWidth / 2 + xOffset; - float y = center ? yOffset : -mHeight / 2 + yOffset; + float x = 0; + float y = 0; + switch (position) { + case TOP_LEFT: + x = -mWidth * 0.5f + xOffset; + y = -mHeight * 0.5f + yOffset; + break; + case TOP_CENTER: + x = -width * 0.5f + xOffset; + y = -mHeight * 0.5f + yOffset; + break; + case TOP_RIGHT: + x = mWidth * 0.5f - width - xOffset; + y = -mHeight * 0.5f + yOffset; + break; + case CENTER_LEFT: + x = -mWidth * 0.5f + xOffset; + y = -height * 0.5f + yOffset; + break; + case CENTER: + x = -width * 0.5f + xOffset; + y = -height * 0.5f + yOffset; + break; + case CENTER_RIGHT: + x = mWidth * 0.5f - width - xOffset; + y = -height * 0.5f + yOffset; + break; + case BOTTOM_LEFT: + x = -mWidth * 0.5f + xOffset; + y = mHeight * 0.5f - height - yOffset; + break; + case BOTTOM_CENTER: + x = -width * 0.5f + xOffset; + y = mHeight * 0.5f - height - yOffset; + break; + case BOTTOM_RIGHT: + x = mWidth * 0.5f - width - xOffset; + y = mHeight * 0.5f - height - yOffset; + break; + } mvp.setTransScale(x, y, invScale); mvp.multiplyLhs(proj); }