Added hover effects

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@166 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-05-06 03:39:34 +02:00
parent 093e156018
commit b8206243a7
5 changed files with 87 additions and 16 deletions

View file

@ -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<Stone, Boolean> setClickEvent = new Event2<Stone, Boolean>();
private Iterable<Pair<Stone, Position>> 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,7 +83,41 @@ 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<Integer, Integer> 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;
}
/**

View file

@ -100,7 +100,7 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
for (Pair<Stone, Position> entry : getStones()) {
getStonePainter().paintStone(g, entry.getFirst(), entry.getSecond(),
selectedStones.contains(entry.getFirst()));
selectedStones.contains(entry.getFirst()), entry.getFirst() == getHoveredStone());
}
}

View file

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

View file

@ -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<StoneColor, Map<Integer, BufferedImage>> defaultStones;
private Map<StoneColor, Map<Integer, BufferedImage>> selectedStones;
private Map<StoneColor, Map<Integer, BufferedImage>> hoveredStones;
private Map<StoneColor, Map<Integer, BufferedImage>> 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<StoneColor, Map<Integer, BufferedImage>>();
selectedStones = new HashMap<StoneColor, Map<Integer, BufferedImage>>();
hoveredStones = new HashMap<StoneColor, Map<Integer, BufferedImage>>();
hoveredSelectedStones = new HashMap<StoneColor, Map<Integer, BufferedImage>>();
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);
if (stone.isJoker()) {
g.drawImage(
(selected ? selectedStones : defaultStones).get(stone.getColor())
.get(0), x, y, null);
Map<StoneColor, Map<Integer, BufferedImage>> stoneMap;
if (selected) {
if (hovered) {
stoneMap = hoveredSelectedStones;
} else {
stoneMap = selectedStones;
}
} else {
g.drawImage(
(selected ? selectedStones : defaultStones).get(stone.getColor())
.get(stone.getValue()), x, y, null);
if (hovered) {
stoneMap = hoveredStones;
} else {
stoneMap = defaultStones;
}
}
if (stone.isJoker()) {
g.drawImage(stoneMap.get(stone.getColor()).get(0), x, y, null);
} else {
g.drawImage(stoneMap.get(stone.getColor()).get(stone.getValue()), x, y,
null);
}
}
}

View file

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