Deck recognition (work in progress)

Card.isSick returned
some DeckEditor-related classes renamed
This commit is contained in:
Maxmtg
2011-09-21 01:14:42 +00:00
parent ffe83b0af3
commit 33ce88cb56
10 changed files with 184 additions and 205 deletions

View File

@@ -2767,10 +2767,9 @@ public class Card extends GameEntity implements Comparable<Card> {
*
* @return a boolean.
*/
public final boolean hasSickness() {
return !hasKeyword("Haste") && sickness;
}
public final boolean hasSickness() { return !hasKeyword("Haste") && sickness; }
public final boolean isSick() { return !hasKeyword("Haste") && sickness && isCreature(); }
/**
* <p>Setter for the field <code>imageName</code>.</p>
*

View File

@@ -0,0 +1,66 @@
package forge.deck;
import java.util.Map.Entry;
import forge.item.CardPrinted;
/**
* <p>DeckRecognizer class.</p>
*
* @author Forge
* @version $Id: DeckRecognizer.java 10499 2011-09-17 15:08:47Z Max mtg $
*
*/
public class DeckRecognizer {
public enum TokenType {
KnownCardWithNumber,
UnknownCardWithNumber,
SectionName,
Comment,
Unknown
}
public static class Token {
private final TokenType type;
private final CardPrinted card;
private final int number;
private final String text;
public Token(CardPrinted knownCard, int count) {
card = knownCard;
text = null;
number = count;
type = TokenType.KnownCardWithNumber;
}
public Token(String unknownCard, int count) {
card = null;
text = unknownCard;
number = count;
type = TokenType.UnknownCardWithNumber;
}
public Token(TokenType type1, int count, String message)
{
if (type1 == TokenType.KnownCardWithNumber || type1 == TokenType.UnknownCardWithNumber) {
throw new IllegalArgumentException("Use specialized constructors for recognized card lines");
}
card = null;
number = count;
type = type1;
text = message;
}
public String getText() { return text; }
public CardPrinted getCard() { return card; }
public TokenType getType() { return type; }
public int getNumber() { return number; }
}
public static Token recognizeLine(String line)
{
return new Token(TokenType.Unknown, 0, line);
}
}

View File

@@ -1,153 +0,0 @@
package forge.deck;
import forge.item.CardDb;
/**
* <p>DownloadDeck class.</p>
*
* @author Forge
* @version $Id$
*/
public class DownloadDeck {
/**
* <p>foundNumberCard.</p>
*
* @param rStr a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public String foundNumberCard(String rStr) {
int temp;
int i;
for (i = 0; i < rStr.length(); i++) {
temp = rStr.codePointAt(i);
if (temp >= 48 && temp <= 57) {
break;
}
}
if (rStr.codePointAt(i + 1) >= 48 && rStr.codePointAt(i + 1) <= 57) {
return rStr.substring(i, i + 2);
} else {
return rStr.substring(i, i + 1);
}
}
/**
* <p>foundNameCard.</p>
*
* @param rStr a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public String foundNameCard(String rStr) {
int temp;
int i;
for (i = 0; i < rStr.length(); i++) {
temp = rStr.codePointAt(i);
if (temp >= 48 && temp <= 57) {
break;
}
}
return rStr.substring(0, i - 1);
}
/**
* <p>removeSpace.</p>
*
* @param rStr a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public String removeSpace(String rStr) {
int temp;
int i;
for (i = 0; i < rStr.length(); i++) {
temp = rStr.codePointAt(i);
if (temp != 32) {
break;
}
}
return rStr.substring(i);
}
/**
* <p>removeSpaceBack.</p>
*
* @param rStr a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public String removeSpaceBack(String rStr) {
int temp;
int i;
for (i = rStr.length() - 1; i > -1; i = i - 1) {
temp = rStr.codePointAt(i);
if (temp != 32) {
break;
}
}
return rStr.substring(0, i + 1);
}
/**
* <p>removeFoundNumberCard.</p>
*
* @param rStr a {@link java.lang.String} object.
* @param Number a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public String removeFoundNumberCard(String rStr, String Number) {
int a;
int temp;
a = rStr.indexOf(Number);
temp = rStr.codePointAt(a + 1);
if (temp >= 48 && temp <= 57) {
return rStr.substring(a + 2);
} else {
return rStr.substring(a + 1);
}
}
/**
* <p>removeFoundNameCard.</p>
*
* @param rStr a {@link java.lang.String} object.
* @param Name a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public String removeFoundNameCard(String rStr, String Name) {
int a;
a = Name.length();
return rStr.substring(a);
}
/**
* <p>isCardSupport.</p>
*
* @param CardName a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean isCardSupport(String cardName) {
// TODO: using AllZone.getCardFactory().getCard() would probably be much faster.
return CardDb.instance().isCardSupported(cardName);
}
}

View File

@@ -10,7 +10,7 @@ import javax.swing.JCheckBox;
* Custom check box class for filter icon
*
*/
public class GuiFilterCheckBox extends JCheckBox {
public class CheckBoxWithIcon extends JCheckBox {
/* CHOPPIC */
/* Custom check box class for filter icons */
private static final long serialVersionUID = -8099263807219520120L;
@@ -18,9 +18,9 @@ public class GuiFilterCheckBox extends JCheckBox {
private String imagePath = "res/images/deckeditor/";
private String iconYes;
private String iconNo;
private GuiFilterCheckBox cb;
private CheckBoxWithIcon cb;
GuiFilterCheckBox(String filterName, String toolTip) {
CheckBoxWithIcon(String filterName, String toolTip) {
super("", true);
cb = this;
iconYes = imagePath + "filter_" + filterName + "_y.png";

View File

@@ -3,6 +3,7 @@ package forge.gui.deckeditor;
import java.awt.Container;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
@@ -37,11 +38,11 @@ import forge.item.ItemPoolView;
* @author Forge
* @version $Id$
*/
public final class DeckEditor extends DeckEditorBase {
public final class DeckEditorCommon extends DeckEditorBase {
/** Constant <code>serialVersionUID=130339644136746796L</code> */
private static final long serialVersionUID = 130339644136746796L;
public DeckEditorMenu customMenu;
public DeckEditorCommonMenu customMenu;
private JButton removeButton = new JButton();
private JButton addButton = new JButton();
@@ -58,12 +59,12 @@ public final class DeckEditor extends DeckEditorBase {
private static final long serialVersionUID = 5210924838133689758L;
public void execute() {
DeckEditor.this.dispose();
DeckEditorCommon.this.dispose();
exitCommand.execute();
}
};
customMenu = new DeckEditorMenu(this, AllZone.getDeckManager(), exit);
customMenu = new DeckEditorCommonMenu(this, AllZone.getDeckManager(), exit);
this.setJMenuBar(customMenu);
// do not change this!!!!
@@ -113,7 +114,7 @@ public final class DeckEditor extends DeckEditorBase {
}
public DeckEditor(GameType gameType) {
public DeckEditorCommon(GameType gameType) {
super(gameType);
try {
filterBoxes = new FilterCheckBoxes(true);
@@ -288,8 +289,8 @@ public final class DeckEditor extends DeckEditorBase {
void importButton_actionPerformed(ActionEvent e) {
DeckEditorBase g = this;
DeckImport dImport = new DeckImport(g);
dImport.setModalityType(ModalityType.APPLICATION_MODAL);
dImport.setVisible(true);
g.setEnabled(false);
}
}

View File

@@ -36,7 +36,7 @@ import javax.swing.SwingUtilities;
* @author Forge
* @version $Id$
*/
public final class DeckEditorMenu extends JMenuBar implements NewConstants {
public final class DeckEditorCommonMenu extends JMenuBar implements NewConstants {
/** Constant <code>serialVersionUID=-4037993759604768755L</code> */
private static final long serialVersionUID = -4037993759604768755L;
@@ -55,7 +55,7 @@ public final class DeckEditorMenu extends JMenuBar implements NewConstants {
public DeckEditorMenu(final DeckDisplay in_display, final DeckManager dckManager, final Command exit) {
public DeckEditorCommonMenu(final DeckDisplay in_display, final DeckManager dckManager, final Command exit) {
deckDisplay = in_display;
exitCommand = exit;
deckManager = dckManager;
@@ -92,7 +92,7 @@ public final class DeckEditorMenu extends JMenuBar implements NewConstants {
name.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent ev) {
((DeckEditor) deckDisplay).getTopTableModel().sort(1, true);
((DeckEditorCommon) deckDisplay).getTopTableModel().sort(1, true);
}
});
@@ -100,32 +100,32 @@ public final class DeckEditorMenu extends JMenuBar implements NewConstants {
//private String column[] = {"Qty", "Name", "Cost", "Color", "Type", "Stats", "Rarity"};
cost.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent ev) {
((DeckEditor) deckDisplay).getTopTableModel().sort(4).sort(3).sort(2);
((DeckEditorCommon) deckDisplay).getTopTableModel().sort(4).sort(3).sort(2);
}
});
color.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent ev) {
((DeckEditor) deckDisplay).getTopTableModel().sort(4).sort(2).sort(3);
((DeckEditorCommon) deckDisplay).getTopTableModel().sort(4).sort(2).sort(3);
}
});
type.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent ev) {
((DeckEditor) deckDisplay).getTopTableModel().sort(2).sort(3).sort(4);
((DeckEditorCommon) deckDisplay).getTopTableModel().sort(2).sort(3).sort(4);
}
});
stats.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent ev) {
((DeckEditor) deckDisplay).getTopTableModel().sort(4).sort(2).sort(3).sort(5);
((DeckEditorCommon) deckDisplay).getTopTableModel().sort(4).sort(2).sort(3).sort(5);
}
});
rarity.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent ev) {
//sort by cost, type, color, rarity
((DeckEditor) deckDisplay).getTopTableModel().sort(2).sort(4).sort(3).sort(6);
((DeckEditorCommon) deckDisplay).getTopTableModel().sort(2).sort(4).sort(3).sort(6);
}
});
} //setupSortMenu()

