From ebd07950a40a0a5a43d4f7d4a707d1f31ef30a89 Mon Sep 17 00:00:00 2001 From: Emux Date: Tue, 23 Aug 2016 14:03:09 +0300 Subject: [PATCH] libGDX layer gestures, closes #151 --- docs/Changelog.md | 3 +- vtm-gdx/src/org/oscim/gdx/GdxMap.java | 14 ++-- vtm-gdx/src/org/oscim/gdx/GdxMotionEvent.java | 76 +++++++++++++++++++ vtm-gdx/src/org/oscim/gdx/LayerHandler.java | 42 ++++++++++ 4 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 vtm-gdx/src/org/oscim/gdx/GdxMotionEvent.java create mode 100644 vtm-gdx/src/org/oscim/gdx/LayerHandler.java diff --git a/docs/Changelog.md b/docs/Changelog.md index 00923ec40..97c223dc2 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -8,10 +8,11 @@ - Mapsforge multilingual maps [#34](https://github.com/mapsforge/vtm/issues/34) - vtm-ios update module [#29](https://github.com/mapsforge/vtm/issues/29) - Native libraries for all platforms [#14](https://github.com/mapsforge/vtm/issues/14) -- LWJGL desktop libGDX backend [#129](https://github.com/mapsforge/vtm/issues/129) - Line stipple and texture rendering [#105](https://github.com/mapsforge/vtm/issues/105) - Layer groups [#99](https://github.com/mapsforge/vtm/issues/99) [#103](https://github.com/mapsforge/vtm/issues/103) - Map scale bar multi-platform [#84](https://github.com/mapsforge/vtm/issues/84) +- libGDX layer gestures [#151](https://github.com/mapsforge/vtm/issues/151) +- LWJGL desktop libGDX backend [#129](https://github.com/mapsforge/vtm/issues/129) - Render theme area tessellation option [#37](https://github.com/mapsforge/vtm/issues/37) - Graphics API platform enhancements [#92](https://github.com/mapsforge/vtm/issues/92) - vtm-jts module [#53](https://github.com/mapsforge/vtm/issues/53) diff --git a/vtm-gdx/src/org/oscim/gdx/GdxMap.java b/vtm-gdx/src/org/oscim/gdx/GdxMap.java index 281232465..85bc0e194 100644 --- a/vtm-gdx/src/org/oscim/gdx/GdxMap.java +++ b/vtm-gdx/src/org/oscim/gdx/GdxMap.java @@ -21,6 +21,7 @@ import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.InputMultiplexer; +import com.badlogic.gdx.input.GestureDetector; import com.badlogic.gdx.utils.Timer; import com.badlogic.gdx.utils.Timer.Task; @@ -33,15 +34,12 @@ import org.oscim.renderer.MapRenderer; import org.oscim.theme.VtmThemes; import org.oscim.tiling.TileSource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public abstract class GdxMap implements ApplicationListener { - final static Logger log = LoggerFactory.getLogger(GdxMap.class); protected Map mMap; + protected GestureDetector mGestureDetector; - VectorTileLayer mMapLayer; private MapRenderer mMapRenderer; public GdxMap() { @@ -52,14 +50,14 @@ protected void initDefaultLayers(TileSource tileSource, boolean tileGrid, boolea Layers layers = mMap.layers(); if (tileSource != null) { - mMapLayer = mMap.setBaseMap(tileSource); + VectorTileLayer mapLayer = mMap.setBaseMap(tileSource); mMap.setTheme(VtmThemes.DEFAULT); if (buildings) - layers.add(new BuildingLayer(mMap, mMapLayer)); + layers.add(new BuildingLayer(mMap, mapLayer)); if (labels) - layers.add(new LabelLayer(mMap, mMapLayer)); + layers.add(new LabelLayer(mMap, mapLayer)); } if (tileGrid) @@ -82,6 +80,8 @@ public void create() { mMapRenderer.onSurfaceChanged(w, h); InputMultiplexer mux = new InputMultiplexer(); + mGestureDetector = new GestureDetector(new LayerHandler(mMap)); + mux.addProcessor(mGestureDetector); mux.addProcessor(new InputHandler(this)); //mux.addProcessor(new GestureDetector(20, 0.5f, 2, 0.05f, // new MapController(mMap))); diff --git a/vtm-gdx/src/org/oscim/gdx/GdxMotionEvent.java b/vtm-gdx/src/org/oscim/gdx/GdxMotionEvent.java new file mode 100644 index 000000000..4989ec555 --- /dev/null +++ b/vtm-gdx/src/org/oscim/gdx/GdxMotionEvent.java @@ -0,0 +1,76 @@ +/* + * Copyright 2016 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 + * 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 . + */ +package org.oscim.gdx; + +import com.badlogic.gdx.Input; + +import org.oscim.event.MotionEvent; + +public class GdxMotionEvent extends MotionEvent { + + private final int action; + private final float x, y; + private final int button; + + public GdxMotionEvent(int action, float x, float y) { + this(action, x, y, Input.Buttons.LEFT); + } + + public GdxMotionEvent(int action, float x, float y, int button) { + this.action = action; + this.x = x; + this.y = y; + this.button = button; + } + + public int getButton() { + return button; + } + + @Override + public int getAction() { + return action; + } + + @Override + public int getPointerCount() { + return 0; + } + + @Override + public long getTime() { + return 0; + } + + @Override + public float getX() { + return x; + } + + @Override + public float getX(int idx) { + return x; + } + + @Override + public float getY() { + return y; + } + + @Override + public float getY(int idx) { + return y; + } +} diff --git a/vtm-gdx/src/org/oscim/gdx/LayerHandler.java b/vtm-gdx/src/org/oscim/gdx/LayerHandler.java new file mode 100644 index 000000000..6fd90c263 --- /dev/null +++ b/vtm-gdx/src/org/oscim/gdx/LayerHandler.java @@ -0,0 +1,42 @@ +/* + * Copyright 2016 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 + * 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 . + */ +package org.oscim.gdx; + +import com.badlogic.gdx.input.GestureDetector; + +import org.oscim.event.Gesture; +import org.oscim.event.MotionEvent; +import org.oscim.map.Map; + +public class LayerHandler extends GestureDetector.GestureAdapter { + + private final Map map; + + public LayerHandler(Map map) { + this.map = map; + } + + @Override + public boolean longPress(float x, float y) { + map.handleGesture(Gesture.LONG_PRESS, new GdxMotionEvent(MotionEvent.ACTION_DOWN, x, y)); + return true; + } + + @Override + public boolean tap(float x, float y, int count, int button) { + map.handleGesture(Gesture.TAP, new GdxMotionEvent(MotionEvent.ACTION_UP, x, y, button)); + return false; + } +}