Merge branch 'zoom-controls'

This commit is contained in:
Holt59 2018-03-03 15:45:25 +01:00
commit 701a94784e
6 changed files with 433 additions and 126 deletions

View File

@ -5,18 +5,19 @@ import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Image; import java.awt.Image;
import java.awt.event.ActionEvent;
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.NoninvertibleTransformException; import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D; import java.awt.geom.Point2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
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;
@ -284,7 +285,10 @@ 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
private MapZoomControls zoomControls;
/** /**
* Create a new BasicDrawing. * Create a new BasicDrawing.
@ -292,11 +296,52 @@ public class BasicDrawing extends JPanel implements Drawing {
*/ */
public BasicDrawing() { public BasicDrawing() {
this.zoomAndPanListener = new ZoomAndPanListener(this, ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20, 1.2); this.zoomAndPanListener = new ZoomAndPanListener(this, ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20, 1.2);
// Try...
try {
this.zoomControls = new MapZoomControls(this, 0, ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20);
this.zoomControls.addZoomInListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
zoomAndPanListener.zoomIn();
}
});
this.zoomControls.addZoomOutListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
zoomAndPanListener.zoomOut();
}
});
}
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 @Override
public void paintComponent(Graphics g1) { public void paintComponent(Graphics g1) {
super.paintComponent(g1);
Graphics2D g = (Graphics2D) g1; Graphics2D g = (Graphics2D) g1;
AffineTransform sTransform = g.getTransform();
g.clearRect(0, 0, getWidth(), getHeight()); g.clearRect(0, 0, getWidth(), getHeight());
g.setTransform(zoomAndPanListener.getCoordTransform()); g.setTransform(zoomAndPanListener.getCoordTransform());
@ -311,6 +356,14 @@ public class BasicDrawing extends JPanel implements Drawing {
overlay.draw(g); overlay.draw(g);
} }
} }
g.setTransform(sTransform);
if (this.zoomControls != null) {
this.zoomControls.setZoomLevel(this.zoomAndPanListener.getZoomLevel());
this.zoomControls.draw(g, getWidth() - this.zoomControls.getWidth() - 20,
this.getHeight() - this.zoomControls.getHeight() - 10, this);
}
} }
/** /**
@ -355,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
@ -513,6 +554,7 @@ public class BasicDrawing extends JPanel implements Drawing {
(this.getHeight() - this.height * scale) / 2); (this.getHeight() - this.height * scale) / 2);
this.zoomAndPanListener.getCoordTransform().scale(scale, scale); this.zoomAndPanListener.getCoordTransform().scale(scale, scale);
this.zoomAndPanListener.setZoomLevel(0); this.zoomAndPanListener.setZoomLevel(0);
this.zoomControls.setZoomLevel(0);
// Repaint // Repaint
this.repaint(); this.repaint();

View File

@ -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);
} }
} }

View File

@ -0,0 +1,187 @@
package org.insa.graphics.drawing.components;
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;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
public class MapZoomControls {
// Default ID for action events
private static final int ZOOM_IN_ACTION_ID = 0x1;
private static final String ZOOM_IN_ACTION_NAME = "ZoomIn";
private static final int ZOOM_OUT_ACTION_ID = 0x2;
private static final String ZOOM_OUT_ACTION_NAME = "ZoomOut";
// Height
private static final int DEFAULT_HEIGHT = 20;
// Default spacing between mark
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;
// Zoom ticks color
private static final Color ZOOM_TICK_COLOR = Color.GRAY;
// Current zoom ticks ratio from height
private static final double CURRENT_ZOOM_TICK_HEIGHT_RATIO = 0.8;
// Zoom ticks color
private static final Color CURRENT_ZOOM_TICK_COLOR = new Color(25, 25, 25);
// Use half mark or not
private boolean halfMark = true;
private int currentLevel = 0;
private final int minLevel, maxLevel;
// Zoom in/out image and their rectangles.
private final Image zoomIn, zoomOut;
private final Rectangle zoomInRect = new Rectangle(0, 0, 0, 0), zoomOutRect = new Rectangle(0, 0, 0, 0);
// List of listeners
private final List<ActionListener> zoomInListeners = new ArrayList<>();
private final List<ActionListener> zoomOutListeners = new ArrayList<>();
public MapZoomControls(Component component, final int defaultZoom, final int minZoom, final int maxZoom)
throws IOException {
zoomIn = ImageIO.read(new File("zoomIn.png")).getScaledInstance(DEFAULT_HEIGHT, DEFAULT_HEIGHT,
Image.SCALE_SMOOTH);
zoomOut = ImageIO.read(new File("zoomOut.png")).getScaledInstance(DEFAULT_HEIGHT, DEFAULT_HEIGHT,
Image.SCALE_SMOOTH);
this.currentLevel = defaultZoom;
this.minLevel = minZoom;
this.maxLevel = maxZoom;
component.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (zoomInRect.contains(e.getPoint()) && currentLevel < maxLevel) {
currentLevel += 1;
for (ActionListener al: zoomInListeners) {
al.actionPerformed(new ActionEvent(this, ZOOM_IN_ACTION_ID, ZOOM_IN_ACTION_NAME));
}
}
else if (zoomOutRect.contains(e.getPoint()) && currentLevel > minLevel) {
currentLevel -= 1;
for (ActionListener al: zoomOutListeners) {
al.actionPerformed(new ActionEvent(this, ZOOM_OUT_ACTION_ID, ZOOM_OUT_ACTION_NAME));
}
}
component.repaint();
}
});
}
/**
* Add a zoom-in listener.
*
* @param listener
*/
public void addZoomInListener(ActionListener listener) {
this.zoomInListeners.add(listener);
}
/**
* Add a zoom-out listener.
*
* @param listener
*/
public void addZoomOutListener(ActionListener listener) {
this.zoomOutListeners.add(listener);
}
/**
* @return the current zoom level.
*/
public int getZoomLevel() {
return this.currentLevel;
}
/**
* Set the current zoom level.
*
* @param level
*/
public void setZoomLevel(int level) {
this.currentLevel = level;
}
/**
* @return Height of this "component" when drawn.
*/
public int getHeight() {
return DEFAULT_HEIGHT;
}
/**
* @return Width of this "component" when drawn.
*/
public int getWidth() {
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();
// Draw icon
g.drawImage(zoomOut, xoffset, yoffset, observer);
zoomOutRect.setBounds(xoffset, yoffset, DEFAULT_HEIGHT, DEFAULT_HEIGHT);
// Draw ticks
xoffset += DEFAULT_HEIGHT + 2;
g.setColor(ZOOM_TICK_COLOR);
g.drawLine(xoffset, yoffset + height / 2, xoffset + (this.maxLevel - this.minLevel) * DEFAULT_SPACING + 1,
yoffset + height / 2);
for (int i = 0; i <= (this.maxLevel - this.minLevel); i += halfMark ? 2 : 1) {
g.drawLine(xoffset + i * DEFAULT_SPACING, yoffset + (int) (height * (1 - ZOOM_TICK_HEIGHT_RATIO) / 2),
xoffset + i * DEFAULT_SPACING, yoffset + (int) (height * (1 + ZOOM_TICK_HEIGHT_RATIO) / 2));
}
// Draw current ticks
g.setColor(CURRENT_ZOOM_TICK_COLOR);
g.drawLine(xoffset + (currentLevel - this.minLevel) * DEFAULT_SPACING,
yoffset + (int) (height * (1 - CURRENT_ZOOM_TICK_HEIGHT_RATIO) / 2),
xoffset + (currentLevel - this.minLevel) * DEFAULT_SPACING,
yoffset + (int) (height * (1 + CURRENT_ZOOM_TICK_HEIGHT_RATIO) / 2));
xoffset += (this.maxLevel - this.minLevel) * DEFAULT_SPACING + 1 + 2;
g.drawImage(zoomIn, xoffset, yoffset, observer);
zoomInRect.setBounds(xoffset, yoffset, DEFAULT_HEIGHT, DEFAULT_HEIGHT);
}
}

