From 241691b153fd2eda3aaf2087c5cf4a3d4d0e8196 Mon Sep 17 00:00:00 2001 From: spjspj Date: Sat, 29 Apr 2017 17:25:54 +1000 Subject: [PATCH] spjspj - Add in Tokens to the Card Viewer (first version) --- .../viewer/CollectionViewerPanel.java | 11 ++ .../collection/viewer/MageBook.java | 124 +++++++++++++++++- .../src/main/java/mage/view/CardView.java | 10 +- .../main/java/mage/view/PermanentView.java | 6 +- .../mage/game/permanent/PermanentImpl.java | 4 +- 5 files changed, 142 insertions(+), 13 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/CollectionViewerPanel.java b/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/CollectionViewerPanel.java index 447db6bc4b..518de4d4e8 100644 --- a/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/CollectionViewerPanel.java +++ b/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/CollectionViewerPanel.java @@ -142,6 +142,17 @@ public final class CollectionViewerPanel extends JPanel { next.addActionListener(e -> mageBook.next()); buttonPanel.add(next); + JLabel label4 = new JLabel("Show cards or tokens:"); + label3.setAlignmentX(Component.LEFT_ALIGNMENT); + label3.setForeground(Color.white); + jPanel1.add(label4); + + JCheckBox cardsOrTokens = new JCheckBox("Display Cards"); + cardsOrTokens.setSelected(true); + cardsOrTokens.setToolTipText("Select to show Cards or Tokens(and emblems) for the chosen set"); + cardsOrTokens.addActionListener(e -> mageBook.cardsOrTokens(cardsOrTokens.isSelected())); + jPanel1.add(cardsOrTokens); + formats.addActionListener(e -> { if (mageBook != null) { String format = (String)formats.getSelectedItem(); diff --git a/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/MageBook.java b/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/MageBook.java index bf9fdb1ecf..f724d2d0d9 100644 --- a/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/MageBook.java +++ b/Mage.Client/src/main/java/mage/client/deckeditor/collection/viewer/MageBook.java @@ -57,9 +57,17 @@ import java.awt.*; import java.awt.image.BufferedImage; import java.io.FileNotFoundException; import java.io.InputStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; import java.util.UUID; +import java.util.logging.Level; +import mage.game.permanent.PermanentToken; +import mage.game.permanent.token.Token; +import mage.view.PermanentView; +import org.mage.plugins.card.images.CardDownloadData; +import static org.mage.plugins.card.images.DownloadPictures.getTokenCardUrls; /** * Mage book with cards and page flipping. @@ -113,7 +121,7 @@ public class MageBook extends JComponent { } pageRight.setVisible(true); AudioManager.playPrevPage(); - showCards(); + showCardsOrTokens(); }); image = ImageHelper.loadImage(RIGHT_PAGE_BUTTON_IMAGE_PATH); @@ -125,7 +133,7 @@ public class MageBook extends JComponent { pageLeft.setVisible(true); pageRight.setVisible(false); AudioManager.playNextPage(); - showCards(); + showCardsOrTokens(); }); addSetTabs(); @@ -170,7 +178,7 @@ public class MageBook extends JComponent { final String _set = set; final int _index = count; tab.setObserver(() -> { - if (!currentSet.equals(_set) || currentPage != 0) { + if (!currentSet.equals(_set) || currentPage != 0 || stateChanged) { AudioManager.playAnotherTab(); synchronized (MageBook.this) { selectedTab = _index; @@ -180,7 +188,7 @@ public class MageBook extends JComponent { pageLeft.setVisible(false); pageRight.setVisible(false); addSetTabs(); - showCards(); + showCardsOrTokens(); } }); tabs.add(tab); @@ -203,6 +211,15 @@ public class MageBook extends JComponent { } } + private void showCardsOrTokens() { + stateChanged = false; + if (showCardsOrTokens) { + showCards(); + } else { + showTokens(); + } + } + public void showCards() { jLayeredPane.removeAll(); addLeftRightPageButtons(); @@ -232,6 +249,37 @@ public class MageBook extends JComponent { jLayeredPane.repaint(); } + public void showTokens() { + jLayeredPane.removeAll(); + addLeftRightPageButtons(); + + List tokens = getTokens(currentPage, currentSet); + int size = tokens.size(); + + if (tokens != null && tokens.size() > 0) { + Rectangle rectangle = new Rectangle(); + rectangle.translate(OFFSET_X, OFFSET_Y); + for (int i = 0; i < Math.min(conf.CARDS_PER_PAGE / 2, size); i++) { + Token token = tokens.get(i); + addToken(token, bigCard, null, rectangle); + rectangle = CardPosition.translatePosition(i, rectangle, conf); + } + + // calculate the x offset of the second (right) page + int second_page_x = (conf.WIDTH - 2 * LEFT_RIGHT_PAGES_WIDTH) + - (cardDimensions.frameWidth + CardPosition.GAP_X) * conf.CARD_COLUMNS + CardPosition.GAP_X - OFFSET_X; + + rectangle.setLocation(second_page_x, OFFSET_Y); + for (int i = conf.CARDS_PER_PAGE / 2; i < Math.min(conf.CARDS_PER_PAGE, size); i++) { + Token token = tokens.get(i); + addToken(token, bigCard, null, rectangle); + rectangle = CardPosition.translatePosition(i - conf.CARDS_PER_PAGE / 2, rectangle, conf); + } + + jLayeredPane.repaint(); + } + } + private void addCard(CardView card, BigCard bigCard, UUID gameId, Rectangle rectangle) { if (cardDimension == null) { cardDimension = new Dimension(Config.dimensions.frameWidth, Config.dimensions.frameHeight); @@ -252,6 +300,19 @@ public class MageBook extends JComponent { jLayeredPane.add(label); } + private void addToken(Token token, BigCard bigCard, UUID gameId, Rectangle rectangle) { + if (cardDimension == null) { + cardDimension = new Dimension(Config.dimensions.frameWidth, Config.dimensions.frameHeight); + } + PermanentToken newToken = new PermanentToken(token, null, token.getOriginalExpansionSetCode(), null); + PermanentView theToken = new PermanentView(newToken, null, null, null); + final MageCard cardImg = Plugins.instance.getMagePermanent(theToken, bigCard, cardDimension, gameId, true); + cardImg.setBounds(rectangle); + jLayeredPane.add(cardImg, JLayeredPane.DEFAULT_LAYER, 10); + cardImg.update(theToken); + cardImg.setCardBounds(rectangle.x, rectangle.y, cardDimensions.frameWidth, cardDimensions.frameHeight); + } + private List getCards(int page, String set) { CardCriteria criteria = new CardCriteria(); criteria.setCodes(set); @@ -268,6 +329,50 @@ public class MageBook extends JComponent { return cards.subList(start, end); } + private List getTokens(int page, String set) { + ArrayList allTokens = getTokenCardUrls(); + ArrayList tokens = new ArrayList<>(); + + for (CardDownloadData token : allTokens) { + if (token.getSet().equals(set)) { + try { + String className = token.getName(); + className = className.replaceAll("[^a-zA-Z0-9]", ""); + className = className + "Token"; + Class c = Class.forName("mage.game.permanent.token." + className); + Constructor cons = c.getConstructor(); + Object newToken = cons.newInstance(); + if (newToken != null && newToken instanceof mage.game.permanent.token.Token) { + tokens.add((Token) newToken); + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(MageBook.class.getName()).log(Level.SEVERE, null, ex); + } catch (NoSuchMethodException ex) { + java.util.logging.Logger.getLogger(MageBook.class.getName()).log(Level.SEVERE, null, ex); + } catch (SecurityException ex) { + java.util.logging.Logger.getLogger(MageBook.class.getName()).log(Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(MageBook.class.getName()).log(Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(MageBook.class.getName()).log(Level.SEVERE, null, ex); + } catch (IllegalArgumentException ex) { + java.util.logging.Logger.getLogger(MageBook.class.getName()).log(Level.SEVERE, null, ex); + } catch (InvocationTargetException ex) { + java.util.logging.Logger.getLogger(MageBook.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + int start = page * conf.CARDS_PER_PAGE; + int end = page * conf.CARDS_PER_PAGE + conf.CARDS_PER_PAGE; + if (end > tokens.size()) { + end = tokens.size(); + } + if (tokens.size() > end) { + pageRight.setVisible(true); + } + return tokens.subList(start, end); + } + private ImagePanel getImagePanel(String filename, ImagePanelStyle type) { try { InputStream is = this.getClass().getResourceAsStream(filename); @@ -320,6 +425,15 @@ public class MageBook extends JComponent { } } + public void cardsOrTokens(boolean showCards) { + synchronized (this) { + selectedTab = 0; + showCardsOrTokens = !showCardsOrTokens; + stateChanged = true; + tabs.get(selectedTab).execute(); + } + } + public void updateSize(String size) { switch (size) { case LAYOUT_3x3: @@ -415,6 +529,8 @@ public class MageBook extends JComponent { private java.util.List setsToDisplay = new ArrayList<>(); private final java.util.List tabs = new ArrayList<>(); private int selectedTab; + private boolean showCardsOrTokens = true; + private boolean stateChanged = false; private static final String CENTER_PANEL_IMAGE_PATH = "/book_bg.jpg"; private static final String RIGHT_PANEL_IMAGE_PATH = "/book_right.jpg"; diff --git a/Mage.Common/src/main/java/mage/view/CardView.java b/Mage.Common/src/main/java/mage/view/CardView.java index f59aecdecf..3218f2729b 100644 --- a/Mage.Common/src/main/java/mage/view/CardView.java +++ b/Mage.Common/src/main/java/mage/view/CardView.java @@ -346,12 +346,12 @@ public class CardView extends SimpleCardView { if (card instanceof Permanent) { this.mageObjectType = MageObjectType.PERMANENT; Permanent permanent = (Permanent) card; - this.loyalty = Integer.toString(permanent.getCounters(game).getCount(CounterType.LOYALTY)); - this.pairedCard = permanent.getPairedCard() != null ? permanent.getPairedCard().getSourceId() : null; - if (!permanent.getControllerId().equals(permanent.getOwnerId())) { - controlledByOwner = false; - } if (game != null && permanent.getCounters(game) != null && !permanent.getCounters(game).isEmpty()) { + this.loyalty = Integer.toString(permanent.getCounters(game).getCount(CounterType.LOYALTY)); + this.pairedCard = permanent.getPairedCard() != null ? permanent.getPairedCard().getSourceId() : null; + if (!permanent.getControllerId().equals(permanent.getOwnerId())) { + controlledByOwner = false; + } counters = new ArrayList<>(); for (Counter counter : permanent.getCounters(game).values()) { counters.add(new CounterView(counter)); diff --git a/Mage.Common/src/main/java/mage/view/PermanentView.java b/Mage.Common/src/main/java/mage/view/PermanentView.java index 003af1f693..5dee91191f 100644 --- a/Mage.Common/src/main/java/mage/view/PermanentView.java +++ b/Mage.Common/src/main/java/mage/view/PermanentView.java @@ -62,8 +62,8 @@ public class PermanentView extends CardView { private final boolean attachedToPermanent; public PermanentView(Permanent permanent, Card card, UUID createdForPlayerId, Game game) { - super(permanent, game, permanent.getControllerId().equals(createdForPlayerId)); - this.controlled = permanent.getControllerId().equals(createdForPlayerId); + super(permanent, game, (permanent.getControllerId() == null) ? false : permanent.getControllerId().equals(createdForPlayerId)); + this.controlled = (permanent.getControllerId() == null) ? false : permanent.getControllerId().equals(createdForPlayerId); this.rules = permanent.getRules(game); this.tapped = permanent.isTapped(); this.flipped = permanent.isFlipped(); @@ -106,7 +106,7 @@ public class PermanentView extends CardView { } } } - if (!permanent.getOwnerId().equals(permanent.getControllerId())) { + if (permanent.getOwnerId() != null && !permanent.getOwnerId().equals(permanent.getControllerId())) { Player owner = game.getPlayer(permanent.getOwnerId()); if (owner != null) { this.nameOwner = owner.getName(); diff --git a/Mage/src/main/java/mage/game/permanent/PermanentImpl.java b/Mage/src/main/java/mage/game/permanent/PermanentImpl.java index e296f0648f..c0659276c3 100644 --- a/Mage/src/main/java/mage/game/permanent/PermanentImpl.java +++ b/Mage/src/main/java/mage/game/permanent/PermanentImpl.java @@ -256,7 +256,9 @@ public abstract class PermanentImpl extends CardImpl implements Permanent { Ability copyAbility = ability.copy(); copyAbility.setControllerId(controllerId); copyAbility.setSourceId(objectId); - game.getState().addAbility(copyAbility, this); + if (game != null) { + game.getState().addAbility(copyAbility, this); + } abilities.add(copyAbility); } }