[card.plugin] unified mouse adapter. now tooltips should work.

This commit is contained in:
magenoxx 2010-11-22 12:36:37 +00:00
parent fa99548105
commit b501b10f9a
13 changed files with 205 additions and 57 deletions

View file

@ -382,7 +382,7 @@ public class Card extends MagePermanent implements MouseMotionListener, MouseLis
List<UUID> targets = card.getTargets(); List<UUID> targets = card.getTargets();
if (targets != null) { if (targets != null) {
for (UUID uuid : targets) { for (UUID uuid : targets) {
System.out.println("Getting play area panel for uuid: " + uuid); //System.out.println("Getting play area panel for uuid: " + uuid);
PlayAreaPanel p = session.getGame().getPlayers().get(uuid); PlayAreaPanel p = session.getGame().getPlayers().get(uuid);
if (p != null) { if (p != null) {

View file

@ -58,7 +58,6 @@ import mage.view.CardsView;
public class Cards extends javax.swing.JPanel { public class Cards extends javax.swing.JPanel {
private Map<UUID, MageCard> cards = new HashMap<UUID, MageCard>(); private Map<UUID, MageCard> cards = new HashMap<UUID, MageCard>();
private boolean mouseHandlingEnabled = false;
/** Creates new form Cards */ /** Creates new form Cards */
public Cards() { public Cards() {
@ -90,16 +89,6 @@ public class Cards extends javax.swing.JPanel {
} }
} }
if (!mouseHandlingEnabled) {
synchronized (this) {
if (!mouseHandlingEnabled) {
mouseHandlingEnabled = true;
jScrollPane1.addMouseListener(new MageMouseAdapter(cardArea, gameId));
jScrollPane1.addMouseMotionListener(new MageMouseMotionAdapter(cardArea, bigCard));
}
}
}
cardArea.setPreferredSize(new Dimension(cards.size() * Config.dimensions.frameWidth, Config.dimensions.frameHeight)); cardArea.setPreferredSize(new Dimension(cards.size() * Config.dimensions.frameWidth, Config.dimensions.frameHeight));
cardArea.revalidate(); cardArea.revalidate();
cardArea.repaint(); cardArea.repaint();

View file

@ -89,8 +89,6 @@ public class BattlefieldPanel extends javax.swing.JLayeredPane implements Compon
public void init(UUID gameId, BigCard bigCard) { public void init(UUID gameId, BigCard bigCard) {
this.gameId = gameId; this.gameId = gameId;
this.bigCard = bigCard; this.bigCard = bigCard;
addMouseListener(new MageMouseAdapter(this, gameId));
addMouseMotionListener(new MageMouseMotionAdapter(this, bigCard));
} }
public void update(Map<UUID, PermanentView> battlefield) { public void update(Map<UUID, PermanentView> battlefield) {

View file

@ -0,0 +1,98 @@
package mage.client.plugins.adapters;
import java.awt.Component;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import org.jdesktop.swingx.JXPanel;
import mage.cards.MageCard;
import mage.cards.action.ActionCallback;
import mage.cards.action.TransferData;
import mage.client.MageFrame;
import mage.client.cards.BigCard;
import mage.client.plugins.impl.Plugins;
import mage.client.remote.Session;
import mage.client.util.DefaultActionCallback;
import mage.client.util.ImageHelper;
import mage.client.util.gui.GuiDisplayUtil;
public class MageActionCallback implements ActionCallback {
private Popup popup;
private Component parent;
private BigCard bigCard;
protected static DefaultActionCallback defaultCallback = DefaultActionCallback.getInstance();
protected static Session session = MageFrame.getSession();
public MageActionCallback() {
}
public void setCardPreviewComponent(BigCard bigCard) {
this.bigCard = bigCard;
}
public void refreshSession() {
if (session == null) {
session = MageFrame.getSession();
}
}
@Override
public void mouseClicked(MouseEvent e, TransferData data) {
data.component.requestFocusInWindow();
System.out.println("data="+data);
System.out.println("session="+session+",gameId="+data.gameId+",card="+data.card);
defaultCallback.mouseClicked(e, data.gameId, session, data.card);
}
@Override
public void mouseEntered(MouseEvent e, TransferData data) {
if (popup != null)
popup.hide();
PopupFactory factory = PopupFactory.getSharedInstance();
popup = factory.getPopup(data.component, data.popupText, (int) data.locationOnScreen.getX() + data.popupOffsetX, (int) data.locationOnScreen.getY() + data.popupOffsetY + 40);
popup.show();
//hack to get popup to resize to fit text
popup.hide();
popup = factory.getPopup(data.component, data.popupText, (int) data.locationOnScreen.getX() + data.popupOffsetX, (int) data.locationOnScreen.getY() + data.popupOffsetY + 40);
popup.show();
}
@Override
public void mouseMoved(MouseEvent e, TransferData data) {
if (!Plugins.getInstance().isCardPluginLoaded()) {return;}
if (bigCard == null) {return;}
MageCard card = (MageCard) data.component;
if (card.getOriginal().getId() != bigCard.getCardId()) {
synchronized (MageMouseAdapter.class) {
if (card.getOriginal().getId() != bigCard.getCardId()) {
Image image = card.getImage();
if (image != null && image instanceof BufferedImage) {
image = ImageHelper.getResizedImage((BufferedImage) image, bigCard.getWidth(), bigCard.getHeight());
bigCard.setCard(card.getOriginal().getId(), image, card.getOriginal().getRules());
bigCard.hideTextComponent();
} else {
JXPanel panel = GuiDisplayUtil.getDescription(card.getOriginal(), bigCard.getWidth(), bigCard.getHeight());
panel.setVisible(true);
bigCard.hideTextComponent();
bigCard.addJXPanel(card.getOriginal().getId(), panel);
}
}
}
}
}
@Override
public void mouseExited(MouseEvent e) {
if (popup != null) {
popup.hide();
}
}
}

View file

@ -7,7 +7,6 @@ import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import mage.cards.MageCard; import mage.cards.MageCard;
import mage.cards.MagePermanent;
import mage.client.cards.BigCard; import mage.client.cards.BigCard;
import mage.client.plugins.impl.Plugins; import mage.client.plugins.impl.Plugins;
import mage.client.util.ImageHelper; import mage.client.util.ImageHelper;

View file

@ -18,6 +18,7 @@ import mage.client.cards.BigCard;
import mage.client.cards.Card; import mage.client.cards.Card;
import mage.client.cards.Permanent; import mage.client.cards.Permanent;
import mage.client.plugins.MagePlugins; import mage.client.plugins.MagePlugins;
import mage.client.plugins.adapters.MageActionCallback;
import mage.client.util.Config; import mage.client.util.Config;
import mage.client.util.DefaultActionCallback; import mage.client.util.DefaultActionCallback;
import mage.constants.Constants; import mage.constants.Constants;
@ -34,14 +35,16 @@ import net.xeoh.plugins.base.impl.PluginManagerFactory;
public class Plugins implements MagePlugins { public class Plugins implements MagePlugins {
private static final MagePlugins fINSTANCE = new Plugins(); private final static MagePlugins fINSTANCE = new Plugins();
private static PluginManager pm;
private final static Logger logger = Logging.getLogger(Plugins.class.getName()); private final static Logger logger = Logging.getLogger(Plugins.class.getName());
private static PluginManager pm;
private ThemePlugin themePlugin = null; private ThemePlugin themePlugin = null;
private CardPlugin cardPlugin = null; private CardPlugin cardPlugin = null;
private CounterPlugin counterPlugin = null; private CounterPlugin counterPlugin = null;
protected static DefaultActionCallback defaultCallback = DefaultActionCallback.getInstance(); protected static DefaultActionCallback defaultCallback = DefaultActionCallback.getInstance();
private static final EmptyCallback emptyCallback = new EmptyCallback(); private static final EmptyCallback emptyCallback = new EmptyCallback();
private static final MageActionCallback mageActionCallback = new MageActionCallback();
public static MagePlugins getInstance() { public static MagePlugins getInstance() {
return fINSTANCE; return fINSTANCE;
@ -76,7 +79,9 @@ public class Plugins implements MagePlugins {
@Override @Override
public MagePermanent getMagePermanent(PermanentView card, BigCard bigCard, CardDimensions dimension, UUID gameId) { public MagePermanent getMagePermanent(PermanentView card, BigCard bigCard, CardDimensions dimension, UUID gameId) {
if (cardPlugin != null) { if (cardPlugin != null) {
return cardPlugin.getMagePermanent(card, dimension, gameId, emptyCallback); mageActionCallback.refreshSession();
mageActionCallback.setCardPreviewComponent(bigCard);
return cardPlugin.getMagePermanent(card, dimension, gameId, mageActionCallback);
} else { } else {
return new Permanent(card, bigCard, Config.dimensions, gameId); return new Permanent(card, bigCard, Config.dimensions, gameId);
} }
@ -85,7 +90,9 @@ public class Plugins implements MagePlugins {
@Override @Override
public MageCard getMageCard(CardView card, BigCard bigCard, CardDimensions dimension, UUID gameId) { public MageCard getMageCard(CardView card, BigCard bigCard, CardDimensions dimension, UUID gameId) {
if (cardPlugin != null) { if (cardPlugin != null) {
return cardPlugin.getMageCard(card, dimension, gameId, emptyCallback); mageActionCallback.refreshSession();
mageActionCallback.setCardPreviewComponent(bigCard);
return cardPlugin.getMageCard(card, dimension, gameId, mageActionCallback);
} else { } else {
return new Card(card, bigCard, Config.dimensions, gameId); return new Card(card, bigCard, Config.dimensions, gameId);
} }

View file

@ -18,7 +18,6 @@ public class DefaultActionCallback {
} }
public void mouseClicked(MouseEvent e, UUID gameId, Session session, CardView card) { public void mouseClicked(MouseEvent e, UUID gameId, Session session, CardView card) {
System.out.println("gameId:" + gameId);
if (gameId != null) if (gameId != null)
session.sendPlayerUUID(gameId, card.getId()); session.sendPlayerUUID(gameId, card.getId());
} }

View file

@ -3,6 +3,8 @@ package mage.cards.action;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
public interface ActionCallback { public interface ActionCallback {
void mouseClicked(MouseEvent e); void mouseClicked(MouseEvent e, TransferData data);
void mouseMoved(MouseEvent e); void mouseMoved(MouseEvent e, TransferData data);
void mouseEntered(MouseEvent e, TransferData data);
void mouseExited(MouseEvent e);
} }

View file

@ -0,0 +1,18 @@
package mage.cards.action;
import java.awt.Component;
import java.awt.Point;
import java.util.UUID;
import mage.cards.TextPopup;
import mage.view.CardView;
public class TransferData {
public Component component;
public TextPopup popupText;
public Point locationOnScreen;
public int popupOffsetX;
public int popupOffsetY;
public UUID gameId;
public CardView card;
}

View file

@ -3,6 +3,7 @@ package mage.cards.action.impl;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import mage.cards.action.ActionCallback; import mage.cards.action.ActionCallback;
import mage.cards.action.TransferData;
/** /**
* Callback that does nothing on any action * Callback that does nothing on any action
@ -12,11 +13,19 @@ import mage.cards.action.ActionCallback;
public class EmptyCallback implements ActionCallback { public class EmptyCallback implements ActionCallback {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseMoved(MouseEvent e, TransferData data) {
} }
@Override @Override
public void mouseMoved(MouseEvent e) { public void mouseEntered(MouseEvent e, TransferData data) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e, TransferData data) {
} }
} }

View file

@ -96,8 +96,6 @@ public class CardView implements Serializable {
this.rarity = card.getRarity(); this.rarity = card.getRarity();
this.expansionSetCode = card.getExpansionSetCode(); this.expansionSetCode = card.getExpansionSetCode();
} }
//TODO:remove me
//System.out.println("**** CARD NUMBER **** : " + card.getCardNumber() + " : " + card.getName() + " : " + card.getExpansionSetCode());
this.cardNumber = card.getCardNumber(); this.cardNumber = card.getCardNumber();
if (card instanceof Spell) { if (card instanceof Spell) {
@ -109,7 +107,6 @@ public class CardView implements Serializable {
for (UUID targetUUID : target.getTargets()) { for (UUID targetUUID : target.getTargets()) {
if (this.targets == null) this.targets = new ArrayList<UUID>(); if (this.targets == null) this.targets = new ArrayList<UUID>();
this.targets.add(targetUUID); this.targets.add(targetUUID);
System.out.println("Added target: " + targetUUID);
} }
} }
} }

View file

@ -12,20 +12,21 @@ import java.awt.Rectangle;
import java.awt.RenderingHints; import java.awt.RenderingHints;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.JRootPane; import javax.swing.JRootPane;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import mage.Constants.CardType; import mage.Constants.CardType;
import mage.cards.MagePermanent; import mage.cards.MagePermanent;
import mage.cards.TextPopup; import mage.cards.TextPopup;
import mage.cards.action.ActionCallback; import mage.cards.action.ActionCallback;
import mage.cards.action.TransferData;
import mage.utils.CardUtil; import mage.utils.CardUtil;
import mage.view.AbilityView; import mage.view.AbilityView;
import mage.view.CardView; import mage.view.CardView;
@ -39,7 +40,7 @@ import org.mage.plugins.card.images.ImageCache;
@SuppressWarnings({"unchecked","rawtypes"}) @SuppressWarnings({"unchecked","rawtypes"})
public class CardPanel extends MagePermanent implements MouseListener { public class CardPanel extends MagePermanent implements MouseListener, MouseMotionListener {
private static final long serialVersionUID = -3272134219262184410L; private static final long serialVersionUID = -3272134219262184410L;
private static final Logger log = Logger.getLogger(CardPanel.class); private static final Logger log = Logger.getLogger(CardPanel.class);
@ -82,15 +83,17 @@ public class CardPanel extends MagePermanent implements MouseListener {
private ActionCallback callback; private ActionCallback callback;
protected Popup popup;
protected boolean popupShowing; protected boolean popupShowing;
protected TextPopup popupText = new TextPopup(); protected TextPopup popupText = new TextPopup();
protected UUID gameId;
private TransferData data = new TransferData();
private boolean isPermanent; private boolean isPermanent;
public CardPanel(CardView newGameCard, boolean loadImage, ActionCallback callback) { public CardPanel(CardView newGameCard, UUID gameId, boolean loadImage, ActionCallback callback) {
this.gameCard = newGameCard; this.gameCard = newGameCard;
this.callback = callback; this.callback = callback;
this.gameId = gameId;
this.isPermanent = this.gameCard instanceof PermanentView; this.isPermanent = this.gameCard instanceof PermanentView;
if (isPermanent) { if (isPermanent) {
@ -103,8 +106,9 @@ public class CardPanel extends MagePermanent implements MouseListener {
setBackground(Color.black); setBackground(Color.black);
setOpaque(false); setOpaque(false);
//addMouseListener(this); addMouseListener(this);
addMouseMotionListener(this);
titleText = new GlowText(); titleText = new GlowText();
setText(gameCard); setText(gameCard);
titleText.setFont(getFont().deriveFont(Font.BOLD, 13f)); titleText.setFont(getFont().deriveFont(Font.BOLD, 13f));
@ -554,30 +558,44 @@ public class CardPanel extends MagePermanent implements MouseListener {
} }
@Override @Override
public void mouseClicked(MouseEvent e) {} public void mouseClicked(MouseEvent e) {
data.component = this;
@Override data.card = this.gameCard;
public void mouseEntered(MouseEvent arg0) { data.gameId = this.gameId;
if (!popupShowing) { callback.mouseClicked(e, data);
if (popup != null)
popup.hide();
PopupFactory factory = PopupFactory.getSharedInstance();
popup = factory.getPopup(this, popupText, (int) this.getLocationOnScreen().getX() + cardWidth + cardXOffset, (int) this.getLocationOnScreen().getY() + 40);
popup.show();
//hack to get popup to resize to fit text
popup.hide();
popup = factory.getPopup(this, popupText, (int) this.getLocationOnScreen().getX() + cardWidth + cardXOffset, (int) this.getLocationOnScreen().getY() + 40);
popup.show();
popupShowing = true;
}
} }
@Override @Override
public void mouseExited(MouseEvent arg0) { public void mouseEntered(MouseEvent e) {
if (!popupShowing) {
synchronized (this) {
if (!popupShowing) {
popupShowing = true;
callback.mouseEntered(e, getTransferDataForMouseEntered());
}
}
}
}
@Override
public void mouseDragged(MouseEvent e) {}
@Override
public void mouseMoved(MouseEvent e) {
data.component = this;
callback.mouseMoved(e, data);
}
@Override
public void mouseExited(MouseEvent e) {
if(getMousePosition(true) != null) return; if(getMousePosition(true) != null) return;
if (popup != null) { if (popupShowing) {
popup.hide(); synchronized (this) {
popupShowing = false; if (popupShowing) {
popupShowing = false;
callback.mouseExited(e);
}
}
} }
} }
@ -587,6 +605,20 @@ public class CardPanel extends MagePermanent implements MouseListener {
@Override @Override
public void mouseReleased(MouseEvent e) {} public void mouseReleased(MouseEvent e) {}
/**
* Prepares data to be sent to action callback on client side.
*
* @return
*/
private TransferData getTransferDataForMouseEntered() {
data.component = this;
data.popupText = popupText;
data.popupOffsetX = cardWidth + cardXOffset;
data.popupOffsetY = 40;
data.locationOnScreen = this.getLocationOnScreen();
return data;
}
protected String getType(CardView card) { protected String getType(CardView card) {
StringBuilder sbType = new StringBuilder(); StringBuilder sbType = new StringBuilder();

View file

@ -77,7 +77,7 @@ public class CardPluginImpl implements CardPlugin {
@Override @Override
public MagePermanent getMagePermanent(PermanentView permanent, CardDimensions dimension, UUID gameId, ActionCallback callback) { public MagePermanent getMagePermanent(PermanentView permanent, CardDimensions dimension, UUID gameId, ActionCallback callback) {
CardPanel cardPanel = new CardPanel(permanent, true, callback); CardPanel cardPanel = new CardPanel(permanent, gameId, true, callback);
cardPanel.setShowCastingCost(true); cardPanel.setShowCastingCost(true);
cardPanel.setCardBounds(0, 0, dimension.frameWidth, dimension.frameHeight); cardPanel.setCardBounds(0, 0, dimension.frameWidth, dimension.frameHeight);
return cardPanel; return cardPanel;
@ -85,7 +85,7 @@ public class CardPluginImpl implements CardPlugin {
@Override @Override
public MagePermanent getMageCard(CardView permanent, CardDimensions dimension, UUID gameId, ActionCallback callback) { public MagePermanent getMageCard(CardView permanent, CardDimensions dimension, UUID gameId, ActionCallback callback) {
CardPanel cardPanel = new CardPanel(permanent, true, callback); CardPanel cardPanel = new CardPanel(permanent, gameId, true, callback);
cardPanel.setShowCastingCost(true); cardPanel.setShowCastingCost(true);
cardPanel.setCardBounds(0, 0, dimension.frameWidth, dimension.frameHeight); cardPanel.setCardBounds(0, 0, dimension.frameWidth, dimension.frameHeight);
return cardPanel; return cardPanel;