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:
parent
e7613bfd93
commit
344d63598a
6 changed files with 392 additions and 308 deletions
|
@ -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()
|
||||
.add(new IListener2<Stone, Boolean>() {
|
||||
@Override
|
||||
|
@ -149,6 +165,7 @@ public class JRummikub {
|
|||
|
||||
}
|
||||
});
|
||||
|
||||
view.getTablePanel().getStoneCollectionPanel().getRangeClickEvent()
|
||||
.add(new IListener2<Stone, Boolean>() {
|
||||
@Override
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package jrummikub.model;
|
||||
|
||||
/** Class specifing possible StoneColors */
|
||||
/** Class specifying possible StoneColors */
|
||||
public enum StoneColor {
|
||||
BLACK, ORANGE, BLUE, RED
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import java.util.Map;
|
|||
|
||||
import jrummikub.model.Position;
|
||||
import jrummikub.model.StoneSet;
|
||||
import jrummikub.util.Event1;
|
||||
|
||||
/**
|
||||
* The view of the table, where the stone sets lie
|
||||
|
@ -47,5 +48,21 @@ public interface ITablePanel extends IStonePanel, IClickable {
|
|||
*
|
||||
* @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();
|
||||
}
|
||||
|
|
|
@ -65,7 +65,9 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel,
|
|||
Stone stone = getStoneAt(pos);
|
||||
|
||||
if (stone == null) {
|
||||
if (!handleOtherClickEvent(pos))
|
||||
clickEvent.emit(pos);
|
||||
|
||||
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) {
|
||||
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());
|
||||
Rectangle2D rect = new Rectangle2D.Float(p.getX(), p.getY(), 1, 1);
|
||||
|
||||
if (rect.contains(pos.getX(), pos.getY()))
|
||||
return stone;
|
||||
|
|
|
@ -51,7 +51,7 @@ class StoneCollectionPanel extends AbstractStonePanel implements
|
|||
|
||||
for (Stone stone : selectedStones) {
|
||||
stones.put(stone, new Position(x, 0));
|
||||
x += stone.getWidth();
|
||||
x++;
|
||||
}
|
||||
|
||||
setStones(stones);
|
||||
|
|
|
@ -8,6 +8,7 @@ import java.awt.RenderingHints;
|
|||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import java.awt.event.ComponentListener;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
|
@ -19,6 +20,7 @@ import javax.swing.JLabel;
|
|||
import jrummikub.model.Position;
|
||||
import jrummikub.model.Stone;
|
||||
import jrummikub.model.StoneSet;
|
||||
import jrummikub.util.Event1;
|
||||
import jrummikub.view.IStoneCollectionPanel;
|
||||
import jrummikub.view.ITablePanel;
|
||||
|
||||
|
@ -31,6 +33,7 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
|
|||
HandPanel.class.getResource("/jrummikub/resource/felt.png"));
|
||||
|
||||
private final static float DEFAULT_SCALE = 1;
|
||||
private final static float CONNECTOR_WIDTH = 0.25f;
|
||||
private final int COLLECTION_GAP = 5;
|
||||
|
||||
private JLabel leftPlayerLabel, topPlayerLabel, rightPlayerLabel;
|
||||
|
@ -39,6 +42,9 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
|
|||
private Map<StoneSet, Position> stoneSets = Collections.emptyMap();
|
||||
private Collection<Stone> selectedStones = Collections.emptyList();
|
||||
|
||||
private Event1<StoneSet> leftConnectorClickEvent = new Event1<StoneSet>();
|
||||
private Event1<StoneSet> rightConnectorClickEvent = new Event1<StoneSet>();
|
||||
|
||||
@Override
|
||||
public void setLeftPlayerName(String playerName) {
|
||||
leftPlayerLabel.setText(playerName);
|
||||
|
@ -54,6 +60,16 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
|
|||
rightPlayerLabel.setText(playerName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Event1<StoneSet> getLeftConnectorClickEvent() {
|
||||
return leftConnectorClickEvent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Event1<StoneSet> getRightConnectorClickEvent() {
|
||||
return rightConnectorClickEvent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStoneSets(Map<StoneSet, Position> stoneSets) {
|
||||
Map<Stone, Position> stones = new HashMap<Stone, Position>();
|
||||
|
@ -63,7 +79,7 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
|
|||
|
||||
for (Stone stone : entry.getKey()) {
|
||||
stones.put(stone, new Position(x, y));
|
||||
x += stone.getWidth();
|
||||
x++;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -145,14 +161,38 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
|
|||
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) {
|
||||
float x = pos.getX();
|
||||
int width = getStonePainter().getStoneWidth(), height = getStonePainter()
|
||||
.getStoneHeight();
|
||||
|
||||
g.setColor(new Color(0, 0, 0, 0.25f));
|
||||
g.fillRect((int) (x * width) - width / 4, (int) (pos.getY() * height),
|
||||
width / 4, height);
|
||||
g.fillRect((int) (x * width - width * CONNECTOR_WIDTH),
|
||||
(int) (pos.getY() * height), (int) (width * CONNECTOR_WIDTH), height);
|
||||
|
||||
for (Stone stone : stoneSet) {
|
||||
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.fillRect((int) (x * width), (int) (pos.getY() * height), width / 4,
|
||||
height);
|
||||
g.fillRect((int) (x * width), (int) (pos.getY() * height),
|
||||
(int) (width * CONNECTOR_WIDTH), height);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -178,8 +218,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
|
|||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
for (Map.Entry<StoneSet, Position> stoneSet : stoneSets.entrySet()) {
|
||||
paintStoneSet(g, stoneSet.getKey(), stoneSet.getValue());
|
||||
for (Map.Entry<StoneSet, Position> entry : stoneSets.entrySet()) {
|
||||
paintStoneSet(g, entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue