Skip to content

Commit 92094f5

Browse files
authored
Some code clean ups to UnitDrawer and TileManager. (#10433)
* Some code clean ups to UnitDrawer and TileManager. Reduce use of Tectangle() to UnitsDrawer.
1 parent b228095 commit 92094f5

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

game-app/game-core/src/main/java/games/strategy/triplea/ui/screen/TileManager.java

+8-22
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.Map.Entry;
4949
import java.util.Set;
5050
import java.util.stream.Collectors;
51+
import javax.annotation.Nullable;
5152
import org.triplea.util.Tuple;
5253

5354
/** Orchestrates the rendering of all map tiles. */
@@ -352,13 +353,7 @@ private void drawUnits(
352353
uiContext);
353354
drawing.add(drawable);
354355
allUnitDrawables.add(drawable);
355-
for (final Tile tile :
356-
getTiles(
357-
new Rectangle(
358-
lastPlace.x,
359-
lastPlace.y,
360-
uiContext.getUnitImageFactory().getUnitImageWidth(),
361-
uiContext.getUnitImageFactory().getUnitImageHeight()))) {
356+
for (final Tile tile : getTiles(drawable.getPlacementRectangle())) {
362357
tile.addDrawable(drawable);
363358
drawnOn.add(tile);
364359
}
@@ -503,22 +498,17 @@ private void drawForCreate(
503498
* null} if no such rectangle exists. Because the units are assumed to be drawn stacked, the
504499
* returned rectangle will always have a size equal to the standard unit image size.
505500
*/
506-
public Rectangle getUnitRect(final List<Unit> units, final GameData data) {
501+
public @Nullable Rectangle getUnitRect(final List<Unit> units, final GameData data) {
507502
if (units.isEmpty()) {
508503
return null;
509504
}
510505
data.acquireReadLock();
511506
try {
512507
synchronized (mutex) {
513508
for (final UnitsDrawer drawer : allUnitDrawables) {
514-
final List<Unit> drawerUnits = drawer.getUnits(data).getSecond();
509+
final List<Unit> drawerUnits = drawer.getUnits(data);
515510
if (!drawerUnits.isEmpty() && units.containsAll(drawerUnits)) {
516-
final Point placementPoint = drawer.getPlacementPoint();
517-
return new Rectangle(
518-
placementPoint.x,
519-
placementPoint.y,
520-
uiContext.getUnitImageFactory().getUnitImageWidth(),
521-
uiContext.getUnitImageFactory().getUnitImageHeight());
511+
return drawer.getPlacementRectangle();
522512
}
523513
}
524514
return null;
@@ -532,18 +522,14 @@ public Rectangle getUnitRect(final List<Unit> units, final GameData data) {
532522
* Returns the territory and units at the specified point or {@code null} if the point does not
533523
* lie within the bounds of any {@link UnitsDrawer}.
534524
*/
535-
public Tuple<Territory, List<Unit>> getUnitsAtPoint(
525+
public @Nullable Tuple<Territory, List<Unit>> getUnitsAtPoint(
536526
final double x, final double y, final GameData gameData) {
537527
gameData.acquireReadLock();
538528
try {
539529
synchronized (mutex) {
540530
for (final UnitsDrawer drawer : allUnitDrawables) {
541-
final Point placementPoint = drawer.getPlacementPoint();
542-
if (x > placementPoint.x
543-
&& x < placementPoint.x + uiContext.getUnitImageFactory().getUnitImageWidth()
544-
&& y > placementPoint.y
545-
&& y < placementPoint.y + uiContext.getUnitImageFactory().getUnitImageHeight()) {
546-
return drawer.getUnits(gameData);
531+
if (drawer.getPlacementRectangle().contains(x, y)) {
532+
return Tuple.of(drawer.getTerritory(gameData), drawer.getUnits(gameData));
547533
}
548534
}
549535
return null;

game-app/game-core/src/main/java/games/strategy/triplea/ui/screen/UnitsDrawer.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import games.strategy.triplea.Properties;
1212
import games.strategy.triplea.delegate.Matches;
1313
import games.strategy.triplea.image.MapImage;
14+
import games.strategy.triplea.image.UnitImageFactory;
1415
import games.strategy.triplea.settings.ClientSetting;
1516
import games.strategy.triplea.ui.UiContext;
1617
import games.strategy.triplea.ui.mapdata.MapData;
@@ -23,7 +24,6 @@
2324
import java.awt.RenderingHints;
2425
import java.util.List;
2526
import java.util.function.Predicate;
26-
import org.triplea.util.Tuple;
2727

2828
/**
2929
* Draws units for the associated territory.
@@ -48,7 +48,7 @@ public class UnitsDrawer extends AbstractDrawable {
4848
public enum UnitFlagDrawMode {
4949
NONE,
5050
SMALL_FLAG,
51-
LARGE_FLAG;
51+
LARGE_FLAG,
5252
}
5353

5454
public UnitsDrawer(
@@ -78,6 +78,15 @@ public Point getPlacementPoint() {
7878
return placementPoint;
7979
}
8080

81+
public Rectangle getPlacementRectangle() {
82+
UnitImageFactory factory = uiContext.getUnitImageFactory();
83+
return new Rectangle(
84+
placementPoint.x,
85+
placementPoint.y,
86+
factory.getUnitImageWidth(),
87+
factory.getUnitImageHeight());
88+
}
89+
8190
public String getPlayer() {
8291
return playerName;
8392
}
@@ -269,7 +278,7 @@ public static void drawOutlinedText(
269278
}
270279
}
271280

272-
Tuple<Territory, List<Unit>> getUnits(final GameState data) {
281+
List<Unit> getUnits(final GameState data) {
273282
// note - it may be the case where the territory is being changed as a result to a mouse click,
274283
// and the map units
275284
// haven't updated yet, so the unit count from the territory wont match the units in count
@@ -284,7 +293,11 @@ Tuple<Territory, List<Unit>> getUnits(final GameState data) {
284293
bombingUnitDamage > 0
285294
? Matches.unitHasTakenSomeBombingUnitDamage()
286295
: Matches.unitHasNotTakenAnyBombingUnitDamage());
287-
return Tuple.of(t, t.getUnitCollection().getMatches(selectedUnits));
296+
return t.getUnitCollection().getMatches(selectedUnits);
297+
}
298+
299+
public Territory getTerritory(GameData data) {
300+
return data.getMap().getTerritory(territoryName);
288301
}
289302

290303
@Override

0 commit comments

Comments
 (0)