use arrays for listeners (avoid allocating iterators)

This commit is contained in:
Hannes Janetzek 2014-01-17 15:09:50 +01:00
parent 828c6d9212
commit bfb86e0a57
2 changed files with 57 additions and 56 deletions

View File

@ -58,8 +58,8 @@ public final class Layers extends AbstractList<Layer> {
if (layer instanceof UpdateListener) if (layer instanceof UpdateListener)
mMap.bind((UpdateListener) layer); mMap.bind((UpdateListener) layer);
//if (layer instanceof InputListener) if (layer instanceof InputListener)
// mMap.bind((InputListener) layer); mMap.bind((InputListener) layer);
mLayerList.add(index, layer); mLayerList.add(index, layer);
mDirtyLayers = true; mDirtyLayers = true;
@ -73,8 +73,8 @@ public final class Layers extends AbstractList<Layer> {
if (remove instanceof UpdateListener) if (remove instanceof UpdateListener)
mMap.unbind((UpdateListener) remove); mMap.unbind((UpdateListener) remove);
//if (remove instanceof InputListener) if (remove instanceof InputListener)
// mMap.unbind((InputListener) remove); mMap.unbind((InputListener) remove);
return remove; return remove;
} }
@ -90,8 +90,8 @@ public final class Layers extends AbstractList<Layer> {
// unbind replaced layer // unbind replaced layer
if (remove instanceof UpdateListener) if (remove instanceof UpdateListener)
mMap.unbind((UpdateListener) remove); mMap.unbind((UpdateListener) remove);
//if (remove instanceof InputListener) if (remove instanceof InputListener)
// mMap.unbind((InputListener) remove); mMap.unbind((InputListener) remove);
return remove; return remove;
} }
@ -114,8 +114,6 @@ public final class Layers extends AbstractList<Layer> {
for (Layer o : mLayers) for (Layer o : mLayers)
o.onDetach(); o.onDetach();
// TODO need to clear lists here?
} }
boolean handleGesture(Gesture g, MotionEvent e) { boolean handleGesture(Gesture g, MotionEvent e) {
@ -130,15 +128,6 @@ public final class Layers extends AbstractList<Layer> {
return false; return false;
} }
void handleMotionEvent(MotionEvent e) {
if (mDirtyLayers)
updateLayers();
for (Layer o : mLayers)
if (o instanceof InputListener)
((InputListener) o).onMotionEvent(e);
}
private synchronized void updateLayers() { private synchronized void updateLayers() {
mLayers = new Layer[mLayerList.size()]; mLayers = new Layer[mLayerList.size()];
int numRenderLayers = 0; int numRenderLayers = 0;

View File

@ -74,11 +74,13 @@ public abstract class Map {
private VectorTileLayer mBaseLayer; private VectorTileLayer mBaseLayer;
private Set<InputListener> mInputListeners = new LinkedHashSet<InputListener>(); private Set<InputListener> mInputListenerSet = new LinkedHashSet<InputListener>();
private Set<UpdateListener> mUpdateListeners = new LinkedHashSet<UpdateListener>(); private InputListener[] mInputListeners;
private Set<UpdateListener> mUpdateListenerSet = new LinkedHashSet<UpdateListener>();
private UpdateListener[] mUpdateListeners;
public Map() { public Map() {
mViewport = new Viewport(this); mViewport = new Viewport(this);
mAnimator = new MapAnimator(this, mViewport); mAnimator = new MapAnimator(this, mViewport);
@ -88,8 +90,6 @@ public abstract class Map {
mEventLayer = new MapEventLayer(this); mEventLayer = new MapEventLayer(this);
mLayers.add(0, mEventLayer); mLayers.add(0, mEventLayer);
//mGestureDetector = new GestureDetector(this, mLayers);
} }
public MapEventLayer getEventLayer() { public MapEventLayer getEventLayer() {
@ -200,22 +200,6 @@ public abstract class Map {
mClearMap = true; mClearMap = true;
} }
/**
* This function is run on main-loop before rendering a frame.
* Caution: Do not call directly!
*/
protected void updateLayers() {
boolean changed = false;
// get the current MapPosition
changed |= mViewport.getMapPosition(mMapPosition);
for (UpdateListener l : mUpdateListeners)
l.onMapUpdate(mMapPosition, changed, mClearMap);
mClearMap = false;
}
/** /**
* Set {@link MapPosition} of {@link Viewport} and trigger a redraw. * Set {@link MapPosition} of {@link Viewport} and trigger a redraw.
*/ */
@ -262,37 +246,65 @@ public abstract class Map {
return mAnimator; return mAnimator;
} }
/**
* Register InputListener
*/
public void bind(InputListener listener) {
mInputListeners.add(listener);
}
/**
* Unregister InputListener
*/
public void unbind(InputListener listener) {
mInputListeners.remove(listener);
}
/** /**
* Register UpdateListener * Register UpdateListener
*/ */
public void bind(UpdateListener l) { public void bind(UpdateListener l) {
mUpdateListeners.add(l); if (mUpdateListenerSet.add(l))
mUpdateListeners = null;
} }
/** /**
* Unregister UpdateListener * Unregister UpdateListener
*/ */
public void unbind(UpdateListener l) { public void unbind(UpdateListener l) {
mUpdateListeners.remove(l); if (mUpdateListenerSet.remove(l))
mUpdateListeners = null;
}
/**
* This function is run on main-loop before rendering a frame.
* Caution: Do not call directly!
*/
protected void updateLayers() {
boolean changed = false;
// get the current MapPosition
changed |= mViewport.getMapPosition(mMapPosition);
if (mUpdateListeners == null) {
mUpdateListeners = new UpdateListener[mUpdateListenerSet.size()];
mUpdateListenerSet.toArray(mUpdateListeners);
}
for (UpdateListener l : mUpdateListeners)
l.onMapUpdate(mMapPosition, changed, mClearMap);
mClearMap = false;
}
/**
* Register InputListener
*/
public void bind(InputListener listener) {
if (mInputListenerSet.add(listener))
mInputListeners = null;
}
/**
* Unregister InputListener
*/
public void unbind(InputListener listener) {
if (mInputListenerSet.remove(listener))
mInputListeners = null;
} }
// TODO make protected
public void handleMotionEvent(MotionEvent e) { public void handleMotionEvent(MotionEvent e) {
mLayers.handleMotionEvent(e);
if (mInputListeners == null) {
mInputListeners = new InputListener[mInputListenerSet.size()];
mInputListenerSet.toArray(mInputListeners);
}
for (InputListener l : mInputListeners) for (InputListener l : mInputListeners)
l.onMotionEvent(e); l.onMotionEvent(e);