Support updating card rules and display when saving script changes

This commit is contained in:
drdev
2013-11-18 14:35:19 +00:00
parent e830527f74
commit fabc7a3a27
8 changed files with 101 additions and 48 deletions

View File

@@ -8701,9 +8701,9 @@ public class Card extends GameEntity implements Comparable<Card> {
private static final Map<PaperCard, Card> cp2card = new HashMap<PaperCard, Card>(); private static final Map<PaperCard, Card> cp2card = new HashMap<PaperCard, Card>();
public static Card getCardForUi(IPaperCard pc) { public static Card getCardForUi(IPaperCard pc) {
if( pc instanceof PaperCard ) { if (pc instanceof PaperCard) {
Card res = cp2card.get(pc); Card res = cp2card.get(pc);
if (null == res) { if (res == null) {
res = fromPaperCard(pc, null); res = fromPaperCard(pc, null);
cp2card.put((PaperCard) pc, res); cp2card.put((PaperCard) pc, res);
} }
@@ -8712,5 +8712,14 @@ public class Card extends GameEntity implements Comparable<Card> {
return fromPaperCard(pc, null); 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 } // end Card class

View File

@@ -30,16 +30,19 @@ import forge.card.mana.ManaCost;
* @version $Id: CardRules.java 9708 2011-08-09 19:34:12Z jendave $ * @version $Id: CardRules.java 9708 2011-08-09 19:34:12Z jendave $
*/ */
public final class CardRules implements ICardCharacteristics { public final class CardRules implements ICardCharacteristics {
private final CardSplitType splitType; private CardSplitType splitType;
private final ICardFace mainPart; private ICardFace mainPart;
private final ICardFace otherPart; private ICardFace otherPart;
//private final Map<String, CardInSet> setsPrinted = new TreeMap<String, CardInSet>(String.CASE_INSENSITIVE_ORDER); //private final Map<String, CardInSet> setsPrinted = new TreeMap<String, CardInSet>(String.CASE_INSENSITIVE_ORDER);
private CardAiHints aiHints; private CardAiHints aiHints;
private ColorSet colorIdentity = null; private ColorSet colorIdentity = null;
private File sourceFile; private File sourceFile;
public CardRules(ICardFace[] faces, CardSplitType altMode, CardAiHints cah) { public CardRules() {
}
public void setup(ICardFace[] faces, CardSplitType altMode, CardAiHints cah) {
splitType = altMode; splitType = altMode;
mainPart = faces[0]; mainPart = faces[0];
otherPart = faces[1]; otherPart = faces[1];
@@ -65,6 +68,12 @@ public final class CardRules implements ICardCharacteristics {
colMask |= calculateColorIdentity(otherPart); colMask |= calculateColorIdentity(otherPart);
} }
colorIdentity = ColorSet.fromMask(colMask); colorIdentity = ColorSet.fromMask(colMask);
//reset these
this.deltaHand = 0;
this.deltaLife = 0;
this.dlUrl = null;
this.dlUrlOtherSide = null;
} }
private byte calculateColorIdentity(ICardFace face) { private byte calculateColorIdentity(ICardFace face) {
@@ -136,7 +145,6 @@ public final class CardRules implements ICardCharacteristics {
} }
} }
@Override @Override
public ManaCost getManaCost() { public ManaCost getManaCost() {
switch(splitType.getAggregationMethod()) { switch(splitType.getAggregationMethod()) {

View File

@@ -18,6 +18,7 @@
package forge.card; package forge.card;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import forge.card.mana.IParserManaCost; import forge.card.mana.IParserManaCost;
@@ -70,33 +71,41 @@ public class CardRulesReader {
} }
/** /**
* Gets the card. * Create new CardRules from read properties
* *
* @return the card * @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 ); CardAiHints cah = new CardAiHints(removedFromAIDecks, removedFromRandomDecks, hints, needs );
faces[0].assignMissingFields(); faces[0].assignMissingFields();
if (null != faces[1]) faces[1].assignMissingFields(); if (faces[1] != null) {
final CardRules result = new CardRules(faces, altMode, cah); faces[1].assignMissingFields();
result.setDlUrls(pictureUrl); }
if (StringUtils.isNotBlank(handLife)) rules.setup(faces, altMode, cah);
result.setVanguardProperties(handLife); rules.setDlUrls(pictureUrl);
return result; if (StringUtils.isNotBlank(handLife)) {
rules.setVanguardProperties(handLife);
}
} }
public final CardRules readCard(final Iterable<String> script) { public final CardRules readCard(final Iterable<String> script) {
this.reset(); this.reset();
for (String line : script) { for (String line : script) {
if (line.isEmpty() || line.charAt(0) == '#') {
continue;
}
this.parseLine(line); this.parseLine(line);
} }
return this.getCard(); return this.createCardRules();
} }
/** /**
* Parses the line. * Parses the line.
* *
@@ -104,6 +113,10 @@ public class CardRulesReader {
* the line * the line
*/ */
public final void parseLine(final String line) { public final void parseLine(final String line) {
if (line.isEmpty() || line.charAt(0) == '#') {
return;
}
int colonPos = line.indexOf(':'); int colonPos = line.indexOf(':');
String key = colonPos > 0 ? line.substring(0, colonPos) : line; String key = colonPos > 0 ? line.substring(0, colonPos) : line;
String value = colonPos > 0 ? line.substring(1+colonPos).trim() : null; 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. * Instantiates class, reads a card. Do not use for batch operations.
*
* @param script * @param script
* @return * @return
*/ */
@@ -233,7 +247,22 @@ public class CardRulesReader {
for(String line : script) { for(String line : script) {
crr.parseLine(line); 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);
} }
/** /**

View File

@@ -7,7 +7,6 @@ import java.awt.event.KeyEvent;
import javax.swing.AbstractAction; import javax.swing.AbstractAction;
import javax.swing.Action; import javax.swing.Action;
import javax.swing.KeyStroke; import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener; import javax.swing.event.DocumentListener;
import javax.swing.event.UndoableEditEvent; import javax.swing.event.UndoableEditEvent;

View File

@@ -540,10 +540,6 @@ public abstract class ItemManager<T extends InventoryItem> extends JPanel {
*/ */
public void focus() { public void focus() {
this.table.requestFocusInWindow(); this.table.requestFocusInWindow();
if (this.table.getRowCount() > 0) {
this.table.changeSelection(0, 0, false, false);
}
} }
public void addSelectionListener(ListSelectionListener listener) { public void addSelectionListener(ListSelectionListener listener) {

View File

@@ -11,6 +11,8 @@ import forge.Command;
import forge.Singletons; import forge.Singletons;
import forge.gui.framework.FScreen; import forge.gui.framework.FScreen;
import forge.gui.framework.ICDoc; 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.toolbox.FTextEditor;
import forge.gui.workshop.menus.WorkshopFileMenu; import forge.gui.workshop.menus.WorkshopFileMenu;
import forge.gui.workshop.views.VCardDesigner; import forge.gui.workshop.views.VCardDesigner;
@@ -139,6 +141,11 @@ public enum CCardScript implements ICDoc {
this.baseText = text; this.baseText = text;
updateDirtyFlag(); updateDirtyFlag();
this.currentCard.updateRules(text);
VWorkshopCatalog.SINGLETON_INSTANCE.getCardManager().repaint();
CDetail.SINGLETON_INSTANCE.showCard(this.currentCard);
CPicture.SINGLETON_INSTANCE.showImage(this.currentCard);
return true; return true;
} catch (final Exception ex) { } catch (final Exception ex) {
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "FileUtil : writeFile() error, problem writing file - " + sourceFile + " : " + ex); JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "FileUtil : writeFile() error, problem writing file - " + sourceFile + " : " + ex);

View File

@@ -19,8 +19,10 @@ package forge.item;
import com.google.common.base.Function; import com.google.common.base.Function;
import forge.Card;
import forge.card.CardRarity; import forge.card.CardRarity;
import forge.card.CardRules; 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 { public final class PaperCard implements Comparable<IPaperCard>, InventoryItemFromSet, IPaperCard {
// Reference to rules // Reference to rules
private final transient CardRules card; private final transient CardRules rules;
// These fields are kinda PK for PrintedCard // These fields are kinda PK for PrintedCard
public final String name; public String name;
public final String edition; public final String edition;
public final int artIndex; public final int artIndex;
public final boolean foil; public final boolean foil;
@@ -72,7 +74,7 @@ public final class PaperCard implements Comparable<IPaperCard>, InventoryItemFro
@Override @Override
public CardRules getRules() { public CardRules getRules() {
return this.card; return this.rules;
} }
@Override @Override
@@ -80,15 +82,11 @@ public final class PaperCard implements Comparable<IPaperCard>, InventoryItemFro
return this.rarity; return this.rarity;
} }
// @Override // @Override
// public String getImageKey() { // public String getImageKey() {
// return getImageLocator(getImageName(), getArtIndex(), true, false); // return getImageLocator(getImageName(), getArtIndex(), true, false);
// } // }
@Override @Override
public String getItemType() { public String getItemType() {
return "Card"; 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>() { public static final Function<PaperCard, CardRules> FN_GET_RULES = new Function<PaperCard, CardRules>() {
@Override @Override
public CardRules apply(final PaperCard from) { 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>() { 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) { public PaperCard(final CardRules rules0, final String edition0, final CardRarity rarity0, final int artIndex0) {
this(c, edition0, rare, index, false); this(rules0, edition0, rarity0, artIndex0, false);
} }
public PaperCard(final CardRules c, final String edition0, final CardRarity rare, final int index, final boolean foil) { public PaperCard(final CardRules rules0, final String edition0, final CardRarity rarity0, final int artIndex0, final boolean foil0) {
if ( edition0 == null || c == null || rare == null ) if (edition0 == null || rules0 == null || rarity0 == null) {
throw new IllegalArgumentException("Cannot create card without rules, edition or rarity"); 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.edition = edition0;
this.artIndex = index; this.rarity = rarity0;
this.foil = foil; this.artIndex = artIndex0;
this.rarity = rare; this.foil = foil0;
} }
// Want this class to be a key for HashTable // 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 // TODO compare sets properly
return this.edition.compareTo(o.getEdition()); return this.edition.compareTo(o.getEdition());
} }
public void updateRules(String script) {
CardRulesReader.updateCardRules(this.rules, script);
this.name = this.rules.getName();
Card.updateCard(this);
}
} }

View File

@@ -138,7 +138,7 @@ public class DeckHintsTest {
crr.parseLine(line); crr.parseLine(line);
} }
// Don't care what the actual set or rarity is here. // 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);
} }
} }