From bcc3f95847eafa1b61bb32dac047101c7adc0e64 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 3 May 2011 17:29:52 +0200 Subject: Make StonePanel emit Stone click events git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@80 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/JRummikub.java | 89 ++++++++++++++--------- src/jrummikub/model/Stone.java | 8 ++ src/jrummikub/view/IClickable.java | 26 +------ src/jrummikub/view/IHandPanel.java | 2 +- src/jrummikub/view/IStoneCollectionPanel.java | 2 +- src/jrummikub/view/IStonePanel.java | 38 ++++++++++ src/jrummikub/view/ITablePanel.java | 2 +- src/jrummikub/view/impl/AbstractStonePanel.java | 77 +++++++++++++++++--- src/jrummikub/view/impl/HandPanel.java | 5 +- src/jrummikub/view/impl/StoneCollectionPanel.java | 27 +++++-- src/jrummikub/view/impl/TablePanel.java | 14 ++++ 11 files changed, 207 insertions(+), 83 deletions(-) create mode 100644 src/jrummikub/view/IStonePanel.java diff --git a/src/jrummikub/JRummikub.java b/src/jrummikub/JRummikub.java index 6b76af6..54566d5 100644 --- a/src/jrummikub/JRummikub.java +++ b/src/jrummikub/JRummikub.java @@ -13,6 +13,7 @@ import jrummikub.model.Stone; import jrummikub.model.StoneColor; import jrummikub.model.StoneSet; import jrummikub.util.IListener; +import jrummikub.util.IListener1; import jrummikub.util.IListener2; import jrummikub.view.IView; @@ -70,82 +71,98 @@ public class JRummikub { view.getPlayerPanel().getHandPanel().setStones(stones); view.getPlayerPanel().getHandPanel().getClickEvent() - .add(new IListener2() { + .add(new IListener1() { @Override - public void fire(Position p, Boolean collect) { - System.out.println("Board clicked at " + p + public void fire(Position p) { + System.out.println("Hand clicked at " + p); + } + }); + view.getPlayerPanel().getHandPanel().getStoneClickEvent() + .add(new IListener2() { + @Override + public void fire(Stone s, Boolean collect) { + System.out.println("Hand clicked on " + s + (collect ? ", collect" : "")); } }); view.getPlayerPanel().getHandPanel().getRangeClickEvent() - .add(new IListener2() { + .add(new IListener2() { @Override - public void fire(Position p, Boolean collect) { - System.out.println("Board range-clicked at " + p + public void fire(Stone s, Boolean collect) { + System.out.println("Hand range-clicked on " + s + (collect ? ", collect" : "")); } }); view.getPlayerPanel().getHandPanel().getSetClickEvent() - .add(new IListener2() { + .add(new IListener2() { @Override - public void fire(Position p, Boolean collect) { - System.out.println("Board set-clicked at " + p + public void fire(Stone s, Boolean collect) { + System.out.println("Hand set-clicked at " + s + (collect ? ", collect" : "")); } }); - view.getTablePanel().getClickEvent().add(new IListener2() { - @Override - public void fire(Position p, Boolean collect) { - System.out.println("Table clicked at " + p - + (collect ? ", collect" : "")); + view.getTablePanel().getClickEvent() + .add(new IListener1() { + @Override + public void fire(Position p) { + System.out.println("Table clicked at " + p); + } + }); + view.getTablePanel().getStoneClickEvent() + .add(new IListener2() { + @Override + public void fire(Stone s, Boolean collect) { + System.out.println("Table clicked on " + s + + (collect ? ", collect" : "")); - } - }); + } + }); view.getTablePanel().getRangeClickEvent() - .add(new IListener2() { + .add(new IListener2() { @Override - public void fire(Position p, Boolean collect) { - System.out.println("Table range-clicked at " + p + public void fire(Stone s, Boolean collect) { + System.out.println("Table range-clicked on " + s + (collect ? ", collect" : "")); } }); - view.getTablePanel().getSetClickEvent().add(new IListener2() { - @Override - public void fire(Position p, Boolean collect) { - System.out.println("Table set-clicked at " + p - + (collect ? ", collect" : "")); + view.getTablePanel().getSetClickEvent() + .add(new IListener2() { + @Override + public void fire(Stone s, Boolean collect) { + System.out.println("Table set-clicked at " + s + + (collect ? ", collect" : "")); - } - }); + } + }); - view.getTablePanel().getStoneCollectionPanel().getClickEvent() - .add(new IListener2() { + view.getTablePanel().getStoneCollectionPanel().getStoneClickEvent() + .add(new IListener2() { @Override - public void fire(Position p, Boolean collect) { - System.out.println("Collection clicked at " + p + public void fire(Stone s, Boolean collect) { + System.out.println("Collection clicked on " + s + (collect ? ", collect" : "")); } }); view.getTablePanel().getStoneCollectionPanel().getRangeClickEvent() - .add(new IListener2() { + .add(new IListener2() { @Override - public void fire(Position p, Boolean collect) { - System.out.println("Collection range-clicked at " + p + public void fire(Stone s, Boolean collect) { + System.out.println("Collection range-clicked on " + s + (collect ? ", collect" : "")); } }); view.getTablePanel().getStoneCollectionPanel().getSetClickEvent() - .add(new IListener2() { + .add(new IListener2() { @Override - public void fire(Position p, Boolean collect) { - System.out.println("Collection set-clicked at " + p + public void fire(Stone s, Boolean collect) { + System.out.println("Collection set-clicked at " + s + (collect ? ", collect" : "")); } diff --git a/src/jrummikub/model/Stone.java b/src/jrummikub/model/Stone.java index da8052b..800425e 100644 --- a/src/jrummikub/model/Stone.java +++ b/src/jrummikub/model/Stone.java @@ -71,4 +71,12 @@ public class Stone implements Sizeable { return 1; } + @Override + public String toString() { + if (joker) { + return "Stone[joker,color=" + color + "]"; + } else { + return "Stone[value=" + value + ",color=" + color + "]"; + } + } } diff --git a/src/jrummikub/view/IClickable.java b/src/jrummikub/view/IClickable.java index c827042..4f44b5e 100644 --- a/src/jrummikub/view/IClickable.java +++ b/src/jrummikub/view/IClickable.java @@ -1,38 +1,18 @@ package jrummikub.view; import jrummikub.model.Position; -import jrummikub.util.IEvent2; +import jrummikub.util.IEvent1; /** * An interface for view elements that can emit click events */ public interface IClickable { /** - * the click event is emitted when the player clicks on the table/board/etc. + * the click event is emitted when the player clicks on the table/hand/etc. * * @return the event; the first parameter is the position of the click in grid * coordinates, the second is true when the player wants to add stones * to his selection instead of replacing them */ - public IEvent2 getClickEvent(); - - /** - * the range click event is emitted when the player clicks on the table/board/ - * etc. and wants to select a range instead of a single stone - * - * @return the event; the first parameter is the position of the click in grid - * coordinates, the second is true when the player wants to add stones - * to his selection instead of replacing them - */ - public IEvent2 getRangeClickEvent(); - - /** - * the set click event is emitted when the player clicks on the table/board/ - * etc. and wants to select a whole set instead of a single stone - * - * @return the event; the first parameter is the position of the click in grid - * coordinates, the second is true when the player wants to add stones - * to his selection instead of replacing them - */ - public IEvent2 getSetClickEvent(); + public IEvent1 getClickEvent(); } diff --git a/src/jrummikub/view/IHandPanel.java b/src/jrummikub/view/IHandPanel.java index 4573b93..23cd0ca 100644 --- a/src/jrummikub/view/IHandPanel.java +++ b/src/jrummikub/view/IHandPanel.java @@ -8,7 +8,7 @@ import jrummikub.model.Stone; /** * The view for a player's hand that displays his stones */ -public interface IHandPanel extends IClickable { +public interface IHandPanel extends IStonePanel, IClickable { /** * Set the player's stones to display on the board * diff --git a/src/jrummikub/view/IStoneCollectionPanel.java b/src/jrummikub/view/IStoneCollectionPanel.java index 4c84508..5fa234c 100644 --- a/src/jrummikub/view/IStoneCollectionPanel.java +++ b/src/jrummikub/view/IStoneCollectionPanel.java @@ -3,5 +3,5 @@ package jrummikub.view; /** * The view of the collection that shows the stones a player has selected */ -public interface IStoneCollectionPanel extends IClickable { +public interface IStoneCollectionPanel extends IStonePanel { } diff --git a/src/jrummikub/view/IStonePanel.java b/src/jrummikub/view/IStonePanel.java new file mode 100644 index 0000000..6720902 --- /dev/null +++ b/src/jrummikub/view/IStonePanel.java @@ -0,0 +1,38 @@ +package jrummikub.view; + +import jrummikub.model.Stone; +import jrummikub.util.IEvent2; + +/** + * An interface for view elements that can emit click events for clicks on stones + */ +public interface IStonePanel { + /** + * the click event is emitted when the player clicks on the table/hand/etc. + * + * @return the event; the first parameter is the position of the click in grid + * coordinates, the second is true when the player wants to add stones + * to his selection instead of replacing them + */ + public IEvent2 getStoneClickEvent(); + + /** + * the range click event is emitted when the player clicks on the table/hand/ + * etc. and wants to select a range instead of a single stone + * + * @return the event; the first parameter is the position of the click in grid + * coordinates, the second is true when the player wants to add stones + * to his selection instead of replacing them + */ + public IEvent2 getRangeClickEvent(); + + /** + * the set click event is emitted when the player clicks on the table/hand/ + * etc. and wants to select a whole set instead of a single stone + * + * @return the event; the first parameter is the position of the click in grid + * coordinates, the second is true when the player wants to add stones + * to his selection instead of replacing them + */ + public IEvent2 getSetClickEvent(); +} diff --git a/src/jrummikub/view/ITablePanel.java b/src/jrummikub/view/ITablePanel.java index 48eb357..ec7f3ea 100644 --- a/src/jrummikub/view/ITablePanel.java +++ b/src/jrummikub/view/ITablePanel.java @@ -8,7 +8,7 @@ import jrummikub.model.StoneSet; /** * The view of the table, where the stone sets lie */ -public interface ITablePanel extends IClickable { +public interface ITablePanel extends IStonePanel, IClickable { /** * Sets the player name on the left label * diff --git a/src/jrummikub/view/impl/AbstractStonePanel.java b/src/jrummikub/view/impl/AbstractStonePanel.java index 8ab18d8..19e5891 100644 --- a/src/jrummikub/view/impl/AbstractStonePanel.java +++ b/src/jrummikub/view/impl/AbstractStonePanel.java @@ -3,23 +3,33 @@ package jrummikub.view.impl; import java.awt.Insets; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.awt.geom.Rectangle2D; +import java.util.Collections; +import java.util.Map; import javax.swing.JPanel; import jrummikub.model.Position; +import jrummikub.model.Stone; +import jrummikub.util.Event1; import jrummikub.util.Event2; import jrummikub.view.IClickable; +import jrummikub.view.IStonePanel; /** * Base class for panels that draw stones */ @SuppressWarnings("serial") -abstract class AbstractStonePanel extends JPanel implements IClickable { +abstract class AbstractStonePanel extends JPanel implements IStonePanel, + IClickable { private StonePainter stonePainter; - private Event2 clickEvent = new Event2(); - private Event2 rangeClickEvent = new Event2(); - private Event2 setClickEvent = new Event2(); + private Event1 clickEvent = new Event1(); + private Event2 stoneClickEvent = new Event2(); + private Event2 rangeClickEvent = new Event2(); + private Event2 setClickEvent = new Event2(); + + private Map stones = Collections.emptyMap(); /** * @return the stone painter @@ -50,32 +60,77 @@ abstract class AbstractStonePanel extends JPanel implements IClickable { @Override public void mouseClicked(MouseEvent e) { Insets insets = getInsets(); - Event2 event = clickEvent; + Position pos = stonePainter.calculatePosition(e.getX() - insets.left, + e.getY() - insets.top); + Stone stone = getStoneAt(pos); + + if (stone == null) { + clickEvent.fire(pos); + return; + } + + Event2 event = stoneClickEvent; if (e.isShiftDown()) event = rangeClickEvent; else if (e.getClickCount() >= 2) event = setClickEvent; - event.fire( - stonePainter.calculatePosition(e.getX() - insets.left, e.getY() - - insets.top), e.isControlDown()); + event.fire(stone, e.isControlDown()); } }); } + private Stone getStoneAt(Position pos) { + for (Map.Entry entry : stones.entrySet()) { + Stone stone = entry.getKey(); + Position p = entry.getValue(); + Rectangle2D rect = new Rectangle2D.Float(p.getX(), p.getY(), + stone.getWidth(), stone.getHeight()); + + if (rect.contains(pos.getX(), pos.getY())) + return stone; + } + + return null; + } + + /** + * Sets the list of stones that can be clicked on + * + * @param stones + * the stones and positions + */ + protected void setStones(Map stones) { + this.stones = stones; + } + + /** + * Returns the list of stones and positions currently set + * + * @return the stones + */ + protected Map getStones() { + return stones; + } + @Override - public Event2 getClickEvent() { + public Event1 getClickEvent() { return clickEvent; } @Override - public Event2 getRangeClickEvent() { + public Event2 getStoneClickEvent() { + return stoneClickEvent; + } + + @Override + public Event2 getRangeClickEvent() { return rangeClickEvent; } @Override - public Event2 getSetClickEvent() { + public Event2 getSetClickEvent() { return setClickEvent; } diff --git a/src/jrummikub/view/impl/HandPanel.java b/src/jrummikub/view/impl/HandPanel.java index 1610c00..0533181 100644 --- a/src/jrummikub/view/impl/HandPanel.java +++ b/src/jrummikub/view/impl/HandPanel.java @@ -39,7 +39,6 @@ class HandPanel extends AbstractStonePanel implements IHandPanel { } private BufferedImage scaledBackground = BACKGROUND; - private Map stones = Collections.emptyMap(); private Collection selectedStones = Collections.emptyList(); /** @@ -97,7 +96,7 @@ class HandPanel extends AbstractStonePanel implements IHandPanel { g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - for (Map.Entry entry : stones.entrySet()) { + for (Map.Entry entry : getStones().entrySet()) { getStonePainter().paintStone(g, entry.getKey(), entry.getValue(), selectedStones.contains(entry.getKey())); } @@ -105,7 +104,7 @@ class HandPanel extends AbstractStonePanel implements IHandPanel { @Override public void setStones(Map stones) { - this.stones = stones; + super.setStones(stones); repaint(); } diff --git a/src/jrummikub/view/impl/StoneCollectionPanel.java b/src/jrummikub/view/impl/StoneCollectionPanel.java index e4ca1c6..8729170 100644 --- a/src/jrummikub/view/impl/StoneCollectionPanel.java +++ b/src/jrummikub/view/impl/StoneCollectionPanel.java @@ -6,6 +6,8 @@ import java.awt.Graphics2D; import java.awt.Insets; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import javax.swing.border.EmptyBorder; @@ -17,7 +19,8 @@ import jrummikub.view.IStoneCollectionPanel; * Implementation of the stone collection (selection) */ @SuppressWarnings("serial") -class StoneCollectionPanel extends AbstractStonePanel implements IStoneCollectionPanel { +class StoneCollectionPanel extends AbstractStonePanel implements + IStoneCollectionPanel { private final static int INSET = 7; private final static float STONE_SCALE = 1.1f; @@ -37,17 +40,27 @@ class StoneCollectionPanel extends AbstractStonePanel implements IStoneCollectio /** * Sets the stones to be shown in the collection * - * @param stones + * @param selectedStones * the selected stones */ - void setSelectedStones(Collection stones) { - selectedStones = stones; + void setSelectedStones(Collection selectedStones) { + this.selectedStones = selectedStones; - if (stones.isEmpty()) { + Map stones = new HashMap(); + float x = 0; + + for (Stone stone : selectedStones) { + stones.put(stone, new Position(x, 0)); + x += stone.getWidth(); + } + + setStones(stones); + + if (selectedStones.isEmpty()) { setVisible(false); } else { - setSize(getStonePainter().getStoneWidth() * stones.size() + 2 * INSET, - getStonePainter().getStoneHeight() + 2 * INSET); + setSize(getStonePainter().getStoneWidth() * selectedStones.size() + 2 + * INSET, getStonePainter().getStoneHeight() + 2 * INSET); setVisible(true); repaint(); diff --git a/src/jrummikub/view/impl/TablePanel.java b/src/jrummikub/view/impl/TablePanel.java index b02a9a9..3eb33d8 100644 --- a/src/jrummikub/view/impl/TablePanel.java +++ b/src/jrummikub/view/impl/TablePanel.java @@ -10,6 +10,7 @@ import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import javax.swing.ImageIcon; @@ -55,7 +56,20 @@ class TablePanel extends AbstractStonePanel implements ITablePanel { @Override public void setStoneSets(Map stoneSets) { + Map stones = new HashMap(); + + for (Map.Entry entry : stoneSets.entrySet()) { + float x = entry.getValue().getX(), y = entry.getValue().getY(); + + for (Stone stone : entry.getKey()) { + stones.put(stone, new Position(x, y)); + x += stone.getWidth(); + } + } + + setStones(stones); this.stoneSets = stoneSets; + repaint(); } -- cgit v1.2.3