View File

@ -1,7 +1,12 @@
package org.insa.graphics.drawing.components; package org.insa.graphics.drawing.components;
import java.awt.*; import java.awt.Component;
import java.awt.event.*; import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
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;
@ -26,7 +31,8 @@ public class ZoomAndPanListener implements MouseListener, MouseMotionListener, M
this.targetComponent = targetComponent; this.targetComponent = targetComponent;
} }
public ZoomAndPanListener(Component targetComponent, int minZoomLevel, int maxZoomLevel, double zoomMultiplicationFactor) { public ZoomAndPanListener(Component targetComponent, int minZoomLevel, int maxZoomLevel,
double zoomMultiplicationFactor) {
this.targetComponent = targetComponent; this.targetComponent = targetComponent;
this.minZoomLevel = minZoomLevel; this.minZoomLevel = minZoomLevel;
this.maxZoomLevel = maxZoomLevel; this.maxZoomLevel = maxZoomLevel;
@ -38,7 +44,6 @@ public class ZoomAndPanListener implements MouseListener, MouseMotionListener, M
targetComponent.repaint(); targetComponent.repaint();
} }
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
} }
@ -48,7 +53,6 @@ public class ZoomAndPanListener implements MouseListener, MouseMotionListener, M
} }
public void mouseReleased(MouseEvent e) { public void mouseReleased(MouseEvent e) {
// moveCamera(e);
} }
public void mouseEntered(MouseEvent e) { public void mouseEntered(MouseEvent e) {
@ -79,7 +83,8 @@ public class ZoomAndPanListener implements MouseListener, MouseMotionListener, M
dragStartScreen = dragEndScreen; dragStartScreen = dragEndScreen;
dragEndScreen = null; dragEndScreen = null;
targetComponent.repaint(); targetComponent.repaint();
} catch (NoninvertibleTransformException ex) { }
catch (NoninvertibleTransformException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
@ -88,26 +93,28 @@ public class ZoomAndPanListener implements MouseListener, MouseMotionListener, M
try { try {
int wheelRotation = e.getWheelRotation(); int wheelRotation = e.getWheelRotation();
Point p = e.getPoint(); Point p = e.getPoint();
if (wheelRotation > 0) { if (wheelRotation < 0) {
if (zoomLevel < maxZoomLevel) { if (zoomLevel < maxZoomLevel) {
zoomLevel++; zoomLevel++;
Point2D p1 = transformPoint(p); Point2D p1 = transformPoint(p);
coordTransform.scale(1 / zoomMultiplicationFactor, 1 / zoomMultiplicationFactor);
Point2D p2 = transformPoint(p);
coordTransform.translate(p2.getX() - p1.getX(), p2.getY() - p1.getY());
targetComponent.repaint();
}
} else {
if (zoomLevel > minZoomLevel) {
zoomLevel--;
Point2D p1 = transformPoint(p);
coordTransform.scale(zoomMultiplicationFactor, zoomMultiplicationFactor); coordTransform.scale(zoomMultiplicationFactor, zoomMultiplicationFactor);
Point2D p2 = transformPoint(p); Point2D p2 = transformPoint(p);
coordTransform.translate(p2.getX() - p1.getX(), p2.getY() - p1.getY()); coordTransform.translate(p2.getX() - p1.getX(), p2.getY() - p1.getY());
targetComponent.repaint(); targetComponent.repaint();
} }
} }
} catch (NoninvertibleTransformException ex) { else {
if (zoomLevel > minZoomLevel) {
zoomLevel--;
Point2D p1 = transformPoint(p);
coordTransform.scale(1 / zoomMultiplicationFactor, 1 / zoomMultiplicationFactor);
Point2D p2 = transformPoint(p);
coordTransform.translate(p2.getX() - p1.getX(), p2.getY() - p1.getY());
targetComponent.repaint();
}
}
}
catch (NoninvertibleTransformException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
@ -125,11 +132,39 @@ public class ZoomAndPanListener implements MouseListener, MouseMotionListener, M
return zoomLevel; return zoomLevel;
} }
public void setZoomLevel(int zoomLevel) { public void setZoomLevel(int zoomLevel) {
this.zoomLevel = zoomLevel; this.zoomLevel = zoomLevel;
} }
public void zoomIn() {
try {
Point p = new Point(targetComponent.getWidth() / 2, targetComponent.getHeight() / 2);
zoomLevel++;
Point2D p1 = transformPoint(p);
coordTransform.scale(zoomMultiplicationFactor, zoomMultiplicationFactor);
Point2D p2 = transformPoint(p);
coordTransform.translate(p2.getX() - p1.getX(), p2.getY() - p1.getY());
targetComponent.repaint();
}
catch (NoninvertibleTransformException ex) {
ex.printStackTrace();
}
}
public void zoomOut() {
try {
Point p = new Point(targetComponent.getWidth() / 2, targetComponent.getHeight() / 2);
zoomLevel--;
Point2D p1 = transformPoint(p);
coordTransform.scale(1 / zoomMultiplicationFactor, 1 / zoomMultiplicationFactor);
Point2D p2 = transformPoint(p);
coordTransform.translate(p2.getX() - p1.getX(), p2.getY() - p1.getY());
targetComponent.repaint();
}
catch (NoninvertibleTransformException ex) {
ex.printStackTrace();
}
}
public AffineTransform getCoordTransform() { public AffineTransform getCoordTransform() {
return coordTransform; return coordTransform;

BIN
zoomIn.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

BIN
zoomOut.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB