Add click effect to opague and selectable FLabels to make them feel more like buttons

This commit is contained in:
drdev
2013-10-30 14:40:36 +00:00
parent e4b91416dd
commit a66e7ad14f

View File

@@ -263,7 +263,7 @@ public class FLabel extends JLabel implements ILocalRepaint {
private double iconScaleFactor; private double iconScaleFactor;
private int fontStyle, iconAlignX; private int fontStyle, iconAlignX;
private int iw, ih; private int iw, ih;
private boolean selectable, selected, hoverable, hovered, opaque, private boolean selectable, selected, hoverable, hovered, pressed, opaque,
iconInBackground, iconScaleAuto, reactOnMouseDown; iconInBackground, iconScaleAuto, reactOnMouseDown;
private Point iconInsets; private Point iconInsets;
@@ -307,16 +307,12 @@ public class FLabel extends JLabel implements ILocalRepaint {
private void _doMouseAction() { private void _doMouseAction() {
if (selectable) { setSelected(!selected); } if (selectable) { setSelected(!selected); }
if (cmdClick != null && isEnabled()) { if (cmdClick != null && isEnabled()) {
hovered = false;
repaintSelf();
cmdClick.run(); cmdClick.run();
} }
} }
private void _doRightClickAction() { private void _doRightClickAction() {
if (cmdRightClick != null && isEnabled()) { if (cmdRightClick != null && isEnabled()) {
hovered = false;
repaintSelf();
cmdRightClick.run(); cmdRightClick.run();
} }
} }
@@ -338,8 +334,16 @@ public class FLabel extends JLabel implements ILocalRepaint {
@Override @Override
public void onLeftMouseDown(MouseEvent e) { public void onLeftMouseDown(MouseEvent e) {
if (reactOnMouseDown) { if (reactOnMouseDown) {
_doMouseAction(); _doMouseAction(); //for best responsiveness, do action before repainting for pressed state
} }
pressed = true;
repaintSelf();
}
@Override
public void onLeftMouseUp(MouseEvent e) {
pressed = false;
repaintSelf();
} }
@Override @Override
@@ -501,8 +505,12 @@ public class FLabel extends JLabel implements ILocalRepaint {
if (hoverable) { if (hoverable) {
g2d.setComposite(paintWithHover ? alphaStrong : alphaDim); g2d.setComposite(paintWithHover ? alphaStrong : alphaDim);
} }
if (opaque) { boolean paintPressedState = pressed && hovered && isEnabled() && (opaque || selectable);
if (paintPressedState) {
paintPressed(g2d, w, h);
}
else if (opaque) {
if (selected) { if (selected) {
paintDown(g2d, w, h); paintDown(g2d, w, h);
} else { } else {
@@ -516,6 +524,10 @@ public class FLabel extends JLabel implements ILocalRepaint {
} }
} }
if (paintPressedState) { //while pressed, translate graphics so icon and text appear shifted down and to the right
g2d.translate(1, 1);
}
// Icon in background // Icon in background
if (iconInBackground) { if (iconInBackground) {
int sh = (int) (h * iconScaleFactor); int sh = (int) (h * iconScaleFactor);
@@ -531,6 +543,10 @@ public class FLabel extends JLabel implements ILocalRepaint {
} }
super.paintComponent(g); super.paintComponent(g);
if (paintPressedState) { //reset translation after icon and text painted
g2d.translate(-1, -1);
}
if (hoverable) { if (hoverable) {
g2d.setComposite(oldComp); g2d.setComposite(oldComp);
@@ -547,6 +563,16 @@ public class FLabel extends JLabel implements ILocalRepaint {
skin.setGraphicsColor(g, l30); skin.setGraphicsColor(g, l30);
g.drawRect(1, 1, w - 4, h - 4); g.drawRect(1, 1, w - 4, h - 4);
} }
private void paintPressed(final Graphics2D g, int w, int h) {
skin.setGraphicsGradientPaint(g, 0, h, d50, 0, 0, d10);
g.fillRect(0, 0, w - 1, h - 1);
skin.setGraphicsColor(g, d50);
g.drawRect(0, 0, w - 2, h - 2);
skin.setGraphicsColor(g, d10);
g.drawRect(1, 1, w - 4, h - 4);
}
private void paintUp(final Graphics2D g, int w, int h) { private void paintUp(final Graphics2D g, int w, int h) {
skin.setGraphicsGradientPaint(g, 0, h, d10, 0, 0, l20); skin.setGraphicsGradientPaint(g, 0, h, d10, 0, 0, l20);