Search in sources :

Example 1 with UnitImageFactory

use of games.strategy.triplea.image.UnitImageFactory in project triplea by triplea-game.

the class UnitType method getPlayerUnitsWithImages.

private static List<UnitType> getPlayerUnitsWithImages(final PlayerID player, final GameData data, final UiContext uiContext) {
    final ArrayList<UnitType> unitTypes = new ArrayList<>();
    data.acquireReadLock();
    try {
        // add first based on current production ability
        if (player.getProductionFrontier() != null) {
            for (final ProductionRule productionRule : player.getProductionFrontier()) {
                for (final Entry<NamedAttachable, Integer> entry : productionRule.getResults().entrySet()) {
                    if (UnitType.class.isAssignableFrom(entry.getKey().getClass())) {
                        final UnitType ut = (UnitType) entry.getKey();
                        if (!unitTypes.contains(ut)) {
                            unitTypes.add(ut);
                        }
                    }
                }
            }
        }
        // use the units on a map.
        for (final Territory t : data.getMap()) {
            for (final Unit u : t.getUnits()) {
                if (u.getOwner().equals(player)) {
                    final UnitType ut = u.getType();
                    if (!unitTypes.contains(ut)) {
                        unitTypes.add(ut);
                    }
                }
            }
        }
        // now check if we have the art for anything that is left
        for (final UnitType ut : data.getUnitTypeList().getAllUnitTypes()) {
            if (!unitTypes.contains(ut)) {
                try {
                    final UnitImageFactory imageFactory = uiContext.getUnitImageFactory();
                    if (imageFactory != null) {
                        final Optional<Image> unitImage = imageFactory.getImage(ut, player, false, false);
                        if (unitImage.isPresent()) {
                            if (!unitTypes.contains(ut)) {
                                unitTypes.add(ut);
                            }
                        }
                    }
                } catch (final Exception e) {
                    // TODO: does this cause excessive logging noise, or is the message useful?
                    ClientLogger.logQuietly("Quietly ignoring an exception while drawing unit type: " + ut + ", ", e);
                }
            }
        }
    } finally {
        data.releaseReadLock();
    }
    return unitTypes;
}
Also used : ArrayList(java.util.ArrayList) TripleAUnit(games.strategy.triplea.TripleAUnit) Image(java.awt.Image) UnitImageFactory(games.strategy.triplea.image.UnitImageFactory)

Example 2 with UnitImageFactory

use of games.strategy.triplea.image.UnitImageFactory in project triplea by triplea-game.

the class HelpMenu method getUnitImageUrl.

private static String getUnitImageUrl(final UnitType unitType, final PlayerID player, final UiContext uiContext) {
    final UnitImageFactory unitImageFactory = uiContext.getUnitImageFactory();
    if (player == null || unitImageFactory == null) {
        return "no image";
    }
    final Optional<URL> imageUrl = unitImageFactory.getBaseImageUrl(unitType.getName(), player);
    final String imageLocation = imageUrl.isPresent() ? imageUrl.get().toString() : "";
    return "<img src=\"" + imageLocation + "\" border=\"0\"/>";
}
Also used : URL(java.net.URL) UnitImageFactory(games.strategy.triplea.image.UnitImageFactory)

Example 3 with UnitImageFactory

use of games.strategy.triplea.image.UnitImageFactory in project triplea by triplea-game.

the class EditProductionPanel method initRules.

@Override
protected void initRules(final PlayerID player, final IntegerMap<ProductionRule> initialPurchase) {
    this.data.acquireReadLock();
    try {
        id = player;
        final Set<UnitType> unitsAllowed = new HashSet<>();
        if (player.getProductionFrontier() != null) {
            for (final ProductionRule productionRule : player.getProductionFrontier()) {
                final Rule rule = new Rule(productionRule, player);
                for (final Entry<NamedAttachable, Integer> entry : productionRule.getResults().entrySet()) {
                    if (UnitType.class.isAssignableFrom(entry.getKey().getClass())) {
                        unitsAllowed.add((UnitType) entry.getKey());
                    }
                }
                final int initialQuantity = initialPurchase.getInt(productionRule);
                rule.setQuantity(initialQuantity);
                rules.add(rule);
            }
        }
        // use the units on a map.
        for (final Territory t : data.getMap()) {
            for (final Unit u : t.getUnits()) {
                if (u.getOwner().equals(player)) {
                    final UnitType ut = u.getType();
                    if (!unitsAllowed.contains(ut)) {
                        unitsAllowed.add(ut);
                        final IntegerMap<NamedAttachable> result = new IntegerMap<>();
                        result.add(ut, 1);
                        final IntegerMap<Resource> cost = new IntegerMap<>();
                        cost.add(data.getResourceList().getResource(Constants.PUS), 1);
                        final ProductionRule newRule = new ProductionRule(ut.getName(), data, result, cost);
                        final Rule rule = new Rule(newRule, player);
                        rule.setQuantity(0);
                        rules.add(rule);
                    }
                }
            }
        }
        // now check if we have the art for anything that is left
        for (final UnitType ut : data.getUnitTypeList().getAllUnitTypes()) {
            if (!unitsAllowed.contains(ut)) {
                try {
                    final UnitImageFactory imageFactory = uiContext.getUnitImageFactory();
                    if (imageFactory != null) {
                        final Optional<Image> unitImage = imageFactory.getImage(ut, player, false, false);
                        if (unitImage.isPresent()) {
                            unitsAllowed.add(ut);
                            final IntegerMap<NamedAttachable> result = new IntegerMap<>();
                            result.add(ut, 1);
                            final IntegerMap<Resource> cost = new IntegerMap<>();
                            cost.add(data.getResourceList().getResource(Constants.PUS), 1);
                            final ProductionRule newRule = new ProductionRule(ut.getName(), data, result, cost);
                            final Rule rule = new Rule(newRule, player);
                            rule.setQuantity(0);
                            rules.add(rule);
                        }
                    }
                } catch (final Exception e) {
                // ignore
                }
            }
        }
    } finally {
        this.data.releaseReadLock();
    }
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) NamedAttachable(games.strategy.engine.data.NamedAttachable) Resource(games.strategy.engine.data.Resource) Unit(games.strategy.engine.data.Unit) Image(java.awt.Image) ProductionRule(games.strategy.engine.data.ProductionRule) UnitType(games.strategy.engine.data.UnitType) ProductionRule(games.strategy.engine.data.ProductionRule) HashSet(java.util.HashSet) UnitImageFactory(games.strategy.triplea.image.UnitImageFactory)

Aggregations

UnitImageFactory (games.strategy.triplea.image.UnitImageFactory)3 Image (java.awt.Image)2 NamedAttachable (games.strategy.engine.data.NamedAttachable)1 ProductionRule (games.strategy.engine.data.ProductionRule)1 Resource (games.strategy.engine.data.Resource)1 Territory (games.strategy.engine.data.Territory)1 Unit (games.strategy.engine.data.Unit)1 UnitType (games.strategy.engine.data.UnitType)1 TripleAUnit (games.strategy.triplea.TripleAUnit)1 IntegerMap (games.strategy.util.IntegerMap)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1