[card.plugin] Added card info panel. At the moment can be seen only in deck editor with -DtestCardInfo VM parameter.

This commit is contained in:
magenoxx 2010-12-14 19:33:30 +03:00
parent 53a26bb3b5
commit 49e44b92b1
12 changed files with 289 additions and 37 deletions

View file

@ -137,7 +137,7 @@ public class MageFrame extends javax.swing.JFrame {
initComponents();
setSize(1024,768);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
//this.setExtendedState(JFrame.MAXIMIZED_BOTH);
session = new Session(this);
connectDialog = new ConnectDialog();

View file

@ -51,6 +51,7 @@ import java.util.UUID;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import mage.client.components.CardInfoPane;
import mage.client.plugins.impl.Plugins;
import org.jdesktop.swingx.JXPanel;
@ -66,6 +67,8 @@ public class BigCard extends javax.swing.JPanel {
protected JXPanel panel;
protected boolean initState;
protected CardInfoPane cardInfoPane;
public BigCard() {
initComponents();
if (!Plugins.getInstance().isCardPluginLoaded()) {

View file

@ -34,7 +34,20 @@
package mage.client.deckeditor;
import java.awt.Cursor;
import mage.cards.Card;
import mage.cards.decks.Deck;
import mage.cards.decks.DeckCardLists;
import mage.client.MageFrame;
import mage.client.plugins.impl.Plugins;
import mage.client.util.Event;
import mage.client.util.Listener;
import mage.components.CardInfoPane;
import mage.game.GameException;
import mage.view.CardsView;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
@ -42,19 +55,6 @@ import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import mage.cards.Card;
import mage.cards.decks.Deck;
import mage.cards.decks.DeckCardLists;
import mage.client.MageFrame;
import mage.client.util.Event;
import mage.client.util.Listener;
import mage.game.GameException;
import mage.view.CardsView;
/**
*
* @author BetaSteward_at_googlemail.com
@ -64,6 +64,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
private JFileChooser fcSelectDeck;
private JFileChooser fcImportDeck;
private Deck deck = new Deck();
private boolean isShowCardInfo = false;
/** Creates new form DeckEditorPanel */
public DeckEditorPanel() {
@ -96,6 +97,9 @@ public class DeckEditorPanel extends javax.swing.JPanel {
if (System.getProperty("draft") != null) {
cardSelector.getCardsList().removeCard(card.getId());
}
if (cardInfoPane instanceof CardInfoPane) {
((CardInfoPane)cardInfoPane).setCard(card);
}
break;
}
}
@ -150,13 +154,6 @@ public class DeckEditorPanel extends javax.swing.JPanel {
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jSplitPane1 = new javax.swing.JSplitPane();
@ -179,6 +176,16 @@ public class DeckEditorPanel extends javax.swing.JPanel {
bigCard.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
cardInfoPane = Plugins.getInstance().getCardInfoPane();
if (cardInfoPane != null && System.getProperty("testCardInfo") != null) {
cardInfoPane.setPreferredSize(new Dimension(170,230));
cardInfoPane.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
isShowCardInfo = true;
} else {
cardInfoPane = new JLabel();
cardInfoPane.setVisible(false);
}
lblDeckName.setLabelFor(txtDeckName);
lblDeckName.setText("Deck Name:");
@ -225,10 +232,11 @@ public class DeckEditorPanel extends javax.swing.JPanel {
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(lblDeckName)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtDeckName, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE))
.addGap(6, 6, 6)
.addComponent(lblDeckName)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtDeckName, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE))
.addComponent(cardInfoPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bigCard, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
@ -249,17 +257,18 @@ public class DeckEditorPanel extends javax.swing.JPanel {
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtDeckName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblDeckName))
.addComponent(txtDeckName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblDeckName))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnSave)
.addComponent(btnLoad)
.addComponent(btnNew)
.addComponent(btnExit))
.addComponent(btnSave)
.addComponent(btnLoad)
.addComponent(btnNew)
.addComponent(btnExit))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnImport)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 159, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, isShowCardInfo ? 30 : 159, Short.MAX_VALUE)
.addComponent(cardInfoPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bigCard, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
@ -277,7 +286,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 615, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
}
private void btnLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadActionPerformed
String lastFolder = MageFrame.getPreferences().get("lastDeckFolder", "");
@ -396,6 +405,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
private javax.swing.JTextField txtDeckName;
// End of variables declaration//GEN-END:variables
private JComponent cardInfoPane;
}
class DeckFilter extends FileFilter {

View file

@ -34,4 +34,5 @@ public interface MagePlugins {
Image getManaSymbolImage(String symbol);
void onAddCard(MagePermanent card);
void onRemoveCard(MagePermanent card);
JComponent getCardInfoPane();
}

View file

@ -181,4 +181,14 @@ public class Plugins implements MagePlugins {
this.cardPlugin.onRemoveCard(card);
}
}
@Override
public JComponent getCardInfoPane() {
if (this.cardPlugin != null) {
return this.cardPlugin.getCardInfoPane();
}
return null;
}
}