View File

@@ -5,19 +5,29 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.ElementIterator;
import net.miginfocom.swing.MigLayout;
import forge.Singletons;
import forge.deck.Deck;
import forge.deck.DeckRecognizer;
import forge.gui.GuiUtils;
/**
@@ -28,31 +38,25 @@ public class DeckImport extends JDialog {
private static final long serialVersionUID = -5837776824284093004L;
private JTextArea txtInput = new JTextArea();
private JEditorPane htmlOutput = new JEditorPane();
private JEditorPane htmlOutput = new JEditorPane("text/html", "<html><style>.rose { color:#ebaeba; }</style><h3 class=\"rose\">Expect result here</h3></html>");
private JScrollPane scrollInput = new JScrollPane(txtInput);
private JScrollPane scrollOutput = new JScrollPane(htmlOutput);
private JButton cmdAccept = new JButton("Import Deck");
private JButton cmdCancel = new JButton("Cancel");
List<DeckRecognizer.Token> tokens = new ArrayList<DeckRecognizer.Token>();
DeckEditorBase host;
public DeckImport(DeckEditorBase g) {
host = g;
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
host.setEnabled(true);
}
});
int wWidth = 800;
int wHeight = 600;
setPreferredSize(new java.awt.Dimension(wWidth, wHeight));
setSize(wWidth, wHeight);
GuiUtils.centerFrame(this);
setResizable(false);
setTitle("Deck Import (wip)");
@@ -63,8 +67,10 @@ public class DeckImport extends JDialog {
cmdCancel.setFont(fButtons);
txtInput.setFont(fButtons);
htmlOutput.setFont(fButtons);
//htmlOutput.setFont(fButtons);
}
htmlOutput.setEditable(false);
scrollInput.setBorder(new TitledBorder(BorderFactory.createEtchedBorder(), "Paste or type a decklist"));
scrollOutput.setBorder(new TitledBorder(BorderFactory.createEtchedBorder(), "Expect the recognized lines to appear"));
@@ -83,11 +89,71 @@ public class DeckImport extends JDialog {
cmdAccept.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
buildDeck();
JOptionPane.showMessageDialog(DeckImport.this, "This dialog still in development, don't expect any changes to deck yet.");
processWindowEvent(new WindowEvent(DeckImport.this, WindowEvent.WINDOW_CLOSING)); } });
txtInput.getDocument().addDocumentListener(new OnChangeTextUpdate());
}
private void readInput()
{
tokens.clear();
ElementIterator it = new ElementIterator(txtInput.getDocument().getDefaultRootElement());
Element e;
while ((e=it.next()) != null) {
if (!e.isLeaf()) { continue; }
int rangeStart = e.getStartOffset();
int rangeEnd = e.getEndOffset();
try {
String line = txtInput.getText(rangeStart, rangeEnd-rangeStart);
tokens.add(DeckRecognizer.recognizeLine(line));
} catch (BadLocationException ex) {
}
}
}
private static String stylesheet = "<style>" +
"body, h1, h2, h3, h4, h5, h6, table, tr, td, p {margin: 0; padding: 0; font-weight: normal; font-style: normal; text-decoration: none; font-family: Arial; font-size: 10px;} " +
//"h1 {border-bottom: solid 1px black; color: blue; font-size: 12px; margin: 3px 0 9px 0; } " +
".unknown {color: #660000;} " +
".comment {color: #006666;} " +
".KnownCardWithNumber {color: #009900;} " +
".UnknownCardWithNumber {color: #000099;} " +
".SectionName {font-weight: bold;} " +
"</style>";
private void displayTokens()
{
StringBuilder sbOut = new StringBuilder("<html>");
sbOut.append(stylesheet);
for (DeckRecognizer.Token t : tokens) {
sbOut.append(makeHtmlViewOfToken(t));
}
sbOut.append("</html>");
htmlOutput.setText(sbOut.toString());
}
private Deck buildDeck()
{
return new Deck();
}
protected class OnChangeTextUpdate implements DocumentListener {
private void onChange() { readInput(); displayTokens(); }
@Override public void insertUpdate(DocumentEvent e) { onChange(); }
@Override public void removeUpdate(DocumentEvent e) { onChange(); }
@Override public void changedUpdate(DocumentEvent e) { } // Happend only on ENTER pressed
}
private String makeHtmlViewOfToken(DeckRecognizer.Token token) {
switch(token.getType())
{
case Unknown:
return String.format("<div class='unknown'>%s</div>", token.getText());
default:
return String.format("<div class='%s'>%s</div>", token.getType(), token.getText());
}
}
}

View File

@@ -36,20 +36,20 @@ class FilterCheckBoxes {
public FilterCheckBoxes(final boolean useGraphicalBoxes) {
if (useGraphicalBoxes) {
white = new GuiFilterCheckBox("white", "White");
blue = new GuiFilterCheckBox("blue", "Blue");
black = new GuiFilterCheckBox("black", "Black");
red = new GuiFilterCheckBox("red", "Red");
green = new GuiFilterCheckBox("green", "Green");
colorless = new GuiFilterCheckBox("colorless", "Colorless");
white = new CheckBoxWithIcon("white", "White");
blue = new CheckBoxWithIcon("blue", "Blue");
black = new CheckBoxWithIcon("black", "Black");
red = new CheckBoxWithIcon("red", "Red");
green = new CheckBoxWithIcon("green", "Green");
colorless = new CheckBoxWithIcon("colorless", "Colorless");
land = new GuiFilterCheckBox("land", "Land");
creature = new GuiFilterCheckBox("creature", "Creature");
sorcery = new GuiFilterCheckBox("sorcery", "Sorcery");
instant = new GuiFilterCheckBox("instant", "Instant");
planeswalker = new GuiFilterCheckBox("planeswalker", "Planeswalker");
artifact = new GuiFilterCheckBox("artifact", "Artifact");
enchantment = new GuiFilterCheckBox("enchant", "Enchantment");
land = new CheckBoxWithIcon("land", "Land");
creature = new CheckBoxWithIcon("creature", "Creature");
sorcery = new CheckBoxWithIcon("sorcery", "Sorcery");
instant = new CheckBoxWithIcon("instant", "Instant");
planeswalker = new CheckBoxWithIcon("planeswalker", "Planeswalker");
artifact = new CheckBoxWithIcon("artifact", "Artifact");
enchantment = new CheckBoxWithIcon("enchant", "Enchantment");
} else {
white = new JCheckBox("W", true);
blue = new JCheckBox("U", true);

View File

@@ -15,7 +15,7 @@ import forge.game.limited.SealedDeck;
import forge.gui.GuiUtils;
import forge.gui.ListChooser;
import forge.gui.deckeditor.DeckEditorDraft;
import forge.gui.deckeditor.DeckEditor;
import forge.gui.deckeditor.DeckEditorCommon;
import forge.item.CardPrinted;
import forge.item.ItemPool;
import forge.properties.ForgePreferences;
@@ -635,7 +635,7 @@ public class OldGuiNewGame extends JFrame implements NewConstants, NewConstants.
*/
final void deckEditorButtonActionPerformed(final GameType gt, final Deck deck) {
DeckEditor editor = new DeckEditor(gt);
DeckEditorCommon editor = new DeckEditorCommon(gt);
Command exit = new Command() {
private static final long serialVersionUID = -9133358399503226853L;