From f56d735d904b7c42ac4cecb5dfed8954eb1cf022 Mon Sep 17 00:00:00 2001 From: Holt59 Date: Sat, 3 Mar 2018 15:36:35 +0100 Subject: [PATCH] Add zoom controls to mapview drawing, avoid triggering drawingclick events with zoom control. --- .../drawing/components/BasicDrawing.java | 42 ++++++++++-------- .../drawing/components/MapViewDrawing.java | 43 +++++++++++++++++++ .../drawing/components/MapZoomControls.java | 18 +++++++- 3 files changed, 82 insertions(+), 21 deletions(-) diff --git a/src/main/org/insa/graphics/drawing/components/BasicDrawing.java b/src/main/org/insa/graphics/drawing/components/BasicDrawing.java index cd0230f..d1ace53 100644 --- a/src/main/org/insa/graphics/drawing/components/BasicDrawing.java +++ b/src/main/org/insa/graphics/drawing/components/BasicDrawing.java @@ -9,7 +9,6 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; import java.awt.geom.AffineTransform; import java.awt.geom.NoninvertibleTransformException; import java.awt.geom.Point2D; @@ -17,10 +16,8 @@ import java.awt.image.BufferedImage; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; -import java.util.IdentityHashMap; import java.util.Iterator; import java.util.List; -import java.util.Map; import javax.swing.JPanel; @@ -288,7 +285,7 @@ public class BasicDrawing extends JPanel implements Drawing { private List overlays = Collections.synchronizedList(new ArrayList()); // Mapping DrawingClickListener -> MouseEventListener - private Map listenerMapping = new IdentityHashMap<>(); + private List drawingClickListeners = new ArrayList<>(); // Zoom controls private MapZoomControls zoomControls; @@ -319,6 +316,25 @@ public class BasicDrawing extends JPanel implements Drawing { catch (IOException e) { e.printStackTrace(); } + + this.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent evt) { + if (zoomControls.contains(evt.getPoint())) { + return; + } + Point lonlat = null; + try { + lonlat = getLongitudeLatitude(evt); + } + catch (NoninvertibleTransformException e) { + return; + } + for (DrawingClickListener listener: drawingClickListeners) { + listener.mouseClicked(lonlat); + } + } + }); } @Override @@ -345,7 +361,7 @@ public class BasicDrawing extends JPanel implements Drawing { if (this.zoomControls != null) { this.zoomControls.setZoomLevel(this.zoomAndPanListener.getZoomLevel()); this.zoomControls.draw(g, getWidth() - this.zoomControls.getWidth() - 20, - this.getHeight() - this.zoomControls.getHeight() - 20, this); + this.getHeight() - this.zoomControls.getHeight() - 10, this); } } @@ -392,24 +408,12 @@ public class BasicDrawing extends JPanel implements Drawing { @Override public void addDrawingClickListener(DrawingClickListener listener) { - MouseListener mListener = new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent evt) { - try { - listener.mouseClicked(getLongitudeLatitude(evt)); - } - catch (NoninvertibleTransformException e) { - e.printStackTrace(); - } - } - }; - this.addMouseListener(mListener); - this.listenerMapping.put(listener, mListener); + this.drawingClickListeners.add(listener); } @Override public void removeDrawingClickListener(DrawingClickListener listener) { - this.removeMouseListener(this.listenerMapping.get(listener)); + this.drawingClickListeners.remove(listener); } @Override diff --git a/src/main/org/insa/graphics/drawing/components/MapViewDrawing.java b/src/main/org/insa/graphics/drawing/components/MapViewDrawing.java index 0365d1f..d5c1ea3 100644 --- a/src/main/org/insa/graphics/drawing/components/MapViewDrawing.java +++ b/src/main/org/insa/graphics/drawing/components/MapViewDrawing.java @@ -1,7 +1,12 @@ package org.insa.graphics.drawing.components; import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.UUID; @@ -207,6 +212,9 @@ public class MapViewDrawing extends MapView implements Drawing { // Tile size private int tileSize; + // Zoom controls + private MapZoomControls zoomControls; + public MapViewDrawing() { super(); Parameters.NUMBER_OF_THREADS = 2; @@ -219,6 +227,37 @@ public class MapViewDrawing extends MapView implements Drawing { this.setZoomLevelMin((byte) 0); this.setZoomLevelMax((byte) 20); + + // Try... + try { + this.zoomControls = new MapZoomControls(this, 0, 0, 20); + this.zoomControls.addZoomInListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + getModel().mapViewPosition.zoomIn(); + } + }); + this.zoomControls.addZoomOutListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + getModel().mapViewPosition.zoomOut(); + } + }); + } + catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void paint(Graphics graphics) { + super.paint(graphics); + if (this.zoomControls != null) { + this.zoomControls.setZoomLevel(this.getModel().mapViewPosition.getZoomLevel()); + this.zoomControls.draw((Graphics2D) graphics, getWidth() - this.zoomControls.getWidth() - 20, + this.getHeight() - this.zoomControls.getHeight() - 10, this); + } + } protected LatLong convertPoint(Point point) { @@ -232,6 +271,9 @@ public class MapViewDrawing extends MapView implements Drawing { @Override public boolean onTap(LatLong tapLatLong, org.mapsforge.core.model.Point layerXY, org.mapsforge.core.model.Point tapXY) { + if (zoomControls.contains(new java.awt.Point((int) tapXY.x, (int) tapXY.y))) { + return false; + } Point pt = new Point(tapLatLong.getLongitude(), tapLatLong.getLatitude()); for (DrawingClickListener listener: MapViewDrawing.this.drawingClickListeners) { listener.mouseClicked(pt); @@ -301,6 +343,7 @@ public class MapViewDrawing extends MapView implements Drawing { byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(), boundingBox, model.displayModel.getTileSize()); model.mapViewPosition.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel)); + zoomControls.setZoomLevel(zoomLevel); } } diff --git a/src/main/org/insa/graphics/drawing/components/MapZoomControls.java b/src/main/org/insa/graphics/drawing/components/MapZoomControls.java index 0e18a33..b892c7f 100644 --- a/src/main/org/insa/graphics/drawing/components/MapZoomControls.java +++ b/src/main/org/insa/graphics/drawing/components/MapZoomControls.java @@ -4,6 +4,7 @@ import java.awt.Color; import java.awt.Component; import java.awt.Graphics2D; import java.awt.Image; +import java.awt.Point; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -27,10 +28,10 @@ public class MapZoomControls { private static final String ZOOM_OUT_ACTION_NAME = "ZoomOut"; // Height - private static final int DEFAULT_HEIGHT = 18; + private static final int DEFAULT_HEIGHT = 20; // Default spacing between mark - private static final int DEFAULT_SPACING = 3; + private static final int DEFAULT_SPACING = 4; // Zoom ticks ratio from height (not the current one) private static final double ZOOM_TICK_HEIGHT_RATIO = 0.3; @@ -138,6 +139,19 @@ public class MapZoomControls { return DEFAULT_HEIGHT + 2 + (this.maxLevel - this.minLevel) * DEFAULT_SPACING + 1 + 2 + DEFAULT_HEIGHT; } + /** + * Check if a point is contained inside an element of this zoom controls, useful + * to avoid spurious click listeners. + * + * @param point + * + * @return true if the given point correspond to an element of this zoom + * controls. + */ + public boolean contains(Point point) { + return zoomInRect.contains(point) || zoomOutRect.contains(point); + } + protected void draw(Graphics2D g, int xoffset, int yoffset, ImageObserver observer) { int height = getHeight();