From 70ed94d2cafb977bae004b7fbcbe8fe89ae6d529 Mon Sep 17 00:00:00 2001 From: bubblobill <45483160+bubblobill@users.noreply.github.com> Date: Fri, 19 Jul 2024 14:31:07 +0800 Subject: [PATCH] BoardTool - Changed text fields to spinners. Added spinners for image size. Added label and tooltip strings. Zone Renderer - Incorporated scale factors into drawing the map image. --- .../AdjustBoardControlPanelView.form | 72 ++++++++--- .../AdjustBoardControlPanelView.java | 2 + .../client/tool/boardtool/BoardTool.java | 120 +++++++++++++----- .../client/ui/zone/renderer/ZoneRenderer.java | 4 +- .../rptools/maptool/language/i18n.properties | 3 + 5 files changed, 152 insertions(+), 49 deletions(-) diff --git a/src/main/java/net/rptools/maptool/client/tool/boardtool/AdjustBoardControlPanelView.form b/src/main/java/net/rptools/maptool/client/tool/boardtool/AdjustBoardControlPanelView.form index d6d6b65789..4e15272cfd 100644 --- a/src/main/java/net/rptools/maptool/client/tool/boardtool/AdjustBoardControlPanelView.form +++ b/src/main/java/net/rptools/maptool/client/tool/boardtool/AdjustBoardControlPanelView.form @@ -1,16 +1,16 @@
- + - + - + @@ -62,43 +62,45 @@ - + - + - + + + + - - - + - + - + + + + - - - + @@ -108,9 +110,49 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/net/rptools/maptool/client/tool/boardtool/AdjustBoardControlPanelView.java b/src/main/java/net/rptools/maptool/client/tool/boardtool/AdjustBoardControlPanelView.java index 724d9e7a5b..4a9b3bbeb7 100644 --- a/src/main/java/net/rptools/maptool/client/tool/boardtool/AdjustBoardControlPanelView.java +++ b/src/main/java/net/rptools/maptool/client/tool/boardtool/AdjustBoardControlPanelView.java @@ -19,6 +19,8 @@ public class AdjustBoardControlPanelView { private JPanel mainPanel; + private JSpinner spinner1; + private JSpinner spinner2; public JComponent getRootComponent() { return mainPanel; diff --git a/src/main/java/net/rptools/maptool/client/tool/boardtool/BoardTool.java b/src/main/java/net/rptools/maptool/client/tool/boardtool/BoardTool.java index a25560d39a..6a67a0e3f3 100644 --- a/src/main/java/net/rptools/maptool/client/tool/boardtool/BoardTool.java +++ b/src/main/java/net/rptools/maptool/client/tool/boardtool/BoardTool.java @@ -14,26 +14,19 @@ */ package net.rptools.maptool.client.tool.boardtool; -import java.awt.Dimension; -import java.awt.Image; -import java.awt.Point; +import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; +import java.awt.geom.Point2D; +import java.text.ParseException; import java.util.Map; -import javax.swing.AbstractAction; -import javax.swing.Action; -import javax.swing.JButton; -import javax.swing.JRadioButton; -import javax.swing.JTextField; -import javax.swing.KeyStroke; -import javax.swing.SwingUtilities; +import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; -import javax.swing.text.JTextComponent; import net.rptools.maptool.client.AppState; import net.rptools.maptool.client.MapTool; import net.rptools.maptool.client.ScreenPoint; @@ -46,7 +39,8 @@ import net.rptools.maptool.model.drawing.DrawablePaint; import net.rptools.maptool.model.drawing.DrawableTexturePaint; import net.rptools.maptool.util.ImageManager; -import net.rptools.maptool.util.StringUtil; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; /** * Allows user to re-position the background map (internally called the 'board'). This entire class @@ -55,6 +49,7 @@ */ public class BoardTool extends DefaultTool { private static final long serialVersionUID = 98389912045059L; + private static final Logger log = LogManager.getLogger(BoardTool.class); // Context variables private static Zone zone; @@ -62,6 +57,8 @@ public class BoardTool extends DefaultTool { // Status variables private static Point boardPosition = new Point(0, 0); + private static Point2D boardSize = new Point2D.Float(1f, 1f); + private static Point2D boardScale = new Point2D.Float(1f, 1f); private static Dimension snap = new Dimension(1, 1); // Action control variables @@ -70,8 +67,10 @@ public class BoardTool extends DefaultTool { private Point boardStart; // UI button fields - private final JTextField boardPositionXTextField; - private final JTextField boardPositionYTextField; + private final JSpinner boardPositionXSpinner; + private final JSpinner boardPositionYSpinner; + private final JSpinner boardSizeXSpinner; + private final JSpinner boardSizeYSpinner; private final AbeillePanel controlPanel; private final JRadioButton snapNoneButton; private final JRadioButton snapGridButton; @@ -82,11 +81,21 @@ public BoardTool() { // Create the control panel controlPanel = new AbeillePanel(new AdjustBoardControlPanelView().getRootComponent()); - boardPositionXTextField = (JTextField) controlPanel.getComponent("offsetX"); - boardPositionXTextField.addKeyListener(new UpdateBoardListener()); + boardPositionXSpinner = (JSpinner) controlPanel.getComponent("offsetX"); + boardPositionXSpinner.setModel( + new SpinnerNumberModel(0, Integer.MIN_VALUE, Integer.MAX_VALUE, 1)); + boardPositionXSpinner.addChangeListener(new spinnerListener()); - boardPositionYTextField = (JTextField) controlPanel.getComponent("offsetY"); - boardPositionYTextField.addKeyListener(new UpdateBoardListener()); + boardPositionYSpinner = (JSpinner) controlPanel.getComponent("offsetY"); + boardPositionYSpinner.setModel( + new SpinnerNumberModel(0, Integer.MIN_VALUE, Integer.MAX_VALUE, 1)); + boardPositionYSpinner.addChangeListener(new spinnerListener()); + + // getBoardSizing(); + + boardSizeXSpinner = (JSpinner) controlPanel.getComponent("sizeX"); + + boardSizeYSpinner = (JSpinner) controlPanel.getComponent("sizeY"); ActionListener enforceRules = evt -> enforceButtonRules(); snapNoneButton = (JRadioButton) controlPanel.getComponent("snapNone"); @@ -132,6 +141,27 @@ private Dimension getTileSize() { return tileSize; } + /** Figure out how big the board image is. */ + private void getBoardSizing() { + if (renderer != null) { + boardScale = new Point2D.Float(zone.getImageScaleX(), zone.getImageScaleY()); + if (zone.getMapAssetId() != null) { + Image board = ImageManager.getImage(zone.getMapAssetId()); + boardSize = new Point2D.Float(board.getWidth(null), board.getHeight(null)); + + boardSizeXSpinner.setModel( + new SpinnerNumberModel( + boardSize.getX() * boardScale.getX(), Grid.MIN_GRID_SIZE, Integer.MAX_VALUE, 1)); + boardSizeYSpinner.setModel( + new SpinnerNumberModel( + boardSize.getY() * boardScale.getY(), Grid.MIN_GRID_SIZE, Integer.MAX_VALUE, 1)); + + boardSizeXSpinner.addChangeListener(new spinnerListener()); + boardSizeYSpinner.addChangeListener(new spinnerListener()); + } + } + } + /** Moves the board to the nearest snap intersection. Modifies GUI. */ private void snapBoard() { boardPosition.x = (Math.round(boardPosition.x / snap.width) * snap.width); @@ -152,8 +182,8 @@ private void setSnap(int x, int y) { } private void updateGUI() { - boardPositionXTextField.setText(Integer.toString(boardPosition.x)); - boardPositionYTextField.setText(Integer.toString(boardPosition.y)); + boardPositionXSpinner.setValue(boardPosition.x); + boardPositionYSpinner.setValue(boardPosition.y); } /** @@ -163,13 +193,16 @@ private void updateGUI() { private void copyBoardToControlPanel() { boardPosition.x = zone.getBoardX(); boardPosition.y = zone.getBoardY(); + getBoardSizing(); snapBoard(); updateGUI(); } private void copyControlPanelToBoard() { - boardPosition.x = getInt(boardPositionXTextField, 0); - boardPosition.y = getInt(boardPositionYTextField, 0); + boardPosition.x = (int) boardPositionXSpinner.getModel().getValue(); + boardPosition.y = (int) boardPositionYSpinner.getModel().getValue(); + zone.setImageScaleX((float) boardScale.getX()); + zone.setImageScaleY((float) boardScale.getY()); zone.setBoard(boardPosition); } @@ -183,16 +216,6 @@ public String getInstructions() { return "tool.boardtool.instructions"; } - /** - * Parses the text field of the component into a number, returning the default value if the text - * field is _not_ a number. - */ - private int getInt(JTextComponent component, int defaultValue) { - // Get the string from the component, then - // call the more-generic getInt-from-a-string - return StringUtil.parseInteger(component.getText(), defaultValue); - } - /* * private double getDouble(String value, double defaultValue) { try { return value.length() > 0 ? Double.parseDouble(value.trim()) : defaultValue; } catch (NumberFormatException e) { return 0; } * } @@ -353,4 +376,37 @@ private void enforceButtonRules() { updateGUI(); zone.setBoard(boardPosition); } + + private class spinnerListener implements ChangeListener { + @Override + public void stateChanged(ChangeEvent e) { + JSpinner spinner = (JSpinner) e.getSource(); + try { + spinner.commitEdit(); + } catch (ParseException pe) { + // Edited value is invalid, revert the spinner to the last valid value, + JComponent editor = spinner.getEditor(); + if (editor instanceof JSpinner.DefaultEditor) { + ((JSpinner.DefaultEditor) editor).getTextField().setValue(spinner.getValue()); + } + } + if (spinner.getName().startsWith("size")) { + updateImageScale(spinner); + } + + copyControlPanelToBoard(); + } + + private void updateImageScale(JSpinner spinner) { + if (spinner.getName().endsWith("X")) { + boardScale.setLocation( + ((SpinnerNumberModel) spinner.getModel()).getNumber().floatValue() / boardSize.getX(), + boardScale.getY()); + } else { + boardScale.setLocation( + boardScale.getX(), + ((SpinnerNumberModel) spinner.getModel()).getNumber().floatValue() / boardSize.getY()); + } + } + } } diff --git a/src/main/java/net/rptools/maptool/client/ui/zone/renderer/ZoneRenderer.java b/src/main/java/net/rptools/maptool/client/ui/zone/renderer/ZoneRenderer.java index 99c7a02dce..7f409a549e 100644 --- a/src/main/java/net/rptools/maptool/client/ui/zone/renderer/ZoneRenderer.java +++ b/src/main/java/net/rptools/maptool/client/ui/zone/renderer/ZoneRenderer.java @@ -1284,8 +1284,8 @@ protected void renderBoard(Graphics2D g, PlayerView view) { mapImage, getViewOffsetX() + (int) (zone.getBoardX() * scaleFactor), getViewOffsetY() + (int) (zone.getBoardY() * scaleFactor), - (int) (mapImage.getWidth() * scaleFactor), - (int) (mapImage.getHeight() * scaleFactor), + (int) (mapImage.getWidth() * scaleFactor * zone.getImageScaleX()), + (int) (mapImage.getHeight() * scaleFactor * zone.getImageScaleY()), null); } bbg.dispose(); diff --git a/src/main/resources/net/rptools/maptool/language/i18n.properties b/src/main/resources/net/rptools/maptool/language/i18n.properties index ed0bf163ea..3bbf2c92cc 100644 --- a/src/main/resources/net/rptools/maptool/language/i18n.properties +++ b/src/main/resources/net/rptools/maptool/language/i18n.properties @@ -397,6 +397,9 @@ AdjustBoardDialog.snapto = Snap to: AdjustBoardDialog.none.tooltip = No snap. AdjustBoardDialog.grid.tooltip = Snap to the token grid. AdjustBoardDialog.tile.tooltip = Snap to the repeating background texture. +AdjustBoardDialog.size.x = Horizontal Size +AdjustBoardDialog.size.y = Vertical Size +AdjustBoardDialog.size.tooltip = Change the size of the image GridControlPanel.sizeInPixels = Grid Size: GridControlPanel.sizeInPixels.second = 2nd Size: