Search in sources :

Example 36 with TerritoryAttachment

use of games.strategy.triplea.attachments.TerritoryAttachment in project triplea by triplea-game.

the class TerritoryNameDrawable method draw.

@Override
public void draw(final Rectangle bounds, final GameData data, final Graphics2D graphics, final MapData mapData, final AffineTransform unscaled, final AffineTransform scaled) {
    final Territory territory = data.getMap().getTerritory(territoryName);
    final TerritoryAttachment ta = TerritoryAttachment.get(territory);
    final boolean drawFromTopLeft = mapData.drawNamesFromTopLeft();
    final boolean showSeaNames = mapData.drawSeaZoneNames();
    final boolean showComments = mapData.drawComments();
    boolean drawComments = false;
    String commentText = null;
    if (territory.isWater()) {
        // this is for special comments, like convoy zones, etc.
        if (ta != null && showComments) {
            if (ta.getConvoyRoute() && ta.getProduction() > 0 && ta.getOriginalOwner() != null) {
                drawComments = true;
                if (ta.getConvoyAttached().isEmpty()) {
                    commentText = MyFormatter.defaultNamedToTextList(TerritoryAttachment.getWhatTerritoriesThisIsUsedInConvoysFor(territory, data)) + " " + ta.getOriginalOwner().getName() + " Blockade Route";
                } else {
                    commentText = MyFormatter.defaultNamedToTextList(ta.getConvoyAttached()) + " " + ta.getOriginalOwner().getName() + " Convoy Route";
                }
            } else if (ta.getConvoyRoute()) {
                drawComments = true;
                if (ta.getConvoyAttached().isEmpty()) {
                    commentText = MyFormatter.defaultNamedToTextList(TerritoryAttachment.getWhatTerritoriesThisIsUsedInConvoysFor(territory, data)) + " Blockade Route";
                } else {
                    commentText = MyFormatter.defaultNamedToTextList(ta.getConvoyAttached()) + " Convoy Route";
                }
            } else if (ta.getProduction() > 0 && ta.getOriginalOwner() != null) {
                drawComments = true;
                final PlayerID originalOwner = ta.getOriginalOwner();
                commentText = originalOwner.getName() + " Convoy Center";
            }
        }
        if (!drawComments && !showSeaNames) {
            return;
        }
    }
    graphics.setFont(MapImage.getPropertyMapFont());
    graphics.setColor(MapImage.getPropertyTerritoryNameAndPuAndCommentColor());
    final FontMetrics fm = graphics.getFontMetrics();
    // if we specify a placement point, use it otherwise try to center it
    final int x;
    final int y;
    final Optional<Point> namePlace = mapData.getNamePlacementPoint(territory);
    if (namePlace.isPresent()) {
        x = namePlace.get().x;
        y = namePlace.get().y;
    } else {
        final Rectangle territoryBounds = getBestTerritoryNameRect(mapData, territory, fm);
        x = territoryBounds.x + (int) territoryBounds.getWidth() / 2 - fm.stringWidth(territory.getName()) / 2;
        y = territoryBounds.y + (int) territoryBounds.getHeight() / 2 + fm.getAscent() / 2;
    }
    // draw comments above names
    if (showComments && drawComments && commentText != null) {
        final Optional<Point> place = mapData.getCommentMarkerLocation(territory);
        if (place.isPresent()) {
            draw(bounds, graphics, place.get().x, place.get().y, null, commentText, drawFromTopLeft);
        } else {
            draw(bounds, graphics, x, y - fm.getHeight(), null, commentText, drawFromTopLeft);
        }
    }
    // draw territory names
    if (mapData.drawTerritoryNames() && mapData.shouldDrawTerritoryName(territoryName)) {
        if (!territory.isWater() || showSeaNames) {
            final Image nameImage = mapData.getTerritoryNameImages().get(territory.getName());
            draw(bounds, graphics, x, y, nameImage, territory.getName(), drawFromTopLeft);
        }
    }
    // draw the PUs.
    if (ta != null && ta.getProduction() > 0 && mapData.drawResources()) {
        final Image img = uiContext.getPuImageFactory().getPuImage(ta.getProduction());
        final String prod = Integer.valueOf(ta.getProduction()).toString();
        final Optional<Point> place = mapData.getPuPlacementPoint(territory);
        // if pu_place.txt is specified draw there
        if (place.isPresent()) {
            draw(bounds, graphics, place.get().x, place.get().y, img, prod, drawFromTopLeft);
        } else {
            // otherwise, draw under the territory name
            draw(bounds, graphics, x + ((fm.stringWidth(territoryName)) >> 1) - ((fm.stringWidth(prod)) >> 1), y + fm.getLeading() + fm.getAscent(), img, prod, drawFromTopLeft);
        }
    }
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) FontMetrics(java.awt.FontMetrics) Rectangle(java.awt.Rectangle) Point(java.awt.Point) Image(java.awt.Image) MapImage(games.strategy.triplea.image.MapImage) Point(java.awt.Point)

Aggregations

TerritoryAttachment (games.strategy.triplea.attachments.TerritoryAttachment)36 Territory (games.strategy.engine.data.Territory)29 Unit (games.strategy.engine.data.Unit)24 TripleAUnit (games.strategy.triplea.TripleAUnit)19 ArrayList (java.util.ArrayList)14 PlayerID (games.strategy.engine.data.PlayerID)13 GameData (games.strategy.engine.data.GameData)9 HashSet (java.util.HashSet)8 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)7 ProTerritory (games.strategy.triplea.ai.pro.data.ProTerritory)6 UnitType (games.strategy.engine.data.UnitType)5 ProBattleResult (games.strategy.triplea.ai.pro.data.ProBattleResult)5 CompositeChange (games.strategy.engine.data.CompositeChange)4 Resource (games.strategy.engine.data.Resource)4 ProOtherMoveOptions (games.strategy.triplea.ai.pro.data.ProOtherMoveOptions)4 PlayerAttachment (games.strategy.triplea.attachments.PlayerAttachment)4 IntegerMap (games.strategy.util.IntegerMap)4 RelationshipTracker (games.strategy.engine.data.RelationshipTracker)3 ProPlaceTerritory (games.strategy.triplea.ai.pro.data.ProPlaceTerritory)3 ProPurchaseTerritory (games.strategy.triplea.ai.pro.data.ProPurchaseTerritory)3