Skip to content

Commit

Permalink
Add option "Show cell grid" to area viewer
Browse files Browse the repository at this point in the history
Displays a grid that aligns with search map cells.
  • Loading branch information
Argent77 committed Apr 18, 2024
1 parent 42fda63 commit 90b768b
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 40 deletions.
64 changes: 49 additions & 15 deletions src/org/infinity/resource/are/viewer/AreaViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ public class AreaViewer extends ChildFrame {
private static final String LABEL_DRAW_CLOSED = "Draw closed";
private static final String LABEL_DRAW_OVERLAYS = "Enable overlays";
private static final String LABEL_ANIMATE_OVERLAYS = "Animate overlays";
private static final String LABEL_DRAW_GRID = "Show grid";
private static final String LABEL_DRAW_TILE_GRID = "Show tile grid";
private static final String LABEL_DRAW_CELL_GRID = "Show cell grid";

private final Listeners listeners;
private final Map map;
Expand Down Expand Up @@ -160,7 +161,8 @@ public class AreaViewer extends ChildFrame {
private JCheckBox cbDrawClosed;
private JCheckBox cbDrawOverlays;
private JCheckBox cbAnimateOverlays;
private JCheckBox cbDrawGrid;
private JCheckBox cbDrawTileGrid;
private JCheckBox cbDrawCellGrid;
private JCheckBox cbEnableSchedules;
private JComboBox<String> cbZoomLevel;
private JCheckBox cbLayerAmbientRange;
Expand Down Expand Up @@ -345,8 +347,11 @@ private void init() {
cbDrawClosed.setToolTipText("Draw opened or closed states of doors");
cbDrawClosed.addActionListener(getListeners());

cbDrawGrid = new JCheckBox(LABEL_DRAW_GRID);
cbDrawGrid.addActionListener(getListeners());
cbDrawTileGrid = new JCheckBox(LABEL_DRAW_TILE_GRID);
cbDrawTileGrid.addActionListener(getListeners());

cbDrawCellGrid = new JCheckBox(LABEL_DRAW_CELL_GRID);
cbDrawCellGrid.addActionListener(getListeners());

cbDrawOverlays = new JCheckBox(LABEL_DRAW_OVERLAYS);
cbDrawOverlays.setToolTipText("<html>Shows overlay tilesets.<br>"
Expand Down Expand Up @@ -376,7 +381,8 @@ private void init() {
t.add(new DefaultMutableTreeNode(bpwDayTime));
t.add(new DefaultMutableTreeNode(cbEnableSchedules));
t.add(new DefaultMutableTreeNode(cbDrawClosed));
t.add(new DefaultMutableTreeNode(cbDrawGrid));
t.add(new DefaultMutableTreeNode(cbDrawTileGrid));
t.add(new DefaultMutableTreeNode(cbDrawCellGrid));
t2 = new DefaultMutableTreeNode(cbDrawOverlays);
t2.add(new DefaultMutableTreeNode(cbAnimateOverlays));
t.add(t2);
Expand Down Expand Up @@ -757,9 +763,13 @@ private void initGuiSettings() {
setDoorState(Settings.DrawClosed);
}

// initializing grid
cbDrawGrid.setSelected(Settings.DrawGrid);
setTileGridEnabled(Settings.DrawGrid);
// initializing tile grid
cbDrawTileGrid.setSelected(Settings.DrawTileGrid);
setTileGridEnabled(Settings.DrawTileGrid);

// initializing cell grid
cbDrawCellGrid.setSelected(Settings.DrawCellGrid);
setCellGridEnabled(Settings.DrawCellGrid);

// initializing overlays
cbDrawOverlays.setSelected(Settings.DrawOverlays);
Expand Down Expand Up @@ -872,10 +882,13 @@ private void updateWindowTitle() {
overlayState = "disabled";
}

String gridState = isTileGridEnabled() ? "enabled" : "disabled";
String tileGridState = isTileGridEnabled() ? "enabled" : "disabled";

setTitle(String.format("%s (Time: %02d:00 (%s), Schedules: %s, Doors: %s, Overlays: %s, Grid: %s, Zoom: %d%%)",
windowTitle, getHour(), dayNight, scheduleState, doorState, overlayState, gridState, zoom));
String cellGridState = isCellGridEnabled() ? "enabled" : "disabled";

setTitle(String.format("%s (Time: %02d:00 (%s), Schedules: %s, Doors: %s, Overlays: %s, Tile Grid: %s, "
+ "Cell Grid: %s, Zoom: %d%%)",
windowTitle, getHour(), dayNight, scheduleState, doorState, overlayState, tileGridState, cellGridState, zoom));
}

/** Sets day time to a specific hour (0..23). */
Expand Down Expand Up @@ -1069,14 +1082,28 @@ private void setDoorStateLayers(boolean closed) {

/** Returns whether tile grid on map has been enabled. */
private boolean isTileGridEnabled() {
return Settings.DrawGrid;
return Settings.DrawTileGrid;
}

/** Enable/disable tile grid on map. */
private void setTileGridEnabled(boolean enable) {
Settings.DrawGrid = enable;
Settings.DrawTileGrid = enable;
if (rcCanvas != null) {
rcCanvas.setGridEnabled(Settings.DrawGrid);
rcCanvas.setTileGridEnabled(Settings.DrawTileGrid);
}
updateWindowTitle();
}

/** Returns whether cell grid on map has been enabled. */
private boolean isCellGridEnabled() {
return Settings.DrawCellGrid;
}

/** Enable/disable cell grid on map. */
private void setCellGridEnabled(boolean enable) {
Settings.DrawCellGrid = enable;
if (rcCanvas != null) {
rcCanvas.setCellGridEnabled(Settings.DrawCellGrid);
}
updateWindowTitle();
}
Expand Down Expand Up @@ -2303,13 +2330,20 @@ public void actionPerformed(ActionEvent event) {
} finally {
WindowBlocker.blockWindow(AreaViewer.this, false);
}
} else if (cb == cbDrawGrid) {
} else if (cb == cbDrawTileGrid) {
WindowBlocker.blockWindow(AreaViewer.this, true);
try {
setTileGridEnabled(cb.isSelected());
} finally {
WindowBlocker.blockWindow(AreaViewer.this, false);
}
} else if (cb == cbDrawCellGrid) {
WindowBlocker.blockWindow(AreaViewer.this, true);
try {
setCellGridEnabled(cb.isSelected());
} finally {
WindowBlocker.blockWindow(AreaViewer.this, false);
}
} else if (cb == cbDrawOverlays) {
WindowBlocker.blockWindow(AreaViewer.this, true);
try {
Expand Down
19 changes: 14 additions & 5 deletions src/org/infinity/resource/are/viewer/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public class Settings {
// Current visibility state of overlays
public static boolean DrawOverlays = getDefaultDrawOverlays();
// Current visibility state of the tile grid
public static boolean DrawGrid = getDefaultDrawGrid();
public static boolean DrawTileGrid = getDefaultDrawTileGrid();
// Current visibility state of the cell grid
public static boolean DrawCellGrid = getDefaultDrawCellGrid();
// Current visibility state of ambient range items
public static boolean ShowAmbientRanges = getDefaultAmbientRanges();
// Current visibility state of container target locations
Expand Down Expand Up @@ -132,7 +134,8 @@ public class Settings {
private static final String PREFS_MOUSEWHEELZOOM = "MouseWheelZoom";
private static final String PREFS_DRAWCLOSED = "DrawClosed";
private static final String PREFS_DRAWOVERLAYS = "DrawOverlays";
private static final String PREFS_DRAWGRID = "DrawGrid";
private static final String PREFS_DRAWTILEGRID = "DrawGrid";
private static final String PREFS_DRAWCELLGRID = "DrawCellGrid";
private static final String PREFS_SIDEBARCONTROLS = "SidebarControls";
private static final String PREFS_SHOWACTORFRAME = "ShowActorFrame";
private static final String PREFS_SHOWACTORSELECTION = "ShowActorSelectionCircle";
Expand Down Expand Up @@ -222,7 +225,8 @@ public static void loadSettings(boolean force) {
EnableSchedules = prefs.getBoolean(PREFS_ENABLESCHEDULES, getDefaultEnableSchedules());
DrawClosed = prefs.getBoolean(PREFS_DRAWCLOSED, getDefaultDrawClosed());
DrawOverlays = prefs.getBoolean(PREFS_DRAWOVERLAYS, getDefaultDrawOverlays());
DrawGrid = prefs.getBoolean(PREFS_DRAWGRID, getDefaultDrawGrid());
DrawTileGrid = prefs.getBoolean(PREFS_DRAWTILEGRID, getDefaultDrawTileGrid());
DrawCellGrid = prefs.getBoolean(PREFS_DRAWCELLGRID, getDefaultDrawCellGrid());
ShowAmbientRanges = prefs.getBoolean(PREFS_SHOWAMBIENT, getDefaultAmbientRanges());
ShowContainerTargets = prefs.getBoolean(PREFS_SHOWCONTAINERTARGETS, getDefaultShowContainerTargets());
ShowDoorTargets = prefs.getBoolean(PREFS_SHOWDOORTARGETS, getDefaultShowDoorTargets());
Expand Down Expand Up @@ -286,7 +290,8 @@ public static void storeSettings(boolean force) {
prefs.putBoolean(PREFS_ENABLESCHEDULES, EnableSchedules);
prefs.putBoolean(PREFS_DRAWCLOSED, DrawClosed);
prefs.putBoolean(PREFS_DRAWOVERLAYS, DrawOverlays);
prefs.putBoolean(PREFS_DRAWGRID, DrawGrid);
prefs.putBoolean(PREFS_DRAWTILEGRID, DrawTileGrid);
prefs.putBoolean(PREFS_DRAWCELLGRID, DrawCellGrid);
prefs.putBoolean(PREFS_SHOWAMBIENT, ShowAmbientRanges);
prefs.putBoolean(PREFS_SHOWCONTAINERTARGETS, ShowContainerTargets);
prefs.putBoolean(PREFS_SHOWDOORTARGETS, ShowDoorTargets);
Expand Down Expand Up @@ -381,7 +386,11 @@ public static boolean getDefaultDrawOverlays() {
return true;
}

public static boolean getDefaultDrawGrid() {
public static boolean getDefaultDrawTileGrid() {
return false;
}

public static boolean getDefaultDrawCellGrid() {
return false;
}

Expand Down
69 changes: 49 additions & 20 deletions src/org/infinity/resource/are/viewer/TilesetRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public enum RenderMode {
private boolean hasChangedOverlays;
private boolean hasChangedDoorState;
private boolean isClosed = false; // opened/closed state of door tiles
private boolean showGrid = false; // indicates whether to draw a grid on the tiles
private boolean showTileGrid = false; // indicates whether to draw a grid on the tiles
private boolean showCellGrid = false; // indicates whether to draw a grid on the cells
private boolean forcedInterpolation = false; // indicates whether to use a pre-defined interpolation type or set one
// based on zoom factor
private double zoomFactor = 1.0; // zoom factor for drawing the map
Expand Down Expand Up @@ -347,13 +348,25 @@ public void setLighting(int lighting) {
}
}

public boolean isGridEnabled() {
return showGrid;
public boolean isTileGridEnabled() {
return showTileGrid;
}

public void setGridEnabled(boolean enable) {
if (enable != showGrid) {
showGrid = enable;
public void setTileGridEnabled(boolean enable) {
if (enable != showTileGrid) {
showTileGrid = enable;
hasChangedAppearance = true;
updateDisplay();
}
}

public boolean isCellGridEnabled() {
return showCellGrid;
}

public void setCellGridEnabled(boolean enable) {
if (enable != showCellGrid) {
showCellGrid = enable;
hasChangedAppearance = true;
updateDisplay();
}
Expand Down Expand Up @@ -538,20 +551,11 @@ protected void updateSize() {
@Override
protected void paintCanvas(Graphics g) {
super.paintCanvas(g);
if (showGrid) {
double tileWidth = 64.0 * zoomFactor;
double tileHeight = 64.0 * zoomFactor;
double mapWidth = getMapWidth(true);
double mapHeight = getMapHeight(true);
g.setColor(Color.GRAY);
for (double curY = 0.0; curY < mapHeight; curY += tileHeight) {
for (double curX = 0.0; curX < mapWidth; curX += tileWidth) {
g.drawLine((int) Math.ceil(curX), (int) Math.ceil(curY + tileHeight), (int) Math.ceil(curX + tileWidth),
(int) Math.ceil(curY + tileHeight));
g.drawLine((int) Math.ceil(curX + tileWidth), (int) Math.ceil(curY), (int) Math.ceil(curX + tileWidth),
(int) Math.ceil(curY + tileHeight));
}
}
if (showCellGrid) {
drawGrid(g, 16.0, 12.0, Color.DARK_GRAY);
}
if (showTileGrid) {
drawGrid(g, 64.0, 64.0, Color.GRAY);
}
}

Expand Down Expand Up @@ -754,6 +758,31 @@ private boolean hasOverlay(int ovlIdx) {
return false;
}

// Draws a grid on the map with the specified parameters
private void drawGrid(Graphics g, double gridWidth, double gridHeight, Color color) {
if (g == null) {
System.err.println("TilesetRenderer.drawGrid: Graphics argument is null");
return;
}
if (color == null) {
System.err.println("TilesetRenderer.drawGrid: Color argument is null");
return;
}
final double gridWidthZoomed = gridWidth * zoomFactor;
final double gridHeightZoomed = gridHeight * zoomFactor;
final double mapWidth = getMapWidth(true);
final double mapHeight = getMapHeight(true);
g.setColor(color);
for (double curY = 0.0; curY < mapHeight; curY += gridHeightZoomed) {
for (double curX = 0.0; curX < mapWidth; curX += gridWidthZoomed) {
g.drawLine((int) Math.ceil(curX), (int) Math.ceil(curY + gridHeightZoomed), (int) Math.ceil(curX + gridWidthZoomed),
(int) Math.ceil(curY + gridHeightZoomed));
g.drawLine((int) Math.ceil(curX + gridWidthZoomed), (int) Math.ceil(curY), (int) Math.ceil(curX + gridWidthZoomed),
(int) Math.ceil(curY + gridHeightZoomed));
}
}
}

// draws all tiles of the map
private void drawAllTiles() {
final Tileset ts = listTilesets.get(0);
Expand Down

0 comments on commit 90b768b

Please sign in to comment.