Add zoom controls to mapview drawing, avoid triggering drawingclick events with zoom control.
This commit is contained in:
parent
dede97bc68
commit
f56d735d90
@ -9,7 +9,6 @@ import java.awt.event.ActionEvent;
|
|||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.awt.event.MouseListener;
|
|
||||||
import java.awt.geom.AffineTransform;
|
import java.awt.geom.AffineTransform;
|
||||||
import java.awt.geom.NoninvertibleTransformException;
|
import java.awt.geom.NoninvertibleTransformException;
|
||||||
import java.awt.geom.Point2D;
|
import java.awt.geom.Point2D;
|
||||||
@ -17,10 +16,8 @@ import java.awt.image.BufferedImage;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.IdentityHashMap;
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
@ -288,7 +285,7 @@ public class BasicDrawing extends JPanel implements Drawing {
|
|||||||
private List<BasicOverlay> overlays = Collections.synchronizedList(new ArrayList<BasicOverlay>());
|
private List<BasicOverlay> overlays = Collections.synchronizedList(new ArrayList<BasicOverlay>());
|
||||||
|
|
||||||
// Mapping DrawingClickListener -> MouseEventListener
|
// Mapping DrawingClickListener -> MouseEventListener
|
||||||
private Map<DrawingClickListener, MouseListener> listenerMapping = new IdentityHashMap<>();
|
private List<DrawingClickListener> drawingClickListeners = new ArrayList<>();
|
||||||
|
|
||||||
// Zoom controls
|
// Zoom controls
|
||||||
private MapZoomControls zoomControls;
|
private MapZoomControls zoomControls;
|
||||||
@ -319,6 +316,25 @@ public class BasicDrawing extends JPanel implements Drawing {
|
|||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
e.printStackTrace();
|
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
|
@Override
|
||||||
@ -345,7 +361,7 @@ public class BasicDrawing extends JPanel implements Drawing {
|
|||||||
if (this.zoomControls != null) {
|
if (this.zoomControls != null) {
|
||||||
this.zoomControls.setZoomLevel(this.zoomAndPanListener.getZoomLevel());
|
this.zoomControls.setZoomLevel(this.zoomAndPanListener.getZoomLevel());
|
||||||
this.zoomControls.draw(g, getWidth() - this.zoomControls.getWidth() - 20,
|
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
|
@Override
|
||||||
public void addDrawingClickListener(DrawingClickListener listener) {
|
public void addDrawingClickListener(DrawingClickListener listener) {
|
||||||
MouseListener mListener = new MouseAdapter() {
|
this.drawingClickListeners.add(listener);
|
||||||
@Override
|
|
||||||
public void mouseClicked(MouseEvent evt) {
|
|
||||||
try {
|
|
||||||
listener.mouseClicked(getLongitudeLatitude(evt));
|
|
||||||
}
|
|
||||||
catch (NoninvertibleTransformException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
this.addMouseListener(mListener);
|
|
||||||
this.listenerMapping.put(listener, mListener);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeDrawingClickListener(DrawingClickListener listener) {
|
public void removeDrawingClickListener(DrawingClickListener listener) {
|
||||||
this.removeMouseListener(this.listenerMapping.get(listener));
|
this.drawingClickListeners.remove(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
package org.insa.graphics.drawing.components;
|
package org.insa.graphics.drawing.components;
|
||||||
|
|
||||||
import java.awt.Color;
|
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.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@ -207,6 +212,9 @@ public class MapViewDrawing extends MapView implements Drawing {
|
|||||||
// Tile size
|
// Tile size
|
||||||
private int tileSize;
|
private int tileSize;
|
||||||
|
|
||||||
|
// Zoom controls
|
||||||
|
private MapZoomControls zoomControls;
|
||||||
|
|
||||||
public MapViewDrawing() {
|
public MapViewDrawing() {
|
||||||
super();
|
super();
|
||||||
Parameters.NUMBER_OF_THREADS = 2;
|
Parameters.NUMBER_OF_THREADS = 2;
|
||||||
@ -219,6 +227,37 @@ public class MapViewDrawing extends MapView implements Drawing {
|
|||||||
|
|
||||||
this.setZoomLevelMin((byte) 0);
|
this.setZoomLevelMin((byte) 0);
|
||||||
this.setZoomLevelMax((byte) 20);
|
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) {
|
protected LatLong convertPoint(Point point) {
|
||||||
@ -232,6 +271,9 @@ public class MapViewDrawing extends MapView implements Drawing {
|
|||||||
@Override
|
@Override
|
||||||
public boolean onTap(LatLong tapLatLong, org.mapsforge.core.model.Point layerXY,
|
public boolean onTap(LatLong tapLatLong, org.mapsforge.core.model.Point layerXY,
|
||||||
org.mapsforge.core.model.Point tapXY) {
|
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());
|
Point pt = new Point(tapLatLong.getLongitude(), tapLatLong.getLatitude());
|
||||||
for (DrawingClickListener listener: MapViewDrawing.this.drawingClickListeners) {
|
for (DrawingClickListener listener: MapViewDrawing.this.drawingClickListeners) {
|
||||||
listener.mouseClicked(pt);
|
listener.mouseClicked(pt);
|
||||||
@ -301,6 +343,7 @@ public class MapViewDrawing extends MapView implements Drawing {
|
|||||||
byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(), boundingBox,
|
byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(), boundingBox,
|
||||||
model.displayModel.getTileSize());
|
model.displayModel.getTileSize());
|
||||||
model.mapViewPosition.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
|
model.mapViewPosition.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel));
|
||||||
|
zoomControls.setZoomLevel(zoomLevel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import java.awt.Color;
|
|||||||
import java.awt.Component;
|
import java.awt.Component;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.Image;
|
import java.awt.Image;
|
||||||
|
import java.awt.Point;
|
||||||
import java.awt.Rectangle;
|
import java.awt.Rectangle;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
@ -27,10 +28,10 @@ public class MapZoomControls {
|
|||||||
private static final String ZOOM_OUT_ACTION_NAME = "ZoomOut";
|
private static final String ZOOM_OUT_ACTION_NAME = "ZoomOut";
|
||||||
|
|
||||||
// Height
|
// Height
|
||||||
private static final int DEFAULT_HEIGHT = 18;
|
private static final int DEFAULT_HEIGHT = 20;
|
||||||
|
|
||||||
// Default spacing between mark
|
// 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)
|
// Zoom ticks ratio from height (not the current one)
|
||||||
private static final double ZOOM_TICK_HEIGHT_RATIO = 0.3;
|
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;
|
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) {
|
protected void draw(Graphics2D g, int xoffset, int yoffset, ImageObserver observer) {
|
||||||
|
|
||||||
int height = getHeight();
|
int height = getHeight();
|
||||||
|
Loading…
Reference in New Issue
Block a user