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

Added multiple gridTypes for SquareGrid #4105

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="net.rptools.maptool.client.tool.gridtool.AdjustGridControlPanelView">
<grid id="241de" binding="mainPanel" layout-manager="GridLayoutManager" row-count="8" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="241de" binding="mainPanel" layout-manager="GridLayoutManager" row-count="9" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="5" left="5" bottom="5" right="5"/>
<constraints>
<xy x="10" y="10" width="308" height="258"/>
<xy x="10" y="10" width="351" height="318"/>
</constraints>
<properties/>
<border type="none"/>
Expand All @@ -18,7 +18,7 @@
</component>
<component id="4491d" class="javax.swing.JSpinner">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="2" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<name value="gridSize"/>
Expand All @@ -36,7 +36,7 @@
</component>
<component id="fc376" class="javax.swing.JTextField">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="1" column="2" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<columns value="6"/>
Expand All @@ -54,7 +54,7 @@
</component>
<component id="cbe69" class="javax.swing.JTextField">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" column="2" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<columns value="6"/>
Expand Down Expand Up @@ -89,7 +89,7 @@
</component>
<component id="e83dd" class="javax.swing.JSlider">
<constraints>
<grid row="5" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="5" column="0" row-span="1" col-span="4" vsize-policy="2" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<majorTickSpacing value="20"/>
Expand All @@ -98,7 +98,7 @@
</component>
<component id="3c06e" class="javax.swing.JButton">
<constraints>
<grid row="7" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
<grid row="8" column="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<actionCommand value="Close"/>
Expand All @@ -108,24 +108,48 @@
</component>
<vspacer id="9fd">
<constraints>
<grid row="6" column="2" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="6" column="2" row-span="1" col-span="2" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<hspacer id="47d6b">
<constraints>
<grid row="4" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="4" column="2" row-span="1" col-span="2" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="6cd53" class="javax.swing.JTextField">
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="3" column="2" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<columns value="6"/>
<name value="offsetY"/>
<text value=""/>
</properties>
</component>
<component id="1ed06" class="javax.swing.JLabel">
<constraints>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Grid type"/>
</properties>
</component>
<component id="3f6fb" class="javax.swing.JComboBox">
<constraints>
<grid row="7" column="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<background color="-12236470"/>
<editable value="false"/>
<enabled value="true"/>
<model>
<item value="Line"/>
<item value="Dash"/>
<item value="Intersection"/>
</model>
<name value="GridSelectorBox"/>
</properties>
</component>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,6 +52,8 @@ private enum Size {
private final JSlider zoomSlider;
private final JTextField gridSecondDimension;
private final JLabel gridSecondDimensionLabel;
private final JComboBox<String> gridTypeSelector;

private final AbeillePanel controlPanel;

private int lastZoomIndex;
Expand Down Expand Up @@ -91,6 +86,10 @@ public GridTool() {
colorWell = (ColorWell) controlPanel.getComponent("colorWell");
colorWell.addActionListener(e -> copyControlPanelToGrid());

gridTypeSelector = (JComboBox<String>) controlPanel.getComponent("GridSelectorBox");
gridTypeSelector.addFocusListener(new UpdateGridListener());


JButton closeButton = (JButton) controlPanel.getComponent("closeButton");
closeButton.addActionListener(
e -> {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -207,6 +208,14 @@ private double getDouble(String value, double defaultValue) {
}
}

private String getString(JComboBox<String> component, String defaultValue) {
try {
return component.getSelectedItem().toString();
} catch (NullPointerException e) {
return defaultValue;
}
}

/*
* (non-Javadoc)
*
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/net/rptools/maptool/model/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Area> gridShapeCache = new ConcurrentHashMap<>();
Expand All @@ -81,6 +87,8 @@ public abstract class Grid implements Cloneable {
private Zone zone;
private Area cellShape;

private String gridType;

public Grid() {
setSize(AppPreferences.getDefaultGridSize());
}
Expand Down Expand Up @@ -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.
*/
Expand All @@ -307,6 +319,10 @@ public int getOffsetY() {
return offsetY;
}

public String getGridType() {
return gridType;
}

public ZoneWalker createZoneWalker() {
return null;
}
Expand Down
83 changes: 63 additions & 20 deletions src/main/java/net/rptools/maptool/model/SquareGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down