Rename pressing to tinting. Change to behavior when leaving the rect will remove the tint (would stop tinting only when button released).

This commit is contained in:
John Hitchings 2019-03-10 23:12:48 -07:00
parent 11b93d0771
commit 5c01b38ed6
2 changed files with 18 additions and 7 deletions

View file

@ -15,7 +15,7 @@ public class KeyboundButton extends JButton {
private final String text; private final String text;
private static final Font keyFont = new Font(Font.SANS_SERIF, Font.BOLD, 13); private static final Font keyFont = new Font(Font.SANS_SERIF, Font.BOLD, 13);
private boolean pressing = false; private boolean tinting = false;
public KeyboundButton(String key) { public KeyboundButton(String key) {
text = PreferencesDialog.getCachedKeyText(key); text = PreferencesDialog.getCachedKeyText(key);
@ -27,11 +27,11 @@ public class KeyboundButton extends JButton {
Graphics sg = g.create(); Graphics sg = g.create();
try { try {
ui.update(sg, this); ui.update(sg, this);
if (pressing) { if (tinting) {
sg.setColor(new Color(0, 0, 0, 32)); sg.setColor(new Color(0, 0, 0, 32));
sg.fillRoundRect(2, 2, getWidth() - 4 , getHeight() - 4, 6, 6); sg.fillRoundRect(2, 2, getWidth() - 4 , getHeight() - 4, 6, 6);
} }
sg.setColor(pressing ? Color.lightGray : Color.white); sg.setColor(tinting ? Color.lightGray : Color.white);
sg.setFont(keyFont); sg.setFont(keyFont);
int textWidth = sg.getFontMetrics(keyFont).stringWidth(text); int textWidth = sg.getFontMetrics(keyFont).stringWidth(text);
@ -44,8 +44,8 @@ public class KeyboundButton extends JButton {
} }
} }
public void setPressing(boolean pressing) { public void setTint(boolean tinting) {
this.pressing = pressing; this.tinting = tinting;
repaint(); repaint();
} }

View file

@ -9,6 +9,7 @@ import java.util.function.Consumer;
public class FirstButtonMousePressedAction extends MouseAdapter { public class FirstButtonMousePressedAction extends MouseAdapter {
private final Consumer<MouseEvent> callback; private final Consumer<MouseEvent> callback;
private boolean pressed = false;
private boolean inside = false; private boolean inside = false;
public FirstButtonMousePressedAction(Consumer<MouseEvent> callback) { public FirstButtonMousePressedAction(Consumer<MouseEvent> callback) {
@ -17,17 +18,19 @@ public class FirstButtonMousePressedAction extends MouseAdapter {
@Override @Override
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
pressed = true;
if (e.getSource() instanceof KeyboundButton) { if (e.getSource() instanceof KeyboundButton) {
KeyboundButton button = (KeyboundButton) e.getSource(); KeyboundButton button = (KeyboundButton) e.getSource();
button.setPressing(true); button.setTint(true);
} }
} }
@Override @Override
public void mouseReleased(MouseEvent e) { public void mouseReleased(MouseEvent e) {
pressed = false;
if (e.getSource() instanceof KeyboundButton) { if (e.getSource() instanceof KeyboundButton) {
KeyboundButton button = (KeyboundButton) e.getSource(); KeyboundButton button = (KeyboundButton) e.getSource();
button.setPressing(false); button.setTint(false);
} }
if (e.getButton() == MouseEvent.BUTTON1 && inside) { if (e.getButton() == MouseEvent.BUTTON1 && inside) {
callback.accept(e); callback.accept(e);
@ -37,11 +40,19 @@ public class FirstButtonMousePressedAction extends MouseAdapter {
@Override @Override
public void mouseEntered(MouseEvent e) { public void mouseEntered(MouseEvent e) {
inside = true; inside = true;
if (pressed && e.getSource() instanceof KeyboundButton) {
KeyboundButton button = (KeyboundButton) e.getSource();
button.setTint(true);
}
} }
@Override @Override
public void mouseExited(MouseEvent e) { public void mouseExited(MouseEvent e) {
inside = false; inside = false;
if (e.getSource() instanceof KeyboundButton) {
KeyboundButton button = (KeyboundButton) e.getSource();
button.setTint(false);
}
} }
} }