From 1df4fdd3b520f544875edae455ee54984bffa54f Mon Sep 17 00:00:00 2001 From: Kenneth VanderLinde Date: Tue, 28 May 2024 05:03:50 -0700 Subject: [PATCH 1/5] Remove adjust grid panels as they are unused --- .../client/ui/adjustgrid/AdjustGridPanel.java | 467 ----------------- .../adjustgrid/AdvancedAdjustGridPanel.java | 468 ------------------ 2 files changed, 935 deletions(-) delete mode 100644 src/main/java/net/rptools/maptool/client/ui/adjustgrid/AdjustGridPanel.java delete mode 100644 src/main/java/net/rptools/maptool/client/ui/adjustgrid/AdvancedAdjustGridPanel.java diff --git a/src/main/java/net/rptools/maptool/client/ui/adjustgrid/AdjustGridPanel.java b/src/main/java/net/rptools/maptool/client/ui/adjustgrid/AdjustGridPanel.java deleted file mode 100644 index 722de0ce69..0000000000 --- a/src/main/java/net/rptools/maptool/client/ui/adjustgrid/AdjustGridPanel.java +++ /dev/null @@ -1,467 +0,0 @@ -/* - * This software Copyright by the RPTools.net development team, and - * licensed under the Affero GPL Version 3 or, at your option, any later - * version. - * - * MapTool Source Code is distributed in the hope that it will be - * useful, but WITHOUT ANY WARRANTY; without even the implied warranty - * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public - * License * along with this source Code. If not, please visit - * and specifically the Affero license - * text at . - */ -package net.rptools.maptool.client.ui.adjustgrid; - -import java.awt.Color; -import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.Point; -import java.awt.event.ActionEvent; -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.image.BufferedImage; -import java.beans.PropertyChangeListener; -import java.beans.PropertyChangeSupport; -import javax.swing.AbstractAction; -import javax.swing.JComponent; -import javax.swing.SwingUtilities; -import net.rptools.maptool.client.AppActions; -import net.rptools.maptool.client.swing.SwingUtil; -import net.rptools.maptool.client.ui.Scale; - -public class AdjustGridPanel extends JComponent - implements MouseListener, MouseMotionListener, MouseWheelListener { - - private static final int MINIMUM_GRID_SIZE = 5; - - private enum Direction { - Increase, - Decrease - } - - public static final String PROPERTY_GRID_OFFSET_X = "gridOffsetX"; - public static final String PROPERTY_GRID_OFFSET_Y = "gridOffsetY"; - public static final String PROPERTY_GRID_SIZE = "gridSize"; - public static final String PROPERTY_ZOOM = "zoom"; - - private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this); - - private int gridOffsetX = 0; - private int gridOffsetY = 0; - private int gridSize = 40; - private boolean showGrid = true; - - private int mouseX; - private int mouseY; - - private int dragStartX; - private int dragStartY; - private int dragOffsetX; - private int dragOffsetY; - - private Color gridColor = Color.darkGray; - - private BufferedImage image; - - private Scale scale; - - public AdjustGridPanel() { - setOpaque(false); - addMouseListener(this); - addMouseMotionListener(this); - addMouseWheelListener(this); - - getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) - .put(AppActions.ZOOM_OUT.getKeyStroke(), "zoomOut"); - getActionMap() - .put( - "zoomOut", - new AbstractAction() { - @Override - public void actionPerformed(ActionEvent e) { - zoomOut(); - } - }); - - getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(AppActions.ZOOM_IN.getKeyStroke(), "zoomIn"); - getActionMap() - .put( - "zoomIn", - new AbstractAction() { - @Override - public void actionPerformed(ActionEvent e) { - zoomIn(); - } - }); - - getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) - .put(AppActions.ZOOM_RESET.getKeyStroke(), "zoomReset"); - getActionMap() - .put( - "zoomReset", - new AbstractAction() { - @Override - public void actionPerformed(ActionEvent e) { - zoomReset(); - } - }); - } - - public void setZoneImage(BufferedImage image) { - this.image = image; - - scale = new Scale(image.getWidth(), image.getHeight()); - } - - public void setZoomIndex(int index) { - scale.setScale(index); - repaint(); - } - - @Override - protected void paintComponent(Graphics g) { - - if (image == null) { - return; - } - - Dimension size = getSize(); - - if (scale.initialize(size.width, size.height)) { - propertyChangeSupport.firePropertyChange(PROPERTY_ZOOM, 0, (int) scale.getScale()); - } - - // CALCULATIONS - Dimension imageSize = getScaledImageSize(); - Point imagePosition = getScaledImagePosition(); - - double imgRatio = scale.getScale(); - - // SETUP - Graphics2D g2d = (Graphics2D) g; - - // BG FILL - g2d.setColor(getBackground()); - g2d.fillRect(0, 0, size.width, size.height); - - // IMAGE - g2d.drawImage(image, imagePosition.x, imagePosition.y, imageSize.width, imageSize.height, null); - g2d.setColor(Color.black); - g2d.drawRect(imagePosition.x, imagePosition.y, imageSize.width, imageSize.height); - - // GRID - g2d.setColor(gridColor); - double gridSize = this.gridSize * imgRatio; - - // across - int x = imagePosition.x + (int) (gridOffsetX * imgRatio); - for (double i = gridSize; i <= imageSize.width; i += gridSize) { - g2d.drawLine( - x + (int) i, imagePosition.y, x + (int) i, imagePosition.y + imageSize.height - 1); - } - - // down - int y = imagePosition.y + (int) (gridOffsetY * imgRatio); - for (double i = gridSize; i <= imageSize.height; i += gridSize) { - g2d.drawLine( - imagePosition.x, y + (int) i, imagePosition.x + imageSize.width - 1, y + (int) i); - } - } - - public void setGridColor(Color color) { - gridColor = color; - } - - @Override - public boolean isFocusable() { - return true; - } - - public void setGridOffset(int offsetX, int offsetY) { - gridOffsetX = offsetX; - gridOffsetY = offsetY; - - repaint(); - } - - public int getGridSize() { - return gridSize; - } - - public int getGridOffsetX() { - return gridOffsetX; - } - - public int getGridOffsetY() { - return gridOffsetY; - } - - public void setGridSize(int size) { - gridSize = Math.max(MINIMUM_GRID_SIZE, size); - repaint(); - } - - private Dimension getScaledImageSize() { - - Dimension imageSize = new Dimension(image.getWidth(), image.getHeight()); - imageSize.width *= scale.getScale(); - imageSize.height *= scale.getScale(); - - return imageSize; - } - - private Point getScaledImagePosition() { - - int imgX = scale.getOffsetX(); - int imgY = scale.getOffsetY(); - - return new Point(imgX, imgY); - } - - public void zoomIn() { - scale.zoomIn(mouseX, mouseY); - repaint(); - } - - public void zoomOut() { - scale.zoomOut(mouseX, mouseY); - repaint(); - } - - public void zoomReset() { - scale.reset(); - repaint(); - } - - public void moveGridBy(int dx, int dy) { - - int oldOffsetX = gridOffsetX; - int oldOffsetY = gridOffsetY; - - gridOffsetX += dx; - gridOffsetY += dy; - - gridOffsetX %= gridSize; - gridOffsetY %= gridSize; - - if (gridOffsetY > 0) { - gridOffsetY = gridOffsetY - gridSize; - } - - if (gridOffsetX > 0) { - gridOffsetX = gridOffsetX - gridSize; - } - - repaint(); - - propertyChangeSupport.firePropertyChange(PROPERTY_GRID_OFFSET_X, oldOffsetX, gridOffsetX); - propertyChangeSupport.firePropertyChange(PROPERTY_GRID_OFFSET_Y, oldOffsetY, gridOffsetY); - } - - public void adjustGridSize(int delta) { - - int oldSize = gridSize; - gridSize = Math.max(MINIMUM_GRID_SIZE, gridSize + delta); - - repaint(); - propertyChangeSupport.firePropertyChange(PROPERTY_GRID_SIZE, oldSize, gridSize); - } - - private void adjustGridSize(Direction direction) { - - Point imagePosition = getScaledImagePosition(); - - double gridSize = this.gridSize * scale.getScale(); - - int cellX = (int) ((mouseX - imagePosition.x - gridOffsetX) / gridSize); - int cellY = (int) ((mouseY - imagePosition.y - gridOffsetY) / gridSize); - - switch (direction) { - case Increase: - adjustGridSize(1); - - if (this.gridSize != gridSize) { - moveGridBy(-cellX, -cellY); - } - break; - case Decrease: - adjustGridSize(-1); - - if (this.gridSize != gridSize) { - moveGridBy(cellX, cellY); - } - break; - } - } - - //// - // MOUSE LISTENER - @Override - public void mouseClicked(MouseEvent e) {} - - @Override - public void mouseEntered(MouseEvent e) {} - - @Override - public void mouseExited(MouseEvent e) {} - - @Override - public void mousePressed(MouseEvent e) { - - mouseX = e.getX(); - mouseY = e.getY(); - - dragStartX = e.getX(); - dragStartY = e.getY(); - - Point imagePosition = getScaledImagePosition(); - - int x = (int) ((e.getX() - imagePosition.x) / scale.getScale() - gridOffsetX); - int y = (int) ((e.getY() - imagePosition.y) / scale.getScale() - gridOffsetY); - - dragOffsetX = x % gridSize; - dragOffsetY = y % gridSize; - } - - @Override - public void mouseReleased(MouseEvent e) {} - - //// - // MOUSE MOTION LISTENER - @Override - public void mouseDragged(MouseEvent e) { - - if (SwingUtilities.isLeftMouseButton(e)) { - - Point imagePosition = getScaledImagePosition(); - - int x = (int) ((e.getX() - imagePosition.x) / scale.getScale() - dragOffsetX); - int y = (int) ((e.getY() - imagePosition.y) / scale.getScale() - dragOffsetY); - - int oldOffsetX = gridOffsetX; - int oldOffsetY = gridOffsetY; - - gridOffsetX = x % gridSize; - gridOffsetY = y % gridSize; - - if (gridOffsetY > 0) { - gridOffsetY = gridOffsetY - gridSize; - } - - if (gridOffsetX > 0) { - gridOffsetX = gridOffsetX - gridSize; - } - - repaint(); - propertyChangeSupport.firePropertyChange(PROPERTY_GRID_OFFSET_X, oldOffsetX, gridOffsetX); - propertyChangeSupport.firePropertyChange(PROPERTY_GRID_OFFSET_Y, oldOffsetY, gridOffsetY); - } else { - int offsetX = scale.getOffsetX() + e.getX() - dragStartX; - int offsetY = scale.getOffsetY() + e.getY() - dragStartY; - - scale.setOffset(offsetX, offsetY); - - dragStartX = e.getX(); - dragStartY = e.getY(); - - repaint(); - } - } - - @Override - public void mouseMoved(MouseEvent e) { - - Dimension imgSize = getScaledImageSize(); - Point imgPos = getScaledImagePosition(); - - boolean insideMap = - e.getX() > imgPos.x - && e.getX() < imgPos.x + imgSize.width - && e.getY() > imgPos.y - && e.getY() < imgPos.y + imgSize.height; - if ((insideMap && showGrid) || (!insideMap && !showGrid)) { - showGrid = !insideMap; - repaint(); - } - - mouseX = e.getX(); - mouseY = e.getY(); - } - - //// - // MOUSE WHEEL LISTENER - @Override - public void mouseWheelMoved(MouseWheelEvent e) { - // Fix for hi-res mice - if (e.getWheelRotation() == 0) { - return; - } - if (SwingUtil.isControlDown(e)) { - - double oldScale = scale.getScale(); - if (e.getWheelRotation() > 0) { - scale.zoomOut(e.getX(), e.getY()); - } else { - scale.zoomIn(e.getX(), e.getY()); - } - propertyChangeSupport.firePropertyChange(PROPERTY_ZOOM, oldScale, scale.getScale()); - } else { - - if (e.getWheelRotation() > 0) { - - adjustGridSize(Direction.Increase); - } else { - - adjustGridSize(Direction.Decrease); - } - } - repaint(); - } - - //// - // PROPERTY CHANGE SUPPORT - @Override - public void addPropertyChangeListener(PropertyChangeListener listener) { - propertyChangeSupport.addPropertyChangeListener(listener); - } - - @Override - public void removePropertyChangeListener(PropertyChangeListener listener) { - propertyChangeSupport.removePropertyChangeListener(listener); - } - - @Override - public void addPropertyChangeListener(String name, PropertyChangeListener listener) { - propertyChangeSupport.addPropertyChangeListener(name, listener); - } - - @Override - public void removePropertyChangeListener(String name, PropertyChangeListener listener) { - propertyChangeSupport.removePropertyChangeListener(name, listener); - } - /* - * private final Map KEYSTROKES = new HashMap() { { put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, KeyEvent.SHIFT_DOWN_MASK), new GridSizeAction(Size.Decrease)); - * put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, KeyEvent.SHIFT_DOWN_MASK), new GridSizeAction(Size.Decrease)); put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, KeyEvent.SHIFT_DOWN_MASK), new - * GridSizeAction(Size.Increase)); put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, KeyEvent.SHIFT_DOWN_MASK), new GridSizeAction(Size.Increase)); put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), new - * GridOffsetAction(GridOffsetAction.Direction.Up)); put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), new GridOffsetAction(GridOffsetAction.Direction.Left)); - * put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), new GridOffsetAction(GridOffsetAction.Direction.Down)); put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), new - * GridOffsetAction(GridOffsetAction.Direction.Right)); } }; protected Map getKeyActionMap() { return KEYSTROKES; } - * - * private final class GridSizeAction extends AbstractAction { private final Size size; public GridSizeAction(Size size) { this.size = size; } - * - * public void actionPerformed(ActionEvent e) { ZoneRenderer renderer = (ZoneRenderer) e.getSource(); adjustGridSize(renderer, size); } } - * - * private static final class GridOffsetAction extends AbstractAction { private static enum Direction { Left, Right, Up, Down }; private final Direction direction; - * - * public GridOffsetAction(Direction direction) { this.direction = direction; } - * - * public void actionPerformed(ActionEvent e) { ZoneRenderer renderer = (ZoneRenderer) e.getSource(); switch (direction) { case Left: renderer.moveGridBy(-1, 0); break; case Right: - * renderer.moveGridBy(1, 0); break; case Up: renderer.moveGridBy(0, -1); break; case Down: renderer.moveGridBy(0, 1); break; } } } - */ -} diff --git a/src/main/java/net/rptools/maptool/client/ui/adjustgrid/AdvancedAdjustGridPanel.java b/src/main/java/net/rptools/maptool/client/ui/adjustgrid/AdvancedAdjustGridPanel.java deleted file mode 100644 index d8a89beef0..0000000000 --- a/src/main/java/net/rptools/maptool/client/ui/adjustgrid/AdvancedAdjustGridPanel.java +++ /dev/null @@ -1,468 +0,0 @@ -/* - * This software Copyright by the RPTools.net development team, and - * licensed under the Affero GPL Version 3 or, at your option, any later - * version. - * - * MapTool Source Code is distributed in the hope that it will be - * useful, but WITHOUT ANY WARRANTY; without even the implied warranty - * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public - * License * along with this source Code. If not, please visit - * and specifically the Affero license - * text at . - */ -package net.rptools.maptool.client.ui.adjustgrid; - -import java.awt.Color; -import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.Point; -import java.awt.Rectangle; -import java.awt.RenderingHints; -import java.awt.event.ActionEvent; -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.image.BufferedImage; -import javax.swing.AbstractAction; -import javax.swing.JComponent; -import javax.swing.SwingUtilities; -import net.rptools.maptool.client.AppActions; -import net.rptools.maptool.client.ui.Scale; - -public class AdvancedAdjustGridPanel extends JComponent - implements MouseListener, MouseMotionListener, MouseWheelListener { - - private static final int MINIMUM_GRID_SIZE = 5; - - private int gridCountX = 10; - private int gridCountY = 10; - private boolean showGrid = true; - - private int mouseX; - private int mouseY; - - private BufferedImage image; - - private Scale scale; - - private enum Handle { - TOP, - BOTTOM, - LEFT, - RIGHT - } - - private int topGrid; - private int bottomGrid; - private int leftGrid; - private int rightGrid; - - private boolean showRows = true; - private boolean showCols = true; - - private Handle draggingHandle; - - public AdvancedAdjustGridPanel() { - setOpaque(false); - addMouseListener(this); - addMouseMotionListener(this); - addMouseWheelListener(this); - - getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) - .put(AppActions.ZOOM_OUT.getKeyStroke(), "zoomOut"); - getActionMap() - .put( - "zoomOut", - new AbstractAction() { - @Override - public void actionPerformed(ActionEvent e) { - zoomOut(); - } - }); - - getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(AppActions.ZOOM_IN.getKeyStroke(), "zoomIn"); - getActionMap() - .put( - "zoomIn", - new AbstractAction() { - @Override - public void actionPerformed(ActionEvent e) { - zoomIn(); - } - }); - - getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) - .put(AppActions.ZOOM_RESET.getKeyStroke(), "zoomReset"); - getActionMap() - .put( - "zoomReset", - new AbstractAction() { - @Override - public void actionPerformed(ActionEvent e) { - zoomReset(); - } - }); - } - - public Rectangle getGridBounds() { - return new Rectangle(topGrid, leftGrid, rightGrid - leftGrid, bottomGrid - topGrid); - } - - public void setZoneImage(BufferedImage image) { - this.image = image; - - topGrid = 0; - bottomGrid = image.getHeight(); - leftGrid = 0; - rightGrid = image.getWidth(); - - scale = new Scale(image.getWidth(), image.getHeight()); - } - - public void setShowRows(boolean show) { - showRows = show; - } - - public void setShowCols(boolean show) { - showCols = show; - } - - @Override - protected void paintComponent(Graphics g) { - - if (image == null) { - return; - } - - Dimension size = getSize(); - - scale.initialize(size.width, size.height); - - // CALCULATIONS - Dimension imageSize = getScaledImageSize(); - Point imagePosition = getScaledImagePosition(); - - double imgRatio = scale.getScale(); - - // handles - int top = (int) (topGrid * imgRatio); - int bottom = (int) (bottomGrid * imgRatio); - int left = (int) (leftGrid * imgRatio); - int right = (int) (rightGrid * imgRatio); - - // SETUP - Graphics2D g2d = (Graphics2D) g; - - // BG FILL - g2d.setColor(getBackground()); - g2d.fillRect(0, 0, size.width, size.height); - - // IMAGE - g2d.drawImage(image, imagePosition.x, imagePosition.y, imageSize.width, imageSize.height, null); - - // GRID - g2d.setColor(Color.blue); - double dx = ((rightGrid - leftGrid) / (float) gridCountX) * imgRatio; - double dy = ((bottomGrid - topGrid) / (float) gridCountY) * imgRatio; - - // across - if (showCols) { - int x = imagePosition.x + left; - for (int i = 0; i < gridCountX; i++) { - g2d.drawLine( - x + (int) (i * dx), - imagePosition.y + top, - x + (int) (i * dx), - imagePosition.y + bottom); - } - } - - // down - if (showRows) { - int y = imagePosition.y + top; - for (int i = 0; i < gridCountY; i++) { - g2d.drawLine( - imagePosition.x + left, - y + (int) (i * dy), - imagePosition.x + right, - y + (int) (i * dy)); - } - } - - // HANDLES - int handleSize = 10; - - Object oldValue = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING); - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - - g2d.setColor(Color.red); - - // Top - int halfHandleX = imagePosition.x + imageSize.width / 2; - g2d.fillRect(halfHandleX - handleSize, top + imagePosition.y - 3, handleSize * 2, 3); - g2d.drawLine( - imagePosition.x - handleSize, - top + imagePosition.y, - imagePosition.x + imageSize.width + handleSize, - top + imagePosition.y); - - // Bottom - g2d.fillRect(halfHandleX - handleSize, bottom + imagePosition.y + 1, handleSize * 2, 3); - g2d.drawLine( - imagePosition.x - handleSize, - bottom + imagePosition.y, - imagePosition.x + imageSize.width + handleSize, - bottom + imagePosition.y); - - // Left - int halfHandleY = imagePosition.y + imageSize.height / 2; - g2d.fillRect(left + imagePosition.x - 3, halfHandleY - handleSize, 3, handleSize * 2); - g2d.drawLine( - left + imagePosition.x, - imagePosition.y - handleSize, - left + imagePosition.x, - imagePosition.y + imageSize.height + handleSize); - - // Right - g2d.fillRect(right + 1 + imagePosition.x, halfHandleY - handleSize, 3, handleSize * 2); - g2d.drawLine( - right + imagePosition.x, - imagePosition.y - handleSize, - right + imagePosition.x, - imagePosition.y + imageSize.height + handleSize); - - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldValue); - } - - @Override - public boolean isFocusable() { - return true; - } - - public void setGridCountX(int count) { - gridCountX = count; - repaint(); - } - - public void setGridCountY(int count) { - gridCountY = count; - repaint(); - } - - private float getScaledImageRatio() { - return getScaledImageSize().width / (float) image.getWidth(); - } - - private Dimension getScaledImageSize() { - - Dimension imageSize = new Dimension(image.getWidth(), image.getHeight()); - imageSize.width *= scale.getScale(); - imageSize.height *= scale.getScale(); - - return imageSize; - } - - private Point getScaledImagePosition() { - Dimension size = getSize(); - Dimension imageSize = getScaledImageSize(); - - int imgX = scale.getOffsetX(); - int imgY = scale.getOffsetY(); - - return new Point(imgX, imgY); - } - - private void updateHandles(MouseEvent e) { - // Convert - float imgRatio = getScaledImageRatio(); - Point imgPosition = getScaledImagePosition(); - - Point location = e.getPoint(); - location.translate(-imgPosition.x, -imgPosition.y); - - location = new Point((int) (location.x / imgRatio), (int) (location.y / imgRatio)); - - switch (draggingHandle) { - case TOP: - { - if (location.y < 0) { - location.y = 0; - } - if (location.y > bottomGrid - MINIMUM_GRID_SIZE) { - location.y = bottomGrid - MINIMUM_GRID_SIZE; - } - - topGrid = location.y; - break; - } - case BOTTOM: - { - if (location.y < topGrid + MINIMUM_GRID_SIZE) { - location.y = topGrid + MINIMUM_GRID_SIZE; - } - if (location.y > image.getHeight()) { - location.y = image.getHeight(); - } - - bottomGrid = location.y; - break; - } - case LEFT: - { - if (location.x < 0) { - location.x = 0; - } - if (location.x > rightGrid - MINIMUM_GRID_SIZE) { - location.x = rightGrid - MINIMUM_GRID_SIZE; - } - - leftGrid = location.x; - break; - } - case RIGHT: - { - if (location.x < leftGrid + MINIMUM_GRID_SIZE) { - location.x = leftGrid + MINIMUM_GRID_SIZE; - } - if (location.x > image.getWidth()) { - location.x = image.getWidth(); - } - - rightGrid = location.x; - break; - } - } - repaint(); - } - - public void zoomIn() { - scale.zoomIn(mouseX, mouseY); - repaint(); - } - - public void zoomOut() { - scale.zoomOut(mouseX, mouseY); - repaint(); - } - - public void zoomReset() { - scale.reset(); - repaint(); - } - - //// - // MOUSE LISTENER - @Override - public void mouseClicked(MouseEvent e) {} - - @Override - public void mouseEntered(MouseEvent e) {} - - @Override - public void mouseExited(MouseEvent e) {} - - @Override - public void mousePressed(MouseEvent e) { - - if (SwingUtilities.isLeftMouseButton(e)) { - float imgRatio = getScaledImageRatio(); - Point imgPos = getScaledImagePosition(); - - int top = (int) (topGrid * imgRatio) + imgPos.y; - int bottom = (int) (bottomGrid * imgRatio) + imgPos.y; - int left = (int) (leftGrid * imgRatio) + imgPos.x; - int right = (int) (rightGrid * imgRatio) + imgPos.x; - - int distTop = Math.abs(e.getY() - top); - int distBottom = Math.abs(e.getY() - bottom); - int distLeft = Math.abs(e.getX() - left); - int distRight = Math.abs(e.getX() - right); - - int dist = distTop; - draggingHandle = Handle.TOP; - - if (distBottom < dist) { - dist = distBottom; - draggingHandle = Handle.BOTTOM; - } - if (distLeft < dist) { - dist = distLeft; - draggingHandle = Handle.LEFT; - } - if (distRight < dist) { - dist = distRight; - draggingHandle = Handle.RIGHT; - } - - updateHandles(e); - } else { - mouseX = e.getX(); - mouseY = e.getY(); - } - } - - @Override - public void mouseReleased(MouseEvent e) { - draggingHandle = null; - - repaint(); - } - - //// - // MOUSE MOTION LISTENER - @Override - public void mouseDragged(MouseEvent e) { - - if (SwingUtilities.isLeftMouseButton(e)) { - updateHandles(e); - } else { - int offsetX = scale.getOffsetX() + e.getX() - mouseX; - int offsetY = scale.getOffsetY() + e.getY() - mouseY; - - scale.setOffset(offsetX, offsetY); - - mouseX = e.getX(); - mouseY = e.getY(); - } - - repaint(); - } - - @Override - public void mouseMoved(MouseEvent e) { - - Dimension imgSize = getScaledImageSize(); - Point imgPos = getScaledImagePosition(); - - boolean insideMap = - e.getX() > imgPos.x - && e.getX() < imgPos.x + imgSize.width - && e.getY() > imgPos.y - && e.getY() < imgPos.y + imgSize.height; - if ((insideMap && showGrid) || (!insideMap && !showGrid)) { - showGrid = !insideMap; - repaint(); - } - - mouseX = e.getX(); - mouseY = e.getY(); - } - - //// - // MOUSE WHEEL LISTENER - @Override - public void mouseWheelMoved(MouseWheelEvent e) { - if (e.getWheelRotation() < 0) { - scale.zoomIn(e.getX(), e.getY()); - } else if (e.getWheelRotation() > 0) { - scale.zoomOut(e.getX(), e.getY()); - } - - repaint(); - } -} From 6a48f55f4ba2e85aa4d3e1c6cb5c57f67ac22101 Mon Sep 17 00:00:00 2001 From: Kenneth VanderLinde Date: Tue, 28 May 2024 05:05:15 -0700 Subject: [PATCH 2/5] Cap zoom level to 175, or (31,367,596%) scale The scale and zoom level can never vary independently. The copy constructor and readResolve() now enforce the zoom level being calculated from the scale. Also includes a bunch of clean up for unused fields. --- .../net/rptools/maptool/client/ui/Scale.java | 127 ++++++------------ 1 file changed, 38 insertions(+), 89 deletions(-) diff --git a/src/main/java/net/rptools/maptool/client/ui/Scale.java b/src/main/java/net/rptools/maptool/client/ui/Scale.java index cb19392ccd..25323182a5 100644 --- a/src/main/java/net/rptools/maptool/client/ui/Scale.java +++ b/src/main/java/net/rptools/maptool/client/ui/Scale.java @@ -17,69 +17,49 @@ import java.awt.Point; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; +import java.io.Serial; import java.io.Serializable; public class Scale implements Serializable { + public static final String PROPERTY_SCALE = "scale"; + public static final String PROPERTY_OFFSET = "offset"; + private static final int MAX_ZOOM_LEVEL = 175; private final double oneToOneScale = 1; // Let this be configurable at some point private double scale = oneToOneScale; private final double scaleIncrement = .075; private int zoomLevel = 0; - - public static final String PROPERTY_SCALE = "scale"; - public static final String PROPERTY_OFFSET = "offset"; - - private transient PropertyChangeSupport propertyChangeSupport; - private int offsetX; private int offsetY; - - private final int width; - private final int height; - - private boolean initialized; - - // LEGACY for 1.3b31 and earlier - private transient int scaleIndex; // 'transient' prevents serialization; prep for 1.4 + private transient PropertyChangeSupport propertyChangeSupport; public Scale() { - this(0, 0); - } - - public Scale(int width, int height) { - this.width = width; - this.height = height; + this.offsetX = 0; + this.offsetY = 0; } public Scale(Scale copy) { - this.width = copy.width; - this.height = copy.height; this.offsetX = copy.offsetX; this.offsetY = copy.offsetY; - this.zoomLevel = copy.zoomLevel; - this.initialized = copy.initialized; - this.scale = copy.scale; - // this.oneToOneScale = copy.oneToOneScale; - // this.scaleIncrement = copy.scaleIncrement; + setScale(copy.scale); } - public void addPropertyChangeListener(PropertyChangeListener listener) { - getPropertyChangeSupport().addPropertyChangeListener(listener); + @Serial + private Object readResolve() { + // Make sure the zoom level is correct. + setScale(this.scale); + return this; } - public void addPropertyChangeListener(String property, PropertyChangeListener listener) { - getPropertyChangeSupport().addPropertyChangeListener(property, listener); + public void addPropertyChangeListener(PropertyChangeListener listener) { + getPropertyChangeSupport().addPropertyChangeListener(listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { getPropertyChangeSupport().removePropertyChangeListener(listener); } - public void removePropertyChangeListener(String property, PropertyChangeListener listener) { - getPropertyChangeSupport().removePropertyChangeListener(property, listener); - } - public int getOffsetX() { return offsetX; } @@ -109,15 +89,21 @@ public void setScale(double scale) { return; } + var oldScale = this.scale; + this.scale = scale; // Determine zoomLevel appropriate for given scale - zoomLevel = (int) Math.round(Math.log(scale / oneToOneScale) / Math.log(1 + scaleIncrement)); + this.zoomLevel = + (int) Math.round(Math.log(scale / oneToOneScale) / Math.log(1 + scaleIncrement)); - setScaleNoZoomLevel(scale); + getPropertyChangeSupport().firePropertyChange(PROPERTY_SCALE, oldScale, this.scale); } - private void setScaleNoZoomLevel(double scale) { + private void setScaleFromZoomLevel() { double oldScale = this.scale; - this.scale = scale; + + // Check for zero just to avoid any possible imprecision. + this.scale = + zoomLevel == 0 ? oneToOneScale : oneToOneScale * Math.pow(1 + scaleIncrement, zoomLevel); getPropertyChangeSupport().firePropertyChange(PROPERTY_SCALE, oldScale, scale); } @@ -126,29 +112,27 @@ public double getOneToOneScale() { return oneToOneScale; } - public double reset() { - double oldScale = this.scale; - scale = oneToOneScale; - zoomLevel = 0; + public void reset() { + setZoomLevel(0); + } - getPropertyChangeSupport().firePropertyChange(PROPERTY_SCALE, oldScale, scale); - return oldScale; + private void setZoomLevel(int zoomLevel) { + this.zoomLevel = Math.clamp(zoomLevel, -MAX_ZOOM_LEVEL, MAX_ZOOM_LEVEL); + setScaleFromZoomLevel(); } - public double scaleUp() { - zoomLevel++; - setScaleNoZoomLevel(oneToOneScale * Math.pow(1 + scaleIncrement, zoomLevel)); - return scale; + private void scaleUp() { + setZoomLevel(zoomLevel + 1); } - public double scaleDown() { - zoomLevel--; - setScaleNoZoomLevel(oneToOneScale * Math.pow(1 + scaleIncrement, zoomLevel)); - return scale; + private void scaleDown() { + setZoomLevel(zoomLevel - 1); } public void zoomReset(int x, int y) { - zoomTo(x, y, reset()); + var oldScale = scale; + reset(); + zoomTo(x, y, oldScale); } public void zoomIn(int x, int y) { @@ -169,10 +153,6 @@ public void zoomScale(int x, int y, double scale) { zoomTo(x, y, oldScale); } - public boolean isInitialized() { - return initialized; - } - private PropertyChangeSupport getPropertyChangeSupport() { if (propertyChangeSupport == null) { propertyChangeSupport = new PropertyChangeSupport(this); @@ -180,37 +160,6 @@ private PropertyChangeSupport getPropertyChangeSupport() { return propertyChangeSupport; } - /** - * Fit the image into the given space by finding the zoom level that allows the image to fit. Then - * center the image. - * - * @param width width of the image - * @param height height of the image - * @return true if this call did something, false if the init has already been called - */ - public boolean initialize(int width, int height) { - - if (initialized) { - return false; - } - - centerIn(width, height); - - initialized = true; - return true; - } - - public void centerIn(int width, int height) { - - int currWidth = (int) (this.width * getScale()); - int currHeight = (int) (this.height * getScale()); - - int x = (width - currWidth) / 2; - int y = (height - currHeight) / 2; - - setOffset(x, y); - } - private void zoomTo(int x, int y, double oldScale) { // Keep the current pixel centered From 3cd165845207e73f9f63210379e9449ff8c44435 Mon Sep 17 00:00:00 2001 From: Kenneth VanderLinde Date: Tue, 28 May 2024 05:53:28 -0700 Subject: [PATCH 3/5] Now also clamp explicitly set scales --- .../net/rptools/maptool/client/ui/Scale.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/rptools/maptool/client/ui/Scale.java b/src/main/java/net/rptools/maptool/client/ui/Scale.java index 25323182a5..bb9f925874 100644 --- a/src/main/java/net/rptools/maptool/client/ui/Scale.java +++ b/src/main/java/net/rptools/maptool/client/ui/Scale.java @@ -89,13 +89,21 @@ public void setScale(double scale) { return; } - var oldScale = this.scale; - this.scale = scale; // Determine zoomLevel appropriate for given scale - this.zoomLevel = + var zoomLevel = (int) Math.round(Math.log(scale / oneToOneScale) / Math.log(1 + scaleIncrement)); - - getPropertyChangeSupport().firePropertyChange(PROPERTY_SCALE, oldScale, this.scale); + // Check that we haven't gone out of bounds with our zooming. + if (zoomLevel < -MAX_ZOOM_LEVEL) { + setZoomLevel(-MAX_ZOOM_LEVEL); + } else if (zoomLevel > MAX_ZOOM_LEVEL) { + setZoomLevel(MAX_ZOOM_LEVEL); + } else { + // Acceptable scale. Use it. + var oldScale = this.scale; + this.scale = scale; + this.zoomLevel = zoomLevel; + getPropertyChangeSupport().firePropertyChange(PROPERTY_SCALE, oldScale, this.scale); + } } private void setScaleFromZoomLevel() { From fa56e4cf24fdfd041ba67c05d74bf80f1fe84d20 Mon Sep 17 00:00:00 2001 From: Kenneth VanderLinde Date: Tue, 28 May 2024 07:32:58 -0700 Subject: [PATCH 4/5] Separate minimum zoom level Minimum is still -175, there's just no reason they have to be the same. --- src/main/java/net/rptools/maptool/client/ui/Scale.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/rptools/maptool/client/ui/Scale.java b/src/main/java/net/rptools/maptool/client/ui/Scale.java index bb9f925874..10cdefac9e 100644 --- a/src/main/java/net/rptools/maptool/client/ui/Scale.java +++ b/src/main/java/net/rptools/maptool/client/ui/Scale.java @@ -23,6 +23,7 @@ public class Scale implements Serializable { public static final String PROPERTY_SCALE = "scale"; public static final String PROPERTY_OFFSET = "offset"; + private static final int MIN_ZOOM_LEVEL = -175; private static final int MAX_ZOOM_LEVEL = 175; private final double oneToOneScale = 1; // Let this be configurable at some point @@ -93,8 +94,8 @@ public void setScale(double scale) { var zoomLevel = (int) Math.round(Math.log(scale / oneToOneScale) / Math.log(1 + scaleIncrement)); // Check that we haven't gone out of bounds with our zooming. - if (zoomLevel < -MAX_ZOOM_LEVEL) { - setZoomLevel(-MAX_ZOOM_LEVEL); + if (zoomLevel < MIN_ZOOM_LEVEL) { + setZoomLevel(MIN_ZOOM_LEVEL); } else if (zoomLevel > MAX_ZOOM_LEVEL) { setZoomLevel(MAX_ZOOM_LEVEL); } else { @@ -125,7 +126,8 @@ public void reset() { } private void setZoomLevel(int zoomLevel) { - this.zoomLevel = Math.clamp(zoomLevel, -MAX_ZOOM_LEVEL, MAX_ZOOM_LEVEL); + this.zoomLevel = Math.clamp(zoomLevel, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL); + System.out.println(this.zoomLevel); setScaleFromZoomLevel(); } From 7ceba4d8792932dce7d83f219dd70425dd65359b Mon Sep 17 00:00:00 2001 From: Kenneth VanderLinde Date: Tue, 28 May 2024 07:53:18 -0700 Subject: [PATCH 5/5] Fix the width and alignment of the zoom status bar We were passing 4 (`RIGHT`) as the number of column rather than the alignment. This has been fixed, and the column count bumped up to 9. With the extra space, we now format the numbers to always display 5 digits. --- .../maptool/client/swing/ZoomStatusBar.java | 39 +++++++------------ 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/src/main/java/net/rptools/maptool/client/swing/ZoomStatusBar.java b/src/main/java/net/rptools/maptool/client/swing/ZoomStatusBar.java index 804a6aaa81..5e9f04ba7a 100644 --- a/src/main/java/net/rptools/maptool/client/swing/ZoomStatusBar.java +++ b/src/main/java/net/rptools/maptool/client/swing/ZoomStatusBar.java @@ -14,7 +14,6 @@ */ package net.rptools.maptool.client.swing; -import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.ParseException; @@ -31,10 +30,9 @@ * and changing the zoom level to that amount. */ public class ZoomStatusBar extends JTextField implements ActionListener { - private static final Dimension minSize = new Dimension(50, 10); - public ZoomStatusBar() { - super("", RIGHT); + super("", 9); + setHorizontalAlignment(RIGHT); setToolTipText(I18N.getString("ZoomStatusBar.tooltip")); addActionListener(this); } @@ -61,26 +59,6 @@ public void actionPerformed(ActionEvent e) { } } - /* - * (non-Javadoc) - * - * @see javax.swing.JComponent#getMinimumSize() - */ - @Override - public Dimension getMinimumSize() { - return minSize; - } - - /* - * (non-Javadoc) - * - * @see javax.swing.JComponent#getPreferredSize() - */ - @Override - public Dimension getPreferredSize() { - return getMinimumSize(); - } - public void clear() { setText(""); } @@ -90,7 +68,18 @@ public void update() { if (MapTool.getFrame().getCurrentZoneRenderer() != null) { double scale = MapTool.getFrame().getCurrentZoneRenderer().getZoneScale().getScale(); scale *= 100; - zoom = String.format("%d%%", (int) scale); + + if (scale < 10) { + zoom = String.format("%.4f%%", scale); + } else if (scale < 100) { + zoom = String.format("%.3f%%", scale); + } else if (scale < 1000) { + zoom = String.format("%.2f%%", scale); + } else if (scale < 10000) { + zoom = String.format("%.1f%%", scale); + } else { + zoom = String.format("%.0f%%", scale); + } } setText(zoom); }