/MWClient/src/main/java/mw/client/gui/MWReveal.java
Java | 357 lines | 268 code | 70 blank | 19 comment | 36 complexity | 9b127ff77399ed8d68b2d51fe2c921ef MD5 | raw file
- package mw.client.gui;
-
- import java.awt.Dimension;
- import java.awt.Rectangle;
- import java.awt.event.ActionEvent;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionAdapter;
- import java.awt.event.MouseMotionListener;
- import java.rmi.RemoteException;
- import java.util.ArrayList;
-
- import javax.swing.AbstractAction;
- import javax.swing.BoxLayout;
- import javax.swing.JComponent;
- import javax.swing.JInternalFrame;
- import javax.swing.JLayeredPane;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.KeyStroke;
-
- import org.apache.log4j.Logger;
-
- import mw.client.managers.ConnectionManager;
- import mw.client.managers.ProfileManager;
- import mw.client.managers.WindowManager;
- import mw.client.utils.cache.SaveObjectUtil;
- import mw.mtgforge.AllZone;
- import mw.server.list.CardBeanList;
- import mw.server.model.MagicWarsModel.GameZone;
- import mw.server.model.bean.CardBean;
- import mw.server.socket.ZippedObjectImpl;
-
- @SuppressWarnings("serial")
- public class MWReveal extends JInternalFrame implements MouseMotionListener {
-
- private static final Logger log = Logger.getLogger(MWReveal.class);
-
- public enum RevealType {
- Reveal,
- OpponentHand,
- LookAt
- };
-
- private int headsize;
-
- private JPanel contentPane;
-
- private JLayeredPane layeredPane;
-
- private int revealedSize;
-
- private CardBeanList revealedCards;
-
- private Object cardIsBeingDisplayed;
-
- private double aspectRatio = 1.0;
-
- private RevealType revealType;
-
- public MWReveal(RevealType revealType) {
-
- super("Reveal", false, // resizable
- !revealType.equals(RevealType.OpponentHand), // closable if canPlay is false
- false, // maximizable
- false);// iconifiable
-
- // set the window size
- //setSize((int)(140 * aspectRatio), 130);
-
- this.revealType = revealType;
-
- headsize = 16;
-
- // Create and set up the content pane.
- contentPane = new JPanel();
- contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
- contentPane.setOpaque(true);
-
- // Create and set up the layered pane.
- layeredPane = new JLayeredPane();
- layeredPane.setPreferredSize(new Dimension(200, 120));
-
- showCards();
-
- contentPane.add(layeredPane);
- setContentPane(contentPane);
-
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
-
- if (revealType.equals(RevealType.OpponentHand)) {
- layeredPane.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- int count = e.getClickCount();
-
- if (count == 2) {
- Object o = layeredPane.getComponentAt(e.getPoint());
- if (o instanceof MWCardImpl) {
- MWCardImpl mtgCard = (MWCardImpl) o;
- WindowManager.getManager().getFlowHand().getInputControl().selectCard(mtgCard.getCard(), GameZone.Hand);
- }
- }
- }
- });
- }
-
- layeredPane.addMouseMotionListener(new MouseMotionAdapter() {
-
- @Override
- public void mouseMoved(MouseEvent e) {
- Object o = layeredPane.getComponentAt(e.getPoint());
- if (o instanceof MWCardImpl) {
- if (!o.equals(cardIsBeingDisplayed)) {
- cardIsBeingDisplayed = o;
- MWCardImpl mtgCard = (MWCardImpl) o;
- mtgCard.updateDescription();
- }
- }
- }
- });
-
- layeredPane.addMouseListener(new MouseAdapter() {
-
- @Override
- public void mouseExited(MouseEvent e) {
- cardIsBeingDisplayed = null;
- }
- });
-
- /**
- * Commands for cycling the cards in the hand
- */
- getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,
- ActionEvent.ALT_MASK), "up");
-
- getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,
- ActionEvent.ALT_MASK), "down");
-
- getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,
- ActionEvent.ALT_MASK), "left");
-
- getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,
- ActionEvent.ALT_MASK), "right");
-
- getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ALT,
- 0), "alt");
-
- addKeyListener(new KeyAdapter() {
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == 18) {
- JOptionPane.showMessageDialog(null, "Alt");
- }
- }
- });
-
- getActionMap().put("left", new AbstractAction() {
- public void actionPerformed(ActionEvent e) {
- cycleLeft();
- }
- private static final long serialVersionUID = 1L;
- });
-
- getActionMap().put("right", new AbstractAction() {
- public void actionPerformed(ActionEvent e) {
- cycleRight();
- }
- private static final long serialVersionUID = 1L;
- });
-
- getActionMap().put("up", new AbstractAction() {
- public void actionPerformed(ActionEvent e) {
- //cycleLeft();
- }
- private static final long serialVersionUID = 1L;
- });
-
- getActionMap().put("down", new AbstractAction() {
- public void actionPerformed(ActionEvent e) {
- //cycleRight();
- }
- private static final long serialVersionUID = 1L;
- });
-
- getActionMap().put("alt", new AbstractAction() {
- public void actionPerformed(ActionEvent e) {
- //cycleRight();
- JOptionPane.showMessageDialog(null, "Alt!");
- }
- private static final long serialVersionUID = 1L;
- });
- }
-
- private void addCard(CardBean card, int zOrder, JLayeredPane layeredPane) {
-
- MWCardImpl mtgCard = new MWCardImpl(card);
- Rectangle rec = new Rectangle(0, headsize * (zOrder),
- (int) (130 * aspectRatio), (int) (130 * aspectRatio));
- mtgCard.setBounds(rec);
- layeredPane.add(mtgCard, Integer.valueOf(zOrder));
- }
-
- protected void reorganizeOrders(int cardCount) {
- int i = 0;
-
- for (int j = 0; j < revealedSize ; j++) {
- CardBean card = revealedCards.get(j);
-
- int k = 0;
- while (k < layeredPane.getComponentCount()) {
- Object o = layeredPane.getComponent(k);
- if (o instanceof MWCardImpl) {
- CardBean c = ((MWCardImpl)o).getCard();
- if (c.equals(card)) {
- break;
- }
- }
- k++;
- }
-
- MWCardImpl mtgCard = (MWCardImpl) layeredPane.getComponent(k);
- layeredPane.setComponentZOrder(mtgCard, j);
- Rectangle rec = new Rectangle(0, headsize * (cardCount - j - 1),
- (int)(130 * aspectRatio),
- (int)(130 * aspectRatio));
- mtgCard.setBounds(rec);
- i++;
-
- }
-
- }
- public void showCards() {
-
- try {
- ZippedObjectImpl<CardBeanList> zipped = null;
- if (revealType.equals(RevealType.OpponentHand)) {
- zipped = ConnectionManager.getRMIConnection().getRevealedCardsToPlay(ProfileManager.getMyId());
- } else if (revealType.equals(RevealType.Reveal)) {
- zipped = ConnectionManager.getRMIConnection().getRevealedCards(ProfileManager.getMyId());
- } else if (revealType.equals(RevealType.LookAt)) {
- zipped = ConnectionManager.getRMIConnection().getViewedCards(ProfileManager.getMyId());
- }
- if (zipped != null) {
- revealedCards = zipped.unzip();
- } else {
- log.error("reveal: zipped: null.");
- }
- revealedSize = revealedCards.size();
- SaveObjectUtil.saveObject(zipped, ProfileManager.getMyId(), "revealed_viewed");
-
- /**
- * Merge hand list changes.
- */
- mergeCardChanges(revealedCards, layeredPane);
- reorganizeOrders(revealedSize);
-
- if (revealType.equals(RevealType.OpponentHand)) {
- setTitle("Opp.hand (" + revealedSize + ")");
- } else if (revealType.equals(RevealType.Reveal)) {
- setTitle("Revealed (" + revealedSize + ")");
- } else if (revealType.equals(RevealType.LookAt)) {
- setTitle("Look at");
- }
-
- /**
- * change visual hand size - pack
- */
- if (revealedSize > 0) {
- setSize((int)(139 * aspectRatio),
- (int)(130 * aspectRatio) + (revealedSize + 1) * headsize);
- }
- else {
- setSize((int)(140 * aspectRatio), 0);
- }
- } catch (RemoteException ex) {
- ex.printStackTrace();
- }
- }
-
- /**
- * Remove cards from JLayeredPane.
- */
- protected void mergeCardChanges(CardBeanList cardList, JLayeredPane pane) {
-
- int paneCount = pane.getComponentCount();
- int arrayCount = cardList.size();
-
- ArrayList<CardBean> paneCards = new ArrayList<CardBean>();
- for (int i = paneCount - 1; i >= 0; i--) {
- if (pane.getComponent(i) instanceof MWCardImpl) {
- MWCardImpl mtgCard = (MWCardImpl)pane.getComponent(i);
- CardBean card = mtgCard.getCard();
- paneCards.add(card);
- if (!cardList.contains(card)) {
- pane.remove(mtgCard);
- }
- }
- }
-
- for (int i = 0; i < arrayCount; i++) {
- CardBean card = cardList.get(i);
- if (!paneCards.contains(card)) {
- addCard(card, i + 1, pane);
- }
- }
- }
-
- public void mouseEntered(MouseEvent e) {
- }
-
- public void mouseExited(MouseEvent e) {
- }
-
- public void mouseDragged(MouseEvent e) {
- }
-
- public void mouseMoved(MouseEvent e) {
-
- }
-
- public void cycleRight() {
-
- MWCardImpl card = (MWCardImpl) layeredPane.getComponent(0);
- int i = 0;
- while (!revealedCards.get(i).equals(card.getCard()) && i < revealedSize) {
- i++;
- }
- CardBean c = revealedCards.remove(i);
- mergeCardChanges(revealedCards, layeredPane);
- revealedCards.add(c);
- mergeCardChanges(revealedCards, layeredPane);
- reorganizeOrders(revealedSize);
-
- revalidate();
- repaint();
- }
-
- public void cycleLeft() {
-
- MWCardImpl card = (MWCardImpl) layeredPane.getComponent(revealedCards.size() - 1);
- int i = 0;
- while (!revealedCards.get(i).equals(card.getCard()) && i < revealedSize) {
- i++;
- }
- CardBean c = revealedCards.remove(i);
- mergeCardChanges(revealedCards, layeredPane);
- revealedCards.add(0, c);
- mergeCardChanges(revealedCards, layeredPane);
- reorganizeOrders(revealedSize);
-
- revalidate();
- repaint();
- }
-
- }