View file

@ -0,0 +1,14 @@
package mage.components;
import mage.cards.Card;
/**
* Card info pane for displaying card rules.
* Supports drawing mana symbols.
*
* @author nantuko
*/
public interface CardInfoPane {
public void setCard (final Card card);
public boolean isCurrentCard (Card card);
}

View file

@ -33,4 +33,5 @@ public interface CardPlugin extends Plugin {
Image getManaSymbolImage(String symbol);
void onAddCard(MagePermanent card);
void onRemoveCard(MagePermanent card);
JComponent getCardInfoPane();
}

View file

@ -84,10 +84,10 @@ public class ManaSymbols {
static public synchronized String replaceSymbolsWithHTML (String value, boolean small) {
if (small)
return replaceSymbolsPattern.matcher(value).replaceAll("<img src='file:images/symbols-11/$1$2.png' width=11 height=11>");
return replaceSymbolsPattern.matcher(value).replaceAll("<img src='file:plugins/images/symbols/small/$1$2.jpg' width=11 height=11>");
else {
value = value.replace("{slash}", "<img src='file:images/symbols-13/slash.png' width=10 height=13>");
return replaceSymbolsPattern.matcher(value).replaceAll("<img src='file:images/symbols-13/$1$2.png' width=13 height=13>");
value = value.replace("{slash}", "<img src='file:plugins/images/symbols/medium/slash.jpg' width=10 height=13>");
return replaceSymbolsPattern.matcher(value).replaceAll("<img src='file:plugins/images/symbols/medium/$1$2.jpg' width=13 height=13>");
}
}
}

View file

@ -0,0 +1,44 @@
package org.mage.card.arcane;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Util method to work with threads.
*
* @author ayrat
*/
public class ThreadUtils {
static public ThreadPoolExecutor threadPool;
static private int threadCount;
static {
threadPool = new ThreadPoolExecutor(4, 4, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new ThreadFactory() {
public Thread newThread (Runnable runnable) {
threadCount++;
Thread thread = new Thread(runnable, "Util" + threadCount);
thread.setDaemon(true);
return thread;
}
});
threadPool.prestartAllCoreThreads();
}
static public void sleep (int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException ignored) {
}
}
static public void wait (Object lock) {
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException ex) {
}
}
}
}

View file

