Hook shortcut handlers for Match screen, FButton, and FOptionPane

This commit is contained in:
drdev
2014-05-03 16:38:26 +00:00
parent 8dbe2887ff
commit dcf6bd9c1e
8 changed files with 80 additions and 22 deletions

View File

@@ -236,9 +236,7 @@ public class Forge implements ApplicationListener {
public abstract void onInputEnd();
//also allow handling of keyUp but don't require it
public boolean keyUp(int keyCode) {
return false;
}
public boolean keyUp(int keyCode) { return false; }
}
private static class MainInputProcessor extends FGestureAdapter {
@@ -249,7 +247,7 @@ public class Forge implements ApplicationListener {
@Override
public boolean keyDown(int keyCode) {
if (keyInputAdapter == null) {
//if no active key input adapter, give current screen or overlay a chance to start one
//if no active key input adapter, give current screen or overlay a chance to handle key
FContainer container = FOverlay.getTopOverlay();
if (container == null) {
container = currentScreen;
@@ -257,9 +255,7 @@ public class Forge implements ApplicationListener {
return false;
}
}
if (!container.startKeyInput()) {
return false;
}
return container.keyDown(keyCode);
}
if (keyInputAdapter != null) {
return keyInputAdapter.keyDown(keyCode);

View File

@@ -4,6 +4,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.badlogic.gdx.Input.Keys;
import forge.LobbyPlayer;
import forge.menu.FMenuBar;
import forge.screens.FScreen;
@@ -163,4 +165,16 @@ public class MatchScreen extends FScreen {
bottomPlayerPanel.setBounds(0, height - VPrompt.HEIGHT - bottomPlayerPanelHeight, width, bottomPlayerPanelHeight);
prompt.setBounds(0, height - VPrompt.HEIGHT, width, VPrompt.HEIGHT);
}
@Override
public boolean keyDown(int keyCode) {
switch (keyCode) {
case Keys.ENTER:
case Keys.SPACE:
return prompt.getBtnOk().trigger(); //trigger OK on Enter or Space
case Keys.ESCAPE:
return prompt.getBtnCancel().trigger(); //trigger Cancel on Escape
}
return false;
}
}

View File

@@ -2,6 +2,7 @@ package forge.toolbox;
import org.apache.commons.lang3.StringUtils;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
@@ -151,6 +152,14 @@ public class FButton extends FDisplayObject implements IButton {
return true;
}
public boolean trigger() {
if (isEnabled() && command != null) {
command.handleEvent(new FEvent(this, FEventType.TAP));
return true;
}
return false;
}
@Override
public void draw(Graphics g) {
float x = 0;
@@ -218,4 +227,14 @@ public class FButton extends FDisplayObject implements IButton {
public void setSelected(boolean b0) {
setToggled(b0);
}
@Override
public boolean keyDown(int keyCode) {
switch (keyCode) {
case Keys.ENTER:
case Keys.SPACE:
return trigger(); //trigger button on Enter or Space
}
return false;
}
}

View File

@@ -122,6 +122,10 @@ public class FComboBox<E> extends FTextField {
return true;
}
public void startEdit() {
//don't allow editing text
}
@Override
public void draw(Graphics g) {
super.draw(g);

View File

@@ -101,10 +101,10 @@ public abstract class FContainer extends FDisplayObject {
}
@Override
public boolean startKeyInput() {
//by default, give all children a chance to start keyboard input
public boolean keyDown(int keyCode) {
//by default, give all enabled children a chance handle keyDown
for (FDisplayObject c : children) {
if (c.startKeyInput()) {
if (c.isEnabled() && c.keyDown(keyCode)) {
return true;
}
}

View File

@@ -137,8 +137,7 @@ public abstract class FDisplayObject {
return false;
}
//start keyboard input if possible for this object
public boolean startKeyInput() {
public boolean keyDown(int keyCode) {
return false;
}
}

View File

@@ -1,5 +1,6 @@
package forge.toolbox;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.math.Vector2;
import forge.Forge;
@@ -130,8 +131,9 @@ public class FOptionPane extends FDialog {
private final FDisplayObject displayObj;
private final FButton[] buttons;
private final Callback<Integer> callback;
private final int defaultOption;
public FOptionPane(String message, String title, FSkinImage icon, FDisplayObject displayObj0, String[] options, int defaultOption, final Callback<Integer> callback0) {
public FOptionPane(String message, String title, FSkinImage icon, FDisplayObject displayObj0, String[] options, int defaultOption0, final Callback<Integer> callback0) {
super(title);
if (icon != null) {
@@ -167,6 +169,7 @@ public class FOptionPane extends FDialog {
}
}));
}
defaultOption = defaultOption0;
}
public void setResult(final int option) {
@@ -255,4 +258,18 @@ public class FOptionPane extends FDialog {
return y + BUTTON_HEIGHT + GAP_BELOW_BUTTONS;
}
@Override
public boolean keyDown(int keyCode) {
switch (keyCode) {
case Keys.ENTER:
case Keys.SPACE:
setResult(defaultOption); //set result to default option on Enter/Space
return true;
case Keys.ESCAPE:
setResult(buttons.length - 1); //set result to final option on Escape
return true;
}
return false;
}
}

View File

@@ -35,7 +35,7 @@ public class FTextField extends FDisplayObject {
private FSkinFont font;
private HAlignment alignment;
private int selStart, selLength;
private boolean keyInputActive;
private boolean isEditing;
public FTextField() {
this("");
@@ -136,7 +136,7 @@ public class FTextField extends FDisplayObject {
@Override
public boolean press(float x, float y) {
if (keyInputActive) { //support placing text cursor
if (isEditing) { //support placing text cursor
selStart = getCharIndexAtPoint(x, y);
selLength = 0;
}
@@ -145,12 +145,18 @@ public class FTextField extends FDisplayObject {
@Override
public boolean tap(float x, float y, int count) {
return startKeyInput();
startEdit();
return true;
}
@Override
public boolean startKeyInput() {
if (keyInputActive) { return true; } //do nothing if key input already active
public boolean keyDown(int keyCode) {
startEdit(); //start edit if keyDown received while key input not active
return true;
}
public void startEdit() {
if (isEditing) { return; } //do nothing if already editing
selStart = 0; //select all before starting input
selLength = text.length();
@@ -180,6 +186,10 @@ public class FTextField extends FDisplayObject {
case Keys.ENTER: //end key input on Tab or Enter
Forge.endKeyInput();
return true;
case Keys.ESCAPE:
setText(textBeforeKeyInput); //cancel edit on Escape
Forge.endKeyInput();
return true;
case Keys.BACKSPACE: //also handles Delete since those are processed the same by libgdx
if (text.length() > 0) {
if (selLength == 0) { //delete previous or next character if selection empty
@@ -222,14 +232,13 @@ public class FTextField extends FDisplayObject {
//handle change event if text changed during input
changedHandler.handleEvent(new FEvent(FTextField.this, FEventType.CHANGE, textBeforeKeyInput));
}
keyInputActive = false;
isEditing = false;
selStart = 0;
selLength = 0;
textBeforeKeyInput = null;
}
});
keyInputActive = true;
return true;
isEditing = true;
}
@Override
@@ -239,7 +248,7 @@ public class FTextField extends FDisplayObject {
g.fillRect(BACK_COLOR, 0, 0, w, h);
//draw selection if key input is active
if (keyInputActive) {
if (isEditing) {
float selLeft = PADDING;
if (selStart > 0) {
selLeft += font.getFont().getBounds(text.substring(0, selStart)).width;