The basic idea is that you can activate this tool, and then if you click on a token, you can choose to draw "sightlines" from the the token's center, or one of the four corners of the area it occupies, to all of the corners on a target token. This is relatively game-specific but it seemed like an interesting project to push further into MT's source with.
In any case, I've got all parts of it working, except one thing I can't seem to figure out. The basic procedure is that if a user Ctrl-leftclicks on a token, the LOS tool places the "origin" point of the sightlines at the nearest grid vertex. That was easy; there's a handy method for that.
When you then right click on the target, you can select "Check Line of Sight" from a popup menu, and it will draw lines from the origin point to the four corners of the target's boundaries (boundaries at this point; cell footprint, hopefully, in the future). This works great, up until I drag the map. At that point, when I click on a token to start a Line of Sight check, it selects the nearest vertex, but when I actually draw the sightlines, they originate from a point that is offset from that vertex (by, I assume, the map offset).
So I know that it involves offsets somehow, but here's where I'm a bit lost:
I have ScreenPoints, CellPoints, and ZonePoints, and I have view offset and grid offset, and I'm trying to figure out what combination of which is what I need. Here's the general process:
1. The user ctrl-clicks on a token. This uses what is basically "snap-to-grid" to select the nearest vertex: it starts as a ScreenPoint, is converted to a ZonePoint, used to find the nearest vertex, and then converted back into a ScreenPoint in its final form.
Code: Select all
selectedCorner = new ScreenPoint(e.getX(), e.getY());
zp = selectedCorner.convertToZoneRnd(renderer);
zp = renderer.getZone().getNearestVertex(zp);
selectedCorner = ScreenPoint.fromZonePoint(renderer, zp);
Code: Select all
Zone zone = renderer.getZone();
Grid grid = zone.getGrid();
int offY = renderer.getViewOffsetY();
int offX = renderer.getViewOffsetX();
g.setColor(Color.YELLOW);
g.translate(offX, offY);
g.scale(renderer.getScale(), renderer.getScale());
if (isCheckingLOS){
if(losTarget == null){
return;
}
g.setColor(Color.WHITE);
g.drawLine((int)selectedCorner.getX(),
(int)selectedCorner.getY(),
(int)losTarget.getBounds(zone).getMaxX(),
(int)losTarget.getBounds(zone).getMaxY());
// 3 more lines drawn here, to the relevant corners
g.draw(losTarget.getBounds(zone));
g.dispose();
}
However, if I move the map, and then try a new "selectedCorner" (for instance, to check LOS from another token), it will draw the origin from some point offset from where I want it to be.
What I'm not sure is where in the code I need to be putting the offsets, in order to have it be "immune" to map dragging (that is, so that the starting point is always where I expect it to be, and so is the ending point).
Anyway, I've attached the whole targeting tool code, too, if anyone wants to help a n00b out. Hopefully you'll at least see what I was trying to do, and can give me advice.
Remember, I have very little formal experience with programming, so use small words and act as though you are speaking to someone educated in the Penal System.
Thanks!