summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/AbstractStonePanel.java
diff options
context:
space:
mode:
authorBennet Gerlach <bennet_gerlach@web.de>2011-05-03 20:39:21 +0200
committerBennet Gerlach <bennet_gerlach@web.de>2011-05-03 20:39:21 +0200
commit344d63598afa6aea19e1975d4253bb11dfef6182 (patch)
tree9f1cddaf52635cbc49e1efde8b810c59c9ad9fe4 /src/jrummikub/view/impl/AbstractStonePanel.java
parente7613bfd930290025b4c5abaf4acf63683efd5f2 (diff)
downloadJRummikub-344d63598afa6aea19e1975d4253bb11dfef6182.tar
JRummikub-344d63598afa6aea19e1975d4253bb11dfef6182.zip
Added connector click events
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@94 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/view/impl/AbstractStonePanel.java')
-rw-r--r--src/jrummikub/view/impl/AbstractStonePanel.java234
1 files changed, 122 insertions, 112 deletions
diff --git a/src/jrummikub/view/impl/AbstractStonePanel.java b/src/jrummikub/view/impl/AbstractStonePanel.java
index a19ea1f..855b40a 100644
--- a/src/jrummikub/view/impl/AbstractStonePanel.java
+++ b/src/jrummikub/view/impl/AbstractStonePanel.java
@@ -21,117 +21,127 @@ import jrummikub.view.IStonePanel;
*/
@SuppressWarnings("serial")
abstract class AbstractStonePanel extends JPanel implements IStonePanel,
- IClickable {
- private StonePainter stonePainter;
-
- 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
- */
- protected StonePainter getStonePainter() {
- return stonePainter;
- }
-
- /**
- * Create a new StonePanel with default scale factor
- */
- public AbstractStonePanel() {
- this(1);
- }
-
- /**
- * Create a new StonePanel with a given scale factor
- *
- * @param scale
- * the grid scale
- */
- public AbstractStonePanel(float scale) {
- super(true); // Set double buffered
-
- stonePainter = new StonePainter(scale);
-
- addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- Insets insets = getInsets();
- Position pos = stonePainter.calculatePosition(e.getX() - insets.left,
- e.getY() - insets.top);
- Stone stone = getStoneAt(pos);
-
- if (stone == null) {
- clickEvent.emit(pos);
- return;
- }
-
- Event2<Stone, Boolean> event = stoneClickEvent;
-
- if (e.isShiftDown())
- event = rangeClickEvent;
- else if (e.getClickCount() >= 2)
- event = setClickEvent;
-
- event.emit(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 Event1<Position> getClickEvent() {
- return clickEvent;
- }
-
- @Override
- public Event2<Stone, Boolean> getStoneClickEvent() {
- return stoneClickEvent;
- }
-
- @Override
- public Event2<Stone, Boolean> getRangeClickEvent() {
- return rangeClickEvent;
- }
-
- @Override
- public Event2<Stone, Boolean> getSetClickEvent() {
- return setClickEvent;
- }
+ IClickable {
+ private StonePainter stonePainter;
+
+ 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
+ */
+ protected StonePainter getStonePainter() {
+ return stonePainter;
+ }
+
+ /**
+ * Create a new StonePanel with default scale factor
+ */
+ public AbstractStonePanel() {
+ this(1);
+ }
+
+ /**
+ * Create a new StonePanel with a given scale factor
+ *
+ * @param scale
+ * the grid scale
+ */
+ public AbstractStonePanel(float scale) {
+ super(true); // Set double buffered
+
+ stonePainter = new StonePainter(scale);
+
+ addMouseListener(new MouseAdapter() {
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ Insets insets = getInsets();
+ Position pos = stonePainter.calculatePosition(e.getX() - insets.left,
+ e.getY() - insets.top);
+ Stone stone = getStoneAt(pos);
+
+ if (stone == null) {
+ if (!handleOtherClickEvent(pos))
+ clickEvent.emit(pos);
+
+ return;
+ }
+
+ Event2<Stone, Boolean> event = stoneClickEvent;
+
+ if (e.isShiftDown())
+ event = rangeClickEvent;
+ else if (e.getClickCount() >= 2)
+ event = setClickEvent;
+
+ event.emit(stone, e.isControlDown());
+ }
+ });
+ }
+
+ /*
+ * *Overwrite this method* to signal if special zone was clicked
+ *
+ * @return special zone clicked
+ */
+ protected boolean handleOtherClickEvent(Position pos) {
+ return false;
+ }
+
+ 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(), 1, 1);
+
+ 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 Event1<Position> getClickEvent() {
+ return clickEvent;
+ }
+
+ @Override
+ public Event2<Stone, Boolean> getStoneClickEvent() {
+ return stoneClickEvent;
+ }
+
+ @Override
+ public Event2<Stone, Boolean> getRangeClickEvent() {
+ return rangeClickEvent;
+ }
+
+ @Override
+ public Event2<Stone, Boolean> getSetClickEvent() {
+ return setClickEvent;
+ }
}