Update and cleanup card viewing code. Fixes issue with

morph colour leaking, viewing opponent's morphs through
card zoomer, and other minor bugs.
This commit is contained in:
elcnesh
2015-05-18 07:33:21 +00:00
parent 5cba52b5a6
commit 9767b20386
14 changed files with 149 additions and 154 deletions

View File

@@ -190,7 +190,7 @@ public class BoxedProductCardListViewer extends FDialog {
if ((row >= 0) && (row < BoxedProductCardListViewer.this.list.size())) {
final PaperCard cp = BoxedProductCardListViewer.this.list.get(row);
BoxedProductCardListViewer.this.detail.setCard(CardView.getCardForUi(cp));
BoxedProductCardListViewer.this.picture.setCard(cp);
BoxedProductCardListViewer.this.picture.setItem(cp);
}
}
}

View File

@@ -34,7 +34,6 @@ import forge.card.CardDetailUtil;
import forge.card.CardDetailUtil.DetailColors;
import forge.card.CardEdition;
import forge.card.CardRarity;
import forge.card.CardStateName;
import forge.game.GameView;
import forge.game.card.Card;
import forge.game.card.CardView;
@@ -59,7 +58,7 @@ public class CardDetailPanel extends SkinnedPanel {
/** Constant <code>serialVersionUID=-8461473263764812323L</code>. */
private static final long serialVersionUID = -8461473263764812323L;
private static Color fromDetailColor(DetailColors detailColor) {
private static Color fromDetailColor(final DetailColors detailColor) {
return new Color(detailColor.r, detailColor.g, detailColor.b);
}
@@ -110,13 +109,13 @@ public class CardDetailPanel extends SkinnedPanel {
@Override
public void doLayout() {
int insets = 3;
int setInfoWidth = 40;
int x = insets;
final int insets = 3;
final int setInfoWidth = 40;
final int x = insets;
int y = insets;
int lineWidth = getWidth() - 2 * insets;
int lineHeight = nameCostLabel.getPreferredSize().height;
int dy = lineHeight + 1;
final int lineWidth = getWidth() - 2 * insets;
final int lineHeight = nameCostLabel.getPreferredSize().height;
final int dy = lineHeight + 1;
nameCostLabel.setBounds(x, y, lineWidth, lineHeight);
y += dy;
@@ -142,14 +141,14 @@ public class CardDetailPanel extends SkinnedPanel {
cdArea.setText(CardDetailUtil.getItemDescription(item));
updateBorder(item instanceof IPaperCard ? Card.getCardForUi((IPaperCard)item).getView().getCurrentState() : null, true);
String set = item.getEdition();
final String set = item.getEdition();
setInfoLabel.setText(set);
setInfoLabel.setToolTipText("");
if (StringUtils.isEmpty(set)) {
setInfoLabel.setOpaque(false);
setInfoLabel.setBorder(null);
} else {
CardEdition edition = FModel.getMagicDb().getEditions().get(set);
final CardEdition edition = FModel.getMagicDb().getEditions().get(set);
if (null != edition) {
setInfoLabel.setToolTipText(edition.getName());
}
@@ -169,115 +168,86 @@ public class CardDetailPanel extends SkinnedPanel {
}
public final void setCard(final CardView card) {
setCard(card, false);
setCard(card, true, false);
}
public final void setCard(final CardView card, final boolean isInAltState) {
nameCostLabel.setText("");
public final void setCard(final CardView card, final boolean mayView, final boolean isInAltState) {
typeLabel.setVisible(true);
typeLabel.setText("");
powerToughnessLabel.setVisible(true);
powerToughnessLabel.setText("");
idLabel.setText("");
setInfoLabel.setText("");
setInfoLabel.setToolTipText("");
setInfoLabel.setOpaque(false);
setInfoLabel.setBorder(null);
cdArea.setText("");
if (card == null) {
updateBorder(null, false);
return;
}
final CardStateView state = card.getState(isInAltState);
final CardStateView state = card == null ? null : card.getState(isInAltState);
if (state == null) {
nameCostLabel.setText("");
typeLabel.setText("");
powerToughnessLabel.setText("");
idLabel.setText("");
setInfoLabel.setText("");
setInfoLabel.setToolTipText("");
setInfoLabel.setOpaque(false);
setInfoLabel.setBorder(null);
cdArea.setText("");
updateBorder(null, false);
return;
}
boolean canShow = true;
if (state.getManaCost().isNoCost() || !canShow) {
nameCostLabel.setText(CardDetailUtil.formatCardName(card, canShow, isInAltState));
}
else {
final String name = CardDetailUtil.formatCardName(card, mayView, isInAltState), nameCost;
if (state.getManaCost().isNoCost() || !mayView) {
nameCost = name;
} else {
final String manaCost;
if (card.isSplitCard() && card.hasAlternateState() && card.getZone() != ZoneType.Stack) { //only display current state's mana cost when on stack
manaCost = card.getCurrentState().getManaCost() + " // " + card.getAlternateState().getManaCost();
}
else {
} else {
manaCost = state.getManaCost().toString();
}
nameCostLabel.setText(FSkin.encodeSymbols(CardDetailUtil.formatCardName(card, canShow, isInAltState) + " - " + manaCost, true));
nameCost = String.format("%s - %s", name, manaCost);
}
typeLabel.setText(CardDetailUtil.formatCardType(state, canShow));
nameCostLabel.setText(FSkin.encodeSymbols(nameCost, false));
typeLabel.setText(CardDetailUtil.formatCardType(state, mayView));
String set = state.getSetCode();
CardRarity rarity = state.getRarity();
if (!canShow) {
final String set;
final CardRarity rarity;
if (mayView) {
set = state.getSetCode();
rarity = state.getRarity();
} else {
set = CardEdition.UNKNOWN.getCode();
rarity = CardRarity.Unknown;
}
setInfoLabel.setText(set);
if (null != set && !set.isEmpty()) {
if (canShow) {
CardEdition edition = FModel.getMagicDb().getEditions().get(set);
if (mayView) {
final CardEdition edition = FModel.getMagicDb().getEditions().get(set);
final String setTooltip;
if (null == edition) {
setInfoLabel.setToolTipText(rarity.name());
}
else {
setInfoLabel.setToolTipText(String.format("%s (%s)", edition.getName(), rarity.name()));
setTooltip = rarity.name();
} else {
setTooltip = String.format("%s (%s)", edition.getName(), rarity.name());
}
setInfoLabel.setToolTipText(setTooltip);
}
setInfoLabel.setOpaque(true);
Color backColor;
switch (rarity) {
case Uncommon:
backColor = fromDetailColor(DetailColors.UNCOMMON);
break;
case Rare:
backColor = fromDetailColor(DetailColors.RARE);
break;
case MythicRare:
backColor = fromDetailColor(DetailColors.MYTHIC);
break;
case Special: //"Timeshifted" or other Special Rarity Cards
backColor = fromDetailColor(DetailColors.SPECIAL);
break;
default: //case BasicLand: + case Common:
backColor = fromDetailColor(DetailColors.COMMON);
break;
}
Color foreColor = FSkin.getHighContrastColor(backColor);
final Color backColor = fromDetailColor(CardDetailUtil.getRarityColor(rarity));
setInfoLabel.setBackground(backColor);
final Color foreColor = FSkin.getHighContrastColor(backColor);
setInfoLabel.setForeground(foreColor);
setInfoLabel.setBorder(BorderFactory.createLineBorder(foreColor));
}
if (state.getState() == CardStateName.FaceDown) {
updateBorder(state, false); // do not spoil the color of face-down cards
} else {
updateBorder(state, canShow);
}
updateBorder(state, mayView);
powerToughnessLabel.setText(CardDetailUtil.formatPowerToughness(state, canShow));
powerToughnessLabel.setText(CardDetailUtil.formatPowerToughness(state, mayView));
idLabel.setText(canShow ? CardDetailUtil.formatCardId(state) : "");
idLabel.setText(mayView ? CardDetailUtil.formatCardId(state) : "");
// fill the card text
cdArea.setText(FSkin.encodeSymbols(CardDetailUtil.composeCardText(state, gameView, canShow), true));
cdArea.setText(FSkin.encodeSymbols(CardDetailUtil.composeCardText(state, gameView, mayView), true));
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
@Override public void run() {
scrArea.getVerticalScrollBar().setValue(scrArea.getVerticalScrollBar().getMinimum());
}
});
@@ -316,7 +286,7 @@ public class CardDetailPanel extends SkinnedPanel {
return;
}
Color color = fromDetailColor(CardDetailUtil.getBorderColor(card, canShow));
final Color color = fromDetailColor(CardDetailUtil.getBorderColor(card, canShow));
setBorder(BorderFactory.createLineBorder(color, 2));
scrArea.setBorder(BorderFactory.createMatteBorder(2, 0, 0, 0, color));
}

View File

@@ -174,7 +174,7 @@ public class CardListChooser extends FDialog {
if ((row >= 0) && (row < CardListChooser.this.list.size())) {
final PaperCard cp = CardListChooser.this.list.get(row);
CardListChooser.this.detail.setCard(CardView.getCardForUi(cp));
CardListChooser.this.picture.setCard(cp);
CardListChooser.this.picture.setItem(cp);
}
}
}

View File

@@ -173,7 +173,7 @@ public class CardListViewer extends FDialog {
if ((row >= 0) && (row < CardListViewer.this.list.size())) {
final PaperCard cp = CardListViewer.this.list.get(row);
CardListViewer.this.detail.setCard(CardView.getCardForUi(cp));
CardListViewer.this.picture.setCard(cp);
CardListViewer.this.picture.setItem(cp);
}
}
}

View File

@@ -44,6 +44,7 @@ public final class CardPicturePanel extends JPanel {
private static final long serialVersionUID = -3160874016387273383L;
private Object displayed;
private boolean mayView = true;
private final FImagePanel panel;
private BufferedImage currentImage;
@@ -55,18 +56,22 @@ public final class CardPicturePanel extends JPanel {
this.add(this.panel);
}
public void setCard(final InventoryItem cp) {
this.displayed = cp;
this.setImage();
public void setItem(final InventoryItem item) {
setImage(item ,true);
}
public void setCard(final CardStateView c) {
this.displayed = c;
this.setImage();
setCard(c, true);
}
public void setCard(final CardStateView c, final boolean mayView) {
setImage(c, mayView);
}
public void setImage() {
BufferedImage image = getImage();
private void setImage(final Object display, final boolean mayView) {
this.displayed = display;
this.mayView = mayView;
final BufferedImage image = getImage();
if (image != null && image != this.currentImage) {
this.currentImage = image;
this.panel.setImage(image, getAutoSizeImageMode());
@@ -74,6 +79,10 @@ public final class CardPicturePanel extends JPanel {
}
private BufferedImage getImage() {
if (!mayView) {
return ImageCache.getOriginalImage(ImageKeys.getTokenKey(ImageKeys.HIDDEN_CARD), true);
}
if (displayed instanceof InventoryItem) {
final InventoryItem item = (InventoryItem) displayed;
return ImageCache.getOriginalImage(ImageKeys.getImageKey(item, false), true);

View File

@@ -256,7 +256,8 @@ public class ImageView<T extends InventoryItem> extends ItemView<T> {
if (item != null && item.item instanceof IPaperCard) {
setLockHoveredItem(true); //lock hoveredItem while zoomer open
final CardView card = CardView.getCardForUi((IPaperCard) item.item);
CardZoomer.SINGLETON_INSTANCE.doMouseButtonZoom(card);
CardZoomer.SINGLETON_INSTANCE.setCard(card.getCurrentState(), true);
CardZoomer.SINGLETON_INSTANCE.doMouseButtonZoom();
}
}

View File

@@ -99,12 +99,12 @@ public enum VSubmenuAchievements implements IVSubmenu<CSubmenuAchievements> {
}
private void showCard(MouseEvent e) {
Achievement achievement = getAchievementAt(e.getX(), e.getY());
final Achievement achievement = getAchievementAt(e.getX(), e.getY());
if (achievement != null) {
IPaperCard pc = achievement.getPaperCard();
final IPaperCard pc = achievement.getPaperCard();
if (pc != null) {
preventMouseOut = true;
CardZoomer.SINGLETON_INSTANCE.doMouseButtonZoom(CardView.getCardForUi(pc));
CardZoomer.SINGLETON_INSTANCE.setCard(CardView.getCardForUi(pc).getCurrentState(), true);
}
}
}

View File

@@ -113,7 +113,7 @@ public class QuestWinLoseCardViewer extends FPanel {
if ((row >= 0) && (row < QuestWinLoseCardViewer.this.list.size())) {
final PaperCard cp = QuestWinLoseCardViewer.this.list.get(row);
QuestWinLoseCardViewer.this.detail.setCard(CardView.getCardForUi(cp));
QuestWinLoseCardViewer.this.picture.setCard(cp);
QuestWinLoseCardViewer.this.picture.setItem(cp);
}
}
}

View File

@@ -58,7 +58,7 @@ public class CDetail implements ICDoc {
void showCard(final CardView c, final boolean isInAltState, final boolean mayView, final boolean mayFlip) {
final CardView toShow = mayView ? c : null;
view.getLblFlipcard().setVisible(toShow != null && mayFlip);
view.getPnlDetail().setCard(toShow, isInAltState);
view.getPnlDetail().setCard(toShow, mayView, isInAltState);
if (view.getParentCell() != null) {
view.getParentCell().repaintSelf();
}

View File

@@ -72,12 +72,13 @@ public class CPicture implements ICDoc {
void showCard(final CardView c, final boolean isInAltState, final boolean mayView, final boolean mayFlip) {
final CardStateView toShow = c != null && mayView ? c.getState(isInAltState) : null;
flipIndicator.setVisible(toShow != null && mayFlip);
picturePanel.setCard(toShow);
picturePanel.setCard(toShow, mayView);
zoomer.setCard(toShow, mayFlip);
}
void showItem(final InventoryItem item) {
flipIndicator.setVisible(false);
picturePanel.setCard(item);
picturePanel.setItem(item);
}
@Override
@@ -105,7 +106,7 @@ public class CPicture implements ICDoc {
@Override
public void onMiddleMouseDown(final MouseEvent e) {
if (isCardDisplayed()) {
zoomer.doMouseButtonZoom(controller.getCurrentCard());
zoomer.doMouseButtonZoom();
}
}
@@ -130,7 +131,7 @@ public class CPicture implements ICDoc {
public void mouseWheelMoved(final MouseWheelEvent arg0) {
if (isCardDisplayed()) {
if (arg0.getWheelRotation() < 0) {
zoomer.doMouseWheelZoom(controller.getCurrentCard());
zoomer.doMouseWheelZoom();
}
}
}

View File

@@ -196,9 +196,7 @@ public class VStack implements IVDoc<CStack> {
@Override
public void mouseEntered(final MouseEvent e) {
hoveredItem = StackInstanceTextArea.this;
if (!txt.startsWith("Morph ")) {
controller.getMatchUI().setCard(item.getSourceCard());
}
controller.getMatchUI().setCard(item.getSourceCard());
}
@Override

View File

@@ -33,7 +33,6 @@ import javax.swing.Timer;
import net.miginfocom.swing.MigLayout;
import forge.assets.FSkinProp;
import forge.game.card.CardView;
import forge.game.card.CardView.CardStateView;
import forge.gui.SOverlayUtils;
import forge.toolbox.FOverlay;
@@ -59,11 +58,11 @@ public enum CardZoomer {
private final JPanel overlay = FOverlay.SINGLETON_INSTANCE.getPanel();
private JPanel pnlMain;
private FImagePanel imagePanel;
private SkinnedLabel lblFlipcard = new SkinnedLabel();
private final SkinnedLabel lblFlipcard = new SkinnedLabel();
// Details about the current card being displayed.
private CardView thisCard;
private boolean isInAltState;
private CardStateView thisCard;
private boolean mayFlip, isInAltState;
// The zoomer is in button mode when it is activated by holding down the
// middle mouse button or left and right mouse buttons simultaneously.
@@ -83,6 +82,12 @@ public enum CardZoomer {
setKeyListeners();
}
public void setCard(final CardStateView card, final boolean mayFlip) {
this.thisCard = card;
this.mayFlip = mayFlip;
this.isInAltState = card == null ? false : card == card.getCard().getAlternateState();
}
/**
* Creates listener for keys that are recognised by zoomer.
* <p><ul>
@@ -91,8 +96,7 @@ public enum CardZoomer {
*/
private void setKeyListeners() {
overlay.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
@Override public void keyPressed(final KeyEvent e) {
if (!isButtonMode) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
closeZoomer();
@@ -113,8 +117,7 @@ public enum CardZoomer {
*/
private void setMouseButtonListener() {
overlay.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
@Override public void mouseReleased(final MouseEvent e) {
closeZoomer();
}
});
@@ -135,8 +138,7 @@ public enum CardZoomer {
*/
private void setMouseWheelListener() {
overlay.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
@Override public void mouseWheelMoved(final MouseWheelEvent e) {
if (!isButtonMode) {
if (isMouseWheelEnabled) {
isMouseWheelEnabled = false;
@@ -159,9 +161,9 @@ public enum CardZoomer {
* This method should be called if the zoomer is activated by rotating the
* mouse wheel.
*/
public void doMouseWheelZoom(final CardView card) {
public void doMouseWheelZoom() {
isButtonMode = false;
displayCard(card);
displayCard();
startMouseWheelCoolDownTimer(200);
}
@@ -172,7 +174,7 @@ public enum CardZoomer {
* This method should be called if the zoomer is activated by holding down
* the middle mouse button or left and right mouse buttons simultaneously.
*/
public void doMouseButtonZoom(final CardView newCard) {
public void doMouseButtonZoom() {
// don't display zoom if already zoomed or just closed zoom
// (handles mouse wheeling while middle clicking)
if (isOpen || System.currentTimeMillis() - lastClosedTime < 250) {
@@ -180,17 +182,15 @@ public enum CardZoomer {
}
isButtonMode = true;
displayCard(newCard);
displayCard();
}
public boolean isZoomerOpen() {
return isOpen;
}
private void displayCard(final CardView newCard) {
private void displayCard() {
isMouseWheelEnabled = false;
thisCard = newCard;
isInAltState = false;
setLayout();
setImage();
SOverlayUtils.showOverlay();
@@ -201,7 +201,7 @@ public enum CardZoomer {
* Displays a graphical indicator that shows whether the current card can be flipped or transformed.
*/
private void setFlipIndicator() {
if (thisCard != null && thisCard.hasAlternateState()) {
if (thisCard != null && mayFlip) {
imagePanel.setLayout(new MigLayout("insets 0, w 100%!, h 100%!"));
imagePanel.add(lblFlipcard, "pos (100% - 100px) 0");
}
@@ -213,8 +213,8 @@ public enum CardZoomer {
private void setImage() {
imagePanel = new FImagePanel();
final BufferedImage xlhqImage = FImageUtil.getImageXlhq(getState());
imagePanel.setImage(xlhqImage == null ? FImageUtil.getImage(getState()) : xlhqImage, getInitialRotation(), AutoSizeImageMode.SOURCE);
final BufferedImage xlhqImage = FImageUtil.getImageXlhq(thisCard);
imagePanel.setImage(xlhqImage == null ? FImageUtil.getImage(thisCard) : xlhqImage, getInitialRotation(), AutoSizeImageMode.SOURCE);
pnlMain.removeAll();
pnlMain.add(imagePanel, "w 80%!, h 80%!");
@@ -223,8 +223,10 @@ public enum CardZoomer {
}
private int getInitialRotation() {
return thisCard == null ? 0 :
(thisCard.isSplitCard() || thisCard.getCurrentState().getType().isPlane() || thisCard.getCurrentState().getType().isPhenomenon() ? 90 : 0);
if (thisCard == null) {
return 0;
}
return thisCard.getCard().isSplitCard() || thisCard.getType().isPlane() || thisCard.getType().isPhenomenon() ? 90 : 0;
}
private void setLayout() {
@@ -249,7 +251,7 @@ public enum CardZoomer {
* wheel for a short period of time after opening. This will
* prevent flip and double side cards from immediately flipping.
*/
private void startMouseWheelCoolDownTimer(int millisecsDelay) {
private void startMouseWheelCoolDownTimer(final int millisecsDelay) {
isMouseWheelEnabled = false;
createMouseWheelCoolDownTimer(millisecsDelay);
mouseWheelCoolDownTimer.setInitialDelay(millisecsDelay);
@@ -259,11 +261,10 @@ public enum CardZoomer {
/**
* Used to ignore mouse wheel rotation for {@code millisecsDelay} milliseconds.
*/
private void createMouseWheelCoolDownTimer(int millisecsDelay) {
private void createMouseWheelCoolDownTimer(final int millisecsDelay) {
if (mouseWheelCoolDownTimer == null) {
mouseWheelCoolDownTimer = new Timer(millisecsDelay, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@Override public void actionPerformed(final ActionEvent e) {
isMouseWheelEnabled = true;
}
});
@@ -280,7 +281,7 @@ public enum CardZoomer {
* Toggles between primary and alternate image associated with card if applicable.
*/
private void toggleCardImage() {
if (thisCard != null && thisCard.hasAlternateState()) {
if (thisCard != null && mayFlip) {
toggleFlipCard();
}
}
@@ -293,11 +294,9 @@ public enum CardZoomer {
*/
private void toggleFlipCard() {
isInAltState = !isInAltState;
imagePanel.setRotation(thisCard.isFlipCard() && isInAltState ? 180 : 0);
thisCard = thisCard.getCard().getState(isInAltState);
imagePanel.setRotation(thisCard.getCard().isFlipCard() && isInAltState ? 180 : 0);
setImage();
}
private CardStateView getState() {
return thisCard.getState(isInAltState);
}
}

View File

@@ -77,12 +77,14 @@ public abstract class CardPanelContainer extends SkinnedPanel {
private void mouseWheelZoom(final CardView card) {
if (canZoom(card)) {
CardZoomer.SINGLETON_INSTANCE.doMouseWheelZoom(card);
CardZoomer.SINGLETON_INSTANCE.setCard(card.getCurrentState(), false);
CardZoomer.SINGLETON_INSTANCE.doMouseWheelZoom();
}
}
private void mouseButtonZoom(final CardView card) {
if (canZoom(card)) {
CardZoomer.SINGLETON_INSTANCE.doMouseButtonZoom(card);
CardZoomer.SINGLETON_INSTANCE.setCard(card.getCurrentState(), false);
CardZoomer.SINGLETON_INSTANCE.doMouseButtonZoom();
}
}
private boolean canZoom(final CardView card) {

View File

@@ -153,6 +153,21 @@ public class CardDetailUtil {
return borderColors;
}
public static DetailColors getRarityColor(final CardRarity rarity) {
switch (rarity) {
case Uncommon:
return DetailColors.UNCOMMON;
case Rare:
return DetailColors.RARE;
case MythicRare:
return DetailColors.MYTHIC;
case Special: //"Timeshifted" or other Special Rarity Cards
return DetailColors.SPECIAL;
default: //case BasicLand: + case Common:
return DetailColors.COMMON;
}
}
public static String getItemDescription(final InventoryItemFromSet item) {
if (item instanceof SealedProduct) {
return ((SealedProduct)item).getDescription();