diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-05-03 17:29:52 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-05-03 17:29:52 +0200 |
commit | bcc3f95847eafa1b61bb32dac047101c7adc0e64 (patch) | |
tree | 992c9f7d3569f3598cf8e1a0f14fa647974bb5e7 /src/jrummikub/view/impl/AbstractStonePanel.java | |
parent | 7edb66d4ff069a40471258a66ef56dac36598665 (diff) | |
download | JRummikub-bcc3f95847eafa1b61bb32dac047101c7adc0e64.tar JRummikub-bcc3f95847eafa1b61bb32dac047101c7adc0e64.zip |
Make StonePanel emit Stone click events
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@80 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/view/impl/AbstractStonePanel.java')
-rw-r--r-- | src/jrummikub/view/impl/AbstractStonePanel.java | 77 |
1 files changed, 66 insertions, 11 deletions
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<Position, Boolean> clickEvent = new Event2<Position, Boolean>(); - private Event2<Position, Boolean> rangeClickEvent = new Event2<Position, Boolean>(); - private Event2<Position, Boolean> setClickEvent = new Event2<Position, Boolean>(); + private Event1<Position> clickEvent = new Event1<Position>(); + private Event2<Stone, Boolean> stoneClickEvent = new Event2<Stone, Boolean>(); + private Event2<Stone, Boolean> rangeClickEvent = new Event2<Stone, Boolean>(); + private Event2<Stone, Boolean> setClickEvent = new Event2<Stone, Boolean>(); + + private Map<Stone, Position> 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<Position, Boolean> 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<Stone, Boolean> 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<Stone, Position> 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<Stone, Position> stones) { + this.stones = stones; + } + + /** + * Returns the list of stones and positions currently set + * + * @return the stones + */ + protected Map<Stone, Position> getStones() { + return stones; + } + @Override - public Event2<Position, Boolean> getClickEvent() { + public Event1<Position> getClickEvent() { return clickEvent; } @Override - public Event2<Position, Boolean> getRangeClickEvent() { + public Event2<Stone, Boolean> getStoneClickEvent() { + return stoneClickEvent; + } + + @Override + public Event2<Stone, Boolean> getRangeClickEvent() { return rangeClickEvent; } @Override - public Event2<Position, Boolean> getSetClickEvent() { + public Event2<Stone, Boolean> getSetClickEvent() { return setClickEvent; } |