Simulate JButton behavior while still preserving check for BUTTON1 using MouseAdapter. Add a slight tint when button is pressing, but not pressed.

This commit is contained in:
John Hitchings 2019-03-10 23:02:13 -07:00
parent 2b4a01410b
commit 11b93d0771
3 changed files with 82 additions and 89 deletions

View file

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

View file

@ -0,0 +1,47 @@
package mage.client.game;
import mage.client.components.KeyboundButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.function.Consumer;
public class FirstButtonMousePressedAction extends MouseAdapter {
private final Consumer<MouseEvent> callback;
private boolean inside = false;
public FirstButtonMousePressedAction(Consumer<MouseEvent> callback) {
this.callback = callback;
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getSource() instanceof KeyboundButton) {
KeyboundButton button = (KeyboundButton) e.getSource();
button.setPressing(true);
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.getSource() instanceof KeyboundButton) {
KeyboundButton button = (KeyboundButton) e.getSource();
button.setPressing(false);
}
if (e.getButton() == MouseEvent.BUTTON1 && inside) {
callback.accept(e);
}
}
@Override
public void mouseEntered(MouseEvent e) {
inside = true;
}
@Override
public void mouseExited(MouseEvent e) {
inside = false;
}
}

View file

@ -1364,14 +1364,8 @@ public final class GamePanel extends javax.swing.JPanel {
btnToggleMacro.setToolTipText("Toggle Record Macro ("
+ getCachedKeyText(KEY_CONTROL_TOGGLE_MACRO) + ").");
btnToggleMacro.setFocusable(false);
btnToggleMacro.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getButton() == MouseEvent.BUTTON1) {
btnToggleMacroActionPerformed(null);
}
}
});
btnToggleMacro.addMouseListener(new FirstButtonMousePressedAction(e ->
btnToggleMacroActionPerformed(null)));
KeyStroke kst = getCachedKeystroke(KEY_CONTROL_TOGGLE_MACRO);
this.getInputMap(c).put(kst, "F8_PRESS");
@ -1397,14 +1391,8 @@ public final class GamePanel extends javax.swing.JPanel {
btnCancelSkip.setToolTipText("Cancel all skip actions ("
+ getCachedKeyText(KEY_CONTROL_CANCEL_SKIP) + ").");
btnCancelSkip.setFocusable(false);
btnCancelSkip.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getButton() == MouseEvent.BUTTON1) {
restorePriorityActionPerformed(null);
}
}
});
btnCancelSkip.addMouseListener(new FirstButtonMousePressedAction(e ->
restorePriorityActionPerformed(null)));
btnSkipToNextTurn.setContentAreaFilled(false);
btnSkipToNextTurn.setBorder(new EmptyBorder(BORDER_SIZE, BORDER_SIZE, BORDER_SIZE, BORDER_SIZE));
@ -1412,14 +1400,8 @@ public final class GamePanel extends javax.swing.JPanel {
btnSkipToNextTurn.setToolTipText("Skip to next turn ("
+ getCachedKeyText(KEY_CONTROL_NEXT_TURN) + ").");
btnSkipToNextTurn.setFocusable(false);
btnSkipToNextTurn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getButton() == MouseEvent.BUTTON1) {
btnEndTurnActionPerformed(null);
}
}
});
btnSkipToNextTurn.addMouseListener(new FirstButtonMousePressedAction(e ->
btnEndTurnActionPerformed(null)));
KeyStroke ks = getCachedKeystroke(KEY_CONTROL_NEXT_TURN);
this.getInputMap(c).put(ks, "F4_PRESS");
@ -1436,14 +1418,8 @@ public final class GamePanel extends javax.swing.JPanel {
btnSkipToEndTurn.setToolTipText("Skip to (opponents/next) end of turn step ("
+ getCachedKeyText(KEY_CONTROL_END_STEP) + ") - adjust using preferences.");
btnSkipToEndTurn.setFocusable(false);
btnSkipToEndTurn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getButton() == MouseEvent.BUTTON1) {
btnUntilEndOfTurnActionPerformed(null);
}
}
});
btnSkipToEndTurn.addMouseListener(new FirstButtonMousePressedAction(e ->
btnUntilEndOfTurnActionPerformed(null)));
ks = getCachedKeystroke(KEY_CONTROL_END_STEP);
this.getInputMap(c).put(ks, "F5_PRESS");
@ -1469,14 +1445,8 @@ public final class GamePanel extends javax.swing.JPanel {
btnSkipToNextMain.setToolTipText("Skip to (your) next main phase ("
+ getCachedKeyText(KEY_CONTROL_MAIN_STEP) + ") - adjust using preferences.");
btnSkipToNextMain.setFocusable(false);
btnSkipToNextMain.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getButton() == MouseEvent.BUTTON1) {
btnUntilNextMainPhaseActionPerformed(null);
}
}
});
btnSkipToNextMain.addMouseListener(new FirstButtonMousePressedAction(e ->
btnUntilNextMainPhaseActionPerformed(null)));
ks = getCachedKeystroke(KEY_CONTROL_MAIN_STEP);
this.getInputMap(c).put(ks, "F7_PRESS");
@ -1493,14 +1463,8 @@ public final class GamePanel extends javax.swing.JPanel {
btnSkipToYourTurn.setToolTipText("Skip to your next turn ("
+ getCachedKeyText(KEY_CONTROL_YOUR_TURN) + ").");
btnSkipToYourTurn.setFocusable(false);
btnSkipToYourTurn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getButton() == MouseEvent.BUTTON1) {
btnPassPriorityUntilNextYourTurnActionPerformed(null);
}
}
});
btnSkipToYourTurn.addMouseListener(new FirstButtonMousePressedAction(e ->
btnPassPriorityUntilNextYourTurnActionPerformed(null)));
KeyStroke ks9 = getCachedKeystroke(KEY_CONTROL_YOUR_TURN);
this.getInputMap(c).put(ks9, "F9_PRESS");
@ -1517,14 +1481,8 @@ public final class GamePanel extends javax.swing.JPanel {
btnSkipToEndStepBeforeYourTurn.setToolTipText("Skip to the end step before your turn ("
+ getCachedKeyText(KEY_CONTROL_PRIOR_END) + ") - adjust using preferences.");
btnSkipToEndStepBeforeYourTurn.setFocusable(false);
btnSkipToEndStepBeforeYourTurn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getButton() == MouseEvent.BUTTON1) {
btnSkipToEndStepBeforeYourTurnActionPerformed(null);
}
}
});
btnSkipToEndStepBeforeYourTurn.addMouseListener(new FirstButtonMousePressedAction(e ->
btnSkipToEndStepBeforeYourTurnActionPerformed(null)));
KeyStroke ks11 = getCachedKeystroke(KEY_CONTROL_PRIOR_END);
this.getInputMap(c).put(ks11, "F11_PRESS");
@ -1541,14 +1499,8 @@ public final class GamePanel extends javax.swing.JPanel {
btnSkipStack.setToolTipText("Skip until stack is resolved ("
+ getCachedKeyText(KEY_CONTROL_SKIP_STACK) + ").");
btnSkipStack.setFocusable(false);
btnSkipStack.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getButton() == MouseEvent.BUTTON1) {
btnPassPriorityUntilStackResolvedActionPerformed(null);
}
}
});
btnSkipStack.addMouseListener(new FirstButtonMousePressedAction(e ->
btnPassPriorityUntilStackResolvedActionPerformed(null)));
ks = getCachedKeystroke(KEY_CONTROL_SKIP_STACK);
this.getInputMap(c).put(ks, "F10_PRESS");
@ -1564,14 +1516,8 @@ public final class GamePanel extends javax.swing.JPanel {
btnConcede.setIcon(new ImageIcon(ImageManagerImpl.instance.getConcedeButtonImage()));
btnConcede.setToolTipText("Concede the current game.");
btnConcede.setFocusable(false);
btnConcede.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getButton() == MouseEvent.BUTTON1) {
btnConcedeActionPerformed(null);
}
}
});
btnConcede.addMouseListener(new FirstButtonMousePressedAction(e ->
btnConcedeActionPerformed(null)));
KeyStroke ks2 = getCachedKeystroke(KEY_CONTROL_CONFIRM);
this.getInputMap(c).put(ks2, "F2_PRESS");
@ -1672,28 +1618,16 @@ public final class GamePanel extends javax.swing.JPanel {
btnSwitchHands.setIcon(new ImageIcon(ImageManagerImpl.instance.getSwitchHandsButtonImage()));
btnSwitchHands.setFocusable(false);
btnSwitchHands.setToolTipText("Switch between your hand cards and hand cards of controlled players.");
btnSwitchHands.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getButton() == MouseEvent.BUTTON1) {
btnSwitchHandActionPerformed(null);
}
}
});
btnSwitchHands.addMouseListener(new FirstButtonMousePressedAction(e ->
btnSwitchHandActionPerformed(null)));
btnStopWatching.setContentAreaFilled(false);
btnStopWatching.setBorder(new EmptyBorder(0, 0, 0, 0));
btnStopWatching.setIcon(new ImageIcon(ImageManagerImpl.instance.getStopWatchButtonImage()));
btnStopWatching.setFocusable(false);
btnStopWatching.setToolTipText("Stop watching this game.");
btnStopWatching.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getButton() == MouseEvent.BUTTON1) {
btnStopWatchingActionPerformed(null);
}
}
});
btnStopWatching.addMouseListener(new FirstButtonMousePressedAction(e ->
btnStopWatchingActionPerformed(null)));
stackObjects.setBackgroundColor(new Color(0, 0, 0, 40));