From 677d13532b3c1a848ad4155a06bca532733396a0 Mon Sep 17 00:00:00 2001 From: goblin-3 Date: Fri, 2 Jun 2023 01:18:41 +0200 Subject: [PATCH] added multiple gridTypes for SquareGrid --- .../gridtool/AdjustGridControlPanelView.form | 44 +++++++--- .../client/tool/gridtool/GridTool.java | 25 ++++-- .../java/net/rptools/maptool/model/Grid.java | 16 ++++ .../net/rptools/maptool/model/SquareGrid.java | 83 ++++++++++++++----- 4 files changed, 130 insertions(+), 38 deletions(-) diff --git a/src/main/java/net/rptools/maptool/client/tool/gridtool/AdjustGridControlPanelView.form b/src/main/java/net/rptools/maptool/client/tool/gridtool/AdjustGridControlPanelView.form index d7d775ef72..d9fa9a2493 100644 --- a/src/main/java/net/rptools/maptool/client/tool/gridtool/AdjustGridControlPanelView.form +++ b/src/main/java/net/rptools/maptool/client/tool/gridtool/AdjustGridControlPanelView.form @@ -1,9 +1,9 @@
- + - + @@ -18,7 +18,7 @@ - + @@ -36,7 +36,7 @@ - + @@ -54,7 +54,7 @@ - + @@ -89,7 +89,7 @@ - + @@ -98,7 +98,7 @@ - + @@ -108,17 +108,17 @@ - + - + - + @@ -126,6 +126,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/net/rptools/maptool/client/tool/gridtool/GridTool.java b/src/main/java/net/rptools/maptool/client/tool/gridtool/GridTool.java index 737fd69589..413148eca5 100644 --- a/src/main/java/net/rptools/maptool/client/tool/gridtool/GridTool.java +++ b/src/main/java/net/rptools/maptool/client/tool/gridtool/GridTool.java @@ -16,14 +16,7 @@ import java.awt.Color; import java.awt.EventQueue; -import java.awt.event.ActionEvent; -import java.awt.event.FocusEvent; -import java.awt.event.FocusListener; -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.awt.event.MouseWheelEvent; +import java.awt.event.*; import java.util.Map; import javax.swing.*; import javax.swing.event.ChangeEvent; @@ -59,6 +52,8 @@ private enum Size { private final JSlider zoomSlider; private final JTextField gridSecondDimension; private final JLabel gridSecondDimensionLabel; + private final JComboBox gridTypeSelector; + private final AbeillePanel controlPanel; private int lastZoomIndex; @@ -91,6 +86,10 @@ public GridTool() { colorWell = (ColorWell) controlPanel.getComponent("colorWell"); colorWell.addActionListener(e -> copyControlPanelToGrid()); + gridTypeSelector = (JComboBox) controlPanel.getComponent("GridSelectorBox"); + gridTypeSelector.addFocusListener(new UpdateGridListener()); + + JButton closeButton = (JButton) controlPanel.getComponent("closeButton"); closeButton.addActionListener( e -> { @@ -140,6 +139,7 @@ private void copyGridToControlPanel() { gridOffsetXTextField.setText(Integer.toString(grid.getOffsetX())); gridOffsetYTextField.setText(Integer.toString(grid.getOffsetY())); colorWell.setColor(new Color(zone.getGridColor())); + gridTypeSelector.setSelectedItem(grid.getGridType()); // Setting the size must be done last as it triggers a ChangeEvent // which causes copyControlPanelToGrid() to be called. gridSizeSpinner.setValue(grid.getSize()); @@ -175,6 +175,7 @@ private void copyControlPanelToGrid() { grid.setOffset(getInt(gridOffsetXTextField, 0), getInt(gridOffsetYTextField, 0)); zone.setGridColor(colorWell.getColor().getRGB()); grid.setSize(Math.max((Integer) gridSizeSpinner.getValue(), Grid.MIN_GRID_SIZE)); + grid.setGridType(getString(gridTypeSelector, "Line")); } @Override @@ -207,6 +208,14 @@ private double getDouble(String value, double defaultValue) { } } + private String getString(JComboBox component, String defaultValue) { + try { + return component.getSelectedItem().toString(); + } catch (NullPointerException e) { + return defaultValue; + } + } + /* * (non-Javadoc) * diff --git a/src/main/java/net/rptools/maptool/model/Grid.java b/src/main/java/net/rptools/maptool/model/Grid.java index e4c815925a..510a46df7a 100644 --- a/src/main/java/net/rptools/maptool/model/Grid.java +++ b/src/main/java/net/rptools/maptool/model/Grid.java @@ -71,6 +71,12 @@ public abstract class Grid implements Cloneable { protected static final Logger log = LogManager.getLogger(); protected static final int CIRCLE_SEGMENTS = 60; + public static final String LINE_GRID = "Line"; + + public static final String DASH_GRID = "Dash"; + + public static final String INTERSECT_GRID = "Intersection"; + private static final Dimension NO_DIM = new Dimension(); private static final DirectionCalculator calculator = new DirectionCalculator(); private static Map gridShapeCache = new ConcurrentHashMap<>(); @@ -81,6 +87,8 @@ public abstract class Grid implements Cloneable { private Zone zone; private Area cellShape; + private String gridType; + public Grid() { setSize(AppPreferences.getDefaultGridSize()); } @@ -293,6 +301,10 @@ public void setOffset(int offsetX, int offsetY) { fireGridChanged(); } + public void setGridType(String gridType) { + this.gridType = gridType; + } + /** * @return The x component of the grid's offset. */ @@ -307,6 +319,10 @@ public int getOffsetY() { return offsetY; } + public String getGridType() { + return gridType; + } + public ZoneWalker createZoneWalker() { return null; } diff --git a/src/main/java/net/rptools/maptool/model/SquareGrid.java b/src/main/java/net/rptools/maptool/model/SquareGrid.java index 08e697136e..c4cdd86da2 100644 --- a/src/main/java/net/rptools/maptool/model/SquareGrid.java +++ b/src/main/java/net/rptools/maptool/model/SquareGrid.java @@ -14,12 +14,7 @@ */ package net.rptools.maptool.model; -import java.awt.Color; -import java.awt.Dimension; -import java.awt.Font; -import java.awt.FontMetrics; -import java.awt.Graphics2D; -import java.awt.Rectangle; +import java.awt.*; import java.awt.event.KeyEvent; import java.awt.geom.Area; import java.awt.geom.Point2D; @@ -329,27 +324,75 @@ public void draw(ZoneRenderer renderer, Graphics2D g, Rectangle bounds) { int startCol = (int) ((int) (bounds.x / gridSize) * gridSize); int startRow = (int) ((int) (bounds.y / gridSize) * gridSize); + String currentGridType = getGridType(); + float[] dashArray = {4f, 2f}; for (double row = startRow; row < bounds.y + bounds.height + gridSize; row += gridSize) { - if (AppState.getGridSize() == 1) { - g.drawLine(bounds.x, (int) (row + offY), bounds.x + bounds.width, (int) (row + offY)); + if (currentGridType == LINE_GRID) { + if (AppState.getGridSize() == 1) { + g.drawLine(bounds.x, (int) (row + offY), bounds.x + bounds.width, (int) (row + offY)); + } else { + g.fillRect( + bounds.x, + (int) (row + offY - (AppState.getGridSize() / 2)), + bounds.width, + AppState.getGridSize()); + } + } else if (currentGridType == DASH_GRID) { + g.setStroke( + new BasicStroke( + AppState.getGridSize(), + BasicStroke.CAP_BUTT, + BasicStroke.JOIN_ROUND, + 0, + dashArray, + 0)); + if (AppState.getGridSize() == 1) { + g.drawLine(bounds.x, (int) (row + offY), bounds.x + bounds.width, (int) (row + offY)); + } else { + g.drawLine( + bounds.x, + (int) (row + offY - (AppState.getGridSize() / 2)), + bounds.x + bounds.width, + (int) (row + offY - (AppState.getGridSize() / 2))); + } } else { - g.fillRect( - bounds.x, - (int) (row + offY - (AppState.getGridSize() / 2)), - bounds.width, - AppState.getGridSize()); + if (AppState.getGridSize() == 1) { + g.drawLine(bounds.x, (int) (row + offY), bounds.x + bounds.width, (int) (row + offY)); + } else { + g.drawLine(bounds.x, (int) (row + offY), bounds.x + bounds.width, (int) (row + offY)); + } } } for (double col = startCol; col < bounds.x + bounds.width + gridSize; col += gridSize) { - if (AppState.getGridSize() == 1) { - g.drawLine((int) (col + offX), bounds.y, (int) (col + offX), bounds.y + bounds.height); + if (currentGridType == LINE_GRID) { + if (AppState.getGridSize() == 1) { + g.drawLine((int) (col + offX), bounds.y, (int) (col + offX), bounds.y + bounds.height); + } else { + g.fillRect( + (int) (col + offX - (AppState.getGridSize() / 2)), + bounds.y, + AppState.getGridSize(), + bounds.height); + } + } else if (currentGridType == DASH_GRID) { + if (AppState.getGridSize() == 1) { + g.drawLine((int) (col + offX), bounds.y, (int) (col + offX), bounds.y + bounds.height); + } else { + // Draw the dashed line + g.drawLine( + (int) (col + offX - (AppState.getGridSize() / 2)), + bounds.y, + (int) (col + offX - (AppState.getGridSize() / 2)), + bounds.y + bounds.height); + } + } else { - g.fillRect( - (int) (col + offX - (AppState.getGridSize() / 2)), - bounds.y, - AppState.getGridSize(), - bounds.height); + if (AppState.getGridSize() == 1) { + g.drawLine((int) (col + offX), bounds.y, (int) (col + offX), bounds.y + bounds.height); + } else { + g.drawLine((int) (col + offX), bounds.y, (int) (col + offX), bounds.y + bounds.height); + } } } }