Skip to content

Commit

Permalink
Some code clean ups to UnitDrawer and TileManager. (#10433)
Browse files Browse the repository at this point in the history
* Some code clean ups to UnitDrawer and TileManager.

Reduce use of Tectangle() to UnitsDrawer.
  • Loading branch information
asvitkine authored May 11, 2022
1 parent b228095 commit 92094f5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.triplea.util.Tuple;

/** Orchestrates the rendering of all map tiles. */
Expand Down Expand Up @@ -352,13 +353,7 @@ private void drawUnits(
uiContext);
drawing.add(drawable);
allUnitDrawables.add(drawable);
for (final Tile tile :
getTiles(
new Rectangle(
lastPlace.x,
lastPlace.y,
uiContext.getUnitImageFactory().getUnitImageWidth(),
uiContext.getUnitImageFactory().getUnitImageHeight()))) {
for (final Tile tile : getTiles(drawable.getPlacementRectangle())) {
tile.addDrawable(drawable);
drawnOn.add(tile);
}
Expand Down Expand Up @@ -503,22 +498,17 @@ private void drawForCreate(
* null} if no such rectangle exists. Because the units are assumed to be drawn stacked, the
* returned rectangle will always have a size equal to the standard unit image size.
*/
public Rectangle getUnitRect(final List<Unit> units, final GameData data) {
public @Nullable Rectangle getUnitRect(final List<Unit> units, final GameData data) {
if (units.isEmpty()) {
return null;
}
data.acquireReadLock();
try {
synchronized (mutex) {
for (final UnitsDrawer drawer : allUnitDrawables) {
final List<Unit> drawerUnits = drawer.getUnits(data).getSecond();
final List<Unit> drawerUnits = drawer.getUnits(data);
if (!drawerUnits.isEmpty() && units.containsAll(drawerUnits)) {
final Point placementPoint = drawer.getPlacementPoint();
return new Rectangle(
placementPoint.x,
placementPoint.y,
uiContext.getUnitImageFactory().getUnitImageWidth(),
uiContext.getUnitImageFactory().getUnitImageHeight());
return drawer.getPlacementRectangle();
}
}
return null;
Expand All @@ -532,18 +522,14 @@ public Rectangle getUnitRect(final List<Unit> units, final GameData data) {
* Returns the territory and units at the specified point or {@code null} if the point does not
* lie within the bounds of any {@link UnitsDrawer}.
*/
public Tuple<Territory, List<Unit>> getUnitsAtPoint(
public @Nullable Tuple<Territory, List<Unit>> getUnitsAtPoint(
final double x, final double y, final GameData gameData) {
gameData.acquireReadLock();
try {
synchronized (mutex) {
for (final UnitsDrawer drawer : allUnitDrawables) {
final Point placementPoint = drawer.getPlacementPoint();
if (x > placementPoint.x
&& x < placementPoint.x + uiContext.getUnitImageFactory().getUnitImageWidth()
&& y > placementPoint.y
&& y < placementPoint.y + uiContext.getUnitImageFactory().getUnitImageHeight()) {
return drawer.getUnits(gameData);
if (drawer.getPlacementRectangle().contains(x, y)) {
return Tuple.of(drawer.getTerritory(gameData), drawer.getUnits(gameData));
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import games.strategy.triplea.Properties;
import games.strategy.triplea.delegate.Matches;
import games.strategy.triplea.image.MapImage;
import games.strategy.triplea.image.UnitImageFactory;
import games.strategy.triplea.settings.ClientSetting;
import games.strategy.triplea.ui.UiContext;
import games.strategy.triplea.ui.mapdata.MapData;
Expand All @@ -23,7 +24,6 @@
import java.awt.RenderingHints;
import java.util.List;
import java.util.function.Predicate;
import org.triplea.util.Tuple;

/**
* Draws units for the associated territory.
Expand All @@ -48,7 +48,7 @@ public class UnitsDrawer extends AbstractDrawable {
public enum UnitFlagDrawMode {
NONE,
SMALL_FLAG,
LARGE_FLAG;
LARGE_FLAG,
}

public UnitsDrawer(
Expand Down Expand Up @@ -78,6 +78,15 @@ public Point getPlacementPoint() {
return placementPoint;
}

public Rectangle getPlacementRectangle() {
UnitImageFactory factory = uiContext.getUnitImageFactory();
return new Rectangle(
placementPoint.x,
placementPoint.y,
factory.getUnitImageWidth(),
factory.getUnitImageHeight());
}

public String getPlayer() {
return playerName;
}
Expand Down Expand Up @@ -269,7 +278,7 @@ public static void drawOutlinedText(
}
}

Tuple<Territory, List<Unit>> getUnits(final GameState data) {
List<Unit> getUnits(final GameState data) {
// note - it may be the case where the territory is being changed as a result to a mouse click,
// and the map units
// haven't updated yet, so the unit count from the territory wont match the units in count
Expand All @@ -284,7 +293,11 @@ Tuple<Territory, List<Unit>> getUnits(final GameState data) {
bombingUnitDamage > 0
? Matches.unitHasTakenSomeBombingUnitDamage()
: Matches.unitHasNotTakenAnyBombingUnitDamage());
return Tuple.of(t, t.getUnitCollection().getMatches(selectedUnits));
return t.getUnitCollection().getMatches(selectedUnits);
}

public Territory getTerritory(GameData data) {
return data.getMap().getTerritory(territoryName);
}

@Override
Expand Down

0 comments on commit 92094f5

Please sign in to comment.