Can now click on opponent deck in deck games list to view deck.

master
lodici 2017-02-24 23:35:22 +00:00
parent a836f89666
commit b1807aefa5
7 changed files with 173 additions and 15 deletions

View File

@ -249,10 +249,14 @@ public final class ScreenController {
: DeckEditorScreen::new);
}
public static void showDeckViewScreen(MagicDeck deck, MagicCardDefinition selectedCard) {
public static void showDeckScreen(MagicDeck deck, MagicCardDefinition selectedCard) {
showScreen(() -> new DeckScreen(deck, selectedCard));
}
public static void showDeckScreen(MagicDeck deck) {
showScreen(() -> new DeckScreen(deck));
}
public static void showSampleHandScreen(final MagicDeck deck) {
showScreen(() -> new SampleHandScreen(deck));
}
@ -336,4 +340,8 @@ public final class ScreenController {
public static void showPlayerScreen(String guid) {
showScreen(() -> new PlayerScreen(guid));
}
public static boolean isDeckScreenShowing() {
return !screens.isEmpty() && screens.peek() instanceof DeckScreen;
}
}

View File

@ -1,26 +1,66 @@
package magic.ui.deck.games;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import magic.data.DeckType;
import magic.ui.utility.MagicStyle;
import net.miginfocom.swing.MigLayout;
@SuppressWarnings("serial")
class DeckCellRenderer extends DefaultTableCellRenderer {
private static final int BORDER_WIDTH = 2;
private static final BasicStroke BORDER_STROKE = new BasicStroke(BORDER_WIDTH);
private static final Color BORDER_COLOR = MagicStyle.getRolloverColor();
private static final MigLayout layout = new MigLayout("flowy, gap 2, insets 2 2 2 2", "[grow, fill]");
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
final DeckInfo info = (DeckInfo) value;
JLabel lbl = new JLabel(info.deckName);
DeckColorLabel cp = new DeckColorLabel(info.deckColor);
JLabel deckLabel = new JLabel(info.deckName);
DeckColorLabel colorLabel = new DeckColorLabel(info.deckColor);
final JPanel panel = new JPanel(new MigLayout("flowy, gap 2, insets 2 2 2 2", "[grow, fill]"));
panel.add(lbl);
panel.add(cp);
final JPanel panel = new JPanel(layout) {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (showCellHighlight(table, row, col, info)) {
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(BORDER_COLOR);
g2d.setStroke(BORDER_STROKE);
g.drawRect(1, 1, getWidth()-3, getHeight()-3);
}
}
};
panel.add(deckLabel);
panel.add(colorLabel);
return panel;
}
private boolean showCellHighlight(JTable table, int row, int col, DeckInfo info) {
return isEnabled()
&& info.deckType != DeckType.Random
&& isMouseOverCell(table, row, col);
}
private boolean isMouseOverCell(JTable table, int row, int col) {
Point mp = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(mp, table);
int mRow = table.rowAtPoint(mp);
int mCol = table.columnAtPoint(mp);
return row == mRow && col == mCol;
}
}

View File

