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;
}
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\"/>";
}
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();
}
}
Aggregations