mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 12:18:00 +00:00
Support updating card rules and display when saving script changes
This commit is contained in:
@@ -8701,9 +8701,9 @@ public class Card extends GameEntity implements Comparable<Card> {
|
||||
|
||||
private static final Map<PaperCard, Card> cp2card = new HashMap<PaperCard, Card>();
|
||||
public static Card getCardForUi(IPaperCard pc) {
|
||||
if( pc instanceof PaperCard ) {
|
||||
if (pc instanceof PaperCard) {
|
||||
Card res = cp2card.get(pc);
|
||||
if (null == res) {
|
||||
if (res == null) {
|
||||
res = fromPaperCard(pc, null);
|
||||
cp2card.put((PaperCard) pc, res);
|
||||
}
|
||||
@@ -8712,5 +8712,14 @@ public class Card extends GameEntity implements Comparable<Card> {
|
||||
return fromPaperCard(pc, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update Card instance for the given PaperCard if any
|
||||
* @param pc
|
||||
*/
|
||||
public static void updateCard(PaperCard pc) {
|
||||
Card res = cp2card.get(pc);
|
||||
if (res != null) {
|
||||
cp2card.put(pc, fromPaperCard(pc, null));
|
||||
}
|
||||
}
|
||||
} // end Card class
|
||||
|
||||
@@ -30,16 +30,19 @@ import forge.card.mana.ManaCost;
|
||||
* @version $Id: CardRules.java 9708 2011-08-09 19:34:12Z jendave $
|
||||
*/
|
||||
public final class CardRules implements ICardCharacteristics {
|
||||
private final CardSplitType splitType;
|
||||
private final ICardFace mainPart;
|
||||
private final ICardFace otherPart;
|
||||
private CardSplitType splitType;
|
||||
private ICardFace mainPart;
|
||||
private ICardFace otherPart;
|
||||
//private final Map<String, CardInSet> setsPrinted = new TreeMap<String, CardInSet>(String.CASE_INSENSITIVE_ORDER);
|
||||
|
||||
private CardAiHints aiHints;
|
||||
private ColorSet colorIdentity = null;
|
||||
private File sourceFile;
|
||||
|
||||
public CardRules(ICardFace[] faces, CardSplitType altMode, CardAiHints cah) {
|
||||
public CardRules() {
|
||||
}
|
||||
|
||||
public void setup(ICardFace[] faces, CardSplitType altMode, CardAiHints cah) {
|
||||
splitType = altMode;
|
||||
mainPart = faces[0];
|
||||
otherPart = faces[1];
|
||||
@@ -65,6 +68,12 @@ public final class CardRules implements ICardCharacteristics {
|
||||
colMask |= calculateColorIdentity(otherPart);
|
||||
}
|
||||
colorIdentity = ColorSet.fromMask(colMask);
|
||||
|
||||
//reset these
|
||||
this.deltaHand = 0;
|
||||
this.deltaLife = 0;
|
||||
this.dlUrl = null;
|
||||
this.dlUrlOtherSide = null;
|
||||
}
|
||||
|
||||
private byte calculateColorIdentity(ICardFace face) {
|
||||
@@ -136,7 +145,6 @@ public final class CardRules implements ICardCharacteristics {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ManaCost getManaCost() {
|
||||
switch(splitType.getAggregationMethod()) {
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
package forge.card;
|
||||
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import forge.card.mana.IParserManaCost;
|
||||
@@ -70,33 +71,41 @@ public class CardRulesReader {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the card.
|
||||
* Create new CardRules from read properties
|
||||
*
|
||||
* @return the card
|
||||
*/
|
||||
public final CardRules getCard() {
|
||||
public final CardRules createCardRules() {
|
||||
final CardRules rules = new CardRules();
|
||||
apply(rules);
|
||||
return rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply read properties to a CardRules object
|
||||
*
|
||||
*/
|
||||
private final void apply(CardRules rules) {
|
||||
CardAiHints cah = new CardAiHints(removedFromAIDecks, removedFromRandomDecks, hints, needs );
|
||||
faces[0].assignMissingFields();
|
||||
if (null != faces[1]) faces[1].assignMissingFields();
|
||||
final CardRules result = new CardRules(faces, altMode, cah);
|
||||
result.setDlUrls(pictureUrl);
|
||||
if (StringUtils.isNotBlank(handLife))
|
||||
result.setVanguardProperties(handLife);
|
||||
return result;
|
||||
if (faces[1] != null) {
|
||||
faces[1].assignMissingFields();
|
||||
}
|
||||
rules.setup(faces, altMode, cah);
|
||||
rules.setDlUrls(pictureUrl);
|
||||
if (StringUtils.isNotBlank(handLife)) {
|
||||
rules.setVanguardProperties(handLife);
|
||||
}
|
||||
}
|
||||
|
||||
public final CardRules readCard(final Iterable<String> script) {
|
||||
this.reset();
|
||||
for (String line : script) {
|
||||
if (line.isEmpty() || line.charAt(0) == '#') {
|
||||
continue;
|
||||
}
|
||||
this.parseLine(line);
|
||||
}
|
||||
return this.getCard();
|
||||
return this.createCardRules();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parses the line.
|
||||
*
|
||||
@@ -104,6 +113,10 @@ public class CardRulesReader {
|
||||
* the line
|
||||
*/
|
||||
public final void parseLine(final String line) {
|
||||
if (line.isEmpty() || line.charAt(0) == '#') {
|
||||
return;
|
||||
}
|
||||
|
||||
int colonPos = line.indexOf(':');
|
||||
String key = colonPos > 0 ? line.substring(0, colonPos) : line;
|
||||
String value = colonPos > 0 ? line.substring(1+colonPos).trim() : null;
|
||||
@@ -225,6 +238,7 @@ public class CardRulesReader {
|
||||
|
||||
/**
|
||||
* Instantiates class, reads a card. Do not use for batch operations.
|
||||
*
|
||||
* @param script
|
||||
* @return
|
||||
*/
|
||||
@@ -233,7 +247,22 @@ public class CardRulesReader {
|
||||
for(String line : script) {
|
||||
crr.parseLine(line);
|
||||
}
|
||||
return crr.getCard();
|
||||
return crr.createCardRules();
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates class, reads card rules from script, then updates an existing card.
|
||||
*
|
||||
* @param rules
|
||||
* @param script
|
||||
*/
|
||||
public static void updateCardRules(CardRules rules, String script) {
|
||||
CardRulesReader crr = new CardRulesReader();
|
||||
String[] lines = script.split("(\r\n)|\n");
|
||||
for (String line : lines) {
|
||||
crr.parseLine(line);
|
||||
}
|
||||
crr.apply(rules);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,6 @@ import java.awt.event.KeyEvent;
|
||||
import javax.swing.AbstractAction;
|
||||
import javax.swing.Action;
|
||||
import javax.swing.KeyStroke;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.event.DocumentEvent;
|
||||
import javax.swing.event.DocumentListener;
|
||||
import javax.swing.event.UndoableEditEvent;
|
||||
|
||||
@@ -540,10 +540,6 @@ public abstract class ItemManager<T extends InventoryItem> extends JPanel {
|
||||
*/
|
||||
public void focus() {
|
||||
this.table.requestFocusInWindow();
|
||||
|
||||
if (this.table.getRowCount() > 0) {
|
||||
this.table.changeSelection(0, 0, false, false);
|
||||
}
|
||||
}
|
||||
|
||||
public void addSelectionListener(ListSelectionListener listener) {
|
||||
|
||||
@@ -11,6 +11,8 @@ import forge.Command;
|
||||
import forge.Singletons;
|
||||
import forge.gui.framework.FScreen;
|
||||
import forge.gui.framework.ICDoc;
|
||||
import forge.gui.match.controllers.CDetail;
|
||||
import forge.gui.match.controllers.CPicture;
|
||||
import forge.gui.toolbox.FTextEditor;
|
||||
import forge.gui.workshop.menus.WorkshopFileMenu;
|
||||
import forge.gui.workshop.views.VCardDesigner;
|
||||
@@ -139,6 +141,11 @@ public enum CCardScript implements ICDoc {
|
||||
|
||||
this.baseText = text;
|
||||
updateDirtyFlag();
|
||||
|
||||
this.currentCard.updateRules(text);
|
||||
VWorkshopCatalog.SINGLETON_INSTANCE.getCardManager().repaint();
|
||||
CDetail.SINGLETON_INSTANCE.showCard(this.currentCard);
|
||||
CPicture.SINGLETON_INSTANCE.showImage(this.currentCard);
|
||||
return true;
|
||||
} catch (final Exception ex) {
|
||||
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "FileUtil : writeFile() error, problem writing file - " + sourceFile + " : " + ex);
|
||||
|
||||
@@ -19,8 +19,10 @@ package forge.item;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
|
||||
import forge.Card;
|
||||
import forge.card.CardRarity;
|
||||
import forge.card.CardRules;
|
||||
import forge.card.CardRulesReader;
|
||||
|
||||
|
||||
/**
|
||||
@@ -34,10 +36,10 @@ import forge.card.CardRules;
|
||||
*/
|
||||
public final class PaperCard implements Comparable<IPaperCard>, InventoryItemFromSet, IPaperCard {
|
||||
// Reference to rules
|
||||
private final transient CardRules card;
|
||||
private final transient CardRules rules;
|
||||
|
||||
// These fields are kinda PK for PrintedCard
|
||||
public final String name;
|
||||
public String name;
|
||||
public final String edition;
|
||||
public final int artIndex;
|
||||
public final boolean foil;
|
||||
@@ -72,7 +74,7 @@ public final class PaperCard implements Comparable<IPaperCard>, InventoryItemFro
|
||||
|
||||
@Override
|
||||
public CardRules getRules() {
|
||||
return this.card;
|
||||
return this.rules;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -80,15 +82,11 @@ public final class PaperCard implements Comparable<IPaperCard>, InventoryItemFro
|
||||
return this.rarity;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// @Override
|
||||
// public String getImageKey() {
|
||||
// return getImageLocator(getImageName(), getArtIndex(), true, false);
|
||||
// }
|
||||
|
||||
|
||||
@Override
|
||||
public String getItemType() {
|
||||
return "Card";
|
||||
@@ -100,7 +98,7 @@ public final class PaperCard implements Comparable<IPaperCard>, InventoryItemFro
|
||||
public static final Function<PaperCard, CardRules> FN_GET_RULES = new Function<PaperCard, CardRules>() {
|
||||
@Override
|
||||
public CardRules apply(final PaperCard from) {
|
||||
return from.card;
|
||||
return from.rules;
|
||||
}
|
||||
};
|
||||
public static final Function<PaperCard, String> FN_GET_NAME = new Function<PaperCard, String>() {
|
||||
@@ -110,19 +108,20 @@ public final class PaperCard implements Comparable<IPaperCard>, InventoryItemFro
|
||||
}
|
||||
};
|
||||
|
||||
public PaperCard(final CardRules c, final String edition0, final CardRarity rare, final int index) {
|
||||
this(c, edition0, rare, index, false);
|
||||
public PaperCard(final CardRules rules0, final String edition0, final CardRarity rarity0, final int artIndex0) {
|
||||
this(rules0, edition0, rarity0, artIndex0, false);
|
||||
}
|
||||
|
||||
public PaperCard(final CardRules c, final String edition0, final CardRarity rare, final int index, final boolean foil) {
|
||||
if ( edition0 == null || c == null || rare == null )
|
||||
public PaperCard(final CardRules rules0, final String edition0, final CardRarity rarity0, final int artIndex0, final boolean foil0) {
|
||||
if (edition0 == null || rules0 == null || rarity0 == null) {
|
||||
throw new IllegalArgumentException("Cannot create card without rules, edition or rarity");
|
||||
this.card = c;
|
||||
this.name = c.getName();
|
||||
}
|
||||
this.rules = rules0;
|
||||
this.name = rules0.getName();
|
||||
this.edition = edition0;
|
||||
this.artIndex = index;
|
||||
this.foil = foil;
|
||||
this.rarity = rare;
|
||||
this.rarity = rarity0;
|
||||
this.artIndex = artIndex0;
|
||||
this.foil = foil0;
|
||||
}
|
||||
|
||||
// Want this class to be a key for HashTable
|
||||
@@ -193,4 +192,10 @@ public final class PaperCard implements Comparable<IPaperCard>, InventoryItemFro
|
||||
// TODO compare sets properly
|
||||
return this.edition.compareTo(o.getEdition());
|
||||
}
|
||||
|
||||
public void updateRules(String script) {
|
||||
CardRulesReader.updateCardRules(this.rules, script);
|
||||
this.name = this.rules.getName();
|
||||
Card.updateCard(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ public class DeckHintsTest {
|
||||
crr.parseLine(line);
|
||||
}
|
||||
// Don't care what the actual set or rarity is here.
|
||||
return new PaperCard(crr.getCard(), "M11", CardRarity.Common, 0);
|
||||
return new PaperCard(crr.createCardRules(), "M11", CardRarity.Common, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user