Skip to content

Commit

Permalink
Scrolling on map zooms the map in/out
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielsherry committed Jun 12, 2024
1 parent 6101a5e commit cd8778b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public float getZoom() {


public void setZoom(float zoom) {
if (zoom < 1) zoom = 1;
if (zoom > 50) zoom = 50;
this.zoom = zoom;
updateListeners(MapUpdateType.UI_OPTIONS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public void mouseMoved(MouseEvent e) {
canvas.addMouseMotionListener(movementListener);
canvas.addMouseMotionListener(selectionListener);
canvas.addMouseListener(selectionListener);
canvas.addMouseWheelListener(selectionListener);


controller.addListener(t -> {
Expand All @@ -148,6 +149,7 @@ private JPanel createCanvasPanel() {
canvasScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
canvasScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
canvasScroller.setBorder(new EmptyBorder(0, 0, 0, 0));
canvasScroller.setWheelScrollingEnabled(false);
new DraggingScrollPaneListener(canvasScroller.getViewport(), canvas, Buttons.MIDDLE, Buttons.RIGHT);

JPanel canvasContainer = new JPanel(new GridBagLayout());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
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 javax.swing.SwingUtilities;

import org.peakaboo.controller.mapper.MappingController;
import org.peakaboo.framework.cyclops.Coord;
import org.peakaboo.ui.swing.mapping.MapCanvas;

public class MapSelectionListener implements MouseMotionListener, MouseListener {
public class MapSelectionListener implements MouseMotionListener, MouseListener, MouseWheelListener {

private MappingController controller;
private MapCanvas canvas;
Expand Down Expand Up @@ -94,5 +96,23 @@ public void mouseDragged(MouseEvent e) {
public void mouseMoved(MouseEvent e) {
//NOOP
}

@Override
public void mouseWheelMoved(MouseWheelEvent wheel) {
System.out.println("Event " + wheel);
int moves = wheel.getWheelRotation();
double factor = Math.pow(1.1f, Math.abs(moves));

float zoom = controller.getSettings().getZoom();
if (moves < 0) {
// Scrolled Up
zoom *= factor;
} else {
// Scrolled Down
zoom /= factor;
}
controller.getSettings().setZoom(zoom);

}

}

0 comments on commit cd8778b

Please sign in to comment.