Use VAssignGenericAmount dialog to assign DividedAsYouChoose values

This commit is contained in:
Lyu Zong-Hong
2021-06-18 11:21:23 +09:00
parent 5193447625
commit dfecf5d40a
4 changed files with 81 additions and 63 deletions

View File

@@ -74,6 +74,7 @@ public class VAssignGenericAmount {
private final String lblAmount; private final String lblAmount;
private final JLabel lblTotalAmount; private final JLabel lblTotalAmount;
private final boolean atLeastOne;
// Label Buttons // Label Buttons
private final FButton btnOK = new FButton(localizer.getMessage("lblOk")); private final FButton btnOK = new FButton(localizer.getMessage("lblOk"));
private final FButton btnReset = new FButton(localizer.getMessage("lblReset")); private final FButton btnReset = new FButton(localizer.getMessage("lblReset"));
@@ -126,6 +127,7 @@ public class VAssignGenericAmount {
dlg.setTitle(localizer.getMessage("lbLAssignAmountForEffect", amountLabel, effectSource.toString())); dlg.setTitle(localizer.getMessage("lbLAssignAmountForEffect", amountLabel, effectSource.toString()));
totalAmountToAssign = amount; totalAmountToAssign = amount;
this.atLeastOne = atLeastOne;
lblAmount = amountLabel; lblAmount = amountLabel;
lblTotalAmount = new FLabel.Builder().text(localizer.getMessage("lblTotalAmountText", lblAmount)).build(); lblTotalAmount = new FLabel.Builder().text(localizer.getMessage("lblTotalAmountText", lblAmount)).build();
@@ -170,7 +172,7 @@ public class VAssignGenericAmount {
btnOK.addActionListener(new ActionListener() { btnOK.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent arg0) { finish(); } }); @Override public void actionPerformed(ActionEvent arg0) { finish(); } });
btnReset.addActionListener(new ActionListener() { btnReset.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent arg0) { resetAssignedAmount(); initialAssignAmount(atLeastOne); } }); @Override public void actionPerformed(ActionEvent arg0) { resetAssignedAmount(); initialAssignAmount(); } });
// Final UI layout // Final UI layout
pnlMain.setLayout(new MigLayout("insets 0, gap 0, wrap 2, ax center")); pnlMain.setLayout(new MigLayout("insets 0, gap 0, wrap 2, ax center"));
@@ -188,7 +190,7 @@ public class VAssignGenericAmount {
pnlMain.getRootPane().setDefaultButton(btnOK); pnlMain.getRootPane().setDefaultButton(btnOK);
initialAssignAmount(atLeastOne); initialAssignAmount();
SOverlayUtils.showOverlay(); SOverlayUtils.showOverlay();
dlg.setUndecorated(true); dlg.setUndecorated(true);
@@ -237,6 +239,9 @@ public class VAssignGenericAmount {
if (amountToAdd > remainingAmount) { if (amountToAdd > remainingAmount) {
amountToAdd = remainingAmount; amountToAdd = remainingAmount;
} }
if (atLeastOne && assigned + amountToAdd < 1) {
amountToAdd = 1 - assigned;
}
if (0 == amountToAdd || amountToAdd + assigned < 0) { if (0 == amountToAdd || amountToAdd + assigned < 0) {
return; return;
@@ -246,7 +251,7 @@ public class VAssignGenericAmount {
updateLabels(); updateLabels();
} }
private void initialAssignAmount(boolean atLeastOne) { private void initialAssignAmount() {
if (!atLeastOne) { if (!atLeastOne) {
updateLabels(); updateLabels();
return; return;

View File

@@ -61,6 +61,7 @@ public class VAssignGenericAmount extends FDialog {
private final String lblAmount; private final String lblAmount;
private final FLabel lblTotalAmount; private final FLabel lblTotalAmount;
private final boolean atLeastOne;
private final EffectSourcePanel pnlSource; private final EffectSourcePanel pnlSource;
private final TargetsPanel pnlTargets; private final TargetsPanel pnlTargets;
@@ -80,6 +81,7 @@ public class VAssignGenericAmount extends FDialog {
callback = waitCallback; callback = waitCallback;
totalAmountToAssign = amount; totalAmountToAssign = amount;
this.atLeastOne = atLeastOne;
lblAmount = amountLabel; lblAmount = amountLabel;
lblTotalAmount = add(new FLabel.Builder().text(Localizer.getInstance().getMessage("lblTotalAmountText", lblAmount)).align(Align.center).build()); lblTotalAmount = add(new FLabel.Builder().text(Localizer.getInstance().getMessage("lblTotalAmountText", lblAmount)).align(Align.center).build());
@@ -97,11 +99,11 @@ public class VAssignGenericAmount extends FDialog {
@Override @Override
public void handleEvent(FEvent e) { public void handleEvent(FEvent e) {
resetAssignedDamage(); resetAssignedDamage();
initialAssignAmount(atLeastOne); initialAssignAmount();
} }
}); });
initialAssignAmount(atLeastOne); initialAssignAmount();
} }
@Override @Override
@@ -264,6 +266,9 @@ public class VAssignGenericAmount extends FDialog {
if (amountToAdd > remainingAmount) { if (amountToAdd > remainingAmount) {
amountToAdd = remainingAmount; amountToAdd = remainingAmount;
} }
if (atLeastOne && assigned + amountToAdd < 1) {
amountToAdd = 1 - assigned;
}
if (0 == amountToAdd || amountToAdd + assigned < 0) { if (0 == amountToAdd || amountToAdd + assigned < 0) {
return; return;
@@ -273,7 +278,7 @@ public class VAssignGenericAmount extends FDialog {
updateLabels(); updateLabels();
} }
private void initialAssignAmount(boolean atLeastOne) { private void initialAssignAmount() {
if (!atLeastOne) { if (!atLeastOne) {
updateLabels(); updateLabels();
return; return;

View File

@@ -8,7 +8,6 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import forge.game.GameEntity; import forge.game.GameEntity;
@@ -129,10 +128,10 @@ public final class InputSelectTargets extends InputSyncronizedBase {
"(Targeting ERROR)", ""); "(Targeting ERROR)", "");
showMessage(message, sa.getView()); showMessage(message, sa.getView());
if (sa.isDividedAsYouChoose() && sa.getMinTargets() == 0 && sa.getTargets().size() == 0) { if (divisionValues != null && sa.getMinTargets() == 0 && sa.getTargets().size() == 0) {
// extra logic for Divided with min targets = 0, should only work if num targets are 0 too // extra logic for Divided with min targets = 0, should only work if num targets are 0 too
getController().getGui().updateButtons(getOwner(), true, true, false); getController().getGui().updateButtons(getOwner(), true, true, false);
} else if (!sa.isMinTargetChosen() || sa.isDividedAsYouChoose()) { } else if (!sa.isMinTargetChosen() || divisionValues != null) {
// If reached Minimum targets, enable OK button // If reached Minimum targets, enable OK button
if (mandatory && tgt.hasCandidates(sa, true)) { if (mandatory && tgt.hasCandidates(sa, true)) {
// Player has to click on a target // Player has to click on a target
@@ -280,7 +279,7 @@ public final class InputSelectTargets extends InputSyncronizedBase {
return false; return false;
} }
if (sa.isDividedAsYouChoose()) { if (divisionValues != null) {
Boolean val = onDividedAsYouChoose(card); Boolean val = onDividedAsYouChoose(card);
if (val != null) { if (val != null) {
return val; return val;
@@ -322,7 +321,7 @@ public final class InputSelectTargets extends InputSyncronizedBase {
return; return;
} }
if (sa.isDividedAsYouChoose()) { if (divisionValues != null) {
Boolean val = onDividedAsYouChoose(player); Boolean val = onDividedAsYouChoose(player);
if (val != null) { if (val != null) {
return; return;
@@ -332,7 +331,6 @@ public final class InputSelectTargets extends InputSyncronizedBase {
} }
protected Boolean onDividedAsYouChoose(GameObject go) { protected Boolean onDividedAsYouChoose(GameObject go) {
if (divisionValues != null) {
if (divisionValues.isEmpty()) { if (divisionValues.isEmpty()) {
return false; return false;
} }
@@ -355,34 +353,6 @@ public final class InputSelectTargets extends InputSyncronizedBase {
} }
divisionValues.remove(chosen); divisionValues.remove(chosen);
sa.addDividedAllocation(go, chosen); sa.addDividedAllocation(go, chosen);
} else {
final int stillToDivide = sa.getStillToDivide();
int allocatedPortion = 0;
// allow allocation only if the max targets isn't reached and there are more candidates
if ((sa.getTargets().size() + 1 < sa.getMaxTargets()) && (tgt.getNumCandidates(sa, true) - 1 > 0) && stillToDivide > 1) {
final ImmutableList.Builder<Integer> choices = ImmutableList.builder();
for (int i = 1; i <= stillToDivide; i++) {
choices.add(Integer.valueOf(i));
}
String apiBasedMessage = "Distribute how much to ";
if (sa.getApi() == ApiType.DealDamage) {
apiBasedMessage = "Select how much damage to deal to ";
} else if (sa.getApi() == ApiType.PreventDamage) {
apiBasedMessage = "Select how much damage to prevent to ";
}
final StringBuilder sb = new StringBuilder();
sb.append(apiBasedMessage);
sb.append(go.toString());
final Integer chosen = getController().getGui().oneOrNone(sb.toString(), choices.build());
if (null == chosen) {
return true;
}
allocatedPortion = chosen;
} else { // otherwise assign the rest of the damage/protection
allocatedPortion = stillToDivide;
}
sa.addDividedAllocation(go, allocatedPortion);
}
return null; return null;
} }
@@ -419,7 +389,8 @@ public final class InputSelectTargets extends InputSyncronizedBase {
} }
private boolean hasAllTargets() { private boolean hasAllTargets() {
return sa.isMaxTargetChosen() || (sa.isDividedAsYouChoose() && sa.getStillToDivide() == 0); return sa.isMaxTargetChosen() || (divisionValues != null && sa.getStillToDivide() == 0)
|| (sa.isDividedAsYouChoose() && sa.getTargets().size() == sa.getStillToDivide());
} }
@Override @Override

View File

@@ -1951,7 +1951,44 @@ public class PlayerControllerHuman extends PlayerController implements IGameCont
checkSA = checkSA.getSubAbility(); checkSA = checkSA.getSubAbility();
} }
return select.chooseTargets(null, null, null, false, canFilterMustTarget); boolean result = select.chooseTargets(null, null, null, false, canFilterMustTarget);
// assign divided as you choose values
if (result && currentAbility.isDividedAsYouChoose() && currentAbility.getStillToDivide() > 0) {
int amount = currentAbility.getStillToDivide();
final List<GameEntity> targets = currentAbility.getTargets().getTargetEntities();
if (targets.size() == 1) {
currentAbility.addDividedAllocation(targets.get(0), amount);
} else if (targets.size() == amount) {
for (GameEntity e : targets) {
currentAbility.addDividedAllocation(e, 1);
}
} else if (targets.size() > amount) {
return false;
} else {
String label = "lblDamage";
if (currentAbility.getApi() == ApiType.PreventDamage) {
label = "lblShield";
} else if (currentAbility.getApi() == ApiType.PutCounter) {
label = "lblCounters";
}
label = localizer.getMessage(label).toLowerCase();
final CardView vSource = CardView.get(currentAbility.getHostCard());
final Map<GameEntityView, Integer> vTargets = new HashMap<>(targets.size());
for (GameEntity e : targets) {
vTargets.put(GameEntityView.get(e), amount);
}
final Map<GameEntityView, Integer> vResult = getGui().assignGenericAmount(vSource, vTargets, amount, true, label);
for (GameEntity e : targets) {
currentAbility.addDividedAllocation(e, vResult.get(GameEntityView.get(e)));
}
if (currentAbility.getStillToDivide() > 0) {
return false;
}
}
}
return result;
} }
@Override @Override