@ -114,8 +114,18 @@ class DeckGame {
DeckInfo getOpponentDeckInfo() {
return gameInfo.player1DeckName.equals(deck.getName())
? new DeckInfo(gameInfo.player2DeckName, gameInfo.player2DeckColor)
: new DeckInfo(gameInfo.player1DeckName, gameInfo.player1DeckColor);
? new DeckInfo(
gameInfo.player2DeckName,
DeckType.valueOf(gameInfo.player2DeckType),
gameInfo.player2DeckFileChecksum,
gameInfo.player2DeckColor
)
: new DeckInfo(
gameInfo.player1DeckName,
DeckType.valueOf(gameInfo.player1DeckType),
gameInfo.player1DeckFileChecksum,
gameInfo.player1DeckColor
);
}
boolean isWinner() {

View File

@ -2,6 +2,10 @@ package magic.ui.deck.games;
import java.awt.Color;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
@ -10,9 +14,15 @@ import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import magic.data.DeckType;
import magic.model.MagicDeck;
import magic.ui.MagicSound;
import magic.ui.ScreenController;
import magic.ui.helpers.ColorHelper;
import magic.ui.helpers.MouseHelper;
import magic.ui.screen.stats.TableColumnAdjuster;
import magic.ui.widget.cards.table.CardsTableStyle;
import magic.utility.DeckUtils;
@SuppressWarnings("serial")
class DeckGamesJTable extends JTable {
@ -34,14 +44,97 @@ class DeckGamesJTable extends JTable {
private static final Color GRID_COLOR = new Color(194, 197, 203);
private Color DEFAULT_GRID_COLOR;
private final TableColumnAdjuster tca;
private final DeckCellRenderer deckCellRenderer = new DeckCellRenderer();
private MouseHoverListener mouseHoverListener;
DeckGamesJTable(TableModel dm) {
DeckGamesJTable(TableModel dm, boolean hasDeckLinks) {
super(dm);
setDefaultProperties();
setDefaultColumnProperties();
setColumnRenderers();
tca = new TableColumnAdjuster(this);
tca.adjustColumns();
deckCellRenderer.setEnabled(hasDeckLinks);
if (hasDeckLinks) {
mouseHoverListener = new MouseHoverListener();
addMouseMotionListener(mouseHoverListener);
setOnMouseClickAction();
}
}
private void setOnMouseClickAction() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent ev) {
doOnMousePressed(ev.getPoint());
}
@Override
public void mouseExited(MouseEvent e) {
doOnMouseExited();
}
@Override
public void mouseEntered(MouseEvent e) {
doOnMouseEntered();
}
});
}
private void doOnMousePressed(Point mousePosition) {
int col = columnAtPoint(mousePosition);
if (col == 4) {
final TableModel dm = getModel();
int row = rowAtPoint(mousePosition);
DeckInfo deckInfo = (DeckInfo) dm.getValueAt(row, col);
if (deckInfo.deckType != DeckType.Random) {
long fileChecksum = DeckUtils.getDeckFileChecksum(deckInfo.deckName, deckInfo.deckType);
if (fileChecksum == deckInfo.checksum) {
MouseHelper.showBusyCursor(DeckGamesJTable.this);
MagicDeck deck = DeckUtils.loadDeckFromFile(deckInfo.deckName, deckInfo.deckType);
ScreenController.showDeckScreen(deck);
mouseHoverListener.clear();
MouseHelper.showDefaultCursor();
}
} else {
MagicSound.BEEP.play();
}
}
}
private void doOnMouseEntered() {
deckCellRenderer.setEnabled(true);
}
private void doOnMouseExited() {
deckCellRenderer.setEnabled(false);
mouseHoverListener.clear();
repaint();
}
private class MouseHoverListener extends MouseMotionAdapter {
private int lastMCol = -1;
private int lastMRow = -1;
@Override
public void mouseMoved(MouseEvent e) {
JTable aTable = (JTable) e.getSource();
int mCol = aTable.columnAtPoint(e.getPoint());
int mRow = aTable.rowAtPoint(e.getPoint());
if (mCol != lastMCol || mRow != lastMRow) {
lastMCol = mCol;
lastMRow = mRow;
if (mCol == 4) {
aTable.repaint();
MouseHelper.showHandCursor(aTable);
} else {
aTable.repaint();
MouseHelper.showDefaultCursor(aTable);
}
}
}
private void clear() {
lastMCol = -1;
lastMRow = -1;
}
}
private void setColumnRenderers() {
@ -49,7 +142,7 @@ class DeckGamesJTable extends JTable {
getColumnModel().getColumn(1).setCellRenderer(new GameCellRenderer());
getColumnModel().getColumn(2).setCellRenderer(new PlayerCellRenderer());
getColumnModel().getColumn(3).setCellRenderer(new PlayerCellRenderer());
getColumnModel().getColumn(4).setCellRenderer(new DeckCellRenderer());
getColumnModel().getColumn(4).setCellRenderer(deckCellRenderer);
}
private void setDefaultColumnProperties() {
@ -61,7 +154,7 @@ class DeckGamesJTable extends JTable {
cm.getColumn(3).setMinWidth(170);
cm.getColumn(4).setMinWidth(180);
final JTableHeader header = getTableHeader();
header.setReorderingAllowed(true);
header.setEnabled(false);
final DefaultTableCellRenderer renderer =
(DefaultTableCellRenderer) header.getDefaultRenderer();
// center the column header captions.

View File

@ -8,6 +8,7 @@ import magic.data.GeneralConfig;
import magic.model.MagicCardDefinition;
import magic.model.MagicDeck;
import magic.ui.FontsAndBorders;
import magic.ui.ScreenController;
import magic.ui.screen.deck.editor.IDeckEditorView;
import magic.ui.screen.decks.IDeckView;
import magic.ui.screen.stats.IPagination;
@ -54,7 +55,7 @@ public class DeckGamesPanel extends TexturedPanel
refreshStatsTable();
}
if (tm != null) {
table = new DeckGamesJTable(tm);
table = new DeckGamesJTable(tm, !ScreenController.isDeckScreenShowing());
scrollpane.setViewportView(table);
paginator.refresh();
}

View File

@ -1,12 +1,18 @@
package magic.ui.deck.games;
import magic.data.DeckType;
class DeckInfo {
final String deckName;
final String deckColor;
long checksum;
DeckType deckType;
DeckInfo(String deckName, String deckColor) {
DeckInfo(String deckName, DeckType deckType, long checksum, String deckColor) {
this.deckName = deckName;
this.deckType = deckType;
this.checksum = checksum;
this.deckColor = getDeckColor(deckColor);
}

View File

@ -101,7 +101,7 @@ public class CardDecksPanel extends JPanel {
final File deckFile = decksJList.getModel().getElementAt(row);
final MagicDeck deck = DeckUtils.loadDeckFromFile(deckFile.toPath());
if (deck.isValid()) {
ScreenController.showDeckViewScreen(deck, card);
ScreenController.showDeckScreen(deck, card);
} else {
ScreenController.showWarningMessage(String.format("<html><b>%s</b><br>%s</html>",
MText.get(_S1),