Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add map zoom out limit #23

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/java/edu/project/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,29 @@ public void setWeatherForecast5Data(WeatherForecast5Data data) {
public void addWeatherForecast5DataListener(Consumer<WeatherForecast5Data> listener) {
weatherForecast5DataListeners.add(listener);
}

/* Zoom */

public void zoomIn() {
SwingUtilities.invokeLater(() -> {
zoomInListeners.forEach(listener -> listener.run());
});
}

public void zoomOut() {
SwingUtilities.invokeLater(() -> {
zoomOutListeners.forEach(listener -> listener.run());
});
}

private ArrayList<Runnable> zoomInListeners = new ArrayList<>();
private ArrayList<Runnable> zoomOutListeners = new ArrayList<>();

public void addZoomInListener(Runnable listener) {
zoomInListeners.add(listener);
}

public void addZoomOutListener(Runnable listener) {
zoomOutListeners.add(listener);
}
}
24 changes: 24 additions & 0 deletions src/main/java/edu/project/components/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,29 @@ public void mouseClicked(MouseEvent e) {
Config.WINDOW_INIT_PADDING,
(int) (Config.WINDOW_INIT_WIDTH * 0.4),
Config.WINDOW_INIT_HEIGHT - Config.WINDOW_INIT_PADDING * 2);

ZoomButtonGroup zoomButtonGroup = new ZoomButtonGroup(context);

// Disable click-throughts to the overlapped map
zoomButtonGroup.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
e.consume();
}
});

layers.add(zoomButtonGroup);
layers.setLayer(zoomButtonGroup, JLayeredPane.MODAL_LAYER + 1);
layers.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
zoomButtonGroup.setBounds(
layers.getWidth() - 50 - 16,
layers.getHeight() - 100 - 16,
50,
100);
}
});
}
}
40 changes: 36 additions & 4 deletions src/main/java/edu/project/components/GeoMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;
import java.io.IOException;
import java.util.HashSet;

Expand All @@ -29,9 +29,34 @@ public GeoMap(Context context) {
addMouseListener(mouseInputListener);
addMouseMotionListener(mouseInputListener);

// Add Zoom Interaction
MouseWheelListener mouseWheelListener = new ZoomMouseWheelListenerCursor(this);
addMouseWheelListener(mouseWheelListener);
// Add Zoom Interaction with a zoom-out limiter
addMouseWheelListener(new ZoomMouseWheelListenerCursor(this) {
@Override
public void mouseWheelMoved(MouseWheelEvent evt) {
if (evt.getWheelRotation() > 0 && !canZoomOut())
return;
super.mouseWheelMoved(evt);
}
});

// Listen Zoom requests from store
context.store.addZoomInListener(() -> {
setZoom(getZoom() - 1);
});
context.store.addZoomOutListener(() -> {
if (!canZoomOut())
return;
setZoom(getZoom() + 1);
});

// Add Focus on click
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
requestFocus();
super.mouseClicked(e);
}
});

WaypointPainter<DefaultWaypoint> waypointPainter = new WaypointPainter<>();
HashSet<DefaultWaypoint> waypoints = new HashSet<>();
Expand Down Expand Up @@ -66,4 +91,11 @@ public void mouseClicked(MouseEvent event) {
setCenterPosition(context.store.getLocation());
setZoom(Config.MAP_INIT_ZOOM);
}

private boolean canZoomOut() {
int zoom = getZoom();
int maxZoom = getTileFactory().getInfo().getMaximumZoomLevel();
int invertedZoom = maxZoom - zoom;
return (invertedZoom >= 4);
}
}
33 changes: 33 additions & 0 deletions src/main/java/edu/project/components/ZoomButtonGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package edu.project.components;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JPanel;

import edu.project.Context;

public class ZoomButtonGroup extends JPanel {
public ZoomButtonGroup(Context context) {
setOpaque(false);
setLayout(new GridLayout(2, 1));

JButton zoomInButton = new JButton("+");
zoomInButton.setFont(new Font("Arial", Font.PLAIN, 32));
zoomInButton.setForeground(Color.GRAY);
add(zoomInButton);
zoomInButton.addActionListener(e -> {
context.store.zoomIn();
});

JButton zoomOutButton = new JButton("-");
zoomOutButton.setForeground(Color.GRAY);
zoomOutButton.setFont(new Font("Arial", Font.PLAIN, 32));
add(zoomOutButton);
zoomOutButton.addActionListener(e -> {
context.store.zoomOut();
});
}
}
Loading