html:
- turn off libgdx load screen - block contextmenu - add osm attribution - read initial position from url hash - disable text stroke for firefox/linux (canvas2d is 10 times slower than chromium..)
This commit is contained in:
@@ -9,6 +9,7 @@ import com.google.gwt.canvas.dom.client.Context2d.LineJoin;
|
||||
|
||||
public class GwtCanvas implements org.oscim.backend.canvas.Canvas {
|
||||
GwtBitmap bitmap;
|
||||
|
||||
public GwtCanvas() {
|
||||
// canvas comes with gdx pixmap
|
||||
}
|
||||
@@ -16,38 +17,38 @@ public class GwtCanvas implements org.oscim.backend.canvas.Canvas {
|
||||
@Override
|
||||
public void setBitmap(Bitmap bitmap) {
|
||||
this.bitmap = (GwtBitmap) bitmap;
|
||||
//this.bitmap.pixmap.setColor(0x00ffffff);
|
||||
this.bitmap.pixmap.getContext().clearRect(0, 0, this.bitmap.getWidth(), this.bitmap.getHeight());
|
||||
Context2d ctx = this.bitmap.pixmap.getContext();
|
||||
|
||||
ctx.clearRect(0, 0, this.bitmap.getWidth(), this.bitmap.getHeight());
|
||||
ctx.setLineJoin(LineJoin.BEVEL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawText(String string, float x, float y, Paint paint) {
|
||||
if (bitmap == null){
|
||||
Log.d("", "no bitmap set on canvas");
|
||||
if (bitmap == null) {
|
||||
Log.d("BUG", "no bitmap set");
|
||||
return;
|
||||
}
|
||||
|
||||
GwtPaint p = (GwtPaint) paint;
|
||||
|
||||
if (p.stroke && GwtCanvasAdapter.NO_STROKE_TEXT)
|
||||
return;
|
||||
|
||||
Context2d ctx = bitmap.pixmap.getContext();
|
||||
ctx.setFont(p.font);
|
||||
ctx.setLineJoin(LineJoin.ROUND);
|
||||
|
||||
if (p.stroke){
|
||||
//Log.d("", "stroke " + p.stroke + " " + p.color + " " + p.font + " "+ string);
|
||||
|
||||
if (p.stroke) {
|
||||
ctx.setLineWidth(p.strokeWidth);
|
||||
ctx.setStrokeStyle(p.color);
|
||||
//ctx.strokeText(string, p.strokeWidth + x, p.strokeWidth + y);
|
||||
ctx.strokeText(string, x + 1, y + 1);
|
||||
} else{
|
||||
//Log.d("", "fill " + p.stroke + " " + p.color + " " + p.font + " "+ string);
|
||||
ctx.strokeText(string, (int) (x + 1), (int) (y + 1));
|
||||
} else {
|
||||
ctx.setFillStyle(p.color);
|
||||
ctx.fillText(string, x + 1, y + 1);
|
||||
ctx.fillText(string, (int) (x + 1), (int) (y + 1));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawBitmap(Bitmap bitmap, float x, float y) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,8 +12,11 @@ import com.google.gwt.canvas.dom.client.TextMetrics;
|
||||
|
||||
public class GwtCanvasAdapter extends CanvasAdapter {
|
||||
|
||||
public static boolean NO_STROKE_TEXT = false;
|
||||
|
||||
public static final GwtCanvasAdapter INSTANCE = new GwtCanvasAdapter();
|
||||
static final Context2d ctx;
|
||||
|
||||
static {
|
||||
Canvas canvas = Canvas.createIfSupported();
|
||||
canvas.setCoordinateSpaceWidth(1);
|
||||
@@ -24,7 +27,7 @@ public class GwtCanvasAdapter extends CanvasAdapter {
|
||||
static synchronized float getTextWidth(String text, String font) {
|
||||
ctx.setFont(font);
|
||||
TextMetrics tm = ctx.measureText(text);
|
||||
return (float)tm.getWidth();
|
||||
return (float) tm.getWidth();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,18 +4,32 @@ package org.oscim.gdx.client;
|
||||
import org.oscim.backend.CanvasAdapter;
|
||||
import org.oscim.backend.GL20;
|
||||
import org.oscim.backend.GLAdapter;
|
||||
import org.oscim.backend.Log;
|
||||
import org.oscim.core.MapPosition;
|
||||
import org.oscim.core.MercatorProjection;
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.gdx.GdxMap;
|
||||
import org.oscim.layers.Layer;
|
||||
import org.oscim.layers.tile.bitmap.BitmapTileLayer;
|
||||
import org.oscim.layers.tile.bitmap.NaturalEarth;
|
||||
import org.oscim.renderer.GLRenderer;
|
||||
import org.oscim.tilesource.TileSource;
|
||||
import org.oscim.tilesource.oscimap2.OSciMap2TileSource;
|
||||
import org.oscim.tilesource.oscimap4.OSciMap4TileSource;
|
||||
import org.oscim.view.MapView;
|
||||
|
||||
import com.badlogic.gdx.ApplicationListener;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.backends.gwt.GwtApplication;
|
||||
import com.badlogic.gdx.backends.gwt.GwtApplicationConfiguration;
|
||||
import com.badlogic.gdx.backends.gwt.GwtGraphics;
|
||||
import com.badlogic.gdx.backends.gwt.preloader.Preloader.PreloaderCallback;
|
||||
import com.badlogic.gdx.backends.gwt.preloader.Preloader.PreloaderState;
|
||||
import com.google.gwt.dom.client.Style.Unit;
|
||||
import com.google.gwt.user.client.Window;
|
||||
import com.google.gwt.user.client.ui.DockLayoutPanel;
|
||||
import com.google.gwt.user.client.ui.FlowPanel;
|
||||
import com.google.gwt.user.client.ui.RootPanel;
|
||||
|
||||
public class GwtLauncher extends GwtApplication {
|
||||
|
||||
@@ -24,6 +38,24 @@ public class GwtLauncher extends GwtApplication {
|
||||
GwtApplicationConfiguration cfg = new GwtApplicationConfiguration(
|
||||
GwtGraphics.getWindowWidthJSNI(),
|
||||
GwtGraphics.getWindowHeightJSNI());
|
||||
|
||||
DockLayoutPanel p = new DockLayoutPanel(Unit.EM);
|
||||
p.setHeight("100%");
|
||||
p.setWidth("100%");
|
||||
|
||||
RootPanel.get().add(p);
|
||||
|
||||
//HTML header = new HTML("header");
|
||||
//p.addNorth(header, 2);
|
||||
//header.setStyleName("header");
|
||||
|
||||
//HTML footer = new HTML("footer");
|
||||
//footer.setStyleName("footer");
|
||||
//p.addSouth(footer, 2);
|
||||
|
||||
cfg.rootPanel = new FlowPanel();
|
||||
p.add(cfg.rootPanel);
|
||||
|
||||
cfg.stencil = true;
|
||||
cfg.fps = 30;
|
||||
|
||||
@@ -40,6 +72,21 @@ public class GwtLauncher extends GwtApplication {
|
||||
return new GwtGdxMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreloaderCallback getPreloaderCallback() {
|
||||
return new PreloaderCallback() {
|
||||
|
||||
@Override
|
||||
public void update(PreloaderState state) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String file) {
|
||||
Log.d(this.getClass().getName(), "error loading " + file);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static native String getMapConfig(String key)/*-{
|
||||
return $wnd.mapconfig && $wnd.mapconfig[key] || null;
|
||||
}-*/;
|
||||
@@ -48,34 +95,112 @@ public class GwtLauncher extends GwtApplication {
|
||||
|
||||
@Override
|
||||
public void create() {
|
||||
CanvasAdapter.g = GwtCanvasAdapter.INSTANCE;
|
||||
GLAdapter.g = (GL20)Gdx.graphics.getGL20();
|
||||
GLAdapter.GDX_WEBGL_QUIRKS = true;
|
||||
// stroke text takes about 70% cpu time in firefox:
|
||||
// https://bug568526.bugzilla.mozilla.org/attachment.cgi?id=447932
|
||||
// <- circle/stroke test 800ms firefox, 80ms chromium..
|
||||
// TODO use texture atlas to avoid drawing text-textures
|
||||
if (GwtApplication.agentInfo().isLinux() && GwtApplication.agentInfo().isFirefox())
|
||||
GwtCanvasAdapter.NO_STROKE_TEXT = true;
|
||||
|
||||
//GLAdapter.NON_PREMUL_CANVAS = true;
|
||||
CanvasAdapter.g = GwtCanvasAdapter.INSTANCE;
|
||||
GLAdapter.g = (GL20) Gdx.graphics.getGL20();
|
||||
GLAdapter.GDX_WEBGL_QUIRKS = true;
|
||||
GLRenderer.setBackgroundColor(0xffffff);
|
||||
//Gdx.app.setLogLevel(Application.LOG_DEBUG);
|
||||
|
||||
super.create();
|
||||
|
||||
double lat = Double.parseDouble(getMapConfig("latitude"));
|
||||
double lon = Double.parseDouble(getMapConfig("longitude"));
|
||||
int zoom = Integer.parseInt(getMapConfig("zoom"));
|
||||
double lat = Double.parseDouble(getMapConfig("latitude"));
|
||||
double lon = Double.parseDouble(getMapConfig("longitude"));
|
||||
int zoom = Integer.parseInt(getMapConfig("zoom"));
|
||||
float tilt = 0;
|
||||
float rotation = 0;
|
||||
|
||||
if (Window.Location.getHash() != null) {
|
||||
String hash = Window.Location.getHash();
|
||||
//Log.d("...", "hash: " + hash);
|
||||
hash = hash.substring(1);
|
||||
String[] pairs = hash.split(",");
|
||||
|
||||
for (String p : pairs) {
|
||||
try {
|
||||
if (p.startsWith("lat="))
|
||||
lat = Double.parseDouble(p.substring(4));
|
||||
else if (p.startsWith("lon="))
|
||||
lon = Double.parseDouble(p.substring(4));
|
||||
else if (p.startsWith("scale="))
|
||||
zoom = Integer.parseInt(p.substring(6));
|
||||
else if (p.startsWith("rot="))
|
||||
rotation = Float.parseFloat(p.substring(4));
|
||||
else if (p.startsWith("tilt="))
|
||||
tilt = Float.parseFloat(p.substring(5));
|
||||
} catch (NumberFormatException e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
MapPosition p = new MapPosition();
|
||||
p.setZoomLevel(zoom);
|
||||
p.setPosition(lat, lon);
|
||||
mMapView.setMapPosition(p);
|
||||
|
||||
mMapView.getMapViewPosition().setTilt(tilt);
|
||||
mMapView.getMapViewPosition().setRotation(rotation);
|
||||
|
||||
String url = getMapConfig("tileurl");
|
||||
|
||||
TileSource tileSource;
|
||||
if ("oscimap4".equals(getMapConfig("tilesource")))
|
||||
tileSource = new OSciMap4TileSource();
|
||||
else
|
||||
tileSource = new OSciMap2TileSource();
|
||||
|
||||
tileSource.setOption("url", url);
|
||||
mMapView.setBaseMap(tileSource);
|
||||
|
||||
initDefaultMap(tileSource, false, true, true);
|
||||
|
||||
mMapView.getLayerManager().add(new UrlPositionUpdate(mMapView));
|
||||
mMapView.setBackgroundMap(new BitmapTileLayer(mMapView, NaturalEarth.INSTANCE));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class UrlPositionUpdate extends Layer {
|
||||
|
||||
public UrlPositionUpdate(MapView mapView) {
|
||||
super(mapView);
|
||||
}
|
||||
|
||||
private int curLon, curLat, curZoom, curTilt, curRot;
|
||||
|
||||
@Override
|
||||
public void onUpdate(MapPosition pos, boolean changed, boolean clear) {
|
||||
if (!changed)
|
||||
return;
|
||||
int lat = (int) (MercatorProjection.toLatitude(pos.y) * 1000);
|
||||
int lon = (int) (MercatorProjection.toLongitude(pos.x) * 1000);
|
||||
int rot = (int) (pos.angle);
|
||||
rot = (int) (pos.angle) % 360;
|
||||
rot = rot < 0 ? -rot : rot;
|
||||
|
||||
if (curZoom != pos.zoomLevel
|
||||
|| curLat != lat
|
||||
|| curLon != lon
|
||||
|| curTilt != rot
|
||||
|| curRot != (int) (pos.angle)) {
|
||||
|
||||
curLat = lat;
|
||||
curLon = lon;
|
||||
curZoom = pos.zoomLevel;
|
||||
curTilt = (int) pos.tilt;
|
||||
curRot = rot;
|
||||
|
||||
//String newURL = Window.Location.createUrlBuilder()
|
||||
// .setHash("scale=" + pos.zoomLevel + ",rot=" + curRot + ",tilt=" +
|
||||
// curTilt + ",lat=" + (curLat / 1000f) + ",lon=" + (curLon / 1000f))
|
||||
// .buildString();
|
||||
//Window.Location.replace(newURL);
|
||||
//Window.Location.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user