Discriminate between different click events
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@44 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
5e7c489921
commit
54271eb86d
7 changed files with 124 additions and 70 deletions
|
@ -13,7 +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;
|
||||
|
||||
public class JRummikub {
|
||||
|
@ -66,19 +66,41 @@ public class JRummikub {
|
|||
|
||||
view.getPlayerPanel().getBoard().setStones(stones);
|
||||
|
||||
view.getPlayerPanel().getBoard().getClickEvent().add(new IListener1<Position>(){
|
||||
|
||||
view.getPlayerPanel().getBoard().getClickEvent().add(new IListener2<Position, Boolean>(){
|
||||
@Override
|
||||
public void fire(Position value) {
|
||||
System.out.println("Board clicked at "+value);
|
||||
public void fire(Position p, Boolean collect) {
|
||||
System.out.println("Board clicked at "+p+(collect?", collect":""));
|
||||
|
||||
}});
|
||||
view.getPlayerPanel().getBoard().getRangeClickEvent().add(new IListener2<Position, Boolean>(){
|
||||
@Override
|
||||
public void fire(Position p, Boolean collect) {
|
||||
System.out.println("Board range-clicked at "+p+(collect?", collect":""));
|
||||
|
||||
}});
|
||||
view.getPlayerPanel().getBoard().getSetClickEvent().add(new IListener2<Position, Boolean>(){
|
||||
@Override
|
||||
public void fire(Position p, Boolean collect) {
|
||||
System.out.println("Board set-clicked at "+p+(collect?", collect":""));
|
||||
|
||||
}});
|
||||
|
||||
view.getTable().getClickEvent().add(new IListener1<Position>(){
|
||||
|
||||
view.getTable().getClickEvent().add(new IListener2<Position, Boolean>(){
|
||||
@Override
|
||||
public void fire(Position value) {
|
||||
System.out.println("Table clicked at "+value);
|
||||
public void fire(Position p, Boolean collect) {
|
||||
System.out.println("Table clicked at "+p+(collect?", collect":""));
|
||||
|
||||
}});
|
||||
view.getTable().getRangeClickEvent().add(new IListener2<Position, Boolean>(){
|
||||
@Override
|
||||
public void fire(Position p, Boolean collect) {
|
||||
System.out.println("Table range-clicked at "+p+(collect?", collect":""));
|
||||
|
||||
}});
|
||||
view.getTable().getSetClickEvent().add(new IListener2<Position, Boolean>(){
|
||||
@Override
|
||||
public void fire(Position p, Boolean collect) {
|
||||
System.out.println("Table set-clicked at "+p+(collect?", collect":""));
|
||||
|
||||
}});
|
||||
|
||||
|
|
|
@ -4,10 +4,7 @@ import java.util.Map;
|
|||
|
||||
import jrummikub.model.Position;
|
||||
import jrummikub.model.Stone;
|
||||
import jrummikub.util.IEvent1;
|
||||
|
||||
public interface IBoard {
|
||||
public interface IBoard extends IClickable {
|
||||
public void setStones(Map<Stone, Position> stones);
|
||||
|
||||
public IEvent1<Position> getClickEvent();
|
||||
}
|
||||
|
|
10
src/jrummikub/view/IClickable.java
Normal file
10
src/jrummikub/view/IClickable.java
Normal file
|
@ -0,0 +1,10 @@
|
|||
package jrummikub.view;
|
||||
|
||||
import jrummikub.model.Position;
|
||||
import jrummikub.util.IEvent2;
|
||||
|
||||
public interface IClickable {
|
||||
public IEvent2<Position, Boolean> getClickEvent();
|
||||
public IEvent2<Position, Boolean> getRangeClickEvent();
|
||||
public IEvent2<Position, Boolean> getSetClickEvent();
|
||||
}
|
|
@ -4,14 +4,11 @@ import java.util.Map;
|
|||
|
||||
import jrummikub.model.Position;
|
||||
import jrummikub.model.StoneSet;
|
||||
import jrummikub.util.IEvent1;
|
||||
|
||||
public interface ITable {
|
||||
public interface ITable extends IClickable {
|
||||
public void setLeftPlayerName(String playerName);
|
||||
public void setTopPlayerName(String playerName);
|
||||
public void setRightPlayerName(String playerName);
|
||||
|
||||
public void setStoneSets(Map<StoneSet, Position> stoneSets);
|
||||
|
||||
public IEvent1<Position> getClickEvent();
|
||||
}
|
|
@ -5,38 +5,30 @@ import java.awt.Graphics;
|
|||
import java.awt.Graphics2D;
|
||||
import java.awt.Insets;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import jrummikub.model.Position;
|
||||
import jrummikub.model.Stone;
|
||||
import jrummikub.util.Event1;
|
||||
import jrummikub.util.IEvent1;
|
||||
import jrummikub.view.IBoard;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class Board extends JPanel implements IBoard {
|
||||
private final static ImageIcon background = new ImageIcon(Board.class.getResource("/jrummikub/resource/wood.png"));
|
||||
|
||||
public class Board extends StonePanel implements IBoard {
|
||||
private final static ImageIcon BACKGROUND = new ImageIcon(Board.class.getResource("/jrummikub/resource/wood.png"));
|
||||
private final static float DEFAULT_SCALE = StonePainter.BOARD_SCALE;
|
||||
|
||||
private Map<Stone, Position> stones = Collections.emptyMap();
|
||||
private Collection<Stone> selectedStones = Collections.emptyList();
|
||||
|
||||
private StonePainter stonePainter = new StonePainter(StonePainter.BOARD_SCALE);
|
||||
|
||||
private Event1<Position> clickEvent = new Event1<Position>();
|
||||
|
||||
Board() {
|
||||
super(true); // set double buffered
|
||||
super(DEFAULT_SCALE);
|
||||
|
||||
setBorder(new CustomBorder(Color.DARK_GRAY, 0, 1, 0, 1));
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
/*addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
Insets insets = getInsets();
|
||||
|
@ -44,12 +36,7 @@ public class Board extends JPanel implements IBoard {
|
|||
clickEvent.fire(stonePainter.calculatePosition(e.getX() - insets.left,
|
||||
e.getY() - insets.top));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEvent1<Position> getClickEvent() {
|
||||
return clickEvent;
|
||||
});*/
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -58,18 +45,18 @@ public class Board extends JPanel implements IBoard {
|
|||
int x = insets.left, y = insets.top, width = getWidth()-insets.left-insets.right, height = getHeight()-insets.top-insets.bottom;
|
||||
Graphics2D g = (Graphics2D)g1.create(x, y, width, height);
|
||||
|
||||
for(int xpos = 0; xpos < width; xpos += background.getIconWidth()) {
|
||||
background.paintIcon(this, g, xpos, 0);
|
||||
for(int xpos = 0; xpos < width; xpos += BACKGROUND.getIconWidth()) {
|
||||
BACKGROUND.paintIcon(this, g, xpos, 0);
|
||||
}
|
||||
for(int xpos = -32; xpos < width; xpos += background.getIconWidth()) {
|
||||
background.paintIcon(this, g, xpos, 75);
|
||||
for(int xpos = -32; xpos < width; xpos += BACKGROUND.getIconWidth()) {
|
||||
BACKGROUND.paintIcon(this, g, xpos, 75);
|
||||
}
|
||||
|
||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
for (Map.Entry<Stone, Position> entry : stones.entrySet()) {
|
||||
stonePainter.paintStone(g, entry.getKey(), entry.getValue(),
|
||||
getStonePainter().paintStone(g, entry.getKey(), entry.getValue(),
|
||||
selectedStones.contains(entry.getKey()));
|
||||
}
|
||||
}
|
||||
|
|
62
src/jrummikub/view/impl/StonePanel.java
Normal file
62
src/jrummikub/view/impl/StonePanel.java
Normal file
|
@ -0,0 +1,62 @@
|
|||
package jrummikub.view.impl;
|
||||
|
||||
import java.awt.Insets;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import jrummikub.model.Position;
|
||||
import jrummikub.util.Event2;
|
||||
import jrummikub.view.IClickable;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
abstract class StonePanel extends JPanel implements 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>();
|
||||
|
||||
protected StonePainter getStonePainter() {
|
||||
return stonePainter;
|
||||
}
|
||||
|
||||
public StonePanel(float scale) {
|
||||
super(true); // Set double buffered
|
||||
|
||||
stonePainter = new StonePainter(scale);
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
Insets insets = getInsets();
|
||||
Event2<Position, Boolean> event = clickEvent;
|
||||
|
||||
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());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Event2<Position, Boolean> getClickEvent() {
|
||||
return clickEvent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Event2<Position, Boolean> getRangeClickEvent() {
|
||||
return rangeClickEvent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Event2<Position, Boolean> getSetClickEvent() {
|
||||
return setClickEvent;
|
||||
}
|
||||
|
||||
}
|
|
@ -4,10 +4,7 @@ import java.awt.BorderLayout;
|
|||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Insets;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
@ -19,26 +16,22 @@ import javax.swing.JPanel;
|
|||
import jrummikub.model.Position;
|
||||
import jrummikub.model.Stone;
|
||||
import jrummikub.model.StoneSet;
|
||||
import jrummikub.util.Event1;
|
||||
import jrummikub.util.IEvent1;
|
||||
import jrummikub.view.ITable;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class Table extends JPanel implements ITable {
|
||||
public class Table extends StonePanel implements ITable {
|
||||
private final static ImageIcon background = new ImageIcon(
|
||||
Board.class.getResource("/jrummikub/resource/felt.png"));
|
||||
|
||||
private final static float DEFAULT_SCALE = 1;
|
||||
|
||||
private JLabel leftPlayerLabel, topPlayerLabel, rightPlayerLabel;
|
||||
private JPanel innerPanel;
|
||||
|
||||
private StonePainter stonePainter = new StonePainter(1);
|
||||
|
||||
private StonePainter selectedStonePainter = new StonePainter(1.2f);
|
||||
|
||||
private Map<StoneSet, Position> stoneSets = Collections.emptyMap();
|
||||
private Collection<Stone> selectedStones = Collections.emptyList();
|
||||
|
||||
private Event1<Position> clickEvent = new Event1<Position>();
|
||||
|
||||
|
||||
@Override
|
||||
public void setLeftPlayerName(String playerName) {
|
||||
|
@ -65,14 +58,10 @@ public class Table extends JPanel implements ITable {
|
|||
selectedStones = stones;
|
||||
repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEvent1<Position> getClickEvent() {
|
||||
return clickEvent;
|
||||
}
|
||||
|
||||
Table() {
|
||||
super(true); // set double buffered
|
||||
super(DEFAULT_SCALE);
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
leftPlayerLabel = new JLabel();
|
||||
|
@ -92,23 +81,13 @@ public class Table extends JPanel implements ITable {
|
|||
innerPanel = new JPanel();
|
||||
innerPanel.setOpaque(false);
|
||||
add(innerPanel, BorderLayout.CENTER);
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
Insets insets = getInsets();
|
||||
|
||||
clickEvent.fire(stonePainter.calculatePosition(e.getX() - insets.left,
|
||||
e.getY() - insets.top));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos) {
|
||||
float x = pos.getX();
|
||||
|
||||
for (Stone stone : stoneSet) {
|
||||
stonePainter.paintStone(g, stone, new Position(x, pos.getY()),
|
||||
getStonePainter().paintStone(g, stone, new Position(x, pos.getY()),
|
||||
selectedStones.contains(stone));
|
||||
x++;
|
||||
}
|
||||
|
|
Reference in a new issue