Skip to content

Commit

Permalink
Rework LineCellTemplate path and bounds
Browse files Browse the repository at this point in the history
`calcPath()` has been simplified to essentially Bressenham's algorithm. It only calculates and returns a path, while
`getPath()` (previously unused) will call it if needed.

`getBounds()` now correctly accounts for the path's quadrant and the position of the first vertex to produce a correctly
located and sized bounding box. This method was also simplified to only look at the two endpoints rather than iterating
over the entire line.

All fields except `pathVertex` are now marked as `transient` since they can be recovered from only `getVertex()` and
`pathVertex`. Correspondingly, the quadrant is not longer part of the DTO, and the serialized XML is much smaller.

With the above changes in, it was natural to simplify `LineCellTemplateTool` to avoid a lot of case work, especially in
not relying on mutations of `getPathVertex()`.
  • Loading branch information
kwvanderlinde committed Apr 12, 2024
1 parent 9583e4f commit 2f3bac8
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import javax.swing.SwingUtilities;
import net.rptools.maptool.client.ScreenPoint;
import net.rptools.maptool.client.swing.SwingUtil;
import net.rptools.maptool.client.tool.Tool;
import net.rptools.maptool.client.ui.zone.renderer.ZoneRenderer;
import net.rptools.maptool.model.ZonePoint;
import net.rptools.maptool.model.drawing.AbstractTemplate;
import net.rptools.maptool.model.drawing.AbstractTemplate.Quadrant;
import net.rptools.maptool.model.drawing.LineCellTemplate;
import net.rptools.maptool.model.drawing.Pen;

Expand Down Expand Up @@ -173,7 +171,6 @@ public void mousePressed(MouseEvent aE) {
protected void handleMouseMovement(MouseEvent e) {
// Setting anchor point?
LineCellTemplate lt = (LineCellTemplate) template;
ZonePoint pathVertex = lt.getPathVertex();
ZonePoint vertex = lt.getVertex();

if (!anchorSet) {
Expand All @@ -189,38 +186,21 @@ protected void handleMouseMovement(MouseEvent e) {
template.setRadius(getRadiusAtMouse(e));
controlOffset = null;

// The path vertex remains null until it is set the first time.
if (pathVertex == null) {
pathVertex = new ZonePoint(vertex.x, vertex.y);
lt.setPathVertex(pathVertex);
} // endif
if (setCellAtMouse(e, pathVertex)) lt.clearPath();
ZonePoint pathVertex = getCellAtMouse(e);
lt.setPathVertex(pathVertex);
renderer.repaint();

// Let control move the path anchor
} else if (SwingUtil.isControlDown(e)) {
ZonePoint pathVertex = lt.getPathVertex();
handleControlOffset(e, pathVertex);
lt.setPathVertex(pathVertex);

// Set the final radius
} else {
template.setRadius(getRadiusAtMouse(e));
renderer.repaint();
controlOffset = null;
return;
} // endif

// Quadrant change?
if (pathVertex != null) {
ZonePoint mouse = new ScreenPoint(e.getX(), e.getY()).convertToZone(renderer);
int dx = mouse.x - vertex.x;
int dy = mouse.y - vertex.y;
AbstractTemplate.Quadrant quadrant =
(dx < 0)
? (dy < 0 ? Quadrant.NORTH_WEST : Quadrant.SOUTH_WEST)
: (dy < 0 ? Quadrant.NORTH_EAST : Quadrant.SOUTH_EAST);
if (quadrant != lt.getQuadrant()) {
lt.setQuadrant(quadrant);
renderer.repaint();
} // endif
} // endif
}
}
3 changes: 0 additions & 3 deletions src/main/java/net/rptools/maptool/model/drawing/Drawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,6 @@ static Drawable fromDto(DrawableDto drawableDto) {
drawable.setRadius(dto.getRadius());
var vertex = dto.getVertex();
drawable.setVertex(new ZonePoint(vertex.getX(), vertex.getY()));
if (!dto.getQuadrant().isEmpty()) {
drawable.setQuadrant(AbstractTemplate.Quadrant.valueOf(dto.getQuadrant()));
}
var pathVertex = dto.getPathVertex();
drawable.setPathVertex(new ZonePoint(pathVertex.getX(), pathVertex.getY()));
if (dto.hasName()) {
Expand Down
Loading

0 comments on commit 2f3bac8

Please sign in to comment.