simplify map listener handling
This commit is contained in:
parent
0b880e0f0c
commit
1d6bdcb0f2
@ -218,7 +218,7 @@ public class MapView extends RelativeLayout {
|
|||||||
mPausing = false;
|
mPausing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
AndroidMotionEvent mMotionEvent = new AndroidMotionEvent(this);
|
AndroidMotionEvent mMotionEvent = new AndroidMotionEvent();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onTouchEvent(android.view.MotionEvent motionEvent) {
|
public boolean onTouchEvent(android.view.MotionEvent motionEvent) {
|
||||||
|
@ -18,15 +18,6 @@ import org.oscim.event.MotionEvent;
|
|||||||
|
|
||||||
public class AndroidMotionEvent extends MotionEvent {
|
public class AndroidMotionEvent extends MotionEvent {
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
public AndroidMotionEvent(Object source) {
|
|
||||||
super(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
android.view.MotionEvent mEvent;
|
android.view.MotionEvent mEvent;
|
||||||
|
|
||||||
public void wrap(android.view.MotionEvent e) {
|
public void wrap(android.view.MotionEvent e) {
|
||||||
|
@ -6,12 +6,6 @@ import com.badlogic.gdx.InputProcessor;
|
|||||||
|
|
||||||
public class GdxMotionEvent extends MotionEvent implements InputProcessor {
|
public class GdxMotionEvent extends MotionEvent implements InputProcessor {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
public GdxMotionEvent(Object source) {
|
|
||||||
super(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getAction() {
|
public int getAction() {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1,19 +0,0 @@
|
|||||||
package org.oscim.event;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public abstract class Dispatcher<T> {
|
|
||||||
protected List<T> listeners = new ArrayList<T>();
|
|
||||||
|
|
||||||
public void addListener(T l) {
|
|
||||||
if (!listeners.contains(l))
|
|
||||||
listeners.add(l);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeListener(T l) {
|
|
||||||
listeners.remove(l);
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract void dispatch();
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
package org.oscim.event;
|
|
||||||
|
|
||||||
public interface EventDispatcher {
|
|
||||||
|
|
||||||
public void addListener(String type, EventListener listener);
|
|
||||||
|
|
||||||
public void removeListener(String type, EventListener listener);
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
package org.oscim.event;
|
|
||||||
|
|
||||||
public interface EventListener {
|
|
||||||
public void handleEvent(MapEvent event);
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
package org.oscim.event;
|
|
||||||
|
|
||||||
public interface IListener {
|
|
||||||
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013
|
|
||||||
*
|
|
||||||
* 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.event;
|
|
||||||
|
|
||||||
public class KeyEvent extends MapEvent {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
public KeyEvent(Object source) {
|
|
||||||
super(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
package org.oscim.event;
|
|
||||||
|
|
||||||
import java.util.EventObject;
|
|
||||||
|
|
||||||
public class MapEvent extends EventObject {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
public MapEvent(Object source) {
|
|
||||||
super(source);
|
|
||||||
}
|
|
||||||
}
|
|
@ -14,16 +14,10 @@
|
|||||||
*/
|
*/
|
||||||
package org.oscim.event;
|
package org.oscim.event;
|
||||||
|
|
||||||
public abstract class MotionEvent extends MapEvent {
|
public abstract class MotionEvent {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
public static final String TYPE = "MotionEvent";
|
public static final String TYPE = "MotionEvent";
|
||||||
|
|
||||||
public MotionEvent(Object source) {
|
|
||||||
super(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static final int ACTION_DOWN = 0;
|
public static final int ACTION_DOWN = 0;
|
||||||
public static final int ACTION_UP = 1;
|
public static final int ACTION_UP = 1;
|
||||||
public static final int ACTION_MOVE = 2;
|
public static final int ACTION_MOVE = 2;
|
||||||
|
@ -15,8 +15,6 @@
|
|||||||
package org.oscim.layers;
|
package org.oscim.layers;
|
||||||
|
|
||||||
import org.oscim.core.Tile;
|
import org.oscim.core.Tile;
|
||||||
import org.oscim.event.EventListener;
|
|
||||||
import org.oscim.event.MapEvent;
|
|
||||||
import org.oscim.event.MotionEvent;
|
import org.oscim.event.MotionEvent;
|
||||||
import org.oscim.map.Map;
|
import org.oscim.map.Map;
|
||||||
import org.oscim.map.Viewport;
|
import org.oscim.map.Viewport;
|
||||||
@ -33,7 +31,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
* http://en.wikipedia.org/wiki/Viterbi_algorithm
|
* http://en.wikipedia.org/wiki/Viterbi_algorithm
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class MapEventLayer extends Layer implements EventListener {
|
public class MapEventLayer extends Layer implements Map.InputListener {
|
||||||
private static final boolean debug = false;
|
private static final boolean debug = false;
|
||||||
static final Logger log = LoggerFactory.getLogger(MapEventLayer.class);
|
static final Logger log = LoggerFactory.getLogger(MapEventLayer.class);
|
||||||
|
|
||||||
@ -68,24 +66,15 @@ public class MapEventLayer extends Layer implements EventListener {
|
|||||||
|
|
||||||
public MapEventLayer(Map map) {
|
public MapEventLayer(Map map) {
|
||||||
super(map);
|
super(map);
|
||||||
map.addListener(MotionEvent.TYPE, this);
|
|
||||||
mMapPosition = map.getViewport();
|
mMapPosition = map.getViewport();
|
||||||
mTracker = new VelocityTracker();
|
mTracker = new VelocityTracker();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDetach() {
|
public void onMotionEvent(MotionEvent event) {
|
||||||
mMap.removeListener(MotionEvent.TYPE, this);
|
onTouchEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleEvent(MapEvent event) {
|
|
||||||
if (event instanceof MotionEvent)
|
|
||||||
onTouchEvent((MotionEvent) event);
|
|
||||||
}
|
|
||||||
|
|
||||||
//private long mPrevTime;
|
|
||||||
|
|
||||||
private boolean mEnableRotation = true;
|
private boolean mEnableRotation = true;
|
||||||
private boolean mEnableTilt = true;
|
private boolean mEnableTilt = true;
|
||||||
private boolean mEnableMove = true;
|
private boolean mEnableMove = true;
|
||||||
@ -111,11 +100,8 @@ public class MapEventLayer extends Layer implements EventListener {
|
|||||||
mEnableZoom = enable;
|
mEnableZoom = enable;
|
||||||
}
|
}
|
||||||
|
|
||||||
//@Override
|
|
||||||
public boolean onTouchEvent(MotionEvent e) {
|
public boolean onTouchEvent(MotionEvent e) {
|
||||||
|
|
||||||
//mPrevTime = e.getTime();
|
|
||||||
|
|
||||||
int action = getAction(e);
|
int action = getAction(e);
|
||||||
|
|
||||||
if (action == MotionEvent.ACTION_DOWN) {
|
if (action == MotionEvent.ACTION_DOWN) {
|
||||||
|
@ -15,8 +15,6 @@
|
|||||||
package org.oscim.layers.tile.vector.labeling;
|
package org.oscim.layers.tile.vector.labeling;
|
||||||
|
|
||||||
import org.oscim.core.MapPosition;
|
import org.oscim.core.MapPosition;
|
||||||
import org.oscim.event.EventListener;
|
|
||||||
import org.oscim.event.MapEvent;
|
|
||||||
import org.oscim.event.MotionEvent;
|
import org.oscim.event.MotionEvent;
|
||||||
import org.oscim.layers.Layer;
|
import org.oscim.layers.Layer;
|
||||||
import org.oscim.map.Map;
|
import org.oscim.map.Map;
|
||||||
@ -24,7 +22,7 @@ import org.oscim.tiling.TileRenderer;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class LabelLayer extends Layer implements EventListener, Map.UpdateListener {
|
public class LabelLayer extends Layer implements Map.InputListener, Map.UpdateListener {
|
||||||
static final Logger log = LoggerFactory.getLogger(LabelLayer.class);
|
static final Logger log = LoggerFactory.getLogger(LabelLayer.class);
|
||||||
private final TextRenderer mTextRenderer;
|
private final TextRenderer mTextRenderer;
|
||||||
|
|
||||||
@ -33,8 +31,6 @@ public class LabelLayer extends Layer implements EventListener, Map.UpdateListen
|
|||||||
public LabelLayer(Map map, TileRenderer tileRenderLayer) {
|
public LabelLayer(Map map, TileRenderer tileRenderLayer) {
|
||||||
super(map);
|
super(map);
|
||||||
|
|
||||||
map.addListener(MotionEvent.TYPE, this);
|
|
||||||
|
|
||||||
//mTextLayer = new org.oscim.renderer.layers.TextRenderLayer(map, tileRenderLayer);
|
//mTextLayer = new org.oscim.renderer.layers.TextRenderLayer(map, tileRenderLayer);
|
||||||
mTextRenderer = new TextRenderer(map, tileRenderLayer);
|
mTextRenderer = new TextRenderer(map, tileRenderLayer);
|
||||||
mRenderer = mTextRenderer;
|
mRenderer = mTextRenderer;
|
||||||
@ -42,8 +38,6 @@ public class LabelLayer extends Layer implements EventListener, Map.UpdateListen
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDetach() {
|
public void onDetach() {
|
||||||
mMap.removeListener(MotionEvent.TYPE, this);
|
|
||||||
|
|
||||||
// TODO stop and clear labeling thread
|
// TODO stop and clear labeling thread
|
||||||
log.debug("DETACH");
|
log.debug("DETACH");
|
||||||
mTextRenderer.clearLabels();
|
mTextRenderer.clearLabels();
|
||||||
@ -52,23 +46,19 @@ public class LabelLayer extends Layer implements EventListener, Map.UpdateListen
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleEvent(MapEvent event) {
|
public void onMotionEvent(MotionEvent e) {
|
||||||
if (event instanceof MotionEvent) {
|
int action = e.getAction() & MotionEvent.ACTION_MASK;
|
||||||
MotionEvent e = (MotionEvent) event;
|
if (action == MotionEvent.ACTION_POINTER_DOWN) {
|
||||||
|
multi++;
|
||||||
int action = e.getAction() & MotionEvent.ACTION_MASK;
|
mTextRenderer.hold(true);
|
||||||
if (action == MotionEvent.ACTION_POINTER_DOWN) {
|
} else if (action == MotionEvent.ACTION_POINTER_UP) {
|
||||||
multi++;
|
multi--;
|
||||||
mTextRenderer.hold(true);
|
if (multi == 0)
|
||||||
} else if (action == MotionEvent.ACTION_POINTER_UP) {
|
|
||||||
multi--;
|
|
||||||
if (multi == 0)
|
|
||||||
mTextRenderer.hold(false);
|
|
||||||
} else if (action == MotionEvent.ACTION_CANCEL) {
|
|
||||||
multi = 0;
|
|
||||||
log.debug("cancel " + multi);
|
|
||||||
mTextRenderer.hold(false);
|
mTextRenderer.hold(false);
|
||||||
}
|
} else if (action == MotionEvent.ACTION_CANCEL) {
|
||||||
|
multi = 0;
|
||||||
|
log.debug("cancel " + multi);
|
||||||
|
mTextRenderer.hold(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,15 +15,12 @@
|
|||||||
package org.oscim.map;
|
package org.oscim.map;
|
||||||
|
|
||||||
import java.util.AbstractList;
|
import java.util.AbstractList;
|
||||||
import java.util.ArrayList;
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
import org.oscim.core.BoundingBox;
|
import org.oscim.core.BoundingBox;
|
||||||
import org.oscim.core.MapPosition;
|
import org.oscim.core.MapPosition;
|
||||||
import org.oscim.event.Dispatcher;
|
|
||||||
import org.oscim.event.EventDispatcher;
|
|
||||||
import org.oscim.event.EventListener;
|
|
||||||
import org.oscim.event.IListener;
|
|
||||||
import org.oscim.event.MotionEvent;
|
import org.oscim.event.MotionEvent;
|
||||||
import org.oscim.layers.Layer;
|
import org.oscim.layers.Layer;
|
||||||
import org.oscim.layers.MapEventLayer;
|
import org.oscim.layers.MapEventLayer;
|
||||||
@ -39,10 +36,30 @@ import org.oscim.utils.async.AsyncExecutor;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public abstract class Map implements EventDispatcher {
|
public abstract class Map {
|
||||||
|
|
||||||
static final Logger log = LoggerFactory.getLogger(Map.class);
|
static final Logger log = LoggerFactory.getLogger(Map.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listener interface for map update notifications.
|
||||||
|
* Layers implementing this interface they will be automatically
|
||||||
|
* regiseter when the layer is added to the map and unregistered when
|
||||||
|
* the layer is removed.
|
||||||
|
*/
|
||||||
|
public interface UpdateListener {
|
||||||
|
void onMapUpdate(MapPosition mapPosition, boolean positionChanged, boolean clear);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listener interface for input events.
|
||||||
|
* Layers implementing this interface they will be automatically
|
||||||
|
* regiseter when the layer is added to the map and unregistered when
|
||||||
|
* the layer is removed.
|
||||||
|
*/
|
||||||
|
public interface InputListener {
|
||||||
|
void onMotionEvent(MotionEvent e);
|
||||||
|
}
|
||||||
|
|
||||||
public static final boolean debugTheme = false;
|
public static final boolean debugTheme = false;
|
||||||
|
|
||||||
private final Layers mLayers;
|
private final Layers mLayers;
|
||||||
@ -57,6 +74,9 @@ public abstract class Map implements EventDispatcher {
|
|||||||
|
|
||||||
private VectorTileLayer mBaseLayer;
|
private VectorTileLayer mBaseLayer;
|
||||||
|
|
||||||
|
private Set<InputListener> mMotionListeners = new LinkedHashSet<InputListener>();
|
||||||
|
private Set<UpdateListener> mUpdateListeners = new LinkedHashSet<UpdateListener>();
|
||||||
|
|
||||||
public Map() {
|
public Map() {
|
||||||
|
|
||||||
mViewport = new Viewport(this);
|
mViewport = new Viewport(this);
|
||||||
@ -183,7 +203,15 @@ public abstract class Map implements EventDispatcher {
|
|||||||
* Caution: Do not call directly!
|
* Caution: Do not call directly!
|
||||||
*/
|
*/
|
||||||
protected void updateLayers() {
|
protected void updateLayers() {
|
||||||
mUpdateDispatcher.dispatch();
|
boolean changed = false;
|
||||||
|
|
||||||
|
// get the current MapPosition
|
||||||
|
changed |= mViewport.getMapPosition(mMapPosition);
|
||||||
|
|
||||||
|
for (UpdateListener l : mUpdateListeners)
|
||||||
|
l.onMapUpdate(mMapPosition, changed, mClearMap);
|
||||||
|
|
||||||
|
mClearMap = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -236,64 +264,31 @@ public abstract class Map implements EventDispatcher {
|
|||||||
return mAnimator;
|
return mAnimator;
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayList<EventListener> mMotionListeners = new ArrayList<EventListener>();
|
public void bind(InputListener listener) {
|
||||||
|
mMotionListeners.add(listener);
|
||||||
@Override
|
|
||||||
public void addListener(String type, EventListener listener) {
|
|
||||||
if (type == MotionEvent.TYPE)
|
|
||||||
mMotionListeners.add(listener);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void unbind(InputListener listener) {
|
||||||
public void removeListener(String type, EventListener listener) {
|
mMotionListeners.remove(listener);
|
||||||
if (type == MotionEvent.TYPE)
|
|
||||||
mMotionListeners.remove(listener);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleMotionEvent(MotionEvent e) {
|
public void handleMotionEvent(MotionEvent e) {
|
||||||
for (EventListener l : mMotionListeners)
|
for (InputListener l : mMotionListeners)
|
||||||
l.handleEvent(e);
|
l.onMotionEvent(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Listener interface for map update notifications.
|
|
||||||
* NOTE: Layers implementing this interface they will be automatically
|
|
||||||
* registered when the layer is added to the map and unresitered when
|
|
||||||
* the layer is removed.
|
|
||||||
*/
|
|
||||||
public interface UpdateListener extends IListener {
|
|
||||||
void onMapUpdate(MapPosition mapPosition, boolean positionChanged, boolean clear);
|
|
||||||
}
|
|
||||||
|
|
||||||
private class UpdateDispatcher extends Dispatcher<UpdateListener> {
|
|
||||||
@Override
|
|
||||||
public void dispatch() {
|
|
||||||
boolean changed = false;
|
|
||||||
|
|
||||||
// get the current MapPosition
|
|
||||||
changed |= mViewport.getMapPosition(mMapPosition);
|
|
||||||
|
|
||||||
for (UpdateListener l : listeners)
|
|
||||||
l.onMapUpdate(mMapPosition, changed, mClearMap);
|
|
||||||
|
|
||||||
mClearMap = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private final UpdateDispatcher mUpdateDispatcher = new UpdateDispatcher();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register UpdateListener
|
* Register UpdateListener
|
||||||
*/
|
*/
|
||||||
public void addUpdateListener(UpdateListener l) {
|
public void bind(UpdateListener l) {
|
||||||
mUpdateDispatcher.addListener(l);
|
mUpdateListeners.add(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unregister UpdateListener
|
* Unregister UpdateListener
|
||||||
*/
|
*/
|
||||||
public void removeUpdateListener(UpdateListener l) {
|
public void unbind(UpdateListener l) {
|
||||||
mUpdateDispatcher.removeListener(l);
|
mUpdateListeners.remove(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class Layers extends AbstractList<Layer> {
|
public final class Layers extends AbstractList<Layer> {
|
||||||
@ -320,7 +315,9 @@ public abstract class Map implements EventDispatcher {
|
|||||||
throw new IllegalArgumentException("layer added twice");
|
throw new IllegalArgumentException("layer added twice");
|
||||||
|
|
||||||
if (layer instanceof UpdateListener)
|
if (layer instanceof UpdateListener)
|
||||||
addUpdateListener((UpdateListener) layer);
|
bind((UpdateListener) layer);
|
||||||
|
if (layer instanceof InputListener)
|
||||||
|
bind((InputListener) layer);
|
||||||
|
|
||||||
mLayerList.add(index, layer);
|
mLayerList.add(index, layer);
|
||||||
mDirtyLayers = true;
|
mDirtyLayers = true;
|
||||||
@ -333,7 +330,9 @@ public abstract class Map implements EventDispatcher {
|
|||||||
Layer remove = mLayerList.remove(index);
|
Layer remove = mLayerList.remove(index);
|
||||||
|
|
||||||
if (remove instanceof UpdateListener)
|
if (remove instanceof UpdateListener)
|
||||||
removeUpdateListener((UpdateListener) remove);
|
unbind((UpdateListener) remove);
|
||||||
|
if (remove instanceof InputListener)
|
||||||
|
unbind((InputListener) remove);
|
||||||
|
|
||||||
return remove;
|
return remove;
|
||||||
}
|
}
|
||||||
@ -346,8 +345,11 @@ public abstract class Map implements EventDispatcher {
|
|||||||
mDirtyLayers = true;
|
mDirtyLayers = true;
|
||||||
Layer remove = mLayerList.set(index, layer);
|
Layer remove = mLayerList.set(index, layer);
|
||||||
|
|
||||||
|
// unbind replaced layer
|
||||||
if (remove instanceof UpdateListener)
|
if (remove instanceof UpdateListener)
|
||||||
removeUpdateListener((UpdateListener) remove);
|
unbind((UpdateListener) remove);
|
||||||
|
if (remove instanceof InputListener)
|
||||||
|
unbind((InputListener) remove);
|
||||||
|
|
||||||
return remove;
|
return remove;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user