Symbol rotation (#294)

This commit is contained in:
Longri 2017-02-16 13:56:35 +01:00 committed by Emux
parent 5aaa2ca96b
commit 981536595f
10 changed files with 509 additions and 35 deletions

View File

@ -77,6 +77,9 @@
<activity
android:name=".PathOverlayActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name=".RotateMarkerOverlayActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name=".S3DBMapActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />

View File

@ -0,0 +1,191 @@
/*
* Copyright 2014 Hannes Janetzek
* Copyright 2016 devemux86
* Copyright 2017 Longri
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* 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
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.android.test;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;
import org.oscim.backend.canvas.Bitmap;
import org.oscim.core.GeoPoint;
import org.oscim.event.Gesture;
import org.oscim.event.GestureListener;
import org.oscim.event.MotionEvent;
import org.oscim.layers.Layer;
import org.oscim.layers.TileGridLayer;
import org.oscim.layers.marker.ItemizedLayer;
import org.oscim.layers.marker.MarkerItem;
import org.oscim.layers.marker.MarkerSymbol;
import org.oscim.layers.marker.MarkerSymbol.HotspotPlace;
import org.oscim.map.Map;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;
import static org.oscim.android.canvas.AndroidGraphics.drawableToBitmap;
import static org.oscim.tiling.source.bitmap.DefaultSources.STAMEN_TONER;
public class RotateMarkerOverlayActivity extends BitmapTileMapActivity
implements ItemizedLayer.OnItemGestureListener<MarkerItem> {
protected static final boolean BILLBOARDS = true;
protected MarkerSymbol mFocusMarker;
private ItemizedLayer<MarkerItem> markerLayer;
public RotateMarkerOverlayActivity() {
super(STAMEN_TONER.build());
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBitmapLayer.tileRenderer().setBitmapAlpha(0.5f);
// Map events receiver
mMap.layers().add(new MapEventsReceiver(mMap));
/* directly load bitmap from resources */
Bitmap bitmap = drawableToBitmap(getResources(), R.drawable.marker_poi);
MarkerSymbol symbol;
if (BILLBOARDS)
symbol = new MarkerSymbol(bitmap, HotspotPlace.BOTTOM_CENTER);
else
symbol = new MarkerSymbol(bitmap, HotspotPlace.CENTER, false);
/* another option: use some bitmap drawable */
Drawable d = getResources().getDrawable(R.drawable.marker_focus);
if (BILLBOARDS)
mFocusMarker = new MarkerSymbol(drawableToBitmap(d), HotspotPlace.BOTTOM_CENTER);
else
mFocusMarker = new MarkerSymbol(drawableToBitmap(d), HotspotPlace.CENTER, false);
markerLayer =
new ItemizedLayer<>(mMap, new ArrayList<MarkerItem>(),
symbol, this);
mMap.layers().add(markerLayer);
List<MarkerItem> pts = new ArrayList<>();
for (double lat = -90; lat <= 90; lat += 5) {
for (double lon = -180; lon <= 180; lon += 5)
pts.add(new MarkerItem(lat + "/" + lon, "", new GeoPoint(lat, lon)));
}
markerLayer.addItems(pts);
mMap.layers().add(new TileGridLayer(mMap, getResources().getDisplayMetrics().density));
}
@Override
protected void onResume() {
super.onResume();
/* ignore saved position */
mMap.setMapPosition(0, 0, 1 << 2);
}
Timer timer;
int markerCount = 0;
@Override
public boolean onItemSingleTapUp(int index, final MarkerItem item) {
if (item.getMarker() == null) {
item.setMarker(mFocusMarker);
markerCount++;
final AtomicInteger rotValue = new AtomicInteger(0);
if (timer != null) timer.cancel();
timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
float value = (float) (rotValue.incrementAndGet() * 10);
item.setRotation(value);
if (rotValue.get() > 36) rotValue.set(0);
markerLayer.update();
mMap.updateMap(true);
}
};
timer.schedule(timerTask, 1000, 1000);
} else {
item.setMarker(null);
markerCount--;
if (timer != null && markerCount == 0) timer.cancel();
}
Toast.makeText(this, "Marker tap\n" + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onItemLongPress(int index, final MarkerItem item) {
if (item.getMarker() == null) {
item.setMarker(mFocusMarker);
markerCount++;
final AtomicInteger rotValue = new AtomicInteger(0);
if (timer != null) timer.cancel();
timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
float value = (float) (rotValue.incrementAndGet() * 10);
item.setRotation(value);
if (rotValue.get() > 36) rotValue.set(0);
markerLayer.update();
mMap.updateMap(true);
}
};
timer.schedule(timerTask, 300, 300);
} else {
item.setMarker(null);
markerCount--;
if (timer != null && markerCount == 0) timer.cancel();
}
Toast.makeText(this, "Marker long press\n" + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
protected class MapEventsReceiver extends Layer implements GestureListener {
MapEventsReceiver(Map map) {
super(map);
}
@Override
public boolean onGesture(Gesture g, MotionEvent e) {
if (g instanceof Gesture.Tap) {
GeoPoint p = mMap.viewport().fromScreenPoint(e.getX(), e.getY());
Toast.makeText(RotateMarkerOverlayActivity.this, "Map tap\n" + p, Toast.LENGTH_SHORT).show();
return true;
}
if (g instanceof Gesture.LongPress) {
GeoPoint p = mMap.viewport().fromScreenPoint(e.getX(), e.getY());
Toast.makeText(RotateMarkerOverlayActivity.this, "Map long press\n" + p, Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
}

View File

@ -2,6 +2,7 @@
* Copyright 2010, 2011, 2012, 2013 mapsforge.org
* Copyright 2013 Hannes Janetzek
* Copyright 2016 devemux86
* Copyright 2017 Longri
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
@ -50,6 +51,7 @@ public class Samples extends Activity {
linearLayout.addView(createLabel("Overlays"));
linearLayout.addView(createButton(MarkerOverlayActivity.class));
linearLayout.addView(createButton(RotateMarkerOverlayActivity.class));
linearLayout.addView(createButton(AtlasMarkerOverlayActivity.class));
linearLayout.addView(createButton(PathOverlayActivity.class));
linearLayout.addView(createButton(LineTexActivity.class));

View File

@ -0,0 +1,173 @@
/*
* Copyright 2016 devemux86
* Copyright 2017 Longri
*
* 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
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.test;
import org.oscim.backend.CanvasAdapter;
import org.oscim.backend.canvas.Bitmap;
import org.oscim.core.GeoPoint;
import org.oscim.event.Gesture;
import org.oscim.event.GestureListener;
import org.oscim.event.MotionEvent;
import org.oscim.gdx.GdxMapApp;
import org.oscim.layers.Layer;
import org.oscim.layers.TileGridLayer;
import org.oscim.layers.marker.ItemizedLayer;
import org.oscim.layers.marker.MarkerItem;
import org.oscim.layers.marker.MarkerSymbol;
import org.oscim.layers.tile.bitmap.BitmapTileLayer;
import org.oscim.map.Map;
import org.oscim.tiling.source.bitmap.DefaultSources;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;
import static org.oscim.layers.marker.MarkerSymbol.HotspotPlace;
public class RotateMarkerLayerTest extends GdxMapApp implements ItemizedLayer.OnItemGestureListener<MarkerItem> {
protected static final boolean BILLBOARDS = true;
protected MarkerSymbol mFocusMarker;
private ItemizedLayer<MarkerItem> markerLayer;
@Override
public void createLayers() {
BitmapTileLayer bitmapLayer = new BitmapTileLayer(mMap, DefaultSources.STAMEN_TONER.build());
bitmapLayer.tileRenderer().setBitmapAlpha(0.5f);
mMap.setBaseMap(bitmapLayer);
// Map events receiver
mMap.layers().add(new MapEventsReceiver(mMap));
mMap.setMapPosition(0, 0, 1 << 2);
Bitmap bitmapPoi = CanvasAdapter.decodeBitmap(getClass().getResourceAsStream("/res/marker_poi.png"));
MarkerSymbol symbol;
if (BILLBOARDS)
symbol = new MarkerSymbol(bitmapPoi, HotspotPlace.BOTTOM_CENTER);
else
symbol = new MarkerSymbol(bitmapPoi, HotspotPlace.CENTER, false);
Bitmap bitmapFocus = CanvasAdapter.decodeBitmap(getClass().getResourceAsStream("/res/marker_focus.png"));
if (BILLBOARDS)
mFocusMarker = new MarkerSymbol(bitmapFocus, HotspotPlace.BOTTOM_CENTER);
else
mFocusMarker = new MarkerSymbol(bitmapFocus, HotspotPlace.CENTER, false);
markerLayer = new ItemizedLayer<>(mMap, new ArrayList<MarkerItem>(), symbol, this);
mMap.layers().add(markerLayer);
List<MarkerItem> pts = new ArrayList<>();
for (double lat = -90; lat <= 90; lat += 5) {
for (double lon = -180; lon <= 180; lon += 5)
pts.add(new MarkerItem(lat + "/" + lon, "", new GeoPoint(lat, lon)));
}
markerLayer.addItems(pts);
mMap.layers().add(new TileGridLayer(mMap));
}
Timer timer;
int markerCount = 0;
@Override
public boolean onItemSingleTapUp(int index, final MarkerItem item) {
if (item.getMarker() == null) {
item.setMarker(mFocusMarker);
markerCount++;
final AtomicInteger rotValue = new AtomicInteger(0);
if (timer != null) timer.cancel();
timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
float value = (float) (rotValue.incrementAndGet() * 10);
item.setRotation(value);
if (rotValue.get() > 36) rotValue.set(0);
markerLayer.update();
mMap.updateMap(true);
}
};
timer.schedule(timerTask, 1000, 1000);
} else {
item.setMarker(null);
markerCount--;
if (timer != null && markerCount == 0) timer.cancel();
}
System.out.println("Marker tap " + item.getTitle());
return true;
}
@Override
public boolean onItemLongPress(int index, final MarkerItem item) {
if (item.getMarker() == null) {
item.setMarker(mFocusMarker);
markerCount++;
final AtomicInteger rotValue = new AtomicInteger(0);
if (timer != null) timer.cancel();
timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
float value = (float) (rotValue.incrementAndGet() * 10);
item.setRotation(value);
if (rotValue.get() > 36) rotValue.set(0);
markerLayer.update();
mMap.updateMap(true);
}
};
timer.schedule(timerTask, 300, 300);
} else {
item.setMarker(null);
markerCount--;
if (timer != null && markerCount == 0) timer.cancel();
}
System.out.println("Marker long press " + item.getTitle());
return true;
}
public static void main(String[] args) {
GdxMapApp.init();
GdxMapApp.run(new RotateMarkerLayerTest());
}
protected class MapEventsReceiver extends Layer implements GestureListener {
MapEventsReceiver(Map map) {
super(map);
}
@Override
public boolean onGesture(Gesture g, MotionEvent e) {
if (g instanceof Gesture.Tap) {
GeoPoint p = mMap.viewport().fromScreenPoint(e.getX(), e.getY());
System.out.println("Map tap " + p);
return true;
}
if (g instanceof Gesture.LongPress) {
GeoPoint p = mMap.viewport().fromScreenPoint(e.getX(), e.getY());
System.out.println("Map long press " + p);
return true;
}
return false;
}
}
}

View File

@ -7,6 +7,7 @@
* Copyright 2014 Hannes Janetzek
* Copyright 2016 devemux86
* Copyright 2016 Erik Duisters
* Copyright 2017 Longri
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
@ -75,4 +76,9 @@ public class MarkerItem implements MarkerInterface {
public void setMarker(MarkerSymbol marker) {
mMarker = marker;
}
public void setRotation(float rotation) {
if (mMarker != null)
mMarker.setRotation(rotation);
}
}

View File

@ -7,6 +7,7 @@
* Copyright 2013 Hannes Janetzek
* Copyright 2016 Stephan Leuschner
* Copyright 2016 devemux86
* Copyright 2017 Longri
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
@ -96,6 +97,10 @@ public abstract class MarkerLayer<Item extends MarkerInterface> extends Layer {
return mFocusedItem;
}
public void update() {
mMarkerRenderer.update();
}
/**
* TODO
* Interface definition for overlays that contain items that can be snapped

View File

@ -1,6 +1,7 @@
/*
* Copyright 2013 Hannes Janetzek
* Copyright 2016 Izumi Kawashima
* Copyright 2017 Longri
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
@ -163,9 +164,9 @@ public class MarkerRenderer extends BucketRenderer {
SymbolItem s = SymbolItem.pool.get();
if (marker.isBitmap()) {
s.set(it.x, it.y, marker.getBitmap(), true);
s.set(it.x, it.y, marker.getBitmap(), marker.rotation, true);
} else {
s.set(it.x, it.y, marker.getTextureRegion(), true);
s.set(it.x, it.y, marker.getTextureRegion(), marker.rotation, true);
}
s.offset = marker.getHotspot();
s.billboard = marker.isBillboard();

View File

@ -2,6 +2,7 @@
* Copyright 2013 Hannes Janetzek
* Copyright 2016 devemux86
* Copyright 2016 Izumi Kawashima
* Copyright 2017 Longri
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
@ -44,6 +45,8 @@ public class MarkerSymbol {
final PointF mOffset;
final boolean mBillboard;
float rotation = 0;
public MarkerSymbol(TextureRegion textureRegion, float relX, float relY) {
this(textureRegion, relX, relY, true);
}
@ -180,4 +183,12 @@ public class MarkerSymbol {
return dx >= ox && dy >= oy && dx <= ox + w && dy <= oy + h;
}
public float getRotation() {
return this.rotation;
}
public void setRotation(float rotation) {
this.rotation = rotation;
}
}

View File

@ -1,6 +1,7 @@
/*
* Copyright 2012 Hannes Janetzek
* Copyright 2016 devemux86
* Copyright 2016-2017 devemux86
* Copyright 2017 Longri
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
@ -19,6 +20,7 @@ package org.oscim.renderer.bucket;
import org.oscim.backend.canvas.Bitmap;
import org.oscim.core.PointF;
import org.oscim.renderer.GLMatrix;
import org.oscim.renderer.atlas.TextureAtlas;
import org.oscim.utils.pool.Inlist;
import org.slf4j.Logger;
@ -36,6 +38,10 @@ public final class SymbolBucket extends TextureBucket {
private TextureItem prevTextures;
private List<SymbolItem> mSymbols = new List<SymbolItem>();
private final float[] points = new float[8];
private final GLMatrix rotationMatrix = new GLMatrix();
private final GLMatrix translateMatrix = new GLMatrix();
public SymbolBucket() {
super(RenderBucket.SYMBOL);
fixed = true;
@ -120,47 +126,108 @@ public final class SymbolBucket extends TextureBucket {
PointF prevOffset = null;
short x1 = 0, y1 = 0, x2 = 0, y2 = 0;
float minX, minY, maxX, maxY;
/* add symbol items referencing the same bitmap */
for (SymbolItem prev = it; it != null; it = it.next) {
if (it.rotation == 0) { // without rotation
if (prev.bitmap != null && prev.bitmap != it.bitmap)
break;
if (prev.bitmap != null && prev.bitmap != it.bitmap)
break;
if (prev.texRegion != null && prev.texRegion != it.texRegion)
break;
if (prev.texRegion != null && prev.texRegion != it.texRegion)
break;
if (it == prev || it.offset != prevOffset) {
prevOffset = it.offset;
if (it.offset == null) {
float hw = width / 2f;
float hh = height / 2f;
if (it == prev || it.offset != prevOffset) {
prevOffset = it.offset;
if (it.offset == null) {
float hw = width / 2f;
float hh = height / 2f;
x1 = (short) (SCALE * (-hw));
x2 = (short) (SCALE * (hw));
y1 = (short) (SCALE * (hh));
y2 = (short) (SCALE * (-hh));
} else {
float hw = (float) (it.offset.x * width);
float hh = (float) (it.offset.y * height);
x1 = (short) (SCALE * (-hw));
x2 = (short) (SCALE * (width - hw));
y1 = (short) (SCALE * (height - hh));
y2 = (short) (SCALE * (-hh));
x1 = (short) (SCALE * (-hw));
x2 = (short) (SCALE * (hw));
y1 = (short) (SCALE * (hh));
y2 = (short) (SCALE * (-hh));
} else {
float hw = (float) (it.offset.x * width);
float hh = (float) (it.offset.y * height);
x1 = (short) (SCALE * (-hw));
x2 = (short) (SCALE * (width - hw));
y1 = (short) (SCALE * (height - hh));
y2 = (short) (SCALE * (-hh));
}
}
/* add vertices */
short tx = (short) ((int) (SCALE * it.x) & LBIT_MASK
| (it.billboard ? 1 : 0));
short ty = (short) (SCALE * it.y);
vertexItems.add(tx, ty, x1, y1, u1, v2);
vertexItems.add(tx, ty, x1, y2, u1, v1);
vertexItems.add(tx, ty, x2, y1, u2, v2);
vertexItems.add(tx, ty, x2, y2, u2, v1);
} else { // with rotation
if (prev.bitmap != null && prev.bitmap != it.bitmap && prev.rotation != it.rotation)
break;
if (prev.texRegion != null && prev.texRegion != it.texRegion && prev.rotation != it.rotation)
break;
short offsetX, offsetY;
if (it.offset == null) {
offsetX = 0;
offsetY = 0;
} else {
offsetX = (short) (((width / 2f) - (it.offset.x * width)) * SCALE);
offsetY = (short) (((height / 2f) - (it.offset.y * height)) * SCALE);
}
float hw = width / 2f;
float hh = height / 2f;
minX = (SCALE * (-hw));
maxX = (SCALE * (hw));
minY = (SCALE * (hh));
maxY = (SCALE * (-hh));
// target drawing rectangle
{ // lower-left
points[0] = minX;
points[1] = minY;
}
{ // upper-left
points[2] = minX;
points[3] = maxY;
}
{ // upper-right
points[6] = maxX;
points[7] = maxY;
}
{ // lower-right
points[4] = maxX;
points[5] = minY;
}
if (it.rotation != 0) {
rotationMatrix.setRotation(it.rotation, 0, 0, 1);
rotationMatrix.prj2D(points, 0, 4);
}
/* add vertices */
short tx = (short) (((int) (SCALE * it.x) & LBIT_MASK
| (it.billboard ? 1 : 0)) + offsetX);
short ty = (short) ((SCALE * it.y) + offsetY);
vertexItems.add(tx, ty, points[0], points[1], u1, v2); // lower-left
vertexItems.add(tx, ty, points[2], points[3], u1, v1); // upper-left
vertexItems.add(tx, ty, points[4], points[5], u2, v2); // upper-right
vertexItems.add(tx, ty, points[6], points[7], u2, v1); // lower-right
}
/* add vertices */
short tx = (short) ((int) (SCALE * it.x) & LBIT_MASK
| (it.billboard ? 1 : 0));
short ty = (short) (SCALE * it.y);
vertexItems.add(tx, ty, x1, y1, u1, v2);
vertexItems.add(tx, ty, x1, y2, u1, v1);
vertexItems.add(tx, ty, x2, y1, u2, v2);
vertexItems.add(tx, ty, x2, y2, u2, v1);
/* six elements used to draw the four vertices */
t.indices += TextureBucket.INDICES_PER_SPRITE;
}

View File

@ -1,5 +1,6 @@
/*
* Copyright 2012 Hannes Janetzek
* Copyright 2017 Longri
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
@ -37,6 +38,7 @@ public class SymbolItem extends Inlist<SymbolItem> {
it.bitmap = null;
it.texRegion = null;
it.offset = null;
it.rotation = 0;
return true;
}
};
@ -48,12 +50,24 @@ public class SymbolItem extends Inlist<SymbolItem> {
public TextureRegion texRegion;
public Bitmap bitmap;
public PointF offset;
public float rotation;
public void set(float x, float y, TextureRegion texture, float rotation, boolean billboard) {
set(x, y, texture, billboard);
this.rotation = rotation;
}
public void set(float x, float y, TextureRegion texture, boolean billboard) {
this.x = x;
this.y = y;
this.texRegion = texture;
this.billboard = billboard;
this.rotation = 0;
}
public void set(float x, float y, Bitmap bitmap, float rotation, boolean billboard) {
set(x, y, bitmap, billboard);
this.rotation = rotation;
}
public void set(float x, float y, Bitmap bitmap, boolean billboard) {
@ -61,6 +75,7 @@ public class SymbolItem extends Inlist<SymbolItem> {
this.y = y;
this.bitmap = bitmap;
this.billboard = billboard;
this.rotation = 0;
}
}