diff options
-rw-r--r-- | src/jrummikub/JRummikub.java | 3 | ||||
-rw-r--r-- | src/jrummikub/view/impl/AbstractStonePanel.java | 57 | ||||
-rw-r--r-- | src/jrummikub/view/impl/StoneCollectionPanel.java | 35 | ||||
-rw-r--r-- | src/jrummikub/view/impl/StonePainter.java | 7 | ||||
-rw-r--r-- | src/jrummikub/view/impl/TablePanel.java | 26 |
5 files changed, 95 insertions, 33 deletions
diff --git a/src/jrummikub/JRummikub.java b/src/jrummikub/JRummikub.java index 4400be7..a913e2a 100644 --- a/src/jrummikub/JRummikub.java +++ b/src/jrummikub/JRummikub.java @@ -17,7 +17,8 @@ public class JRummikub { /** * The main method * - * @param args command line arguments + * @param args + * command line arguments */ public static void main(String[] args) { String nativeLF = UIManager.getSystemLookAndFeelClassName(); diff --git a/src/jrummikub/view/impl/AbstractStonePanel.java b/src/jrummikub/view/impl/AbstractStonePanel.java index fd3862b..82bc7b7 100644 --- a/src/jrummikub/view/impl/AbstractStonePanel.java +++ b/src/jrummikub/view/impl/AbstractStonePanel.java @@ -1,6 +1,7 @@ package jrummikub.view.impl; import java.awt.Insets; +import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; @@ -61,27 +62,8 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel, addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { - Insets insets = getInsets(); - Pair<Integer, Integer> trans = getTranslation(); - 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()); + clickAt(e.getPoint(), e.getClickCount(), e.isShiftDown(), + e.isControlDown()); } @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) { Stone oldStone = hoveredStone; hoveredStone = stone; diff --git a/src/jrummikub/view/impl/StoneCollectionPanel.java b/src/jrummikub/view/impl/StoneCollectionPanel.java index 5064b56..3bd57ea 100644 --- a/src/jrummikub/view/impl/StoneCollectionPanel.java +++ b/src/jrummikub/view/impl/StoneCollectionPanel.java @@ -2,6 +2,8 @@ package jrummikub.view.impl; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.Insets; +import java.awt.Point; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.geom.RoundRectangle2D; @@ -14,6 +16,8 @@ import javax.swing.ImageIcon; import jrummikub.model.Position; import jrummikub.model.Stone; +import jrummikub.util.Event1; +import jrummikub.util.IEvent1; import jrummikub.util.Pair; import jrummikub.view.IStoneCollectionPanel; @@ -31,6 +35,8 @@ class StoneCollectionPanel extends AbstractStonePanel implements private Collection<Stone> selectedStones = Collections.emptyList(); + private Event1<Point> otherClickEvent = new Event1<Point>(); + /** * Creates a new StoneCollection instance */ @@ -81,6 +87,32 @@ class StoneCollectionPanel extends AbstractStonePanel implements 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 public void paintComponent(Graphics g1) { for (int xpos = 0; xpos < getWidth(); xpos += BACKGROUND.getIconWidth()) { @@ -110,7 +142,8 @@ class StoneCollectionPanel extends AbstractStonePanel implements float xpos = 0; 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++; } } diff --git a/src/jrummikub/view/impl/StonePainter.java b/src/jrummikub/view/impl/StonePainter.java index bfa61ea..0812beb 100644 --- a/src/jrummikub/view/impl/StonePainter.java +++ b/src/jrummikub/view/impl/StonePainter.java @@ -64,9 +64,9 @@ class StonePainter { } private static Color hover(Color color) { - int r = (int) (color.getRed() * HOVER_RATIO + 255 * (1-HOVER_RATIO)); - int g = (int) (color.getGreen() * HOVER_RATIO + 255 * (1-HOVER_RATIO)); - int b = (int) (color.getBlue() * HOVER_RATIO + 255 * (1-HOVER_RATIO)); + int r = (int) (color.getRed() * HOVER_RATIO + 255 * (1 - HOVER_RATIO)); + int g = (int) (color.getGreen() * HOVER_RATIO + 255 * (1 - HOVER_RATIO)); + int b = (int) (color.getBlue() * HOVER_RATIO + 255 * (1 - HOVER_RATIO)); return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b); } @@ -327,7 +327,6 @@ class StonePainter { */ public void paintStone(Graphics2D g, Stone stone, Position p, boolean selected, boolean hovered) { - // Color background = selected ? SELECTED_COLOR : BACKGROUND_COLOR; int width = getStoneWidth(); int height = getStoneHeight(); int x = (int) (p.getX() * width), y = (int) (p.getY() * height); diff --git a/src/jrummikub/view/impl/TablePanel.java b/src/jrummikub/view/impl/TablePanel.java index f34765e..7eb798d 100644 --- a/src/jrummikub/view/impl/TablePanel.java +++ b/src/jrummikub/view/impl/TablePanel.java @@ -4,6 +4,7 @@ import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; +import java.awt.Point; import java.awt.Shape; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; @@ -17,11 +18,13 @@ import java.util.List; import javax.swing.ImageIcon; import javax.swing.JLabel; +import javax.swing.SwingUtilities; import jrummikub.model.Position; import jrummikub.model.Stone; import jrummikub.model.StoneSet; import jrummikub.util.Event1; +import jrummikub.util.IListener1; import jrummikub.util.Pair; import jrummikub.view.IStoneCollectionPanel; import jrummikub.view.ITablePanel; @@ -114,12 +117,7 @@ class TablePanel extends AbstractStonePanel implements ITablePanel { repaint(); } - /** - * Creates a new Table instance - */ - TablePanel() { - setLayout(null); - + private void createLabels() { leftPlayerLabel = new JLabel(); leftPlayerLabel.setForeground(Color.WHITE); leftPlayerLabel.setHorizontalAlignment(JLabel.LEFT); @@ -139,8 +137,24 @@ class TablePanel extends AbstractStonePanel implements ITablePanel { rightPlayerLabel.setHorizontalAlignment(JLabel.RIGHT); rightPlayerLabel.setHorizontalTextPosition(JLabel.RIGHT); add(rightPlayerLabel); + } + + /** + * Creates a new Table instance + */ + TablePanel() { + setLayout(null); + + createLabels(); 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); addComponentListener(new ComponentAdapter() { |