use of games.strategy.triplea.util.UnitCategory in project triplea by triplea-game.
the class PlayerUnitsPanel method init.
/**
* Sets up components to an initial state.
*/
public void init(final PlayerID id, final List<Unit> units, final boolean land) {
isLand = land;
categories = new ArrayList<>(categorize(id, units));
categories.sort(Comparator.comparing(UnitCategory::getType, (ut1, ut2) -> {
final UnitAttachment u1 = UnitAttachment.get(ut1);
final UnitAttachment u2 = UnitAttachment.get(ut2);
// For land battles, sort by land, air, can't combat move (AA), bombarding
if (land) {
if (u1.getIsSea() != u2.getIsSea()) {
return u1.getIsSea() ? 1 : -1;
}
final boolean u1CanNotCombatMove = Matches.unitTypeCanNotMoveDuringCombatMove().test(ut1) || !Matches.unitTypeCanMove(id).test(ut1);
final boolean u2CanNotCombatMove = Matches.unitTypeCanNotMoveDuringCombatMove().test(ut2) || !Matches.unitTypeCanMove(id).test(ut2);
if (u1CanNotCombatMove != u2CanNotCombatMove) {
return u1CanNotCombatMove ? 1 : -1;
}
if (u1.getIsAir() != u2.getIsAir()) {
return u1.getIsAir() ? 1 : -1;
}
} else {
if (u1.getIsSea() != u2.getIsSea()) {
return u1.getIsSea() ? -1 : 1;
}
}
return u1.getName().compareTo(u2.getName());
}));
removeAll();
final Predicate<UnitType> predicate;
if (land) {
if (defender) {
predicate = Matches.unitTypeIsNotSea();
} else {
predicate = Matches.unitTypeIsNotSea().or(Matches.unitTypeCanBombard(id));
}
} else {
predicate = Matches.unitTypeIsSeaOrAir();
}
final IntegerMap<UnitType> costs;
try {
data.acquireReadLock();
costs = TuvUtils.getCostsForTuv(id, data);
} finally {
data.releaseReadLock();
}
for (final UnitCategory category : categories) {
if (predicate.test(category.getType())) {
final UnitPanel upanel = new UnitPanel(uiContext, category, costs);
upanel.addChangeListener(this::notifyListeners);
add(upanel);
}
}
// TODO: probably do not need to do this much revalidation.
invalidate();
validate();
revalidate();
getParent().invalidate();
}
use of games.strategy.triplea.util.UnitCategory in project triplea by triplea-game.
the class HistoryDetailsPanel method renderUnits.
private void renderUnits(final GridBagConstraints mainConstraints, final Collection<Unit> units) {
final Collection<UnitCategory> unitsCategories = UnitSeperator.categorize(units);
final SimpleUnitPanel unitsPanel = new SimpleUnitPanel(mapPanel.getUiContext());
unitsPanel.setUnitsFromCategories(unitsCategories);
add(unitsPanel, mainConstraints);
}
use of games.strategy.triplea.util.UnitCategory in project triplea by triplea-game.
the class TileManager method drawUnits.
private void drawUnits(final Territory territory, final MapData mapData, final Set<Tile> drawnOn, final Set<IDrawable> drawing, final GameData data) {
final Iterator<Point> placementPoints = mapData.getPlacementPoints(territory).iterator();
if (placementPoints == null || !placementPoints.hasNext()) {
throw new IllegalStateException("No where to place units:" + territory.getName());
}
Point lastPlace = null;
for (final UnitCategory category : getSortedUnitCategories(territory, data)) {
final boolean overflow;
if (placementPoints.hasNext()) {
lastPlace = new Point(placementPoints.next());
overflow = false;
} else {
lastPlace = new Point(lastPlace);
overflow = true;
if (mapData.getPlacementOverflowToLeft(territory)) {
lastPlace.x -= uiContext.getUnitImageFactory().getUnitImageWidth();
} else {
lastPlace.x += uiContext.getUnitImageFactory().getUnitImageWidth();
}
}
final UnitsDrawer drawable = new UnitsDrawer(category.getUnits().size(), category.getType().getName(), category.getOwner().getName(), lastPlace, category.getDamaged(), category.getBombingDamage(), category.getDisabled(), overflow, territory.getName(), uiContext);
drawing.add(drawable);
allUnitDrawables.add(drawable);
for (Tile tile : getTiles(new Rectangle(lastPlace.x, lastPlace.y, uiContext.getUnitImageFactory().getUnitImageWidth(), uiContext.getUnitImageFactory().getUnitImageHeight()))) {
tile.addDrawable(drawable);
drawnOn.add(tile);
}
}
}
use of games.strategy.triplea.util.UnitCategory in project triplea by triplea-game.
the class OrderOfLossesInputPanel method getUnitButtonPanel.
private JPanel getUnitButtonPanel(final List<UnitCategory> categories, final JTextField textField) {
final JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
if (categories != null) {
final Set<UnitType> typesUsed = new HashSet<>();
for (final UnitCategory category : categories) {
// no duplicates or infrastructure allowed. no sea if land, no land if sea.
if (typesUsed.contains(category.getType()) || Matches.unitTypeIsInfrastructure().test(category.getType()) || (land && Matches.unitTypeIsSea().test(category.getType())) || (!land && Matches.unitTypeIsLand().test(category.getType()))) {
continue;
}
final String unitName = OOL_ALL + OOL_AMOUNT_DESCRIPTOR + category.getType().getName();
final String toolTipText = "<html>" + category.getType().getName() + ": " + category.getType().getTooltip(category.getOwner()) + "</html>";
final Optional<Image> img = uiContext.getUnitImageFactory().getImage(category.getType(), category.getOwner(), category.hasDamageOrBombingUnitDamage(), category.getDisabled());
if (img.isPresent()) {
final JButton button = new JButton(new ImageIcon(img.get()));
button.setToolTipText(toolTipText);
button.addActionListener(e -> textField.setText((textField.getText().length() > 0 ? (textField.getText() + OOL_SEPARATOR) : "") + unitName));
panel.add(button);
}
typesUsed.add(category.getType());
}
}
return panel;
}
use of games.strategy.triplea.util.UnitCategory in project triplea by triplea-game.
the class PlayerUnitsPanel method categorize.
/**
* Get all unit type categories that can be in combat first in the order of the player's
* production frontier and then any unit types the player owns on the map. Then populate the list
* of units into the categories.
*/
private Set<UnitCategory> categorize(final PlayerID id, final List<Unit> units) {
// Get all unit types from production frontier and player unit types on the map
final Set<UnitCategory> categories = new LinkedHashSet<>();
for (final UnitType t : getUnitTypes(id)) {
final UnitCategory category = new UnitCategory(t, id);
categories.add(category);
}
// Populate units into each category then add any remaining categories (damaged units, etc)
final Set<UnitCategory> unitCategories = UnitSeperator.categorize(units);
for (final UnitCategory category : categories) {
for (final UnitCategory unitCategory : unitCategories) {
if (category.equals(unitCategory)) {
category.getUnits().addAll(unitCategory.getUnits());
}
}
}
categories.addAll(unitCategories);
return categories;
}
Aggregations