Skip to content

Commit

Permalink
Merge pull request #1065 from RoiEXLab/routeDrawerFix
Browse files Browse the repository at this point in the history
Fix RouteDrawer (#1056)
  • Loading branch information
DanVanAtta authored Aug 15, 2016
2 parents 6cfaf68 + 673b587 commit 761e719
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
44 changes: 25 additions & 19 deletions src/games/strategy/triplea/ui/MapRouteDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,22 @@ public void drawRoute(final Graphics2D graphics, final RouteDescription routeDes
final int imageWidth = mapPanel.getImageWidth();
final int imageHeight = mapPanel.getImageHeight();
final Dimension imageDimension = new Dimension(imageWidth, imageHeight);
final Dimension screenDimension = new Dimension(mapPanel.getWidth(), mapPanel.getHeight());
final Point[] points =
getRoutePoints(routeDescription, mapData, xOffset, yOffset, imageDimension);
getRoutePoints(routeDescription, mapData, xOffset, yOffset, imageDimension, screenDimension);
final boolean tooFewTerritories = numTerritories <= 1;
final boolean tooFewPoints = points.length <= 2;
final double scale = mapPanel.getScale();
if (tooFewTerritories || tooFewPoints) {
if (routeDescription.getEnd() != null) {// AI has no End Point
drawDirectPath(graphics,
getPointOnMap(routeDescription.getStart(), xOffset, yOffset, imageDimension),
getPointOnMap(routeDescription.getEnd(), xOffset, yOffset, imageDimension), xOffset, yOffset, scale);
getPointOnMap(routeDescription.getStart(), xOffset, yOffset, imageDimension, screenDimension),
getPointOnMap(routeDescription.getEnd(), xOffset, yOffset, imageDimension, screenDimension), xOffset,
yOffset, scale);
} else {
drawDirectPath(graphics, getPointOnMap(points[0], xOffset, yOffset, imageDimension),
getPointOnMap(points[points.length - 1], xOffset, yOffset, imageDimension), xOffset, yOffset, scale);
drawDirectPath(graphics, getPointOnMap(points[0], xOffset, yOffset, imageDimension, screenDimension),
getPointOnMap(points[points.length - 1], xOffset, yOffset, imageDimension, screenDimension), xOffset,
yOffset, scale);
}
if (tooFewPoints && !tooFewTerritories) {
drawMoveLength(graphics, points, xOffset, yOffset, scale, numTerritories, maxMovement);
Expand All @@ -85,7 +88,7 @@ public void drawRoute(final Graphics2D graphics, final RouteDescription routeDes
drawMoveLength(graphics, points, xOffset, yOffset, scale, numTerritories, maxMovement);
}
drawJoints(graphics, points, xOffset, yOffset, scale);
drawCustomCursor(graphics, routeDescription, xOffset, yOffset, scale, imageDimension);
drawCustomCursor(graphics, routeDescription, xOffset, yOffset, scale, imageDimension, screenDimension);
}

/**
Expand Down Expand Up @@ -118,10 +121,11 @@ private void drawJoints(Graphics2D graphics, Point[] points, int xOffset, int yO
* @param scale The scale-factor of the Map
*/
private void drawCustomCursor(Graphics2D graphics, RouteDescription routeDescription, int xOffset, int yOffset,
double scale, Dimension imageDimension) {
double scale, Dimension imageDimension, Dimension screenDimension) {
final Image cursorImage = routeDescription.getCursorImage();
if (cursorImage != null) {
Point wrappedEndPoint = getPointOnMap(routeDescription.getEnd(), xOffset, yOffset, imageDimension);
Point wrappedEndPoint =
getPointOnMap(routeDescription.getEnd(), xOffset, yOffset, imageDimension, screenDimension);
graphics.drawImage(cursorImage,
(int) (((wrappedEndPoint.x - xOffset) - (cursorImage.getWidth(null) / 2)) * scale),
(int) (((wrappedEndPoint.y - yOffset) - (cursorImage.getHeight(null) / 2)) * scale), null);
Expand Down Expand Up @@ -201,19 +205,20 @@ private void drawLineWithTranslate(Graphics2D graphics, Line2D line, double xOff
* objects
*/
protected Point[] getRoutePoints(RouteDescription routeDescription, MapData mapData, int xOffset, int yOffset,
Dimension imageDimension) {
Dimension imageDimension, Dimension screenDimension) {
final List<Territory> territories = routeDescription.getRoute().getAllTerritories();
final int numTerritories = territories.size();
final Point[] points = new Point[numTerritories];
for (int i = 0; i < numTerritories; i++) {
points[i] = getPointOnMap(mapData.getCenter(territories.get(i)), xOffset, yOffset, imageDimension);
points[i] =
getPointOnMap(mapData.getCenter(territories.get(i)), xOffset, yOffset, imageDimension, screenDimension);
}
if (routeDescription.getStart() != null) {
points[0] = getPointOnMap(routeDescription.getStart(), xOffset, yOffset, imageDimension);
points[0] = getPointOnMap(routeDescription.getStart(), xOffset, yOffset, imageDimension, screenDimension);
}
if (routeDescription.getEnd() != null && numTerritories > 1) {
points[numTerritories - 1] =
getPointOnMap(routeDescription.getEnd(), xOffset, yOffset, imageDimension);
getPointOnMap(routeDescription.getEnd(), xOffset, yOffset, imageDimension, screenDimension);
}
return points;
}
Expand All @@ -225,18 +230,19 @@ protected Point[] getRoutePoints(RouteDescription routeDescription, MapData mapD
* @param point The reference {@linkplain Point}
* @param xOffset The horizontal pixel-difference between the frame and the Map
* @param yOffset The vertical pixel-difference between the frame and the Map
* @param width The width of the Map
* @param height The height of the Map
* @param dimension The height and width of the Map
* @return The "real" Point
*/
protected static Point getPointOnMap(Point point, int xOffset, int yOffset, Dimension dimension) {
protected static Point getPointOnMap(Point point, int xOffset, int yOffset, Dimension dimension,
Dimension screenDimension) {
Point newPoint = null;
int x = (int) point.getX();
int y = (int) point.getY();
int x = point.x;
int y = point.y;
int width = dimension.width;
if (x - width > xOffset) {
int screenWidth = screenDimension.width;
if (x - width > xOffset || (xOffset < 0 && xOffset + screenWidth < x)) {
newPoint = new Point(x - width, y);
} else if (x < xOffset) {
} else if (x < xOffset && x + width < screenWidth + xOffset) {
newPoint = new Point(x + width, y);
}
return newPoint != null ? newPoint : point;
Expand Down
8 changes: 4 additions & 4 deletions test/games/strategy/triplea/ui/TestRoute.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ public void testPointSplitting() {
@Test
public void testPointAcquisition() {
assertArrayEquals(dummyPoints,
spyRouteDrawer.getRoutePoints(dummyRouteDescription, dummyMapData, 0, 0, new Dimension(0, 0)));
spyRouteDrawer.getRoutePoints(dummyRouteDescription, dummyMapData, 0, 0, new Dimension(), new Dimension()));
for (int i = 0; i < 10; i++) {
assertArrayEquals(dummyPoints, spyRouteDrawer.getRoutePoints(dummyRouteDescription, dummyMapData, randomInt(),
randomInt(), new Dimension(0, 0)));
randomInt(), new Dimension(), new Dimension()));
assertArrayEquals(dummyPoints, spyRouteDrawer.getRoutePoints(dummyRouteDescription, dummyMapData, 0, 0,
new Dimension(randomInt(1000, 100), randomInt(1000, 100))));
new Dimension(randomInt(1000, 100), randomInt(1000, 100)), new Dimension()));
int randX = randomInt(1000, 0);
assertEquals(new Point(randX, 0),
MapRouteDrawer.getPointOnMap(new Point(), randX, 0, new Dimension(randX, 0)));
MapRouteDrawer.getPointOnMap(new Point(randX, 0), randX, 0, new Dimension(randX, 0), new Dimension()));
}
}

Expand Down

0 comments on commit 761e719

Please sign in to comment.