From b8206243a7e6b0ecea7ec9cf568e9d686017c3f9 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 6 May 2011 03:39:34 +0200 Subject: Added hover effects git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@166 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/view/impl/AbstractStonePanel.java | 38 ++++++++++++++- src/jrummikub/view/impl/HandPanel.java | 2 +- src/jrummikub/view/impl/StoneCollectionPanel.java | 2 +- src/jrummikub/view/impl/StonePainter.java | 57 ++++++++++++++++++----- src/jrummikub/view/impl/TablePanel.java | 2 +- 5 files changed, 86 insertions(+), 15 deletions(-) (limited to 'src/jrummikub/view/impl') diff --git a/src/jrummikub/view/impl/AbstractStonePanel.java b/src/jrummikub/view/impl/AbstractStonePanel.java index 7210d1c..fd3862b 100644 --- a/src/jrummikub/view/impl/AbstractStonePanel.java +++ b/src/jrummikub/view/impl/AbstractStonePanel.java @@ -3,6 +3,7 @@ package jrummikub.view.impl; import java.awt.Insets; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.awt.event.MouseMotionAdapter; import java.awt.geom.Rectangle2D; import java.util.Collections; @@ -30,6 +31,7 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel, private Event2 setClickEvent = new Event2(); private Iterable> stones = Collections.emptySet(); + private Stone hoveredStone = null; /** * @return the stone painter @@ -52,7 +54,7 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel, * the grid scale */ public AbstractStonePanel(float scale) { - super(false); // Unset double buffered + super(true); // Set double buffered stonePainter = new StonePainter(scale); @@ -81,9 +83,43 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel, event.emit(stone, e.isControlDown()); } + + @Override + public void mouseExited(MouseEvent e) { + setHoveredStone(null); + } + }); + + addMouseMotionListener(new MouseMotionAdapter() { + @Override + public void mouseMoved(MouseEvent e) { + Insets insets = getInsets(); + Pair trans = getTranslation(); + Position pos = stonePainter.calculatePosition(e.getX() - insets.left + - trans.getFirst(), e.getY() - insets.top - trans.getSecond()); + + setHoveredStone(getStoneAt(pos)); + } }); } + private void setHoveredStone(Stone stone) { + Stone oldStone = hoveredStone; + hoveredStone = stone; + + if (oldStone != hoveredStone) + repaint(); + } + + /** + * Returns the stone the mouse pointer is hovering over + * + * @return the hovered stone + */ + protected Stone getHoveredStone() { + return hoveredStone; + } + /** * *Overwrite this method* to signal if special zone was clicked * diff --git a/src/jrummikub/view/impl/HandPanel.java b/src/jrummikub/view/impl/HandPanel.java index ab4caec..38a8440 100644 --- a/src/jrummikub/view/impl/HandPanel.java +++ b/src/jrummikub/view/impl/HandPanel.java @@ -100,7 +100,7 @@ class HandPanel extends AbstractStonePanel implements IHandPanel { for (Pair entry : getStones()) { getStonePainter().paintStone(g, entry.getFirst(), entry.getSecond(), - selectedStones.contains(entry.getFirst())); + selectedStones.contains(entry.getFirst()), entry.getFirst() == getHoveredStone()); } } diff --git a/src/jrummikub/view/impl/StoneCollectionPanel.java b/src/jrummikub/view/impl/StoneCollectionPanel.java index e4a9c94..5064b56 100644 --- a/src/jrummikub/view/impl/StoneCollectionPanel.java +++ b/src/jrummikub/view/impl/StoneCollectionPanel.java @@ -110,7 +110,7 @@ class StoneCollectionPanel extends AbstractStonePanel implements float xpos = 0; for (Stone stone : selectedStones) { - getStonePainter().paintStone(g, stone, new Position(xpos, 0), false); + 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 68ab72c..bfa61ea 100644 --- a/src/jrummikub/view/impl/StonePainter.java +++ b/src/jrummikub/view/impl/StonePainter.java @@ -31,12 +31,14 @@ class StonePainter { private static final float CIRCLE_WIDTH = 0.45f; private static final Color BACKGROUND_COLOR = new Color(0.9f, 0.9f, 0.6f); - private static final Color SELECTED_COLOR = BACKGROUND_COLOR.darker(); private static final float BRIGHTER_SCALE = 1.15f; + private static final float HOVER_RATIO = 0.7f; private Map> defaultStones; private Map> selectedStones; + private Map> hoveredStones; + private Map> hoveredSelectedStones; /** * The width of one pixel in the scale of 1.0 @@ -55,8 +57,16 @@ class StonePainter { private static Color brighter(Color color) { int r = (int) (color.getRed() * BRIGHTER_SCALE); - int g = (int) (color.getRed() * BRIGHTER_SCALE); - int b = (int) (color.getRed() * BRIGHTER_SCALE); + int g = (int) (color.getGreen() * BRIGHTER_SCALE); + int b = (int) (color.getBlue() * BRIGHTER_SCALE); + + return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b); + } + + 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)); return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b); } @@ -156,16 +166,25 @@ class StonePainter { private void prepaint() { defaultStones = new HashMap>(); selectedStones = new HashMap>(); + hoveredStones = new HashMap>(); + hoveredSelectedStones = new HashMap>(); Color defaultBackground = BACKGROUND_COLOR; - Color selectedBackground = SELECTED_COLOR; + Color selectedBackground = BACKGROUND_COLOR.darker(); + Color hoveredBackground = hover(defaultBackground); + Color hoveredSelectedBackground = hover(selectedBackground); for (StoneColor color : StoneColor.values()) { Color defaultFg = getColor(color); Color selectedFg = defaultFg.darker(); + Color hoveredFg = hover(defaultFg); + Color hoveredSelectedFg = hover(selectedFg); defaultStones.put(color, prepaintColor(defaultFg, defaultBackground)); selectedStones.put(color, prepaintColor(selectedFg, selectedBackground)); + hoveredStones.put(color, prepaintColor(hoveredFg, hoveredBackground)); + hoveredSelectedStones.put(color, + prepaintColor(hoveredSelectedFg, hoveredSelectedBackground)); } } @@ -303,21 +322,37 @@ class StonePainter { * the position of the stone * @param selected * if selected is true the stone will be painted darker + * @param hovered + * if hovered is true the stone will be painted brighter */ - public void paintStone(Graphics2D g, Stone stone, Position p, boolean selected) { + 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); + Map> stoneMap; + + if (selected) { + if (hovered) { + stoneMap = hoveredSelectedStones; + } else { + stoneMap = selectedStones; + } + } else { + if (hovered) { + stoneMap = hoveredStones; + } else { + stoneMap = defaultStones; + } + } + if (stone.isJoker()) { - g.drawImage( - (selected ? selectedStones : defaultStones).get(stone.getColor()) - .get(0), x, y, null); + g.drawImage(stoneMap.get(stone.getColor()).get(0), x, y, null); } else { - g.drawImage( - (selected ? selectedStones : defaultStones).get(stone.getColor()) - .get(stone.getValue()), x, y, null); + g.drawImage(stoneMap.get(stone.getColor()).get(stone.getValue()), x, y, + null); } } } diff --git a/src/jrummikub/view/impl/TablePanel.java b/src/jrummikub/view/impl/TablePanel.java index c118613..f34765e 100644 --- a/src/jrummikub/view/impl/TablePanel.java +++ b/src/jrummikub/view/impl/TablePanel.java @@ -255,7 +255,7 @@ class TablePanel extends AbstractStonePanel implements ITablePanel { for (Stone stone : stoneSet) { getStonePainter().paintStone(g, stone, new Position(x, pos.getY()), - selectedStones.contains(stone)); + selectedStones.contains(stone), stone == getHoveredStone()); x++; } -- cgit v1.2.3