Search in sources :

Example 6 with UnitCategory

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();
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) NamedAttachable(games.strategy.engine.data.NamedAttachable) TuvUtils(games.strategy.triplea.util.TuvUtils) UnitSeperator(games.strategy.triplea.util.UnitSeperator) ArrayList(java.util.ArrayList) UnitCategory(games.strategy.triplea.util.UnitCategory) UiContext(games.strategy.triplea.ui.UiContext) UnitType(games.strategy.engine.data.UnitType) LinkedHashSet(java.util.LinkedHashSet) BoxLayout(javax.swing.BoxLayout) CollectionUtils(games.strategy.util.CollectionUtils) IntegerMap(games.strategy.util.IntegerMap) Unit(games.strategy.engine.data.Unit) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Territory(games.strategy.engine.data.Territory) Component(java.awt.Component) GameData(games.strategy.engine.data.GameData) List(java.util.List) PlayerID(games.strategy.engine.data.PlayerID) ProductionFrontier(games.strategy.engine.data.ProductionFrontier) Matches(games.strategy.triplea.delegate.Matches) ProductionRule(games.strategy.engine.data.ProductionRule) Comparator(java.util.Comparator) JPanel(javax.swing.JPanel) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) UnitType(games.strategy.engine.data.UnitType) UnitCategory(games.strategy.triplea.util.UnitCategory)

Example 7 with UnitCategory

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);
}
Also used : SimpleUnitPanel(games.strategy.triplea.ui.SimpleUnitPanel) UnitCategory(games.strategy.triplea.util.UnitCategory)

Example 8 with UnitCategory

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);
        }
    }
}
Also used : Rectangle(java.awt.Rectangle) Point(java.awt.Point) UnitCategory(games.strategy.triplea.util.UnitCategory)

Example 9 with UnitCategory

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;
}
Also used : JPanel(javax.swing.JPanel) ImageIcon(javax.swing.ImageIcon) UnitType(games.strategy.engine.data.UnitType) BoxLayout(javax.swing.BoxLayout) JButton(javax.swing.JButton) Image(java.awt.Image) UnitCategory(games.strategy.triplea.util.UnitCategory) HashSet(java.util.HashSet)

Example 10 with UnitCategory

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) UnitType(games.strategy.engine.data.UnitType) UnitCategory(games.strategy.triplea.util.UnitCategory)

Aggregations

UnitCategory (games.strategy.triplea.util.UnitCategory)17 Unit (games.strategy.engine.data.Unit)8 ArrayList (java.util.ArrayList)7 Territory (games.strategy.engine.data.Territory)5 Point (java.awt.Point)5 GameData (games.strategy.engine.data.GameData)4 PlayerID (games.strategy.engine.data.PlayerID)4 UnitType (games.strategy.engine.data.UnitType)4 TripleAUnit (games.strategy.triplea.TripleAUnit)4 JPanel (javax.swing.JPanel)4 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)3 Image (java.awt.Image)3 Collection (java.util.Collection)3 LinkedHashSet (java.util.LinkedHashSet)3 List (java.util.List)3 BoxLayout (javax.swing.BoxLayout)3 ImageIcon (javax.swing.ImageIcon)3 JLabel (javax.swing.JLabel)3 Matches (games.strategy.triplea.delegate.Matches)2 UnitSeperator (games.strategy.triplea.util.UnitSeperator)2