@ -22,6 +22,7 @@ import mage.cards.Card;
import mage.cards.CardDimensions;
import mage.cards.MagePermanent;
import mage.cards.action.ActionCallback;
import mage.components.CardInfoPane;
import mage.interfaces.plugin.CardPlugin;
import mage.utils.CardUtil;
import mage.view.CardView;
@ -41,6 +42,7 @@ import org.mage.plugins.card.dl.DownloadJob;
import org.mage.plugins.card.dl.Downloader;
import org.mage.plugins.card.dl.sources.GathererSymbols;
import org.mage.plugins.card.images.DownloadPictures;
import org.mage.plugins.card.info.CardInfoPaneImpl;
/**
* {@link CardPlugin} implementation.
@ -462,4 +464,9 @@ public class CardPluginImpl implements CardPlugin {
}
}
}
@Override
public JComponent getCardInfoPane() {
return new CardInfoPaneImpl();
}
}

View file

@ -0,0 +1,162 @@
package org.mage.plugins.card.info;
import mage.Constants;
import mage.cards.Card;
import mage.components.CardInfoPane;
import mage.utils.CardUtil;
import org.mage.card.arcane.ManaSymbols;
import org.mage.card.arcane.ThreadUtils;
import org.mage.card.arcane.UI;
import javax.swing.*;
import java.awt.*;
import java.util.List;
/**
* Card info pane for displaying card rules.
* Supports drawing mana symbols.
*
* @author nantuko
*/
public class CardInfoPaneImpl extends JEditorPane implements CardInfoPane {
private Card currentCard;
public CardInfoPaneImpl() {
UI.setHTMLEditorKit(this);
setEditable(false);
setBackground(Color.white);
}
public void setCard (final Card card) {
if (card == null) return;
if (isCurrentCard(card)) return;
currentCard = card;
ThreadUtils.threadPool.submit(new Runnable() {
public void run () {
if (!card.equals(currentCard)) return;
String castingCost = UI.getDisplayManaCost(card.getManaCost().getText());
castingCost = ManaSymbols.replaceSymbolsWithHTML(castingCost, false);
int symbolCount = 0;
int offset = 0;
while ((offset = castingCost.indexOf("<img", offset) + 1) != 0)
symbolCount++;
List<String> rulings = card.getRules();
boolean smallImages = true;
int fontSize = 11;
String fontFamily = "tahoma";
/*if (prefs.fontFamily == CardFontFamily.arial)
fontFamily = "arial";
else if (prefs.fontFamily == CardFontFamily.verdana) {
fontFamily = "verdana";
}*/
final StringBuffer buffer = new StringBuffer(512);
buffer.append("<html><body style='font-family:");
buffer.append(fontFamily);
buffer.append(";font-size:");
buffer.append(fontSize);
buffer.append("pt;margin:0px 1px 0px 1px'>");
buffer.append("<table cellspacing=0 cellpadding=0 border=0 width='100%'>");
buffer.append("<tr><td valign='top'><b>");
buffer.append(card.getName());
buffer.append("</b></td><td align='right' valign='top' style='width:");
buffer.append(symbolCount * 11 + 1);
buffer.append("px'>");
buffer.append(castingCost);
buffer.append("</td></tr></table>");
buffer.append("<table cellspacing=0 cellpadding=0 border=0 width='100%'><tr><td>");
buffer.append(getTypes(card));
buffer.append("</td><td align='right'>");
switch (card.getRarity()) {
case RARE:
buffer.append("<b color='#E1D519'>");
break;
case UNCOMMON:
buffer.append("<b color='silver'>");
break;
case COMMON:
buffer.append("<b color='black'>");
break;
case MYTHIC:
buffer.append("<b color='#D5330B'>");
break;
}
buffer.append(card.getExpansionSetCode().toUpperCase());
buffer.append("</td></tr></table>");
String legal = "";
if (rulings.size() > 0) {
legal = legal.replaceAll("#([^#]+)#", "<i>$1</i>");
legal = legal.replaceAll("\\s*//\\s*", "<hr width='50%'>");
legal = legal.replace("\r\n", "<div style='font-size:5pt'></div>");
legal += "<br>";
for (String ruling : rulings) {
legal += ruling;
legal += "<br><br>";
}
}
if (legal.length() > 0) {
buffer.append("<br>");
legal = legal.replaceAll("\\{this\\}", card.getName());
legal = legal.replaceAll("\\{source\\}", card.getName());
buffer.append(ManaSymbols.replaceSymbolsWithHTML(legal, smallImages));
}
String pt = "";
if (card.getCardType().contains(Constants.CardType.CREATURE)) {
pt = card.getPower() + "/" + card.getToughness();
} else if (card.getCardType().contains(Constants.CardType.PLANESWALKER)) {
pt = card.getLoyalty().toString();
}
if (pt.length() > 0) {
buffer.append("<table cellspacing=0 cellpadding=0 border=0 width='100%'><tr><td>");
buffer.append("</td><td align='right'>");
buffer.append("<b>");
buffer.append(pt);
buffer.append("</b>");
buffer.append("</td></tr></table>");
}
buffer.append("<br></body></html>");
SwingUtilities.invokeLater(new Runnable() {
public void run () {
if (!card.equals(currentCard)) return;
setText(buffer.toString());
System.out.println(buffer.toString());
setCaretPosition(0);
}
});
}
});
}
private String getTypes(Card card) {
String types = "";
for (String superType : card.getSupertype()) {
types += superType + " ";
}
for (Constants.CardType cardType : card.getCardType()) {
types += cardType.toString() + " ";
}
if (card.getSubtype().size() > 0) {
types += "- ";
}
for (String subType : card.getSubtype()) {
types += subType + " ";
}
return types.trim();
}
public boolean isCurrentCard (Card card) {
return currentCard != null && card.equals(currentCard);
}
}