Fixed worldmap recenter that failed on some devices. Now handled in Java

instead of Javascript.
This commit is contained in:
Zukero
2018-02-22 15:49:36 +01:00
parent 23faa6d19c
commit 752a3b64f0
3 changed files with 58 additions and 28 deletions

View File

@@ -43,22 +43,6 @@ if (params) {
} else {
player.style.display = \"none\";
}
function scrollToPlayer() {
var elem = document.getElementById(\"playerPosition\");
var x = 0;
var y = 0;
while (elem != null) {
x += elem.offsetLeft;
y += elem.offsetTop;
elem = elem.offsetParent;
}
window.scrollTo(x - (window.innerWidth/2), y - (window.innerHeight/2));
}
window.onload = scrollToPlayer();
</script>
</body>

View File

@@ -2,18 +2,9 @@ package com.gpl.rpg.AndorsTrail.activity;
import java.io.File;
import com.gpl.rpg.AndorsTrail.AndorsTrailApplication;
import com.gpl.rpg.AndorsTrail.R;
import com.gpl.rpg.AndorsTrail.context.WorldContext;
import com.gpl.rpg.AndorsTrail.controller.WorldMapController;
import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment;
import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment.WorldMapSegmentMap;
import com.gpl.rpg.AndorsTrail.util.L;
import com.gpl.rpg.AndorsTrail.util.ThemeHelper;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
@@ -23,6 +14,17 @@ import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
import com.gpl.rpg.AndorsTrail.AndorsTrailApplication;
import com.gpl.rpg.AndorsTrail.R;
import com.gpl.rpg.AndorsTrail.context.WorldContext;
import com.gpl.rpg.AndorsTrail.controller.WorldMapController;
import com.gpl.rpg.AndorsTrail.model.map.PredefinedMap;
import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment;
import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment.WorldMapSegmentMap;
import com.gpl.rpg.AndorsTrail.util.Coord;
import com.gpl.rpg.AndorsTrail.util.L;
import com.gpl.rpg.AndorsTrail.util.ThemeHelper;
public final class DisplayWorldMapActivity extends Activity {
private WorldContext world;
@@ -46,6 +48,8 @@ public final class DisplayWorldMapActivity extends Activity {
displayworldmap_webview.setBackgroundColor(ThemeHelper.getThemeColor(this, R.attr.ui_theme_displayworldmap_bg_color));
displayworldmap_webview.getSettings().setBuiltInZoomControls(true);
displayworldmap_webview.getSettings().setUseWideViewPort(true);
displayworldmap_webview.setVerticalScrollBarEnabled(true);
displayworldmap_webview.setHorizontalScrollBarEnabled(true);
enableJavascript();
Button b = (Button) findViewById(R.id.displayworldmap_close);
@@ -78,6 +82,8 @@ public final class DisplayWorldMapActivity extends Activity {
update();
}
WorldMapSegmentMap map;
Coord offsetWorldmapTo;
@SuppressLint("NewApi")
private void update() {
File worldmap = WorldMapController.getCombinedWorldMapFile(worldMapSegmentName);
@@ -88,11 +94,23 @@ public final class DisplayWorldMapActivity extends Activity {
}
WorldMapSegment segment = world.maps.worldMapSegments.get(worldMapSegmentName);
WorldMapSegmentMap map = segment.maps.get(world.model.currentMap.name);
map = segment.maps.get(world.model.currentMap.name);
if (map == null) {
this.finish();
return;
}
offsetWorldmapTo = new Coord(999999, 999999);
for (WorldMapSegmentMap map : segment.maps.values()) {
PredefinedMap predefinedMap = world.maps.findPredefinedMap(map.mapName);
if (predefinedMap == null) continue;
if (!predefinedMap.visited) continue;
if (!WorldMapController.fileForMapExists(predefinedMap)) continue;
offsetWorldmapTo.x = Math.min(offsetWorldmapTo.x, map.worldPosition.x);
offsetWorldmapTo.y = Math.min(offsetWorldmapTo.y, map.worldPosition.y);
}
String url = "file://" + worldmap.getAbsolutePath() + '?'
+ (world.model.player.position.x + map.worldPosition.x) * WorldMapController.WORLDMAP_DISPLAY_TILESIZE
@@ -115,7 +133,29 @@ public final class DisplayWorldMapActivity extends Activity {
displayworldmap_webview.postDelayed(new Runnable() {
@Override
public void run() {
displayworldmap_webview.loadUrl("javascript:scrollToPlayer();");
if (map != null) {
//Local map to global worldmap
int x = world.model.player.position.x + map.worldPosition.x - offsetWorldmapTo.x;
//Tile coord to pixel coord
x *= WorldMapController.WORLDMAP_DISPLAY_TILESIZE;
x += WorldMapController.WORLDMAP_DISPLAY_TILESIZE/2;
//Zoom level
x = (int)(x * displayworldmap_webview.getScale());
//Center
x -= displayworldmap_webview.getWidth() / 2;
//Local map to global worldmap
int y = world.model.player.position.y + map.worldPosition.y - offsetWorldmapTo.y;
//Tile coord to pixel coord
y *= WorldMapController.WORLDMAP_DISPLAY_TILESIZE;
y += WorldMapController.WORLDMAP_DISPLAY_TILESIZE/2;
//Zoom level
y = (int)(y * displayworldmap_webview.getScale());
//Center
y -= displayworldmap_webview.getHeight() / 2;
displayworldmap_webview.scrollTo(x, y);
}
}
}, 100);
}

View File

@@ -159,6 +159,12 @@ public final class WorldMapController {
File noMediaFile = new File(dir, ".nomedia");
if (!noMediaFile.exists()) noMediaFile.createNewFile();
}
public static boolean fileForMapExists(PredefinedMap map) {
if (map.lastSeenLayoutHash.length() > 0) {
return getPngFile(map.name + '.' + map.lastSeenLayoutHash).exists();
}
return getPngFile(map.name).exists();
}
private static File getFileForMap(PredefinedMap map, boolean verifyFileExists) {
if (map.lastSeenLayoutHash.length() > 0) {
File fileWithHash = getPngFile(map.name + '.' + map.lastSeenLayoutHash);