Make StonePanel emit Stone click events

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@80 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-05-03 17:29:52 +02:00
parent 7edb66d4ff
commit bcc3f95847
11 changed files with 209 additions and 85 deletions

View file

@ -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<Position, Boolean>() {
.add(new IListener1<Position>() {
@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<Stone, Boolean>() {
@Override
public void fire(Stone s, Boolean collect) {
System.out.println("Hand clicked on " + s
+ (collect ? ", collect" : ""));
}
});
view.getPlayerPanel().getHandPanel().getRangeClickEvent()
.add(new IListener2<Position, Boolean>() {
.add(new IListener2<Stone, Boolean>() {
@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<Position, Boolean>() {
.add(new IListener2<Stone, Boolean>() {
@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<Position, Boolean>() {
@Override
public void fire(Position p, Boolean collect) {
System.out.println("Table clicked at " + p
+ (collect ? ", collect" : ""));
view.getTablePanel().getClickEvent()
.add(new IListener1<Position>() {
@Override
public void fire(Position p) {
System.out.println("Table clicked at " + p);
}
});
view.getTablePanel().getStoneClickEvent()
.add(new IListener2<Stone, Boolean>() {
@Override
public void fire(Stone s, Boolean collect) {
System.out.println("Table clicked on " + s
+ (collect ? ", collect" : ""));
}
});
}
});
view.getTablePanel().getRangeClickEvent()
.add(new IListener2<Position, Boolean>() {
.add(new IListener2<Stone, Boolean>() {
@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<Position, Boolean>() {
@Override
public void fire(Position p, Boolean collect) {
System.out.println("Table set-clicked at " + p
+ (collect ? ", collect" : ""));
}
});
view.getTablePanel().getStoneCollectionPanel().getClickEvent()
.add(new IListener2<Position, Boolean>() {
view.getTablePanel().getSetClickEvent()
.add(new IListener2<Stone, Boolean>() {
@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("Table set-clicked at " + s
+ (collect ? ", collect" : ""));
}
});
view.getTablePanel().getStoneCollectionPanel().getStoneClickEvent()
.add(new IListener2<Stone, Boolean>() {
@Override
public void fire(Stone s, Boolean collect) {
System.out.println("Collection clicked on " + s
+ (collect ? ", collect" : ""));
}
});
view.getTablePanel().getStoneCollectionPanel().getRangeClickEvent()
.add(new IListener2<Position, Boolean>() {
.add(new IListener2<Stone, Boolean>() {
@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<Position, Boolean>() {
.add(new IListener2<Stone, Boolean>() {
@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" : ""));
}

View file

@ -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 + "]";
}
}
}

View file

@ -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<Position, Boolean> 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<Position, Boolean> 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<Position, Boolean> getSetClickEvent();
public IEvent1<Position> getClickEvent();
}

View file

@ -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
*

View file

@ -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 {
}

View file

@ -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<Stone, Boolean> 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<Stone, Boolean> 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<Stone, Boolean> getSetClickEvent();
}

View file

@ -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
*

View file

@ -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;
}

View file

@ -39,7 +39,6 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
}
private BufferedImage scaledBackground = BACKGROUND;
private Map<Stone, Position> stones = Collections.emptyMap();
private Collection<Stone> 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<Stone, Position> entry : stones.entrySet()) {
for (Map.Entry<Stone, Position> 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<Stone, Position> stones) {
this.stones = stones;
super.setStones(stones);
repaint();
}

View file

@ -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<Stone> stones) {
selectedStones = stones;
void setSelectedStones(Collection<Stone> selectedStones) {
this.selectedStones = selectedStones;
if (stones.isEmpty()) {
Map<Stone, Position> stones = new HashMap<Stone, Position>();
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();

View file

@ -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<StoneSet, Position> stoneSets) {
Map<Stone, Position> stones = new HashMap<Stone, Position>();
for (Map.Entry<StoneSet, Position> 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();
}