* UI: split cards - fixed wrong card names in rules text and hints (#6549);

This commit is contained in:
Oleg Agafonov 2020-05-18 08:24:17 +04:00
parent 0c2e08f54e
commit 0be7d6579b
3 changed files with 41 additions and 11 deletions

View file

@ -14,9 +14,8 @@ import org.mage.card.arcane.UI;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.*;
import static mage.client.dialog.PreferencesDialog.KEY_MAGE_PANEL_LAST_SIZE;
@ -344,6 +343,9 @@ public final class GuiDisplayUtil {
}
buffer.append("</td></tr></table>");
// split card rules shows up by parts, so no needs to duplicate it later (only dynamic abilities must be shown)
Set<String> duplicatedRules = new HashSet<>();
StringBuilder rule = new StringBuilder("<br/>");
if (card.isSplitCard()) {
rule.append("<table cellspacing=0 cellpadding=0 border=0 width='100%'>");
@ -356,7 +358,9 @@ public final class GuiDisplayUtil {
rule.append("</td></tr></table>");
for (String ruling : card.getLeftSplitRules()) {
if (ruling != null && !ruling.replace(".", "").trim().isEmpty()) {
rule.append("<p style='margin: 2px'>").append(ruling).append("</p>");
// split names must be replaced
duplicatedRules.add(ruling);
rule.append("<p style='margin: 2px'>").append(replaceNamesInRule(ruling, card.getLeftSplitName())).append("</p>");
}
}
rule.append("<table cellspacing=0 cellpadding=0 border=0 width='100%'>");
@ -369,13 +373,18 @@ public final class GuiDisplayUtil {
rule.append("</td></tr></table>");
for (String ruling : card.getRightSplitRules()) {
if (ruling != null && !ruling.replace(".", "").trim().isEmpty()) {
rule.append("<p style='margin: 2px'>").append(ruling).append("</p>");
// split names must be replaced
duplicatedRules.add(ruling);
rule.append("<p style='margin: 2px'>").append(replaceNamesInRule(ruling, card.getRightSplitName())).append("</p>");
}
}
}
if (!textLines.getLines().isEmpty()) {
for (String textLine : textLines.getLines()) {
if (textLine != null && !textLine.replace(".", "").trim().isEmpty()) {
if (duplicatedRules.contains(textLine)) {
continue;
}
rule.append("<p style='margin: 2px'>").append(textLine).append("</p>");
}
}
@ -383,8 +392,7 @@ public final class GuiDisplayUtil {
String legal = rule.toString();
if (!legal.isEmpty()) {
legal = legal.replaceAll("\\{this\\}", card.getName().isEmpty() ? "this" : card.getName());
legal = legal.replaceAll("\\{source\\}", card.getName().isEmpty() ? "this" : card.getName());
legal = replaceNamesInRule(legal, card.getDisplayName()); // must show real display name (e.g. split part, not original card)
buffer.append(ManaSymbols.replaceSymbolsWithHTML(legal, ManaSymbols.Type.TOOLTIP));
}
@ -397,6 +405,12 @@ public final class GuiDisplayUtil {
return buffer;
}
private static String replaceNamesInRule(String rule, String cardName) {
String res = rule.replaceAll("\\{this\\}", cardName.isEmpty() ? "this" : cardName);
res = res.replaceAll("\\{source\\}", cardName.isEmpty() ? "this" : cardName);
return res;
}
private static String getResourcePath(String image) {
return GuiDisplayUtil.class.getClassLoader().getResource(image).toString();
}

View file

@ -140,13 +140,30 @@ public abstract class CardRenderer {
break;
}
// workaround to use real split card names
String realCardName = cardView.getDisplayName();
if (cardView.isSplitCard()) {
for (String partRule : cardView.getLeftSplitRules()) {
if (partRule.equals(rule)) {
realCardName = cardView.getLeftSplitName();
break;
}
}
for (String partRule : cardView.getRightSplitRules()) {
if (partRule.equals(rule)) {
realCardName = cardView.getRightSplitName();
break;
}
}
}
// Kill reminder text
if (PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_RENDERING_REMINDER_TEXT, "false").equals("false")) {
rule = CardRendererUtils.killReminderText(rule).trim();
}
if (!rule.isEmpty()) {
TextboxRule tbRule = TextboxRuleParser.parse(cardView, rule);
TextboxRule tbRule = TextboxRuleParser.parse(cardView, rule, realCardName);
if (tbRule.type == TextboxRuleType.SIMPLE_KEYWORD) {
keywords.add(tbRule);
} else if (tbRule.text.isEmpty()) {

View file

@ -30,7 +30,7 @@ public final class TextboxRuleParser {
// if the ability is a loyalty ability, and returning an TextboxRule
// representing that information, which can be used to render the rule in
// the textbox of a card.
public static TextboxRule parse(CardView source, String rule) {
public static TextboxRule parse(CardView source, String rule, String cardNameToUse) {
// List of regions to apply
List<TextboxRule.AttributeRegion> regions = new ArrayList<>();
@ -104,10 +104,9 @@ public final class TextboxRuleParser {
String contents = rule.substring(index + 1, closeIndex);
if (contents.equals("this") || contents.equals("source")) {
// Replace {this} with the card's name
String cardName = source.getName();
build.append(cardName);
build.append(cardNameToUse);
index += contents.length() + 2;
outputIndex += cardName.length();
outputIndex += cardNameToUse.length();
} else {
Image symbol = ManaSymbols.getSizedManaSymbol(contents.replace("/", ""), 10);
if (symbol != null) {