diff --git a/Mage.Client/src/main/java/mage/client/cards/Card.java b/Mage.Client/src/main/java/mage/client/cards/Card.java index 25d95cf757c..a10b2700586 100644 --- a/Mage.Client/src/main/java/mage/client/cards/Card.java +++ b/Mage.Client/src/main/java/mage/client/cards/Card.java @@ -479,4 +479,9 @@ public class Card extends MagePermanent implements MouseMotionListener, MouseLis public Image getImage() { return image; } + + @Override + public PermanentView getOriginalPermanent() { + return null; + } } diff --git a/Mage.Client/src/main/java/mage/client/cards/Cards.java b/Mage.Client/src/main/java/mage/client/cards/Cards.java index b63d7bec554..1d6ae6130ee 100644 --- a/Mage.Client/src/main/java/mage/client/cards/Cards.java +++ b/Mage.Client/src/main/java/mage/client/cards/Cards.java @@ -34,14 +34,23 @@ package mage.client.cards; +import java.awt.Color; import java.awt.Component; import java.awt.Dimension; +import java.awt.event.MouseListener; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.UUID; +import javax.swing.BorderFactory; + +import mage.cards.CardDimensions; +import mage.cards.MageCard; +import mage.client.plugins.adapters.MageMouseAdapter; +import mage.client.plugins.adapters.MageMouseMotionAdapter; +import mage.client.plugins.impl.Plugins; import mage.client.util.Config; import mage.view.CardView; import mage.view.CardsView; @@ -52,8 +61,9 @@ import mage.view.CardsView; */ public class Cards extends javax.swing.JPanel { - private Map cards = new HashMap(); - + private Map cards = new HashMap(); + private boolean mouseHandlingEnabled = false; + /** Creates new form Cards */ public Cards() { initComponents(); @@ -61,45 +71,86 @@ public class Cards extends javax.swing.JPanel { jScrollPane1.setOpaque(false); jScrollPane1.getViewport().setOpaque(false); cardArea.setOpaque(false); + if (Plugins.getInstance().isCardPluginLoaded()) { + cardArea.setLayout(null); + } } public boolean loadCards(CardsView cardsView, BigCard bigCard, UUID gameId) { boolean changed = false; for (CardView card: cardsView.values()) { if (!cards.containsKey(card.getId())) { - Card cardImg = new Card(card, bigCard, Config.dimensions, gameId); - cards.put(card.getId(), cardImg); - cardArea.add(cardImg); + addCard(card, bigCard, gameId); changed = true; } cards.get(card.getId()).update(card); } - for (Iterator> i = cards.entrySet().iterator(); i.hasNext();) { - Entry entry = i.next(); + for (Iterator> i = cards.entrySet().iterator(); i.hasNext();) { + Entry entry = i.next(); if (!cardsView.containsKey(entry.getKey())) { removeCard(entry.getKey()); i.remove(); changed = true; } } + + if (!mouseHandlingEnabled) { + synchronized (this) { + if (!mouseHandlingEnabled) { + mouseHandlingEnabled = true; + //cardArea.addMouseListener(new MageMouseAdapter(this, gameId)); + //cardArea.addMouseMotionListener(new MageMouseMotionAdapter(this, bigCard)); + jScrollPane1.addMouseListener(new MageMouseAdapter(cardArea, gameId)); + jScrollPane1.addMouseMotionListener(new MageMouseMotionAdapter(cardArea, bigCard)); + //addMouseListener(new MageMouseAdapter(this, gameId)); + } + } + } cardArea.setPreferredSize(new Dimension(cards.size() * Config.dimensions.frameWidth, Config.dimensions.frameHeight)); cardArea.revalidate(); cardArea.repaint(); this.revalidate(); this.repaint(); + if (changed) { + layoutCards(Config.dimensions); + } return changed; } + private void addCard(CardView card, BigCard bigCard, UUID gameId) { + MageCard cardImg = Plugins.getInstance().getMageCard(card, bigCard, Config.dimensions, gameId); + cards.put(card.getId(), cardImg); + cardArea.add(cardImg); + /*if (Plugins.getInstance().isCardPluginLoaded()) { + cardImg.setBorder(BorderFactory.createLineBorder(Color.red)); + }*/ + } + private void removeCard(UUID cardId) { for (Component comp: cardArea.getComponents()) { if (comp instanceof Card) { if (((Card)comp).getCardId().equals(cardId)) { cardArea.remove(comp); } + } else if (comp instanceof MageCard) { + if (((MageCard)comp).getOriginal().equals(cardId)) { + cardArea.remove(comp); + } } } } + + private void layoutCards(CardDimensions dimension) { + if (Plugins.getInstance().isCardPluginLoaded()) { + int dx = 0; + for (MageCard card: cards.values()) { + card.setLocation(dx, 0); + card.setCardBounds(dx, 0, dimension.frameWidth, dimension.frameHeight); + dx += dimension.frameWidth + 5; + } + } + } /** This method is called from within the constructor to diff --git a/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java b/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java index d3a00e16c90..36d094e4792 100644 --- a/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java +++ b/Mage.Client/src/main/java/mage/client/game/BattlefieldPanel.java @@ -120,7 +120,7 @@ public class BattlefieldPanel extends javax.swing.JLayeredPane implements Compon } private void addPermanent(PermanentView permanent) { - MagePermanent perm = Plugins.getInstance().getMagePermanent(permanent, bigCard, Config.dimensions, gameId);; + MagePermanent perm = Plugins.getInstance().getMagePermanent(permanent, bigCard, Config.dimensions, gameId); perm.addComponentListener(this); if (!Plugins.getInstance().isCardPluginLoaded()) { perm.setBounds(findEmptySpace(new Dimension(Config.dimensions.frameWidth, Config.dimensions.frameHeight))); diff --git a/Mage.Client/src/main/java/mage/client/plugins/MagePlugins.java b/Mage.Client/src/main/java/mage/client/plugins/MagePlugins.java index a07e2f30e89..9207495a7b2 100644 --- a/Mage.Client/src/main/java/mage/client/plugins/MagePlugins.java +++ b/Mage.Client/src/main/java/mage/client/plugins/MagePlugins.java @@ -9,8 +9,10 @@ import javax.swing.JComponent; import mage.cards.Card; import mage.cards.CardDimensions; +import mage.cards.MageCard; import mage.cards.MagePermanent; import mage.client.cards.BigCard; +import mage.view.CardView; import mage.view.PermanentView; public interface MagePlugins { @@ -19,6 +21,7 @@ public interface MagePlugins { void updateGamePanel(Map ui); JComponent updateTablePanel(Map ui); MagePermanent getMagePermanent(PermanentView card, BigCard bigCard, CardDimensions dimension, UUID gameId); + MageCard getMageCard(CardView card, BigCard bigCard, CardDimensions dimension, UUID gameId); boolean isThemePluginLoaded(); boolean isCardPluginLoaded(); boolean isCounterPluginLoaded(); diff --git a/Mage.Client/src/main/java/mage/client/plugins/adapters/MageMouseAdapter.java b/Mage.Client/src/main/java/mage/client/plugins/adapters/MageMouseAdapter.java index c8355dea419..6efe3aea921 100644 --- a/Mage.Client/src/main/java/mage/client/plugins/adapters/MageMouseAdapter.java +++ b/Mage.Client/src/main/java/mage/client/plugins/adapters/MageMouseAdapter.java @@ -1,23 +1,14 @@ package mage.client.plugins.adapters; -import java.awt.Color; import java.awt.Component; -import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; -import java.util.List; import java.util.UUID; -import javax.swing.Popup; -import javax.swing.PopupFactory; - -import mage.cards.MagePermanent; +import mage.cards.MageCard; import mage.client.MageFrame; -import mage.client.game.PlayAreaPanel; import mage.client.plugins.impl.Plugins; -import mage.client.util.Config; import mage.client.util.DefaultActionCallback; -import mage.client.util.gui.ArrowBuilder; public class MageMouseAdapter extends MouseAdapter { @@ -38,8 +29,8 @@ public class MageMouseAdapter extends MouseAdapter { int count = e.getClickCount(); if (count > 0) { Object o = parent.getComponentAt(e.getPoint()); - if (o instanceof MagePermanent) { - MagePermanent selectedCard = (MagePermanent) o; + if (o instanceof MageCard) { + MageCard selectedCard = (MageCard) o; // TODO: uncomment when attached cards works in plugin /* * int x = e.getX() - selectedCard.getX(); int y = e.getY() diff --git a/Mage.Client/src/main/java/mage/client/plugins/adapters/MageMouseMotionAdapter.java b/Mage.Client/src/main/java/mage/client/plugins/adapters/MageMouseMotionAdapter.java index d4a104bd289..e67a7e88b38 100644 --- a/Mage.Client/src/main/java/mage/client/plugins/adapters/MageMouseMotionAdapter.java +++ b/Mage.Client/src/main/java/mage/client/plugins/adapters/MageMouseMotionAdapter.java @@ -6,6 +6,7 @@ import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.image.BufferedImage; +import mage.cards.MageCard; import mage.cards.MagePermanent; import mage.client.cards.BigCard; import mage.client.plugins.impl.Plugins; @@ -29,8 +30,8 @@ public class MageMouseMotionAdapter extends MouseMotionAdapter { if (!Plugins.getInstance().isCardPluginLoaded()) {return;} if (bigCard == null) {return;} Object o = parent.getComponentAt(e.getPoint()); - if (o instanceof MagePermanent) { - MagePermanent card = (MagePermanent) o; + if (o instanceof MageCard) { + MageCard card = (MageCard) o; if (card.getOriginal().getId() != bigCard.getCardId()) { synchronized (MageMouseAdapter.class) { if (card.getOriginal().getId() != bigCard.getCardId()) { diff --git a/Mage.Client/src/main/java/mage/client/plugins/impl/Plugins.java b/Mage.Client/src/main/java/mage/client/plugins/impl/Plugins.java index c3bfa509ecd..64b639d8193 100644 --- a/Mage.Client/src/main/java/mage/client/plugins/impl/Plugins.java +++ b/Mage.Client/src/main/java/mage/client/plugins/impl/Plugins.java @@ -10,11 +10,12 @@ import java.util.logging.Logger; import javax.swing.JComponent; -import mage.cards.Card; import mage.cards.CardDimensions; +import mage.cards.MageCard; import mage.cards.MagePermanent; import mage.cards.action.impl.EmptyCallback; import mage.client.cards.BigCard; +import mage.client.cards.Card; import mage.client.cards.Permanent; import mage.client.plugins.MagePlugins; import mage.client.util.Config; @@ -25,10 +26,10 @@ import mage.interfaces.plugin.CardPlugin; import mage.interfaces.plugin.CounterPlugin; import mage.interfaces.plugin.ThemePlugin; import mage.util.Logging; +import mage.view.CardView; import mage.view.PermanentView; import net.xeoh.plugins.base.PluginManager; import net.xeoh.plugins.base.impl.PluginManagerFactory; -import net.xeoh.plugins.base.util.PluginManagerUtil; public class Plugins implements MagePlugins { @@ -73,7 +74,7 @@ public class Plugins implements MagePlugins { } @Override - public MagePermanent getMagePermanent(final PermanentView card, BigCard bigCard, CardDimensions dimension, final UUID gameId) { + public MagePermanent getMagePermanent(PermanentView card, BigCard bigCard, CardDimensions dimension, UUID gameId) { if (cardPlugin != null) { return cardPlugin.getMagePermanent(card, dimension, gameId, emptyCallback); } else { @@ -81,6 +82,15 @@ public class Plugins implements MagePlugins { } } + @Override + public MageCard getMageCard(CardView card, BigCard bigCard, CardDimensions dimension, UUID gameId) { + if (cardPlugin != null) { + return cardPlugin.getMageCard(card, dimension, gameId, emptyCallback); + } else { + return new Card(card, bigCard, Config.dimensions, gameId); + } + } + @Override public boolean isCardPluginLoaded() { return this.cardPlugin != null; @@ -92,7 +102,7 @@ public class Plugins implements MagePlugins { } @Override - public void downloadImage(Set allCards) { + public void downloadImage(Set allCards) { if (this.cardPlugin != null) this.cardPlugin.downloadImages(allCards); } diff --git a/Mage.Common/src/mage/cards/MageCard.java b/Mage.Common/src/mage/cards/MageCard.java new file mode 100644 index 00000000000..e538a1bbec9 --- /dev/null +++ b/Mage.Common/src/mage/cards/MageCard.java @@ -0,0 +1,21 @@ +package mage.cards; + +import java.awt.Image; + +import javax.swing.JPanel; + +import mage.view.CardView; +import mage.view.PermanentView; + +public abstract class MageCard extends JPanel { + private static final long serialVersionUID = 6089945326434301879L; + + abstract public void onBeginAnimation(); + abstract public void onEndAnimation(); + abstract public boolean isTapped(); + abstract public void setAlpha(float transparency); + abstract public CardView getOriginal(); + abstract public void setCardBounds(int x, int y, int width, int height); + abstract public void update(CardView card); + abstract public Image getImage(); +} diff --git a/Mage.Common/src/mage/cards/MagePermanent.java b/Mage.Common/src/mage/cards/MagePermanent.java index 74fe2fd446d..9b5c0508e5f 100644 --- a/Mage.Common/src/mage/cards/MagePermanent.java +++ b/Mage.Common/src/mage/cards/MagePermanent.java @@ -1,22 +1,12 @@ package mage.cards; -import java.awt.Image; import java.util.List; -import javax.swing.JPanel; - import mage.view.PermanentView; -public abstract class MagePermanent extends JPanel { +public abstract class MagePermanent extends MageCard { private static final long serialVersionUID = -3469258620601702171L; abstract public List getLinks(); - - abstract public void onBeginAnimation(); - abstract public void onEndAnimation(); - abstract public boolean isTapped(); - abstract public void setAlpha(float transparency); - abstract public PermanentView getOriginal(); - abstract public void setCardBounds(int x, int y, int width, int height); abstract public void update(PermanentView card); - abstract public Image getImage(); + abstract public PermanentView getOriginalPermanent(); } diff --git a/Mage.Common/src/mage/interfaces/plugin/CardPlugin.java b/Mage.Common/src/mage/interfaces/plugin/CardPlugin.java index 91f2f110ae6..e181f9065ac 100644 --- a/Mage.Common/src/mage/interfaces/plugin/CardPlugin.java +++ b/Mage.Common/src/mage/interfaces/plugin/CardPlugin.java @@ -11,17 +11,21 @@ import mage.cards.Card; import mage.cards.CardDimensions; import mage.cards.MagePermanent; import mage.cards.action.ActionCallback; +import mage.view.CardView; import mage.view.PermanentView; import net.xeoh.plugins.base.Plugin; /** * Interface for card plugins * - * @version 0.1 31.10.2010 + * @version 0.3 21.11.2010 #getMageCard + * @version 0.2 07.11.2010 #downloadImages + * @version 0.1 31.10.2010 #getMagePermanent, #sortPermanents * @author nantuko */ public interface CardPlugin extends Plugin { MagePermanent getMagePermanent(PermanentView permanent, CardDimensions dimension, UUID gameId, ActionCallback callback); + MagePermanent getMageCard(CardView permanent, CardDimensions dimension, UUID gameId, ActionCallback callback); void sortPermanents(Map ui, Collection cards); void downloadImages(Set allCards); } diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/card/arcane/Animation.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/card/arcane/Animation.java index 9f80fab8e38..f39de00dcc0 100644 --- a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/card/arcane/Animation.java +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/card/arcane/Animation.java @@ -110,12 +110,12 @@ abstract public class Animation { protected void update (float percentage) { panel.tappedAngle = CardPanel.TAPPED_ANGLE * percentage; - if (!panel.gameCard.isTapped()) panel.tappedAngle = CardPanel.TAPPED_ANGLE - panel.tappedAngle; + if (!panel.isTapped()) panel.tappedAngle = CardPanel.TAPPED_ANGLE - panel.tappedAngle; panel.repaint(); } protected void end () { - panel.tappedAngle = panel.gameCard.isTapped() ? CardPanel.TAPPED_ANGLE : 0; + panel.tappedAngle = panel.isTapped() ? CardPanel.TAPPED_ANGLE : 0; parent.onEndAnimation(); parent.repaint(); } diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/card/arcane/CardPanel.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/card/arcane/CardPanel.java index 0e2346a8731..a28f125ff81 100644 --- a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/card/arcane/CardPanel.java +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/card/arcane/CardPanel.java @@ -37,8 +37,6 @@ import org.mage.card.arcane.ScaledImagePanel.MultipassType; import org.mage.card.arcane.ScaledImagePanel.ScalingType; import org.mage.plugins.card.images.ImageCache; -import com.google.common.collect.Sets; - @SuppressWarnings({"unchecked","rawtypes"}) public class CardPanel extends MagePermanent implements MouseListener { @@ -62,7 +60,8 @@ public class CardPanel extends MagePermanent implements MouseListener { static private final float rotCenterToTopCorner = 1.0295630140987000315797369464196f; static private final float rotCenterToBottomCorner = 0.7071067811865475244008443621048f; - public PermanentView gameCard; + public CardView gameCard; + public PermanentView gamePermanent; public CardPanel attachedToPanel; public List attachedPanels = new ArrayList(); public double tappedAngle = 0; @@ -86,10 +85,17 @@ public class CardPanel extends MagePermanent implements MouseListener { protected Popup popup; protected boolean popupShowing; protected TextPopup popupText = new TextPopup(); + + private boolean isPermanent; - public CardPanel (PermanentView newGameCard, boolean loadImage, ActionCallback callback) { + public CardPanel(CardView newGameCard, boolean loadImage, ActionCallback callback) { this.gameCard = newGameCard; this.callback = callback; + this.isPermanent = this.gameCard instanceof PermanentView; + + if (isPermanent) { + this.gamePermanent = (PermanentView) this.gameCard; + } //for container debug (don't remove) //setBorder(BorderFactory.createLineBorder(Color.green)); @@ -111,7 +117,7 @@ public class CardPanel extends MagePermanent implements MouseListener { if (CardUtil.isCreature(gameCard)) { ptText.setText(gameCard.getPower() + "/" + gameCard.getToughness()); } else if (CardUtil.isPlaneswalker(gameCard)) { - ptText.setText(gameCard.getOriginal().getLoyalty()); + ptText.setText(gameCard.getLoyalty()); } ptText.setFont(getFont().deriveFont(Font.BOLD, 13f)); ptText.setForeground(Color.white); @@ -146,7 +152,7 @@ public class CardPanel extends MagePermanent implements MouseListener { Util.threadPool.submit(new Runnable() { public void run () { try { - tappedAngle = gameCard.isTapped() ? CardPanel.TAPPED_ANGLE : 0; + tappedAngle = isTapped() ? CardPanel.TAPPED_ANGLE : 0; BufferedImage srcImage = ImageCache.getImageOriginal(gameCard); srcImage = ImageCache.getNormalSizeImage(srcImage); if (srcImage != null) { @@ -165,7 +171,7 @@ public class CardPanel extends MagePermanent implements MouseListener { }); } - private void setText(PermanentView card) { + private void setText(CardView card) { if (hasImage) { titleText.setText(""); } else { @@ -400,7 +406,7 @@ public class CardPanel extends MagePermanent implements MouseListener { return p; } - public PermanentView getCard() { + public CardView getCard() { return this.gameCard; } @@ -426,13 +432,13 @@ public class CardPanel extends MagePermanent implements MouseListener { Util.threadPool.submit(new Runnable() { public void run () { //TODO: BufferedImage srcImage = ImageCache.getImageOriginal(gameCard); - BufferedImage srcImage = null; - tappedAngle = gameCard.isTapped() ? CardPanel.TAPPED_ANGLE : 0; - if (srcImage != null) { + //BufferedImage srcImage = null; + //tappedAngle = isTapped() ? CardPanel.TAPPED_ANGLE : 0; + /*if (srcImage != null) { hasImage = true; setText(gameCard); setImage(srcImage, srcImage); - } + }*/ } }); } @@ -450,7 +456,10 @@ public class CardPanel extends MagePermanent implements MouseListener { @Override public boolean isTapped() { - return gameCard.isTapped(); + if (isPermanent) { + return ((PermanentView)gameCard).isTapped(); + } + return false; } @Override @@ -462,9 +471,11 @@ public class CardPanel extends MagePermanent implements MouseListener { } @Override - public void update(PermanentView card) { - if (this.gameCard.isTapped() != card.isTapped()) { - Animation.tapCardToggle(this, this); + public void update(CardView card) { + if (isPermanent) { + if (isTapped() != ((PermanentView)card).isTapped()) { + Animation.tapCardToggle(this, this); + } } if (CardUtil.isCreature(card) && CardUtil.isPlaneswalker(card)) { ptText.setText(card.getPower() + "/" + card.getToughness() + " (" + card.getLoyalty() + ")"); @@ -533,7 +544,7 @@ public class CardPanel extends MagePermanent implements MouseListener { } @Override - public PermanentView getOriginal() { + public CardView getOriginal() { return this.gameCard; } @@ -632,4 +643,14 @@ public class CardPanel extends MagePermanent implements MouseListener { // sb.append("\n").append(card.getId()); return sb.toString(); } + + @Override + public void update(PermanentView card) { + update((CardView)card); + } + + @Override + public PermanentView getOriginalPermanent() { + throw new IllegalStateException("Is not permanent."); + } } diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/CardPluginImpl.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/CardPluginImpl.java index a4c5a4a6084..96112ea03a5 100644 --- a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/CardPluginImpl.java +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/CardPluginImpl.java @@ -18,6 +18,7 @@ import mage.cards.MagePermanent; import mage.cards.action.ActionCallback; import mage.interfaces.plugin.CardPlugin; import mage.utils.CardUtil; +import mage.view.CardView; import mage.view.PermanentView; import net.xeoh.plugins.base.annotations.PluginImplementation; import net.xeoh.plugins.base.annotations.events.Init; @@ -32,7 +33,9 @@ import org.mage.plugins.card.images.DownloadPictures; /** * {@link CardPlugin} implementation. * - * @version 0.1 01.11.2010 + * @version 0.3 07.11.2010 Mage cards. + * @version 0.2 07.11.2010 Downloading images. + * @version 0.1 01.11.2010 Mage permanents. Sorting card layout. * @author nantuko */ @PluginImplementation @@ -69,16 +72,22 @@ public class CardPluginImpl implements CardPlugin { } public String toString() { - return "[Card plugin, version 0.2]"; + return "[Card plugin, version 0.3]"; } @Override public MagePermanent getMagePermanent(PermanentView permanent, CardDimensions dimension, UUID gameId, ActionCallback callback) { - //log.debug("Card plugin: building mage permanent [w="+dimension.frameWidth+",h="+dimension.frameHeight+"]"); CardPanel cardPanel = new CardPanel(permanent, true, callback); cardPanel.setShowCastingCost(true); cardPanel.setCardBounds(0, 0, dimension.frameWidth, dimension.frameHeight); - //cardPanel.setBorder(BorderFactory.createLineBorder(Color.red)); + return cardPanel; + } + + @Override + public MagePermanent getMageCard(CardView permanent, CardDimensions dimension, UUID gameId, ActionCallback callback) { + CardPanel cardPanel = new CardPanel(permanent, true, callback); + cardPanel.setShowCastingCost(true); + cardPanel.setCardBounds(0, 0, dimension.frameWidth, dimension.frameHeight); return cardPanel; }