From a91e9e6ddb3e956a131775d8af86ad1cbb17f4d6 Mon Sep 17 00:00:00 2001 From: Kenneth VanderLinde Date: Tue, 5 Dec 2023 23:54:00 -0800 Subject: [PATCH] Add a DebugRenderer to render debugging shapes This could be refined, but for now is sufficient to render the two shapes supported by ZoneRenderer. It also renders using a transformed graphics context rather than transforming the shapes when they are set. Some liberties were taken with the renderer vs the existing behaviour: 1. The renderer supports any number of shapes and just cycles through a palette. 2. The shapes are rendered with an opaque border but a translucent fill so they don't complete obscure what is behind them. --- .../ui/zone/renderer/DebugRenderer.java | 64 +++++++++++++++++++ .../client/ui/zone/renderer/ZoneRenderer.java | 56 ++-------------- 2 files changed, 71 insertions(+), 49 deletions(-) create mode 100644 src/main/java/net/rptools/maptool/client/ui/zone/renderer/DebugRenderer.java diff --git a/src/main/java/net/rptools/maptool/client/ui/zone/renderer/DebugRenderer.java b/src/main/java/net/rptools/maptool/client/ui/zone/renderer/DebugRenderer.java new file mode 100644 index 0000000000..9d632439b0 --- /dev/null +++ b/src/main/java/net/rptools/maptool/client/ui/zone/renderer/DebugRenderer.java @@ -0,0 +1,64 @@ +/* + * 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.zone.renderer; + +import com.google.common.collect.Iterators; +import java.awt.AlphaComposite; +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.Shape; + +public class DebugRenderer { + private final RenderHelper renderHelper; + private final Color[] palette; + + public DebugRenderer(RenderHelper renderHelper) { + this.renderHelper = renderHelper; + palette = new Color[] {Color.red, Color.green, Color.blue}; + } + + public void renderShapes(Graphics2D g2d, Iterable shapes) { + renderHelper.render(g2d, worldG -> renderWorld(worldG, shapes)); + } + + private void renderWorld(Graphics2D worldG, Iterable shapes) { + worldG.setComposite(AlphaComposite.SrcOver); + // Keep the line a consistent thickness + worldG.setStroke(new BasicStroke(1 / (float) worldG.getTransform().getScaleX())); + + var paletteIterator = Iterators.cycle(palette); + for (final var shape : shapes) { + final var color = paletteIterator.next(); + + if (shape == null) { + continue; + } + + var fillColor = color.darker(); + fillColor = + new Color( + fillColor.getRed(), + fillColor.getGreen(), + fillColor.getBlue(), + fillColor.getAlpha() / 3); + worldG.setColor(fillColor); + worldG.fill(shape); + + worldG.setColor(color); + worldG.draw(shape); + } + } +} 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 5399e8ab93..e2ef190eec 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 @@ -160,6 +160,7 @@ public class ZoneRenderer extends JComponent private final LumensRenderer lumensRenderer; private final FogRenderer fogRenderer; private final VisionOverlayRenderer visionOverlayRenderer; + private final DebugRenderer debugRenderer; /** * Constructor for the ZoneRenderer from a zone. @@ -183,6 +184,7 @@ public ZoneRenderer(Zone zone) { this.lumensRenderer = new LumensRenderer(renderHelper, zone, zoneView); this.fogRenderer = new FogRenderer(renderHelper, zone, zoneView); this.visionOverlayRenderer = new VisionOverlayRenderer(renderHelper, zone, zoneView); + this.debugRenderer = new DebugRenderer(renderHelper); repaintDebouncer = new DebounceExecutor(1000 / AppPreferences.getFrameRateCap(), this::repaint); setFocusable(true); @@ -1219,6 +1221,9 @@ public void renderZone(Graphics2D g2d, PlayerView view) { lightSourceIconOverlay.paintOverlay(this, g2d); } timer.stop("lightSourceIconOverlay.paintOverlay"); + + debugRenderer.renderShapes(g2d, Arrays.asList(shape, shape2)); + // g2d.setColor(Color.red); // for (AreaMeta meta : getTopologyAreaData().getAreaList()) { // Area area = new @@ -1920,20 +1925,6 @@ public void renderPath( timer.stop("renderPath-3"); } - // g.translate(getViewOffsetX(), getViewOffsetY()); - // g.scale(getScale(), getScale()); - // for debugging purposes... - if (shape != null) { - g.setColor(Color.red); - g.fill(shape); - g.draw(shape); - } - if (shape2 != null) { - g.setColor(Color.blue); - g.fill(shape2); - g.draw(shape2); - } - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldRendering); } @@ -1983,11 +1974,7 @@ public void setShape(Shape shape) { return; } - AffineTransform at = new AffineTransform(); - at.translate(getViewOffsetX(), getViewOffsetY()); - at.scale(getScale(), getScale()); - - this.shape = at.createTransformedShape(shape); + this.shape = shape; } public void setShape2(Shape shape) { @@ -1995,36 +1982,7 @@ public void setShape2(Shape shape) { return; } - AffineTransform at = new AffineTransform(); - at.translate(getViewOffsetX(), getViewOffsetY()); - at.scale(getScale(), getScale()); - - this.shape2 = at.createTransformedShape(shape); - } - - public Shape getShape() { - return shape; - } - - public Shape getShape2() { - return shape2; - } - - public void drawShape(Shape shape, int x, int y) { - Graphics2D g = (Graphics2D) this.getGraphics(); - - Grid grid = zone.getGrid(); - double cwidth = grid.getCellWidth() * getScale(); - double cheight = grid.getCellHeight() * getScale(); - - double iwidth = cwidth; - double iheight = cheight; - - ScreenPoint sp = ScreenPoint.fromZonePoint(this, x, y); - - AffineTransform at = new AffineTransform(); - at.translate(sp.x, sp.y); - g.draw(at.createTransformedShape(shape)); + this.shape2 = shape; } public void showBlockedMoves(