Skip to content

Commit

Permalink
Group layer implementation, closes #99
Browse files Browse the repository at this point in the history
  • Loading branch information
devemux86 committed Jul 27, 2016
1 parent 6801102 commit 42fd941
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import org.oscim.backend.CanvasAdapter;
import org.oscim.core.MapPosition;
import org.oscim.core.MercatorProjection;
import org.oscim.layers.GroupLayer;
import org.oscim.layers.tile.buildings.BuildingLayer;
import org.oscim.layers.tile.vector.labeling.LabelLayer;
import org.oscim.map.Layers;
import org.oscim.renderer.BitmapRenderer;
import org.oscim.renderer.GLViewport;
import org.oscim.theme.IRenderTheme;
Expand All @@ -43,9 +43,10 @@ public class SimpleMapActivity extends BaseMapActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Layers layers = mMap.layers();
layers.add(new BuildingLayer(mMap, mBaseLayer));
layers.add(new LabelLayer(mMap, mBaseLayer));
GroupLayer groupLayer = new GroupLayer(mMap);
groupLayer.layers.add(new BuildingLayer(mMap, mBaseLayer));
groupLayer.layers.add(new LabelLayer(mMap, mBaseLayer));
mMap.layers().add(groupLayer);

mapScaleBar = new DefaultMapScaleBar(mMap);
mapScaleBar.setScaleBarMode(DefaultMapScaleBar.ScaleBarMode.BOTH);
Expand All @@ -57,7 +58,7 @@ public void onCreate(Bundle savedInstanceState) {
BitmapRenderer renderer = (BitmapRenderer) mapScaleBarLayer.getRenderer();
renderer.setPosition(GLViewport.Position.BOTTOM_LEFT);
renderer.setOffset(5 * CanvasAdapter.dpi / 160, 0);
layers.add(mapScaleBarLayer);
mMap.layers().add(mapScaleBarLayer);

mMap.setTheme(VtmThemes.DEFAULT);
}
Expand Down
23 changes: 21 additions & 2 deletions vtm-playground/src/org/oscim/test/MapTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
/*
* Copyright 2016 devemux86
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* 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.test;

import org.oscim.gdx.GdxMapApp;
import org.oscim.layers.GroupLayer;
import org.oscim.layers.tile.buildings.BuildingLayer;
import org.oscim.layers.tile.vector.VectorTileLayer;
import org.oscim.layers.tile.vector.labeling.LabelLayer;
Expand All @@ -16,8 +33,10 @@ public void createLayers() {

VectorTileLayer l = map.setBaseMap(new OSciMap4TileSource());

map.layers().add(new BuildingLayer(map, l));
map.layers().add(new LabelLayer(map, l));
GroupLayer groupLayer = new GroupLayer(mMap);
groupLayer.layers.add(new BuildingLayer(map, l));
groupLayer.layers.add(new LabelLayer(map, l));
map.layers().add(groupLayer);

map.setTheme(VtmThemes.DEFAULT);
map.setMapPosition(53.075, 8.808, 1 << 17);
Expand Down
42 changes: 42 additions & 0 deletions vtm/src/org/oscim/layers/GroupLayer.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
package org.oscim.layers;

import org.oscim.map.Map;

import java.util.ArrayList;
import java.util.List;

/**
* A layer which is a group of other layers.
*/
public class GroupLayer extends Layer {

/**
* The group of other layers.
*/
public final List<Layer> layers = new ArrayList<>();

public GroupLayer(Map map) {
super(map);
}

@Override
public void onDetach() {
for (Layer layer : layers) {
layer.onDetach();
}
}
}
69 changes: 66 additions & 3 deletions vtm/src/org/oscim/map/Layers.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2013 Hannes Janetzek
* Copyright 2016 devemux86
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
Expand All @@ -19,6 +20,7 @@
import org.oscim.event.Gesture;
import org.oscim.event.GestureListener;
import org.oscim.event.MotionEvent;
import org.oscim.layers.GroupLayer;
import org.oscim.layers.Layer;
import org.oscim.map.Map.InputListener;
import org.oscim.map.Map.UpdateListener;
Expand All @@ -38,7 +40,7 @@ public final class Layers extends AbstractList<Layer> {

Layers(Map map) {
mMap = map;
mLayerList = new CopyOnWriteArrayList<Layer>();
mLayerList = new CopyOnWriteArrayList<>();
}

@Override
Expand All @@ -56,12 +58,23 @@ public synchronized void add(int index, Layer layer) {
if (mLayerList.contains(layer))
throw new IllegalArgumentException("layer added twice");

// bind added layer
if (layer instanceof UpdateListener)
mMap.events.bind((UpdateListener) layer);

if (layer instanceof InputListener)
mMap.input.bind((InputListener) layer);

// bind added group layer
if (layer instanceof GroupLayer) {
GroupLayer groupLayer = (GroupLayer) layer;
for (Layer gl : groupLayer.layers) {
if (gl instanceof UpdateListener)
mMap.events.bind((UpdateListener) gl);
if (gl instanceof InputListener)
mMap.input.bind((InputListener) gl);
}
}

mLayerList.add(index, layer);
mDirtyLayers = true;
}
Expand All @@ -72,11 +85,23 @@ public synchronized Layer remove(int index) {

Layer remove = mLayerList.remove(index);

// unbind removed layer
if (remove instanceof UpdateListener)
mMap.events.unbind((UpdateListener) remove);
if (remove instanceof InputListener)
mMap.input.unbind((InputListener) remove);

// unbind removed group layer
if (remove instanceof GroupLayer) {
GroupLayer groupLayer = (GroupLayer) remove;
for (Layer gl : groupLayer.layers) {
if (gl instanceof UpdateListener)
mMap.events.unbind((UpdateListener) gl);
if (gl instanceof InputListener)
mMap.input.unbind((InputListener) gl);
}
}

return remove;
}

Expand All @@ -94,6 +119,17 @@ public synchronized Layer set(int index, Layer layer) {
if (remove instanceof InputListener)
mMap.input.unbind((InputListener) remove);

// unbind replaced group layer
if (remove instanceof GroupLayer) {
GroupLayer groupLayer = (GroupLayer) remove;
for (Layer gl : groupLayer.layers) {
if (gl instanceof UpdateListener)
mMap.events.unbind((UpdateListener) gl);
if (gl instanceof InputListener)
mMap.input.unbind((InputListener) gl);
}
}

return remove;
}

Expand Down Expand Up @@ -121,11 +157,21 @@ boolean handleGesture(Gesture g, MotionEvent e) {
if (mDirtyLayers)
updateLayers();

for (Layer o : mLayers)
for (Layer o : mLayers) {
if (o instanceof GestureListener)
if (((GestureListener) o).onGesture(g, e))
return true;

if (o instanceof GroupLayer) {
GroupLayer groupLayer = (GroupLayer) o;
for (Layer gl : groupLayer.layers) {
if (gl instanceof GestureListener)
if (((GestureListener) gl).onGesture(g, e))
return true;
}
}
}

return false;
}

Expand All @@ -139,6 +185,14 @@ private synchronized void updateLayers() {
if (o.getRenderer() != null)
numRenderLayers++;

if (o instanceof GroupLayer) {
GroupLayer groupLayer = (GroupLayer) o;
for (Layer gl : groupLayer.layers) {
if (gl.getRenderer() != null)
numRenderLayers++;
}
}

mLayers[n - i - 1] = o;
}

Expand All @@ -149,6 +203,15 @@ private synchronized void updateLayers() {
LayerRenderer l = o.getRenderer();
if (l != null)
mLayerRenderer[cnt++] = l;

if (o instanceof GroupLayer) {
GroupLayer groupLayer = (GroupLayer) o;
for (Layer gl : groupLayer.layers) {
l = gl.getRenderer();
if (l != null)
mLayerRenderer[cnt++] = l;
}
}
}

mDirtyLayers = false;
Expand Down

0 comments on commit 42fd941

Please sign in to comment.