Skip to content

Commit

Permalink
Add a DebugRenderer to render debugging shapes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kwvanderlinde committed Dec 9, 2023
1 parent 3a78657 commit a91e9e6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -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
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
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<Shape> shapes) {
renderHelper.render(g2d, worldG -> renderWorld(worldG, shapes));
}

private void renderWorld(Graphics2D worldG, Iterable<Shape> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -1983,48 +1974,15 @@ 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) {
if (shape == null) {
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(
Expand Down

0 comments on commit a91e9e6

Please sign in to comment.