forked from magefree/mage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request magefree#2482 from ZNixian/master
Allows custom binding of keys in game mode
- Loading branch information
Showing
12 changed files
with
4,064 additions
and
3,481 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
Mage.Client/src/main/java/mage/client/components/KeyBindButton.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package mage.client.components; | ||
|
||
import java.awt.event.ActionEvent; | ||
import java.awt.event.ActionListener; | ||
import java.awt.event.KeyEvent; | ||
import java.awt.event.KeyListener; | ||
import javax.swing.JButton; | ||
import javax.swing.JLabel; | ||
import javax.swing.JPopupMenu; | ||
|
||
/** | ||
* | ||
* @author Campbell Suter <[email protected]> | ||
*/ | ||
public class KeyBindButton extends JButton implements ActionListener { | ||
|
||
private final JPopupMenu menu; | ||
private final PopupItem item; | ||
private int keyCode; | ||
private String text; | ||
|
||
public KeyBindButton() { | ||
menu = new JPopupMenu(); | ||
menu.add(item = new PopupItem()); | ||
addActionListener(this); | ||
|
||
fixText(); | ||
} | ||
|
||
private void applyNewKeycode(int code) { | ||
keyCode = code; | ||
switch (keyCode) { | ||
case KeyEvent.VK_ESCAPE: | ||
case KeyEvent.VK_SPACE: | ||
keyCode = 0; | ||
} | ||
fixText(); | ||
menu.setVisible(false); | ||
setSize(getPreferredSize()); | ||
System.out.println("text: " + text); | ||
} | ||
|
||
private void fixText() { | ||
if (keyCode == 0) { | ||
text = "<None>"; | ||
} else { | ||
text = KeyEvent.getKeyText(keyCode); | ||
} | ||
repaint(); | ||
} | ||
|
||
public void setKeyCode(int keyCode) { | ||
this.keyCode = keyCode; | ||
fixText(); | ||
} | ||
|
||
public int getKeyCode() { | ||
return keyCode; | ||
} | ||
|
||
@Override | ||
public String getText() { | ||
return text; | ||
} | ||
|
||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
menu.show(this, 0, 0); | ||
item.requestFocusInWindow(); | ||
} | ||
|
||
private class PopupItem extends JLabel implements KeyListener { | ||
|
||
public PopupItem() { | ||
super("Press a key"); | ||
addKeyListener(this); | ||
setFocusable(true); | ||
} | ||
|
||
@Override | ||
public void keyTyped(KeyEvent e) { | ||
} | ||
|
||
@Override | ||
public void keyPressed(KeyEvent e) { | ||
applyNewKeycode(e.getKeyCode()); | ||
} | ||
|
||
@Override | ||
public void keyReleased(KeyEvent e) { | ||
} | ||
|
||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
Mage.Client/src/main/java/mage/client/components/KeyboundButton.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package mage.client.components; | ||
|
||
import java.awt.Color; | ||
import java.awt.Font; | ||
import java.awt.Graphics; | ||
import javax.swing.JButton; | ||
import mage.client.dialog.PreferencesDialog; | ||
|
||
/** | ||
* | ||
* @author Campbell Suter <[email protected]> | ||
*/ | ||
public class KeyboundButton extends JButton { | ||
|
||
private final String text; | ||
private static final Font keyFont = new Font(Font.SANS_SERIF, Font.BOLD, 13); | ||
|
||
public KeyboundButton(String key) { | ||
text = PreferencesDialog.getCachedKeyText(key); | ||
} | ||
|
||
@Override | ||
protected void paintComponent(Graphics g) { | ||
if (ui != null && g != null) { | ||
Graphics sg = g.create(); | ||
try { | ||
ui.update(sg, this); | ||
sg.setColor(Color.white); | ||
sg.setFont(keyFont); | ||
|
||
int textWidth = sg.getFontMetrics(keyFont).stringWidth(text); | ||
int centerX = (getWidth() - textWidth) / 2; | ||
|
||
sg.drawString(text, centerX, 28); | ||
} finally { | ||
sg.dispose(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.