Skip to content

Commit

Permalink
push-down units to SimpleUnitPanel (#12785)
Browse files Browse the repository at this point in the history
* opt_getUnitTypes #1

PlayerUnitsPanel.java
- break up logic in method getAllUnitCategories into separate methods
- add documentation

* push-down units to SimpleUnitPanel #1

Replaces usages of method SimpleUnitPanel.setUnitsFromCategories with new method setUnits in:
- BottomBar.java (passing through methods setTerritory -> updateTerritoryInfo -> createUnitBar)
- HistoryDetailsPanel.java
- MovePanel.java
- PlacementUnitsCollapsiblePanel.java
- PlacePanel.java
- PurchasePanel.java

SimpleUnitPanel.java
- remove method setUnitsFromCategories
- add new method setUnits
  • Loading branch information
frigoref authored Jul 27, 2024
1 parent 6a82921 commit 5c6d1a0
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import games.strategy.engine.data.Resource;
import games.strategy.engine.data.Territory;
import games.strategy.engine.data.TerritoryEffect;
import games.strategy.engine.data.Unit;
import games.strategy.engine.data.events.TerritoryListener;
import games.strategy.engine.data.events.ZoomMapListener;
import games.strategy.triplea.Constants;
import games.strategy.triplea.attachments.TerritoryAttachment;
import games.strategy.triplea.util.UnitCategory;
import games.strategy.triplea.util.UnitSeparator;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
Expand Down Expand Up @@ -141,10 +140,8 @@ public void setTerritory(final @Nullable Territory territory) {
// Get all the needed data while holding a lock, then invoke UI updates on the EDT.
try (GameData.Unlocker ignored = territory.getData().acquireReadLock()) {
final String territoryName = territory.getName();
final Collection<UnitCategory> units =
uiContext.isShowUnitsInStatusBar()
? UnitSeparator.categorize(territory.getUnits())
: List.of();
final Collection<Unit> units =
uiContext.isShowUnitsInStatusBar() ? territory.getUnits() : List.of();
final TerritoryAttachment ta = TerritoryAttachment.get(territory);
final IntegerMap<Resource> resources = new IntegerMap<>();
final List<String> territoryEffectNames;
Expand All @@ -170,7 +167,7 @@ public void setTerritory(final @Nullable Territory territory) {
private void updateTerritoryInfo(
String territoryName,
List<String> territoryEffectNames,
Collection<UnitCategory> units,
Collection<Unit> units,
IntegerMap<Resource> resources) {
// Box layout with horizontal glue on both sides achieves the following desirable properties:
// 1. If the content is narrower than the available space, it will be centered.
Expand Down Expand Up @@ -237,11 +234,11 @@ private Border createBorderToFillAvailableHeight(int componentHeight, int availa
return BorderFactory.createEmptyBorder(topPad, 0, bottomPad, 0);
}

private SimpleUnitPanel createUnitBar(Collection<UnitCategory> units) {
private SimpleUnitPanel createUnitBar(Collection<Unit> units) {
final var unitBar = new SimpleUnitPanel(uiContext, SimpleUnitPanel.Style.SMALL_ICONS_ROW);
unitBar.setScaleFactor(0.5);
unitBar.setShowCountsForSingleUnits(false);
unitBar.setUnitsFromCategories(units);
unitBar.setUnits(units);
// Constrain the preferred size to the available size so that unit images that may not fully fit
// don't cause layout issues.
final int unitsWidth = unitBar.getPreferredSize().width;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import games.strategy.engine.data.GameStep;
import games.strategy.engine.data.Unit;
import games.strategy.triplea.settings.ClientSetting;
import games.strategy.triplea.util.UnitSeparator;
import java.util.Collection;
import java.util.List;
import javax.swing.SwingUtilities;
Expand Down Expand Up @@ -48,7 +47,7 @@ private void updateStep() {
SwingUtilities.invokeLater(
() -> {
if (shouldRenderPanelForThisGameStep && !playerUnits.isEmpty()) {
unitsToPlacePanel.setUnitsFromCategories(UnitSeparator.categorize(playerUnits));
unitsToPlacePanel.setUnits(playerUnits);
panel.setVisible(true);
} else {
panel.setVisible(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package games.strategy.triplea.ui;

import com.google.common.collect.Lists;
import games.strategy.engine.data.GameData;
import games.strategy.engine.data.GamePlayer;
import games.strategy.engine.data.GameState;
Expand All @@ -17,7 +16,6 @@
import games.strategy.triplea.util.UnitCategory;
import games.strategy.triplea.util.UnitSeparator;
import java.awt.Image;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -118,18 +116,18 @@ public void setUnitsFromRepairRuleMap(
}

/**
* Adds units to the panel based on the specified unit categories.
* Adds {@code units} to the panel in accordance to their {@code UnitCategory}.
*
* @param categories a collection of UnitCategories.
* @param units units to be placed
*/
public void setUnitsFromCategories(final Collection<UnitCategory> categories) {
public void setUnits(final Collection<Unit> units) {
removeAll();
if (categories.isEmpty()) {
if (units.isEmpty()) {
return;
}
final GameData gameData = categories.iterator().next().getUnitAttachment().getData();
final ArrayList<UnitCategory> unitCategories = Lists.newArrayList(categories);
UnitSeparator.sortUnitCategories(unitCategories, gameData);
final GameData gameData = units.iterator().next().getUnitAttachment().getData();
final Collection<UnitCategory> unitCategories =
UnitSeparator.getSortedUnitCategories(units, gameData, uiContext.getMapData());
for (final UnitCategory category : unitCategories) {
addUnits(
category.getOwner(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import games.strategy.triplea.ui.DicePanel;
import games.strategy.triplea.ui.SimpleUnitPanel;
import games.strategy.triplea.ui.panels.map.MapPanel;
import games.strategy.triplea.util.UnitCategory;
import games.strategy.triplea.util.UnitSeparator;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
Expand Down Expand Up @@ -117,9 +115,8 @@ private void showTerritory(final Territory territory) {
}

private void renderUnits(final GridBagConstraints mainConstraints, final Collection<Unit> units) {
final Collection<UnitCategory> unitsCategories = UnitSeparator.categorize(units);
final SimpleUnitPanel unitsPanel = new SimpleUnitPanel(mapPanel.getUiContext());
unitsPanel.setUnitsFromCategories(unitsCategories);
unitsPanel.setUnits(units);
content.add(unitsPanel, mainConstraints);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import games.strategy.triplea.delegate.remote.IAbstractPlaceDelegate;
import games.strategy.triplea.ui.panels.map.MapPanel;
import games.strategy.triplea.ui.panels.map.MapSelectionListener;
import games.strategy.triplea.util.UnitCategory;
import games.strategy.triplea.util.UnitSeparator;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
Expand Down Expand Up @@ -113,7 +111,7 @@ public void territorySelected(final Territory territory, final MouseDetails e) {
}

private void updateStep() {
final Collection<UnitCategory> unitsToPlace;
final Collection<Unit> unitsToPlace;
final boolean showUnitsToPlace;
final GameData data = getData();
try (GameData.Unlocker ignored = data.acquireReadLock()) {
Expand All @@ -134,7 +132,7 @@ private void updateStep() {
// If we're past the production step (even if player didn't produce anything) or
// there are units that are available to place, show the panel (set unitsToPlace).
showUnitsToPlace = (postProductionStep || !playerUnits.isEmpty());
unitsToPlace = showUnitsToPlace ? UnitSeparator.categorize(playerUnits) : null;
unitsToPlace = showUnitsToPlace ? playerUnits : List.of();
if (GameStep.isPurchaseOrBidStep(step.getName())) {
postProductionStep = true;
}
Expand All @@ -150,7 +148,7 @@ private void updateStep() {
SwingUtilities.invokeLater(
() -> {
if (showUnitsToPlace) {
unitsToPlacePanel.setUnitsFromCategories(unitsToPlace);
unitsToPlacePanel.setUnits(unitsToPlace);
SwingComponents.redraw(unitsToPlacePanel);
} else {
unitsToPlacePanel.removeAll();
Expand All @@ -160,19 +158,19 @@ private void updateStep() {

@Override
public void gameDataChanged(final Change change) {
final Collection<UnitCategory> unitsToPlace;
final Collection<Unit> unitsToPlace;
final GameData data = getData();
try (GameData.Unlocker ignored = data.acquireReadLock()) {
final GamePlayer player = data.getSequence().getStep().getPlayerId();
if (player == null) {
return;
}
unitsToPlace = UnitSeparator.categorize(player.getUnits());
unitsToPlace = player.getUnits();
}

SwingUtilities.invokeLater(
() -> {
unitsToPlacePanel.setUnitsFromCategories(unitsToPlace);
unitsToPlacePanel.setUnits(unitsToPlace);
unitsToPlacePanel.revalidate();
unitsToPlacePanel.repaint();
});
Expand Down Expand Up @@ -308,8 +306,6 @@ protected final List<Component> getAdditionalButtons() {
}

private void updateUnits() {
final Collection<UnitCategory> unitCategories =
UnitSeparator.categorize(getCurrentPlayer().getUnits());
unitsToPlacePanel.setUnitsFromCategories(unitCategories);
unitsToPlacePanel.setUnits(getCurrentPlayer().getUnits());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import games.strategy.triplea.delegate.PurchaseDelegate;
import games.strategy.triplea.formatter.MyFormatter;
import games.strategy.triplea.ui.panels.map.MapPanel;
import games.strategy.triplea.util.UnitSeparator;
import java.awt.event.ActionEvent;
import java.util.Collection;
import javax.swing.AbstractAction;
Expand Down Expand Up @@ -134,8 +133,7 @@ public void display(final GamePlayer gamePlayer) {
add(SwingComponents.leftBox(purchasedUnits));

try (GameData.Unlocker ignored = getData().acquireReadLock()) {
purchasedPreviousRoundsUnits.setUnitsFromCategories(
UnitSeparator.categorize(gamePlayer.getUnits()));
purchasedPreviousRoundsUnits.setUnits(gamePlayer.getUnits());
add(Box.createVerticalStrut(4));
if (!gamePlayer.getUnitCollection().isEmpty()) {
add(SwingComponents.leftBox(purchasedPreviousRoundsLabel));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ private boolean showScrambleWarningAndConfirmMove(
new SimpleUnitPanel(
getMap().getUiContext(),
SimpleUnitPanel.Style.SMALL_ICONS_WRAPPED_WITH_LABEL_WHEN_EMPTY);
unitPanel.setUnitsFromCategories(UnitSeparator.categorize(possibleScramblers));
unitPanel.setUnits(possibleScramblers);
final String message = "Warning: Units may scramble from nearby territories to defend:";
final JPanel panel =
new JPanelBuilder()
Expand Down

0 comments on commit 5c6d1a0

Please sign in to comment.