Skip to content

Commit

Permalink
Merge pull request magefree#2482 from ZNixian/master
Browse files Browse the repository at this point in the history
Allows custom binding of keys in game mode
  • Loading branch information
LevelX2 authored Oct 20, 2016
2 parents ae27658 + cde9afa commit c82ce99
Show file tree
Hide file tree
Showing 12 changed files with 4,064 additions and 3,481 deletions.
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) {
}

}
}
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();
}
}
}
}
210 changes: 205 additions & 5 deletions Mage.Client/src/main/java/mage/client/dialog/PreferencesDialog.form
Original file line number Diff line number Diff line change
Expand Up @@ -3949,7 +3949,7 @@
<Component id="checkBoxEndTurnOthers" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="phases_stopSettings" pref="266" max="32767" attributes="0"/>
<Component id="phases_stopSettings" pref="356" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
Expand Down Expand Up @@ -4171,7 +4171,7 @@
<Component id="panelCardImages" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="panelBackgroundImages" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="53" max="32767" attributes="0"/>
<EmptySpace pref="125" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
Expand Down Expand Up @@ -4230,7 +4230,7 @@
</Group>
<Component id="cbUseDefaultImageFolder" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="0" pref="231" max="32767" attributes="0"/>
<EmptySpace min="0" pref="270" max="32767" attributes="0"/>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
Expand Down Expand Up @@ -4744,7 +4744,7 @@
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Component id="avatarPane" pref="484" max="32767" attributes="0"/>
<Component id="avatarPane" pref="584" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
Expand Down Expand Up @@ -5655,7 +5655,7 @@
<Component id="jLabel17" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace pref="91" max="32767" attributes="0"/>
<EmptySpace pref="198" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
Expand Down Expand Up @@ -5874,6 +5874,206 @@
</Container>
</SubComponents>
</Container>
<Container class="javax.swing.JPanel" name="tabControls">
<Constraints>
<Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout" value="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout$JTabbedPaneConstraintsDescription">
<JTabbedPaneConstraints tabName="Controls">
<Property name="tabTitle" type="java.lang.String" value="Controls"/>
</JTabbedPaneConstraints>
</Constraint>
</Constraints>

<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="labelCancel" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="labelNextTurn" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="labelEndStep" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="labelMainStep" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="labelYourTurn" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="lebelSkip" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="labelPriorEnd" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="labelSkipStep" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="labelConfirm" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="keyConfirm" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="keyCancelSkip" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="keyNextTurn" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="keySkipStack" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="keyYourTurn" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="keyMainStep" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="keyPriorEnd" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="keySkipStep" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="keyEndStep" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Component id="controlsDescriptionLabel" pref="498" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Group type="102" attributes="0">
<Group type="103" groupAlignment="3" attributes="0">
<Component id="labelConfirm" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="keyConfirm" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="labelCancel" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="keyCancelSkip" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="labelNextTurn" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="keyNextTurn" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" min="-2" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="labelEndStep" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="keyEndStep" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="labelSkipStep" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="keySkipStep" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="labelMainStep" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="keyMainStep" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="labelYourTurn" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="keyYourTurn" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="lebelSkip" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="keySkipStack" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="labelPriorEnd" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="keyPriorEnd" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
<Component id="controlsDescriptionLabel" max="32767" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="263" max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JLabel" name="labelNextTurn">
<Properties>
<Property name="text" type="java.lang.String" value="Next Turn"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="labelEndStep">
<Properties>
<Property name="text" type="java.lang.String" value="End Step"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="labelMainStep">
<Properties>
<Property name="text" type="java.lang.String" value="Main Step"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="labelYourTurn">
<Properties>
<Property name="text" type="java.lang.String" value="Your Turn"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="lebelSkip">
<Properties>
<Property name="text" type="java.lang.String" value="Skip Stack"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="labelPriorEnd">
<Properties>
<Property name="text" type="java.lang.String" value="Prior End"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="labelCancel">
<Properties>
<Property name="text" type="java.lang.String" value="Cancel Skip"/>
</Properties>
</Component>
<Component class="mage.client.components.KeyBindButton" name="keyCancelSkip">
<Properties>
<Property name="text" type="java.lang.String" value="keyBindButton1"/>
</Properties>
</Component>
<Component class="mage.client.components.KeyBindButton" name="keyNextTurn">
<Properties>
<Property name="text" type="java.lang.String" value="keyBindButton1"/>
</Properties>
</Component>
<Component class="mage.client.components.KeyBindButton" name="keyMainStep">
<Properties>
<Property name="text" type="java.lang.String" value="keyBindButton1"/>
</Properties>
</Component>
<Component class="mage.client.components.KeyBindButton" name="keyEndStep">
<Properties>
<Property name="text" type="java.lang.String" value="keyBindButton1"/>
</Properties>
</Component>
<Component class="mage.client.components.KeyBindButton" name="keyYourTurn">
<Properties>
<Property name="text" type="java.lang.String" value="keyBindButton1"/>
</Properties>
</Component>
<Component class="mage.client.components.KeyBindButton" name="keySkipStack">
<Properties>
<Property name="text" type="java.lang.String" value="keyBindButton1"/>
</Properties>
</Component>
<Component class="mage.client.components.KeyBindButton" name="keyPriorEnd">
<Properties>
<Property name="text" type="java.lang.String" value="keyBindButton1"/>
</Properties>
</Component>
<Component class="mage.client.components.KeyBindButton" name="keySkipStep">
<Properties>
<Property name="text" type="java.lang.String" value="keyBindButton1"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="labelSkipStep">
<Properties>
<Property name="text" type="java.lang.String" value="Skip Step"/>
</Properties>
</Component>
<Component class="mage.client.components.KeyBindButton" name="keyConfirm">
<Properties>
<Property name="text" type="java.lang.String" value="keyBindButton1"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="labelConfirm">
<Properties>
<Property name="text" type="java.lang.String" value="Confirm"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="controlsDescriptionLabel">
<Properties>
<Property name="text" type="java.lang.String" value="&lt;html&gt;Click on a button and press a key to change a keybind.&lt;br&gt;Space and ESC are not available, and will set the keybind to nothing.&lt;br&gt;If you are currently playing a game, the changes will not take effect until you start a new game."/>
<Property name="verticalAlignment" type="int" value="1"/>
</Properties>
</Component>
</SubComponents>
</Container>
</SubComponents>
</Container>
<Component class="javax.swing.JButton" name="saveButton">
Expand Down
Loading

0 comments on commit c82ce99

Please sign in to comment.