Skip to content

Commit

Permalink
Fix unit chooser missing "All" button. (#10509)
Browse files Browse the repository at this point in the history
A 2.6 change resulted in a Predicate being passed to the UnitChooser, which caused the All button to be hidden. That was not intentional and this PR makes the visibility of that button get set explicitly, converting call sites that want that button hidden to specify so.

Additionally, code for the None button, which was never shown, is cleaned up.
  • Loading branch information
asvitkine authored May 26, 2022
1 parent d3cb794 commit 0eeaf9d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public final class UnitChooser extends JPanel {
private final JLabel leftToSelect = new JLabel();
private final boolean allowMultipleHits;
private JButton autoSelectButton;
private JButton selectNoneButton;
private final Predicate<Collection<Unit>> match;
private final ScrollableTextFieldListener textFieldListener =
new ScrollableTextFieldListener() {
Expand All @@ -80,22 +79,13 @@ public void changedValue(final ScrollableTextField field) {

UnitChooser(
final Collection<Unit> units,
final Map<Unit, Collection<Unit>> dependent,
final boolean allowTwoHit,
final UiContext uiContext) {
this(units, List.of(), dependent, allowTwoHit, uiContext);
}

private UnitChooser(
final Collection<Unit> units,
final Collection<Unit> defaultSelections,
final Map<Unit, Collection<Unit>> dependent,
final @Nullable Map<Unit, Collection<Unit>> dependents,
final boolean allowTwoHit,
final UiContext uiContext) {
this(
units,
defaultSelections,
dependent,
List.of(),
dependents,
UnitSeparator.SeparatorCategories.builder().build(),
allowTwoHit,
uiContext);
Expand Down Expand Up @@ -167,7 +157,6 @@ void setMax(final int max) {
total = max;
textFieldListener.changedValue(null);
autoSelectButton.setVisible(false);
selectNoneButton.setVisible(false);
}

void setMaxAndShowMaxButton(final int max) {
Expand All @@ -176,6 +165,10 @@ void setMaxAndShowMaxButton(final int max) {
autoSelectButton.setText("Max");
}

public void setAllButtonVisible(boolean visible) {
autoSelectButton.setVisible(visible);
}

public void setTitle(final String title) {
this.title.setText(title);
this.title.setVisible(true);
Expand Down Expand Up @@ -288,8 +281,6 @@ private void layoutEntries() {
title.setVisible(false);
final Insets emptyInsets = new Insets(0, 0, 0, 0);
final Dimension buttonSize = new Dimension(80, 20);
selectNoneButton = new JButton("None");
selectNoneButton.setPreferredSize(buttonSize);
autoSelectButton = new JButton("All");
autoSelectButton.setPreferredSize(buttonSize);
add(
Expand All @@ -306,7 +297,6 @@ private void layoutEntries() {
emptyInsets,
0,
0));
selectNoneButton.addActionListener(e -> selectNone());
autoSelectButton.addActionListener(e -> autoSelect());
int rowIndex = 1;
for (final ChooserEntry entry : entries) {
Expand Down Expand Up @@ -343,8 +333,6 @@ private void layoutEntries() {
0,
0));
if (match != null) {
autoSelectButton.setVisible(false);
selectNoneButton.setVisible(false);
checkMatches();
}
}
Expand Down Expand Up @@ -385,12 +373,6 @@ List<Unit> getSelectedDamagedMultipleHitPointUnits() {
return selectedUnits;
}

private void selectNone() {
for (final ChooserEntry entry : entries) {
entry.selectNone();
}
}

// does not take into account multiple hit points
private void autoSelect() {
if (total == -1) {
Expand Down Expand Up @@ -463,13 +445,12 @@ public final class ChooserEntry {
allowMultipleHits
&& category.getHitPoints() > 1
&& category.getDamaged() < category.getHitPoints() - 1;
hitTexts = new ArrayList<>(Math.max(1, category.getHitPoints() - category.getDamaged()));
defaultHits = new ArrayList<>(Math.max(1, category.getHitPoints() - category.getDamaged()));
final int maxHitPoints = Math.max(1, category.getHitPoints() - category.getDamaged());
hitTexts = new ArrayList<>(maxHitPoints);
defaultHits = new ArrayList<>(maxHitPoints);
final int numUnits = category.getUnits().size();
int hitsUsedSoFar = 0;
for (int i = 0, m = Math.max(1, category.getHitPoints() - category.getDamaged());
i < m;
i++) {
for (int i = 0; i < maxHitPoints; i++) {
// TODO: check if default value includes damaged points or not
final int hitsToUse = Math.min(numUnits, (defaultValue - hitsUsedSoFar));
hitsUsedSoFar += hitsToUse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public Collection<Unit> getAirTransportsToLoad(
false,
getMap().getUiContext(),
transportsToLoadMatch);
chooser.setAllButtonVisible(false);
chooser.setTitle("Select air transports to load");
if (!confirmUnitChooserDialog(chooser, "What transports do you want to load")) {
return List.of();
Expand Down Expand Up @@ -861,6 +862,7 @@ private Collection<Unit> getUnitsToUnload(
false,
getMap().getUiContext(),
transportsToUnloadMatch);
chooser.setAllButtonVisible(false);
if (!confirmUnitChooserDialog(chooser, "Select transports to unload")) {
return List.of();
}
Expand Down Expand Up @@ -1273,6 +1275,7 @@ private Collection<Unit> getTransportsToLoad(
false,
getMap().getUiContext(),
transportsToLoadMatch);
chooser.setAllButtonVisible(false);
if (!confirmUnitChooserDialog(chooser, "Select transports to load")) {
return List.of();
}
Expand Down Expand Up @@ -1345,6 +1348,7 @@ private void allowSpecificUnitSelection(
false,
getMap().getUiContext(),
matchCriteria);
chooser.setAllButtonVisible(false);
final String text = "Select units to move from " + getFirstSelectedTerritory() + ".";
if (!confirmUnitChooserDialog(chooser, text)) {
units.clear();
Expand Down Expand Up @@ -1398,7 +1402,6 @@ private List<Unit> userChooseUnits(
final Set<Unit> defaultSelections,
final Predicate<Collection<Unit>> unitsToLoadMatch,
final List<Unit> unitsToLoad) {

// Allow player to select which to load.
final UnitChooser chooser =
new UnitChooser(
Expand All @@ -1409,6 +1412,7 @@ private List<Unit> userChooseUnits(
false,
getMap().getUiContext(),
unitsToLoadMatch);
chooser.setAllButtonVisible(false);
chooser.setTitle("Load air transports");
if (!confirmUnitChooserDialog(chooser, "What units do you want to load")) {
return List.of();
Expand Down

0 comments on commit 0eeaf9d

Please sign in to comment.