mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-17 11:18:01 +00:00
Use VAssignGenericAmount dialog to assign DividedAsYouChoose values
This commit is contained in:
@@ -74,6 +74,7 @@ public class VAssignGenericAmount {
|
||||
|
||||
private final String lblAmount;
|
||||
private final JLabel lblTotalAmount;
|
||||
private final boolean atLeastOne;
|
||||
// Label Buttons
|
||||
private final FButton btnOK = new FButton(localizer.getMessage("lblOk"));
|
||||
private final FButton btnReset = new FButton(localizer.getMessage("lblReset"));
|
||||
@@ -126,6 +127,7 @@ public class VAssignGenericAmount {
|
||||
dlg.setTitle(localizer.getMessage("lbLAssignAmountForEffect", amountLabel, effectSource.toString()));
|
||||
|
||||
totalAmountToAssign = amount;
|
||||
this.atLeastOne = atLeastOne;
|
||||
|
||||
lblAmount = amountLabel;
|
||||
lblTotalAmount = new FLabel.Builder().text(localizer.getMessage("lblTotalAmountText", lblAmount)).build();
|
||||
@@ -170,7 +172,7 @@ public class VAssignGenericAmount {
|
||||
btnOK.addActionListener(new ActionListener() {
|
||||
@Override public void actionPerformed(ActionEvent arg0) { finish(); } });
|
||||
btnReset.addActionListener(new ActionListener() {
|
||||
@Override public void actionPerformed(ActionEvent arg0) { resetAssignedAmount(); initialAssignAmount(atLeastOne); } });
|
||||
@Override public void actionPerformed(ActionEvent arg0) { resetAssignedAmount(); initialAssignAmount(); } });
|
||||
|
||||
// Final UI layout
|
||||
pnlMain.setLayout(new MigLayout("insets 0, gap 0, wrap 2, ax center"));
|
||||
@@ -188,7 +190,7 @@ public class VAssignGenericAmount {
|
||||
|
||||
pnlMain.getRootPane().setDefaultButton(btnOK);
|
||||
|
||||
initialAssignAmount(atLeastOne);
|
||||
initialAssignAmount();
|
||||
SOverlayUtils.showOverlay();
|
||||
|
||||
dlg.setUndecorated(true);
|
||||
@@ -237,6 +239,9 @@ public class VAssignGenericAmount {
|
||||
if (amountToAdd > remainingAmount) {
|
||||
amountToAdd = remainingAmount;
|
||||
}
|
||||
if (atLeastOne && assigned + amountToAdd < 1) {
|
||||
amountToAdd = 1 - assigned;
|
||||
}
|
||||
|
||||
if (0 == amountToAdd || amountToAdd + assigned < 0) {
|
||||
return;
|
||||
@@ -246,7 +251,7 @@ public class VAssignGenericAmount {
|
||||
updateLabels();
|
||||
}
|
||||
|
||||
private void initialAssignAmount(boolean atLeastOne) {
|
||||
private void initialAssignAmount() {
|
||||
if (!atLeastOne) {
|
||||
updateLabels();
|
||||
return;
|
||||
|
||||
@@ -61,6 +61,7 @@ public class VAssignGenericAmount extends FDialog {
|
||||
|
||||
private final String lblAmount;
|
||||
private final FLabel lblTotalAmount;
|
||||
private final boolean atLeastOne;
|
||||
|
||||
private final EffectSourcePanel pnlSource;
|
||||
private final TargetsPanel pnlTargets;
|
||||
@@ -80,6 +81,7 @@ public class VAssignGenericAmount extends FDialog {
|
||||
|
||||
callback = waitCallback;
|
||||
totalAmountToAssign = amount;
|
||||
this.atLeastOne = atLeastOne;
|
||||
|
||||
lblAmount = amountLabel;
|
||||
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
|
||||
public void handleEvent(FEvent e) {
|
||||
resetAssignedDamage();
|
||||
initialAssignAmount(atLeastOne);
|
||||
initialAssignAmount();
|
||||
}
|
||||
});
|
||||
|
||||
initialAssignAmount(atLeastOne);
|
||||
initialAssignAmount();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -264,6 +266,9 @@ public class VAssignGenericAmount extends FDialog {
|
||||
if (amountToAdd > remainingAmount) {
|
||||
amountToAdd = remainingAmount;
|
||||
}
|
||||
if (atLeastOne && assigned + amountToAdd < 1) {
|
||||
amountToAdd = 1 - assigned;
|
||||
}
|
||||
|
||||
if (0 == amountToAdd || amountToAdd + assigned < 0) {
|
||||
return;
|
||||
@@ -273,7 +278,7 @@ public class VAssignGenericAmount extends FDialog {
|
||||
updateLabels();
|
||||
}
|
||||
|
||||
private void initialAssignAmount(boolean atLeastOne) {
|
||||
private void initialAssignAmount() {
|
||||
if (!atLeastOne) {
|
||||
updateLabels();
|
||||
return;
|
||||
|
||||
@@ -8,7 +8,6 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import forge.game.GameEntity;
|
||||
@@ -129,10 +128,10 @@ public final class InputSelectTargets extends InputSyncronizedBase {
|
||||
"(Targeting ERROR)", "");
|
||||
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
|
||||
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 (mandatory && tgt.hasCandidates(sa, true)) {
|
||||
// Player has to click on a target
|
||||
@@ -280,7 +279,7 @@ public final class InputSelectTargets extends InputSyncronizedBase {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (sa.isDividedAsYouChoose()) {
|
||||
if (divisionValues != null) {
|
||||
Boolean val = onDividedAsYouChoose(card);
|
||||
if (val != null) {
|
||||
return val;
|
||||
@@ -322,7 +321,7 @@ public final class InputSelectTargets extends InputSyncronizedBase {
|
||||
return;
|
||||
}
|
||||
|
||||
if (sa.isDividedAsYouChoose()) {
|
||||
if (divisionValues != null) {
|
||||
Boolean val = onDividedAsYouChoose(player);
|
||||
if (val != null) {
|
||||
return;
|
||||
@@ -332,57 +331,28 @@ public final class InputSelectTargets extends InputSyncronizedBase {
|
||||
}
|
||||
|
||||
protected Boolean onDividedAsYouChoose(GameObject go) {
|
||||
if (divisionValues != null) {
|
||||
if (divisionValues.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
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 ";
|
||||
}
|
||||
else if (sa.getApi() == ApiType.PutCounter) {
|
||||
apiBasedMessage = "Select how many counters to distribute to ";
|
||||
}
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
sb.append(apiBasedMessage);
|
||||
sb.append(go.toString());
|
||||
final Integer chosen = getController().getGui().oneOrNone(sb.toString(), Lists.newArrayList(divisionValues));
|
||||
if (chosen == null) {
|
||||
return true; //still return true since there was a valid choice
|
||||
}
|
||||
divisionValues.remove(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);
|
||||
if (divisionValues.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
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 ";
|
||||
}
|
||||
else if (sa.getApi() == ApiType.PutCounter) {
|
||||
apiBasedMessage = "Select how many counters to distribute to ";
|
||||
}
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
sb.append(apiBasedMessage);
|
||||
sb.append(go.toString());
|
||||
final Integer chosen = getController().getGui().oneOrNone(sb.toString(), Lists.newArrayList(divisionValues));
|
||||
if (chosen == null) {
|
||||
return true; //still return true since there was a valid choice
|
||||
}
|
||||
divisionValues.remove(chosen);
|
||||
sa.addDividedAllocation(go, chosen);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -419,7 +389,8 @@ public final class InputSelectTargets extends InputSyncronizedBase {
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -1951,7 +1951,44 @@ public class PlayerControllerHuman extends PlayerController implements IGameCont
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user