From bc096fcb3c34639273c8f7938720c24cb090d9f8 Mon Sep 17 00:00:00 2001 From: Hans Mackowiak Date: Fri, 14 Jul 2023 07:23:35 +0200 Subject: [PATCH] InputSelectTargets: able to remove Target --- .../match/input/InputSelectTargets.java | 56 ++++++++++++------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/forge-gui/src/main/java/forge/gamemodes/match/input/InputSelectTargets.java b/forge-gui/src/main/java/forge/gamemodes/match/input/InputSelectTargets.java index 6ca83583028..5baca0aaa59 100644 --- a/forge-gui/src/main/java/forge/gamemodes/match/input/InputSelectTargets.java +++ b/forge-gui/src/main/java/forge/gamemodes/match/input/InputSelectTargets.java @@ -2,13 +2,14 @@ import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; + import java.util.List; -import java.util.Map; -import java.util.Map.Entry; +import java.util.Set; import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import forge.game.GameEntity; import forge.game.GameObject; @@ -34,7 +35,7 @@ public final class InputSelectTargets extends InputSyncronizedBase { private final List choices; // some cards can be targeted several times (eg: distribute damage as you choose) - private final Map targetDepth = new HashMap<>(); + private final Set targets = Sets.newHashSet(); private final TargetRestrictions tgt; private final SpellAbility sa; private final Collection divisionValues; @@ -60,7 +61,7 @@ public InputSelectTargets(final PlayerControllerHuman controller, final List o : targetDepth.entrySet()) { + for (final GameEntity o : targets) { //if it's not in gdx port landscape mode, append the linebreak if (!ForgeConstants.isGdxPortLandscape) sb.append("\n"); - sb.append(o.getKey()); + sb.append(o); //if it's in gdx port landscape mode, instead append the comma with space... if (ForgeConstants.isGdxPortLandscape) sb.append(", "); - if (o.getValue() > 1) { - sb.append(TextUtil.concatNoSpace(" (", String.valueOf(o.getValue()), " times)")); - } } if (!sa.getUniqueTargets().isEmpty()) { sb.append("\nParent Targeted:"); @@ -164,7 +162,8 @@ protected final void onOk() { @Override protected final boolean onCardSelected(final Card card, final List otherCardsToSelect, final ITriggerEvent triggerEvent) { - if (tgt.isUniqueTargets() && targetDepth.containsKey(card)) { + if (targets.contains(card)) { + removeTarget(card); return false; } @@ -230,7 +229,7 @@ protected final boolean onCardSelected(final Card card, final List otherCa // If all cards must have same controllers if (tgt.isSameController()) { final List targetedControllers = new ArrayList<>(); - for (final GameObject o : targetDepth.keySet()) { + for (final GameObject o : targets) { if (o instanceof Card) { final Player p = ((Card) o).getController(); targetedControllers.add(p); @@ -245,7 +244,7 @@ protected final boolean onCardSelected(final Card card, final List otherCa // If all cards must have different controllers if (tgt.isDifferentControllers()) { final List targetedControllers = new ArrayList<>(); - for (final GameObject o : targetDepth.keySet()) { + for (final GameObject o : targets) { if (o instanceof Card) { final Player p = ((Card) o).getController(); targetedControllers.add(p); @@ -260,7 +259,7 @@ protected final boolean onCardSelected(final Card card, final List otherCa // If all cards must have different mana values if (tgt.isDifferentCMC()) { final List targetedCMCs = new ArrayList<>(); - for (final GameObject o : targetDepth.keySet()) { + for (final GameObject o : targets) { if (o instanceof Card) { final Integer cmc = ((Card) o).getCMC(); targetedCMCs.add(cmc); @@ -289,7 +288,7 @@ protected final boolean onCardSelected(final Card card, final List otherCa @Override public String getActivateAction(final Card card) { - if (!tgt.isUniqueTargets() && targetDepth.containsKey(card)) { + if (!tgt.isUniqueTargets() && targets.contains(card)) { return null; } if (choices.contains(card)) { @@ -300,7 +299,8 @@ public String getActivateAction(final Card card) { @Override protected final void onPlayerSelected(final Player player, final ITriggerEvent triggerEvent) { - if (!tgt.isUniqueTargets() && targetDepth.containsKey(player)) { + if (targets.contains(player)) { + removeTarget(player); return; } @@ -360,8 +360,7 @@ private void addTarget(final GameEntity ge) { else if (ge instanceof Player) { getController().getGui().setHighlighted(PlayerView.get((Player) ge), true); } - final Integer val = targetDepth.get(ge); - targetDepth.put(ge, val == null ? Integer.valueOf(1) : Integer.valueOf(val.intValue() + 1) ); + targets.add(ge); if (hasAllTargets()) { bOk = true; @@ -376,8 +375,23 @@ else if (ge instanceof Player) { } } + private void removeTarget(final GameEntity ge) { + targets.remove(ge); + sa.getTargets().remove(ge); + if (ge instanceof Card) { + getController().getGui().setUsedToPay(CardView.get((Card) ge), false); + // try to get last selected card + lastTarget = Iterables.getLast(Iterables.filter(targets, Card.class), null); + } + else if (ge instanceof Player) { + getController().getGui().setHighlighted(PlayerView.get((Player) ge), false); + } + + this.showMessage(); + } + private void done() { - for (final GameEntity c : targetDepth.keySet()) { + for (final GameEntity c : targets) { if (c instanceof Card) { getController().getGui().setUsedToPay(CardView.get((Card) c), false); }