Added connector click events

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@94 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Bennet Gerlach 2011-05-03 20:39:21 +02:00
parent e7613bfd93
commit 344d63598a
6 changed files with 392 additions and 308 deletions

View file

@ -140,6 +140,22 @@ public class JRummikub {
} }
}); });
view.getTablePanel().getLeftConnectorClickEvent()
.add(new IListener1<StoneSet>() {
@Override
public void handle(StoneSet s) {
System.out.println("Left connector clicked on " + s);
}
});
view.getTablePanel().getRightConnectorClickEvent()
.add(new IListener1<StoneSet>() {
@Override
public void handle(StoneSet s) {
System.out.println("Right connector clicked on " + s);
}
});
view.getTablePanel().getStoneCollectionPanel().getStoneClickEvent() view.getTablePanel().getStoneCollectionPanel().getStoneClickEvent()
.add(new IListener2<Stone, Boolean>() { .add(new IListener2<Stone, Boolean>() {
@Override @Override
@ -149,6 +165,7 @@ public class JRummikub {
} }
}); });
view.getTablePanel().getStoneCollectionPanel().getRangeClickEvent() view.getTablePanel().getStoneCollectionPanel().getRangeClickEvent()
.add(new IListener2<Stone, Boolean>() { .add(new IListener2<Stone, Boolean>() {
@Override @Override

View file

@ -1,6 +1,6 @@
package jrummikub.model; package jrummikub.model;
/** Class specifing possible StoneColors */ /** Class specifying possible StoneColors */
public enum StoneColor { public enum StoneColor {
BLACK, ORANGE, BLUE, RED BLACK, ORANGE, BLUE, RED
} }

View file

@ -4,6 +4,7 @@ import java.util.Map;
import jrummikub.model.Position; import jrummikub.model.Position;
import jrummikub.model.StoneSet; import jrummikub.model.StoneSet;
import jrummikub.util.Event1;
/** /**
* The view of the table, where the stone sets lie * The view of the table, where the stone sets lie
@ -47,5 +48,21 @@ public interface ITablePanel extends IStonePanel, IClickable {
* *
* @return the stone collection * @return the stone collection
*/ */
IStoneCollectionPanel getStoneCollectionPanel(); public IStoneCollectionPanel getStoneCollectionPanel();
/**
* the left connector click event is emitted when the player clicks on a left
* connector of a stone set on the table .
*
* @return the event
*/
public Event1<StoneSet> getLeftConnectorClickEvent();
/**
* the right connector click event is emitted when the player clicks on a
* right connector of a stone set on the table .
*
* @return the event
*/
public Event1<StoneSet> getRightConnectorClickEvent();
} }

View file

@ -65,7 +65,9 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel,
Stone stone = getStoneAt(pos); Stone stone = getStoneAt(pos);
if (stone == null) { if (stone == null) {
if (!handleOtherClickEvent(pos))
clickEvent.emit(pos); clickEvent.emit(pos);
return; return;
} }
@ -81,12 +83,20 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel,
}); });
} }
/*
* *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) { private Stone getStoneAt(Position pos) {
for (Map.Entry<Stone, Position> entry : stones.entrySet()) { for (Map.Entry<Stone, Position> entry : stones.entrySet()) {
Stone stone = entry.getKey(); Stone stone = entry.getKey();
Position p = entry.getValue(); Position p = entry.getValue();
Rectangle2D rect = new Rectangle2D.Float(p.getX(), p.getY(), Rectangle2D rect = new Rectangle2D.Float(p.getX(), p.getY(), 1, 1);
stone.getWidth(), stone.getHeight());
if (rect.contains(pos.getX(), pos.getY())) if (rect.contains(pos.getX(), pos.getY()))
return stone; return stone;

View file

@ -51,7 +51,7 @@ class StoneCollectionPanel extends AbstractStonePanel implements
for (Stone stone : selectedStones) { for (Stone stone : selectedStones) {
stones.put(stone, new Position(x, 0)); stones.put(stone, new Position(x, 0));
x += stone.getWidth(); x++;
} }
setStones(stones); setStones(stones);

View file

@ -8,6 +8,7 @@ import java.awt.RenderingHints;
import java.awt.event.ComponentAdapter; import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener; import java.awt.event.ComponentListener;
import java.awt.geom.Rectangle2D;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -19,6 +20,7 @@ import javax.swing.JLabel;
import jrummikub.model.Position; import jrummikub.model.Position;
import jrummikub.model.Stone; import jrummikub.model.Stone;
import jrummikub.model.StoneSet; import jrummikub.model.StoneSet;
import jrummikub.util.Event1;
import jrummikub.view.IStoneCollectionPanel; import jrummikub.view.IStoneCollectionPanel;
import jrummikub.view.ITablePanel; import jrummikub.view.ITablePanel;
@ -31,6 +33,7 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
HandPanel.class.getResource("/jrummikub/resource/felt.png")); HandPanel.class.getResource("/jrummikub/resource/felt.png"));
private final static float DEFAULT_SCALE = 1; private final static float DEFAULT_SCALE = 1;
private final static float CONNECTOR_WIDTH = 0.25f;
private final int COLLECTION_GAP = 5; private final int COLLECTION_GAP = 5;
private JLabel leftPlayerLabel, topPlayerLabel, rightPlayerLabel; private JLabel leftPlayerLabel, topPlayerLabel, rightPlayerLabel;
@ -39,6 +42,9 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
private Map<StoneSet, Position> stoneSets = Collections.emptyMap(); private Map<StoneSet, Position> stoneSets = Collections.emptyMap();
private Collection<Stone> selectedStones = Collections.emptyList(); private Collection<Stone> selectedStones = Collections.emptyList();
private Event1<StoneSet> leftConnectorClickEvent = new Event1<StoneSet>();
private Event1<StoneSet> rightConnectorClickEvent = new Event1<StoneSet>();
@Override @Override
public void setLeftPlayerName(String playerName) { public void setLeftPlayerName(String playerName) {
leftPlayerLabel.setText(playerName); leftPlayerLabel.setText(playerName);
@ -54,6 +60,16 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
rightPlayerLabel.setText(playerName); rightPlayerLabel.setText(playerName);
} }
@Override
public Event1<StoneSet> getLeftConnectorClickEvent() {
return leftConnectorClickEvent;
}
@Override
public Event1<StoneSet> getRightConnectorClickEvent() {
return rightConnectorClickEvent;
}
@Override @Override
public void setStoneSets(Map<StoneSet, Position> stoneSets) { public void setStoneSets(Map<StoneSet, Position> stoneSets) {
Map<Stone, Position> stones = new HashMap<Stone, Position>(); Map<Stone, Position> stones = new HashMap<Stone, Position>();
@ -63,7 +79,7 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
for (Stone stone : entry.getKey()) { for (Stone stone : entry.getKey()) {
stones.put(stone, new Position(x, y)); stones.put(stone, new Position(x, y));
x += stone.getWidth(); x++;
} }
} }
@ -145,14 +161,38 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
stoneCollection.addComponentListener(rescaleListener); stoneCollection.addComponentListener(rescaleListener);
} }
protected boolean handleOtherClickEvent(Position pos) {
for (Map.Entry<StoneSet, Position> entry : stoneSets.entrySet()) {
Position p = entry.getValue();
StoneSet stoneSet = entry.getKey();
float x = p.getX(), y = p.getY();
// left connector
Rectangle2D rect = new Rectangle2D.Float(x - CONNECTOR_WIDTH, y,
CONNECTOR_WIDTH, 1);
if (rect.contains(pos.getX(), pos.getY())) {
leftConnectorClickEvent.emit(stoneSet);
return true;
}
// right connector
rect = new Rectangle2D.Float(x + stoneSet.size(), y, CONNECTOR_WIDTH, 1);
if (rect.contains(pos.getX(), pos.getY())) {
rightConnectorClickEvent.emit(stoneSet);
return true;
}
}
return false;
}
private void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos) { private void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos) {
float x = pos.getX(); float x = pos.getX();
int width = getStonePainter().getStoneWidth(), height = getStonePainter() int width = getStonePainter().getStoneWidth(), height = getStonePainter()
.getStoneHeight(); .getStoneHeight();
g.setColor(new Color(0, 0, 0, 0.25f)); g.setColor(new Color(0, 0, 0, 0.25f));
g.fillRect((int) (x * width) - width / 4, (int) (pos.getY() * height), g.fillRect((int) (x * width - width * CONNECTOR_WIDTH),
width / 4, height); (int) (pos.getY() * height), (int) (width * CONNECTOR_WIDTH), height);
for (Stone stone : stoneSet) { for (Stone stone : stoneSet) {
getStonePainter().paintStone(g, stone, new Position(x, pos.getY()), getStonePainter().paintStone(g, stone, new Position(x, pos.getY()),
@ -161,8 +201,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
} }
g.setColor(new Color(0, 0, 0, 0.25f)); g.setColor(new Color(0, 0, 0, 0.25f));
g.fillRect((int) (x * width), (int) (pos.getY() * height), width / 4, g.fillRect((int) (x * width), (int) (pos.getY() * height),
height); (int) (width * CONNECTOR_WIDTH), height);
} }
@Override @Override
@ -178,8 +218,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON); RenderingHints.VALUE_ANTIALIAS_ON);
for (Map.Entry<StoneSet, Position> stoneSet : stoneSets.entrySet()) { for (Map.Entry<StoneSet, Position> entry : stoneSets.entrySet()) {
paintStoneSet(g, stoneSet.getKey(), stoneSet.getValue()); paintStoneSet(g, entry.getKey(), entry.getValue());
} }
} }
} }