Skip to content

Commit 3a1744e

Browse files
committed
Re-organize code
1 parent 03c38b7 commit 3a1744e

File tree

1 file changed

+76
-146
lines changed

1 file changed

+76
-146
lines changed

src/main/java/net/rptools/maptool/model/drawing/RightAngleConeTemplate.java

Lines changed: 76 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@
2727
import net.rptools.maptool.server.proto.drawing.DrawableDto;
2828
import net.rptools.maptool.server.proto.drawing.RightAngleConeTemplateDto;
2929

30-
/**
31-
* The radius template draws a highlight over all the squares effected from a specific spine.
32-
*
33-
* @author jgorrell
34-
* @version $Revision: 5945 $ $Date: 2013-06-03 04:35:50 +0930 (Mon, 03 Jun 2013) $ $Author:
35-
* azhrei_fje $
36-
*/
3730
public class RightAngleConeTemplate extends AbstractTemplate {
3831
// The definition of the cone is it is as wide as it is
3932
// long, so the cone angle is tan inverse of 1/2, since
@@ -95,40 +88,6 @@ public void calculateTheta(MouseEvent e, ZoneRenderer renderer) {
9588
setTheta(Math.atan2(opposite, adjacent));
9689
}
9790

98-
/**
99-
* Paint the border at a specific radius.
100-
*
101-
* @param g Where to paint
102-
* @param x Distance from vertex along X axis in cell coordinates.
103-
* @param y Distance from vertex along Y axis in cell coordinates.
104-
* @param xOff Distance from vertex along X axis in screen coordinates.
105-
* @param yOff Distance from vertex along Y axis in screen coordinates.
106-
* @param gridSize The size of one side of the grid in screen coordinates.
107-
* @param distance The distance in cells from the vertex to the cell which is offset from the
108-
* vertex by {@code x & y}.
109-
* @param radius The radius where the border is painted.
110-
* @see AbstractTemplate#paintBorder(Graphics2D, int, int, int, int, int, int)
111-
*/
112-
protected void paintBorderAtRadius(
113-
Graphics2D g, int x, int y, int xOff, int yOff, int gridSize, int distance, int radius) {
114-
// At the border?
115-
if (distance == radius) {
116-
// Paint lines between vertical boundaries if needed
117-
if (getDistance(x + 1, y) > radius) {
118-
for (Quadrant q : Quadrant.values()) {
119-
paintFarVerticalBorder(g, xOff, yOff, gridSize, q);
120-
}
121-
}
122-
123-
// Paint lines between horizontal boundaries if needed
124-
if (getDistance(x, y + 1) > radius) {
125-
for (Quadrant q : Quadrant.values()) {
126-
paintFarHorizontalBorder(g, xOff, yOff, gridSize, q);
127-
}
128-
}
129-
}
130-
}
131-
13291
/*---------------------------------------------------------------------------------------------
13392
* Overridden AbstractTemplate Methods
13493
*-------------------------------------------------------------------------------------------*/
@@ -139,7 +98,14 @@ protected void paintBorderAtRadius(
13998
@Override
14099
protected void paintBorder(
141100
Graphics2D g, int x, int y, int xOff, int yOff, int gridSize, int distance) {
142-
paintBorderAtRadius(g, x, y, xOff, yOff, gridSize, distance, getRadius());
101+
// NO-OP, not used. I overrode AbstractTemplate#paint in this class and I don't call paintBorder
102+
// and paintArea separately anymore.
103+
}
104+
105+
@Override
106+
protected void paintArea(Graphics2D g, int x, int y, int xOff, int yOff, int gridSize, int distance) {
107+
// NO-OP, not used. I overrode AbstractTemplate#paint in this class and I don't call paintBorder
108+
// and paintArea separately anymore.
143109
}
144110

145111
private void splitAndQueueVertically(Rectangle r, PriorityQueue<Rectangle> q, int gridSize) {
@@ -179,20 +145,54 @@ private void splitAndQueueHorizontally(Rectangle r, PriorityQueue<Rectangle> q,
179145

180146
@Override
181147
protected void paint(Graphics2D g, boolean border, boolean area) {
182-
if (MapTool.getCampaign().getZone(getZoneId()) == null) {
183-
return;
148+
Area aoe = this.getArea();
149+
Path2D.Double path = getConePath();
150+
151+
// Paint what is needed.
152+
if (area) {
153+
g.fill(aoe);
154+
}
155+
if (border) {
156+
if (this.showAOEOverlay) { // While drawing, it's helpful to see the cone overlay.
157+
g.draw(path);
158+
}
159+
// g.draw(boundingBox);
160+
// g.draw(gridSnappedBoundingBox);
161+
g.draw(aoe);
184162
}
163+
// endif
164+
}
165+
166+
/*---------------------------------------------------------------------------------------------
167+
* Drawable Interface Methods
168+
*-------------------------------------------------------------------------------------------*/
169+
170+
/**
171+
* @see Drawable#getBounds()
172+
*/
173+
public Rectangle getBounds() {
174+
if (getZoneId() == null) {
175+
// This avoids a NPE when loading up a campaign
176+
return new Rectangle();
177+
}
178+
Zone zone = MapTool.getCampaign().getZone(getZoneId());
179+
if (zone == null) {
180+
return new Rectangle();
181+
}
182+
int gridSize = zone.getGrid().getSize();
183+
int quadrantSize = getRadius() * gridSize + BOUNDS_PADDING;
184+
ZonePoint vertex = getVertex();
185+
return new Rectangle(
186+
vertex.x - quadrantSize, vertex.y - quadrantSize, quadrantSize * 2, quadrantSize * 2);
187+
}
188+
189+
private Path2D.Double getConePath() {
185190
ZonePoint sp = getVertex();
186191

187192
// Only paint if the start and endpoints are not equal and the
188193
// radius is non-zero.
189-
// TODO: Radius is pretty "sticky" when going cornerwise.. It flips from
190-
// 5 -> 15 too easily...
191-
// Might need to checkout how getRadius is being calculated? It's probably
192-
// snapping to an endpoint when all you really want to do is round to
193-
// the nearest "gridSize" in game units.
194194
double radius = getRadius();
195-
if (getRadius() == 0) return;
195+
if (getRadius() == 0) return new Path2D.Double();
196196

197197
Grid grid = MapTool.getCampaign().getZone(getZoneId()).getGrid();
198198
int gridSize = grid.getSize();
@@ -212,37 +212,49 @@ protected void paint(Graphics2D g, boolean border, boolean area) {
212212
path.lineTo(vertex1X, vertex1Y);
213213
path.lineTo(vertex2X, vertex2Y);
214214
path.lineTo(sp.x, sp.y);
215+
return path;
216+
}
217+
218+
@Override
219+
public Area getArea() {
220+
if (MapTool.getCampaign().getZone(getZoneId()) == null) {
221+
return new Area();
222+
}
223+
Grid grid = MapTool.getCampaign().getZone(getZoneId()).getGrid();
224+
int gridSize = grid.getSize();
225+
226+
Path2D.Double path = getConePath();
215227

216228
// boundingBox is the minimal bounding box of the cone.
217229
// griddedBoundingBox is the bounding box of all game squares
218230
// that the boundingBox intersects.
219231
Rectangle boundingBox = path.getBounds();
220232
ZonePoint leftUpper =
221-
new ZonePoint(
222-
gridSize * (int) Math.floor(boundingBox.x / gridSize),
223-
gridSize * (int) Math.floor(boundingBox.y / gridSize));
233+
new ZonePoint(
234+
gridSize * (int) Math.floor(boundingBox.x / gridSize),
235+
gridSize * (int) Math.floor(boundingBox.y / gridSize));
224236

225237
float bottomRightX = boundingBox.x + boundingBox.width;
226238
int bottomRightXSnapped = gridSize * (int) Math.ceil(bottomRightX / gridSize);
227239
float bottomRightY = boundingBox.y + boundingBox.height;
228240
int bottomRightYSnapped = gridSize * (int) Math.ceil(bottomRightY / gridSize);
229241
ZonePoint bottomRight = new ZonePoint(bottomRightXSnapped, bottomRightYSnapped);
230242
Rectangle gridSnappedBoundingBox =
231-
new Rectangle(
232-
leftUpper.x, leftUpper.y, bottomRight.x - leftUpper.x, bottomRight.y - leftUpper.y);
243+
new Rectangle(
244+
leftUpper.x, leftUpper.y, bottomRight.x - leftUpper.x, bottomRight.y - leftUpper.y);
233245

234246
Area cone = new Area(path);
235247
Area aoe = new Area(); // Empty rectangle that we will update.
236248
/** */
237249
PriorityQueue<Rectangle> queue =
238-
new PriorityQueue<Rectangle>(
239-
new Comparator<Rectangle>() {
240-
@Override
241-
/** We prioritize smaller rectangles so the Queue stays smaller. */
242-
public int compare(Rectangle o1, Rectangle o2) {
243-
return o1.height * o1.width - o2.height * o2.width;
244-
}
245-
});
250+
new PriorityQueue<Rectangle>(
251+
new Comparator<Rectangle>() {
252+
@Override
253+
/** We prioritize smaller rectangles so the Queue stays smaller. */
254+
public int compare(Rectangle o1, Rectangle o2) {
255+
return o1.height * o1.width - o2.height * o2.width;
256+
}
257+
});
246258
queue.add(gridSnappedBoundingBox);
247259
while (queue.size() > 0) {
248260
// if fully contained, then add it to the shape
@@ -313,95 +325,13 @@ else if (!candidateAreaForAoe.equals(new Area(candidateRectForAoe))) {
313325
} // else there is no overlap, so there is nothing to do.
314326
}
315327

316-
// Paint what is needed.
317-
if (area) {
318-
g.fill(aoe);
319-
}
320-
if (border) {
321-
if (this.showAOEOverlay) { // While drawing, it's helpful to see the cone overlay.
322-
g.draw(path);
323-
}
324-
// g.draw(boundingBox);
325-
// g.draw(gridSnappedBoundingBox);
326-
g.draw(aoe);
327-
}
328-
// endif
329-
330-
// debugging timings...
331-
// System.out.println(System.nanoTime() - startTime);
332-
}
333-
334-
/**
335-
* @see AbstractTemplate#paintArea(Graphics2D, int, int, int, int, int, int)
336-
*/
337-
@Override
338-
protected void paintArea(
339-
Graphics2D g, int x, int y, int xOff, int yOff, int gridSize, int distance) {
340-
// Only squares w/in the radius
341-
if (distance <= getRadius()) {
342-
// Paint the squares
343-
for (Quadrant q : Quadrant.values()) {
344-
paintArea(g, xOff, yOff, gridSize, q);
345-
}
346-
}
347-
}
348-
349-
/*---------------------------------------------------------------------------------------------
350-
* Drawable Interface Methods
351-
*-------------------------------------------------------------------------------------------*/
352-
353-
/**
354-
* @see Drawable#getBounds()
355-
*/
356-
public Rectangle getBounds() {
357-
if (getZoneId() == null) {
358-
// This avoids a NPE when loading up a campaign
359-
return new Rectangle();
360-
}
361-
Zone zone = MapTool.getCampaign().getZone(getZoneId());
362-
if (zone == null) {
363-
return new Rectangle();
364-
}
365-
int gridSize = zone.getGrid().getSize();
366-
int quadrantSize = getRadius() * gridSize + BOUNDS_PADDING;
367-
ZonePoint vertex = getVertex();
368-
return new Rectangle(
369-
vertex.x - quadrantSize, vertex.y - quadrantSize, quadrantSize * 2, quadrantSize * 2);
328+
return aoe;
370329
}
371330

372331
public PathIterator getPathIterator() {
373332
return getArea().getPathIterator(new AffineTransform());
374333
}
375334

376-
public Area getArea() {
377-
if (getZoneId() == null) {
378-
return new Area();
379-
}
380-
Zone zone = getCampaign().getZone(getZoneId());
381-
if (zone == null) {
382-
return new Area();
383-
}
384-
int gridSize = zone.getGrid().getSize();
385-
int r = getRadius();
386-
ZonePoint vertex = getVertex();
387-
Area result = new Area();
388-
for (int x = 0; x < r; x++) {
389-
for (int y = 0; y < r; y++) {
390-
if (getDistance(x, y) <= r) {
391-
int xOff = x * gridSize;
392-
int yOff = y * gridSize;
393-
// Add all four quadrants
394-
for (Quadrant q : Quadrant.values()) {
395-
int rx = vertex.x + getXMult(q) * xOff + ((getXMult(q) - 1) / 2) * gridSize;
396-
int ry = vertex.y + getYMult(q) * yOff + ((getYMult(q) - 1) / 2) * gridSize;
397-
result.add(new Area(new Rectangle(rx, ry, gridSize, gridSize)));
398-
}
399-
}
400-
}
401-
}
402-
return result;
403-
}
404-
405335
@Override
406336
public DrawableDto toDto() {
407337
var dto = RightAngleConeTemplateDto.newBuilder();

0 commit comments

Comments
 (0)