From 4c10d5848b55ad6c6dafd42133a015482cf8bccf Mon Sep 17 00:00:00 2001 From: Emux Date: Thu, 17 Oct 2019 15:00:53 +0300 Subject: [PATCH] Location renderers: allow fixed center position --- vtm/src/org/oscim/map/ViewController.java | 37 +++++++++++++-- .../org/oscim/renderer/LocationRenderer.java | 45 ++++++++++++------- .../renderer/LocationTextureRenderer.java | 40 +++++++++++------ 3 files changed, 87 insertions(+), 35 deletions(-) diff --git a/vtm/src/org/oscim/map/ViewController.java b/vtm/src/org/oscim/map/ViewController.java index 1295f92f..beddc15f 100644 --- a/vtm/src/org/oscim/map/ViewController.java +++ b/vtm/src/org/oscim/map/ViewController.java @@ -1,4 +1,5 @@ /* + * Copyright 2014 Hannes Janetzek * Copyright 2016-2019 devemux86 * Copyright 2017 Luca Osten * Copyright 2018 Izumi Kawashima @@ -74,21 +75,49 @@ public class ViewController extends Viewport { } /** - * Get pivot horizontal / vertical relative to view center. + * Get pivot horizontal / vertical relative to view center in [-1, 1]. * e.g. pivotY 0.5 is usually preferred for navigation, moving center to 25% of view height. - * Range is [-1, 1]. */ public float[] getMapViewCenter() { return new float[]{mPivotX, mPivotY}; } /** - * Set pivot horizontal / vertical relative to view center. + * Get pivot horizontal relative to view center in [-1, 1]. + */ + public float getMapViewCenterX() { + return mPivotX; + } + + /** + * Get pivot vertical relative to view center in [-1, 1]. + * e.g. pivotY 0.5 is usually preferred for navigation, moving center to 25% of view height. + */ + public float getMapViewCenterY() { + return mPivotY; + } + + /** + * Set pivot horizontal / vertical relative to view center in [-1, 1]. * e.g. pivotY 0.5 is usually preferred for navigation, moving center to 25% of view height. - * Range is [-1, 1]. */ public void setMapViewCenter(float pivotX, float pivotY) { + setMapViewCenterX(pivotX); + setMapViewCenterY(pivotY); + } + + /** + * Set pivot horizontal relative to view center in [-1, 1]. + */ + public void setMapViewCenterX(float pivotX) { mPivotX = FastMath.clamp(pivotX, -1, 1) * 0.5f; + } + + /** + * Set pivot horizontal / vertical relative to view center in [-1, 1]. + * e.g. pivotY 0.5 is usually preferred for navigation, moving center to 25% of view height. + */ + public void setMapViewCenterY(float pivotY) { mPivotY = FastMath.clamp(pivotY, -1, 1) * 0.5f; } diff --git a/vtm/src/org/oscim/renderer/LocationRenderer.java b/vtm/src/org/oscim/renderer/LocationRenderer.java index 5922fc84..a8861649 100644 --- a/vtm/src/org/oscim/renderer/LocationRenderer.java +++ b/vtm/src/org/oscim/renderer/LocationRenderer.java @@ -66,6 +66,7 @@ public class LocationRenderer extends LayerRenderer { private boolean mRunAnim; private boolean mAnimate = true; private long mAnimStart; + private boolean mCenter; private Callback mCallback; private final float[] mColors = new float[4]; @@ -97,6 +98,10 @@ public class LocationRenderer extends LayerRenderer { mCallback = callback; } + public void setCenter(boolean center) { + mCenter = center; + } + public void setColor(int color) { float a = Color.aToFloat(color); mColors[0] = a * Color.rToFloat(color); @@ -168,33 +173,39 @@ public class LocationRenderer extends LayerRenderer { return; } - /*if (!v.changed() && isReady()) - return;*/ + /*if (!v.changed() && isReady()) + return;*/ setReady(true); int width = mMap.getWidth(); int height = mMap.getHeight(); - // clamp location to a position that can be - // savely translated to screen coordinates - v.getBBox(mBBox, 0); + double x, y; + if (mCenter) { + x = (width >> 1) + width * mMap.viewport().getMapViewCenterX(); + y = (height >> 1) + height * mMap.viewport().getMapViewCenterY(); + } else { + // clamp location to a position that can be + // safely translated to screen coordinates + v.getBBox(mBBox, 0); - double x = mLocation.x; - double y = mLocation.y; + x = mLocation.x; + y = mLocation.y; - if (!mBBox.contains(mLocation)) { - x = FastMath.clamp(x, mBBox.xmin, mBBox.xmax); - y = FastMath.clamp(y, mBBox.ymin, mBBox.ymax); + if (!mBBox.contains(mLocation)) { + x = FastMath.clamp(x, mBBox.xmin, mBBox.xmax); + y = FastMath.clamp(y, mBBox.ymin, mBBox.ymax); + } + + // get position of Location in pixel relative to + // screen center + v.toScreenPoint(x, y, mScreenPoint); + + x = mScreenPoint.x + (width >> 1); + y = mScreenPoint.y + (height >> 1); } - // get position of Location in pixel relative to - // screen center - v.toScreenPoint(x, y, mScreenPoint); - - x = mScreenPoint.x + width / 2; - y = mScreenPoint.y + height / 2; - // clip position to screen boundaries int visible = 0; diff --git a/vtm/src/org/oscim/renderer/LocationTextureRenderer.java b/vtm/src/org/oscim/renderer/LocationTextureRenderer.java index 80a3c3cf..1c522978 100644 --- a/vtm/src/org/oscim/renderer/LocationTextureRenderer.java +++ b/vtm/src/org/oscim/renderer/LocationTextureRenderer.java @@ -1,6 +1,6 @@ /* * Copyright 2017-2018 Longri - * Copyright 2018 devemux86 + * Copyright 2018-2019 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 @@ -98,6 +98,7 @@ public class LocationTextureRenderer extends BucketRenderer { private boolean runAnim; private boolean animate = true; private long animStart; + private boolean center; private boolean update; private float bearing; @@ -124,6 +125,10 @@ public class LocationTextureRenderer extends BucketRenderer { this.billboard = billboard; } + public void setCenter(boolean center) { + this.center = center; + } + public void setIndicatorColor(int color) { this.viewShedColor = color; } @@ -194,23 +199,30 @@ public class LocationTextureRenderer extends BucketRenderer { int width = map.getWidth(); int height = map.getHeight(); - v.getBBox(boundingBox, 0); - double x = mapPoint.x; - double y = mapPoint.y; + double x, y; + if (center) { + x = (width >> 1) + width * map.viewport().getMapViewCenterX(); + y = (height >> 1) + height * map.viewport().getMapViewCenterY(); + } else { + v.getBBox(boundingBox, 0); - if (!boundingBox.contains(mapPoint)) { - x = FastMath.clamp(x, boundingBox.xmin, boundingBox.xmax); - y = FastMath.clamp(y, boundingBox.ymin, boundingBox.ymax); + x = mapPoint.x; + y = mapPoint.y; + + if (!boundingBox.contains(mapPoint)) { + x = FastMath.clamp(x, boundingBox.xmin, boundingBox.xmax); + y = FastMath.clamp(y, boundingBox.ymin, boundingBox.ymax); + } + + // get position of Location in pixel relative to + // screen center + v.toScreenPoint(x, y, screenPoint); + + x = screenPoint.x + width / 2; + y = screenPoint.y + height / 2; } - // get position of Location in pixel relative to - // screen center - v.toScreenPoint(x, y, screenPoint); - - x = screenPoint.x + width / 2; - y = screenPoint.y + height / 2; - // clip position to screen boundaries int visible = 0;