From 418329441ff9bbdbca7aa3e20e647a99bb1db667 Mon Sep 17 00:00:00 2001
From: Andrey Novikov <novikov@gmail.com>
Date: Tue, 19 Jul 2016 12:24:37 +0300
Subject: [PATCH] Set screen offset for bitmap rendering

---
 .../org/oscim/renderer/BitmapRenderer.java    | 12 +++++++-
 vtm/src/org/oscim/renderer/GLViewport.java    | 28 +++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/vtm/src/org/oscim/renderer/BitmapRenderer.java b/vtm/src/org/oscim/renderer/BitmapRenderer.java
index 8ad96af0..c2c67de5 100644
--- a/vtm/src/org/oscim/renderer/BitmapRenderer.java
+++ b/vtm/src/org/oscim/renderer/BitmapRenderer.java
@@ -1,5 +1,6 @@
 /*
  * Copyright 2013 Hannes Janetzek
+ * Copyright 2016 Andrey Novikov
  *
  * This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
  *
@@ -31,6 +32,9 @@ public class BitmapRenderer extends BucketRenderer {
     private int mHeight;
     private boolean initialized;
     private boolean mUpdateBitmap;
+    private boolean center;
+    private int xOffset;
+    private int yOffset;
 
     /**
      * @param bitmap    with dimension being power of two
@@ -46,6 +50,12 @@ public class BitmapRenderer extends BucketRenderer {
         initialized = false;
     }
 
+    public synchronized void setDrawOffset(boolean center, int xOffset, int yOffset) {
+        this.center = center;
+        this.xOffset = xOffset;
+        this.yOffset = yOffset;
+    }
+
     public synchronized void updateBitmap() {
         mUpdateBitmap = true;
     }
@@ -80,7 +90,7 @@ public class BitmapRenderer extends BucketRenderer {
 
     @Override
     public synchronized void render(GLViewport v) {
-        v.useScreenCoordinates(false, 8);
+        v.setScreenOffset(center, 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 e9d88d81..4673026b 100644
--- a/vtm/src/org/oscim/renderer/GLViewport.java
+++ b/vtm/src/org/oscim/renderer/GLViewport.java
@@ -1,3 +1,20 @@
+/*
+ * Copyright 2013 Hannes Janetzek
+ * Copyright 2016 Andrey Novikov
+ *
+ * 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.renderer;
 
 import org.oscim.core.MapPosition;
@@ -45,6 +62,17 @@ public class GLViewport extends Viewport {
         mvp.multiplyLhs(proj);
     }
 
+    /**
+     * Set MVP offset in screen pixel coordinates
+     */
+    public void setScreenOffset(boolean center, int xOffset, int yOffset, float scale) {
+        float invScale = 1f / scale;
+        float x = center ? xOffset : -mWidth / 2 + xOffset;
+        float y = center ? yOffset : -mHeight / 2 + yOffset;
+        mvp.setTransScale(x, y, invScale);
+        mvp.multiplyLhs(proj);
+    }
+
     protected boolean changed;
 
     public boolean changed() {