Handle click events next to the collection as click events for the table
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@168 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
706406ed6f
commit
4390c76630
5 changed files with 95 additions and 33 deletions
|
@ -17,7 +17,8 @@ public class JRummikub {
|
||||||
/**
|
/**
|
||||||
* The main method
|
* The main method
|
||||||
*
|
*
|
||||||
* @param args command line arguments
|
* @param args
|
||||||
|
* command line arguments
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String nativeLF = UIManager.getSystemLookAndFeelClassName();
|
String nativeLF = UIManager.getSystemLookAndFeelClassName();
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package jrummikub.view.impl;
|
package jrummikub.view.impl;
|
||||||
|
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
|
import java.awt.Point;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.awt.event.MouseMotionAdapter;
|
import java.awt.event.MouseMotionAdapter;
|
||||||
|
@ -61,27 +62,8 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel,
|
||||||
addMouseListener(new MouseAdapter() {
|
addMouseListener(new MouseAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent e) {
|
public void mouseClicked(MouseEvent e) {
|
||||||
Insets insets = getInsets();
|
clickAt(e.getPoint(), e.getClickCount(), e.isShiftDown(),
|
||||||
Pair<Integer, Integer> trans = getTranslation();
|
e.isControlDown());
|
||||||
Position pos = stonePainter.calculatePosition(e.getX() - insets.left
|
|
||||||
- trans.getFirst(), e.getY() - insets.top - trans.getSecond());
|
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -103,6 +85,39 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* clickAt is called when a click has occured
|
||||||
|
*
|
||||||
|
* @param p the point in component coordinates
|
||||||
|
* @param clickCount the click count
|
||||||
|
* @param shift is shift down?
|
||||||
|
* @param control is control down?
|
||||||
|
*/
|
||||||
|
protected void clickAt(Point p, int clickCount, boolean shift, boolean control) {
|
||||||
|
Insets insets = getInsets();
|
||||||
|
Pair<Integer, Integer> trans = getTranslation();
|
||||||
|
Position pos = stonePainter.calculatePosition(
|
||||||
|
p.x - insets.left - trans.getFirst(),
|
||||||
|
p.y - insets.top - trans.getSecond());
|
||||||
|
Stone stone = getStoneAt(pos);
|
||||||
|
|
||||||
|
if (stone == null) {
|
||||||
|
if (!handleOtherClickEvent(pos))
|
||||||
|
clickEvent.emit(pos);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Event2<Stone, Boolean> event = stoneClickEvent;
|
||||||
|
|
||||||
|
if (shift)
|
||||||
|
event = rangeClickEvent;
|
||||||
|
else if (clickCount >= 2)
|
||||||
|
event = setClickEvent;
|
||||||
|
|
||||||
|
event.emit(stone, control);
|
||||||
|
}
|
||||||
|
|
||||||
private void setHoveredStone(Stone stone) {
|
private void setHoveredStone(Stone stone) {
|
||||||
Stone oldStone = hoveredStone;
|
Stone oldStone = hoveredStone;
|
||||||
hoveredStone = stone;
|
hoveredStone = stone;
|
||||||
|
|
|
@ -2,6 +2,8 @@ package jrummikub.view.impl;
|
||||||
|
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.Insets;
|
||||||
|
import java.awt.Point;
|
||||||
import java.awt.event.ComponentAdapter;
|
import java.awt.event.ComponentAdapter;
|
||||||
import java.awt.event.ComponentEvent;
|
import java.awt.event.ComponentEvent;
|
||||||
import java.awt.geom.RoundRectangle2D;
|
import java.awt.geom.RoundRectangle2D;
|
||||||
|
@ -14,6 +16,8 @@ import javax.swing.ImageIcon;
|
||||||
|
|
||||||
import jrummikub.model.Position;
|
import jrummikub.model.Position;
|
||||||
import jrummikub.model.Stone;
|
import jrummikub.model.Stone;
|
||||||
|
import jrummikub.util.Event1;
|
||||||
|
import jrummikub.util.IEvent1;
|
||||||
import jrummikub.util.Pair;
|
import jrummikub.util.Pair;
|
||||||
import jrummikub.view.IStoneCollectionPanel;
|
import jrummikub.view.IStoneCollectionPanel;
|
||||||
|
|
||||||
|
@ -31,6 +35,8 @@ class StoneCollectionPanel extends AbstractStonePanel implements
|
||||||
|
|
||||||
private Collection<Stone> selectedStones = Collections.emptyList();
|
private Collection<Stone> selectedStones = Collections.emptyList();
|
||||||
|
|
||||||
|
private Event1<Point> otherClickEvent = new Event1<Point>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new StoneCollection instance
|
* Creates a new StoneCollection instance
|
||||||
*/
|
*/
|
||||||
|
@ -81,6 +87,32 @@ class StoneCollectionPanel extends AbstractStonePanel implements
|
||||||
return new Pair<Integer, Integer>(x + inset, inset);
|
return new Pair<Integer, Integer>(x + inset, inset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The other click event is emitted by the stone collection when the player
|
||||||
|
* has clicked on it, but hasn't hit a stone. This is rather probable as the
|
||||||
|
* stone collection panel is filling the whole table width while being mostly
|
||||||
|
* invisible.
|
||||||
|
*
|
||||||
|
* @return the event
|
||||||
|
*/
|
||||||
|
IEvent1<Point> getOtherClickEvent() {
|
||||||
|
return otherClickEvent;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean handleOtherClickEvent(Position pos) {
|
||||||
|
Insets insets = getInsets();
|
||||||
|
Pair<Integer, Integer> trans = getTranslation();
|
||||||
|
int x = (int) (pos.getX() * getStonePainter().getStoneWidth())
|
||||||
|
+ insets.left + trans.getFirst();
|
||||||
|
int y = (int) (pos.getY() * getStonePainter().getStoneHeight())
|
||||||
|
+ insets.top + trans.getSecond();
|
||||||
|
|
||||||
|
otherClickEvent.emit(new Point(x, y));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void paintComponent(Graphics g1) {
|
public void paintComponent(Graphics g1) {
|
||||||
for (int xpos = 0; xpos < getWidth(); xpos += BACKGROUND.getIconWidth()) {
|
for (int xpos = 0; xpos < getWidth(); xpos += BACKGROUND.getIconWidth()) {
|
||||||
|
@ -110,7 +142,8 @@ class StoneCollectionPanel extends AbstractStonePanel implements
|
||||||
float xpos = 0;
|
float xpos = 0;
|
||||||
|
|
||||||
for (Stone stone : selectedStones) {
|
for (Stone stone : selectedStones) {
|
||||||
getStonePainter().paintStone(g, stone, new Position(xpos, 0), false, stone == getHoveredStone());
|
getStonePainter().paintStone(g, stone, new Position(xpos, 0), false,
|
||||||
|
stone == getHoveredStone());
|
||||||
xpos++;
|
xpos++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -327,7 +327,6 @@ class StonePainter {
|
||||||
*/
|
*/
|
||||||
public void paintStone(Graphics2D g, Stone stone, Position p,
|
public void paintStone(Graphics2D g, Stone stone, Position p,
|
||||||
boolean selected, boolean hovered) {
|
boolean selected, boolean hovered) {
|
||||||
// Color background = selected ? SELECTED_COLOR : BACKGROUND_COLOR;
|
|
||||||
int width = getStoneWidth();
|
int width = getStoneWidth();
|
||||||
int height = getStoneHeight();
|
int height = getStoneHeight();
|
||||||
int x = (int) (p.getX() * width), y = (int) (p.getY() * height);
|
int x = (int) (p.getX() * width), y = (int) (p.getY() * height);
|
||||||
|
|
|
@ -4,6 +4,7 @@ import java.awt.Color;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
|
import java.awt.Point;
|
||||||
import java.awt.Shape;
|
import java.awt.Shape;
|
||||||
import java.awt.event.ComponentAdapter;
|
import java.awt.event.ComponentAdapter;
|
||||||
import java.awt.event.ComponentEvent;
|
import java.awt.event.ComponentEvent;
|
||||||
|
@ -17,11 +18,13 @@ import java.util.List;
|
||||||
|
|
||||||
import javax.swing.ImageIcon;
|
import javax.swing.ImageIcon;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
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.util.Event1;
|
||||||
|
import jrummikub.util.IListener1;
|
||||||
import jrummikub.util.Pair;
|
import jrummikub.util.Pair;
|
||||||
import jrummikub.view.IStoneCollectionPanel;
|
import jrummikub.view.IStoneCollectionPanel;
|
||||||
import jrummikub.view.ITablePanel;
|
import jrummikub.view.ITablePanel;
|
||||||
|
@ -114,12 +117,7 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private void createLabels() {
|
||||||
* Creates a new Table instance
|
|
||||||
*/
|
|
||||||
TablePanel() {
|
|
||||||
setLayout(null);
|
|
||||||
|
|
||||||
leftPlayerLabel = new JLabel();
|
leftPlayerLabel = new JLabel();
|
||||||
leftPlayerLabel.setForeground(Color.WHITE);
|
leftPlayerLabel.setForeground(Color.WHITE);
|
||||||
leftPlayerLabel.setHorizontalAlignment(JLabel.LEFT);
|
leftPlayerLabel.setHorizontalAlignment(JLabel.LEFT);
|
||||||
|
@ -139,8 +137,24 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
|
||||||
rightPlayerLabel.setHorizontalAlignment(JLabel.RIGHT);
|
rightPlayerLabel.setHorizontalAlignment(JLabel.RIGHT);
|
||||||
rightPlayerLabel.setHorizontalTextPosition(JLabel.RIGHT);
|
rightPlayerLabel.setHorizontalTextPosition(JLabel.RIGHT);
|
||||||
add(rightPlayerLabel);
|
add(rightPlayerLabel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new Table instance
|
||||||
|
*/
|
||||||
|
TablePanel() {
|
||||||
|
setLayout(null);
|
||||||
|
|
||||||
|
createLabels();
|
||||||
|
|
||||||
stoneCollection = new StoneCollectionPanel();
|
stoneCollection = new StoneCollectionPanel();
|
||||||
|
stoneCollection.getOtherClickEvent().add(new IListener1<Point>() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(Point p) {
|
||||||
|
Point p2 = SwingUtilities.convertPoint(stoneCollection, p, TablePanel.this);
|
||||||
|
clickAt(p2, 1, false, false);
|
||||||
|
}});
|
||||||
add(stoneCollection);
|
add(stoneCollection);
|
||||||
|
|
||||||
addComponentListener(new ComponentAdapter() {
|
addComponentListener(new ComponentAdapter() {
|
||||||
|
|
Reference in a new issue