Call.textInput (#8355)
* Update Menus.java
* Update EventType.java
* Update UI.java
* WHY
* WHY x2
* fix
* 💀
This commit is contained in:
@@ -261,28 +261,32 @@ public class UI implements ApplicationListener, Loadable{
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showTextInput(String titleText, String dtext, int textLength, String def, boolean inumeric, Cons<String> confirmed){
|
public void showTextInput(String titleText, String text, int textLength, String def, boolean numbers, Cons<String> confirmed, Runnable closed){
|
||||||
if(mobile){
|
if(mobile){
|
||||||
Core.input.getTextInput(new TextInput(){{
|
Core.input.getTextInput(new TextInput(){{
|
||||||
this.title = (titleText.startsWith("@") ? Core.bundle.get(titleText.substring(1)) : titleText);
|
this.title = (titleText.startsWith("@") ? Core.bundle.get(titleText.substring(1)) : titleText);
|
||||||
this.text = def;
|
this.text = def;
|
||||||
this.numeric = inumeric;
|
this.numeric = numbers;
|
||||||
this.maxLength = textLength;
|
this.maxLength = textLength;
|
||||||
this.accepted = confirmed;
|
this.accepted = confirmed;
|
||||||
this.allowEmpty = false;
|
this.allowEmpty = false;
|
||||||
}});
|
}});
|
||||||
}else{
|
}else{
|
||||||
new Dialog(titleText){{
|
new Dialog(titleText){{
|
||||||
cont.margin(30).add(dtext).padRight(6f);
|
cont.margin(30).add(text).padRight(6f);
|
||||||
TextFieldFilter filter = inumeric ? TextFieldFilter.digitsOnly : (f, c) -> true;
|
TextFieldFilter filter = numbers ? TextFieldFilter.digitsOnly : (f, c) -> true;
|
||||||
TextField field = cont.field(def, t -> {}).size(330f, 50f).get();
|
TextField field = cont.field(def, t -> {}).size(330f, 50f).get();
|
||||||
field.setFilter((f, c) -> field.getText().length() < textLength && filter.acceptChar(f, c));
|
field.setFilter((f, c) -> field.getText().length() < textLength && filter.acceptChar(f, c));
|
||||||
buttons.defaults().size(120, 54).pad(4);
|
buttons.defaults().size(120, 54).pad(4);
|
||||||
buttons.button("@cancel", this::hide);
|
buttons.button("@cancel", () -> {
|
||||||
|
closed.run();
|
||||||
|
hide();
|
||||||
|
});
|
||||||
buttons.button("@ok", () -> {
|
buttons.button("@ok", () -> {
|
||||||
confirmed.get(field.getText());
|
confirmed.get(field.getText());
|
||||||
hide();
|
hide();
|
||||||
}).disabled(b -> field.getText().isEmpty());
|
}).disabled(b -> field.getText().isEmpty());
|
||||||
|
|
||||||
keyDown(KeyCode.enter, () -> {
|
keyDown(KeyCode.enter, () -> {
|
||||||
String text = field.getText();
|
String text = field.getText();
|
||||||
if(!text.isEmpty()){
|
if(!text.isEmpty()){
|
||||||
@@ -290,9 +294,10 @@ public class UI implements ApplicationListener, Loadable{
|
|||||||
hide();
|
hide();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
keyDown(KeyCode.escape, this::hide);
|
|
||||||
keyDown(KeyCode.back, this::hide);
|
closeOnBack(closed);
|
||||||
show();
|
show();
|
||||||
|
|
||||||
Core.scene.setKeyboardFocus(field);
|
Core.scene.setKeyboardFocus(field);
|
||||||
field.setCursorPosition(def.length());
|
field.setCursorPosition(def.length());
|
||||||
}};
|
}};
|
||||||
@@ -303,8 +308,12 @@ public class UI implements ApplicationListener, Loadable{
|
|||||||
showTextInput(title, text, 32, def, confirmed);
|
showTextInput(title, text, 32, def, confirmed);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showTextInput(String titleText, String text, int textLength, String def, Cons<String> confirmed){
|
public void showTextInput(String title, String text, int textLength, String def, Cons<String> confirmed){
|
||||||
showTextInput(titleText, text, textLength, def, false, confirmed);
|
showTextInput(title, text, textLength, def, false, confirmed);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showTextInput(String title, String text, int textLength, String def, boolean numeric, Cons<String> confirmed){
|
||||||
|
showTextInput(title, text, textLength, def, numeric, confirmed, () -> {});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showInfoFade(String info){
|
public void showInfoFade(String info){
|
||||||
@@ -584,15 +593,11 @@ public class UI implements ApplicationListener, Loadable{
|
|||||||
|
|
||||||
/** Shows a menu that fires a callback when an option is selected. If nothing is selected, -1 is returned. */
|
/** Shows a menu that fires a callback when an option is selected. If nothing is selected, -1 is returned. */
|
||||||
public void showMenu(String title, String message, String[][] options, Intc callback){
|
public void showMenu(String title, String message, String[][] options, Intc callback){
|
||||||
new Dialog("[accent]" + title){{
|
new Dialog(title){{
|
||||||
setFillParent(true);
|
setFillParent(true);
|
||||||
removeChild(titleTable);
|
removeChild(titleTable);
|
||||||
cont.add(titleTable).width(400f);
|
cont.add(titleTable).width(400f);
|
||||||
|
|
||||||
getStyle().titleFontColor = Color.white;
|
|
||||||
title.getStyle().fontColor = Color.white;
|
|
||||||
title.setStyle(title.getStyle());
|
|
||||||
|
|
||||||
cont.row();
|
cont.row();
|
||||||
cont.image().width(400f).pad(2).colspan(2).height(4f).color(Pal.accent).bottom();
|
cont.image().width(400f).pad(2).colspan(2).height(4f).color(Pal.accent).bottom();
|
||||||
cont.row();
|
cont.row();
|
||||||
|
|||||||
@@ -161,7 +161,7 @@ public class EventType{
|
|||||||
this.host = host;
|
this.host = host;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ClientServerConnectEvent{
|
public static class ClientServerConnectEvent{
|
||||||
public final String ip;
|
public final String ip;
|
||||||
public final int port;
|
public final int port;
|
||||||
@@ -179,8 +179,21 @@ public class EventType{
|
|||||||
|
|
||||||
public MenuOptionChooseEvent(Player player, int menuId, int option){
|
public MenuOptionChooseEvent(Player player, int menuId, int option){
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.option = option;
|
|
||||||
this.menuId = menuId;
|
this.menuId = menuId;
|
||||||
|
this.option = option;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Consider using Menus.registerTextInput instead. */
|
||||||
|
public static class TextInputEvent{
|
||||||
|
public final Player player;
|
||||||
|
public final int textInputId;
|
||||||
|
public final @Nullable String text;
|
||||||
|
|
||||||
|
public TextInputEvent(Player player, int textInputId, String text){
|
||||||
|
this.player = player;
|
||||||
|
this.textInputId = textInputId;
|
||||||
|
this.text = text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import static mindustry.Vars.*;
|
|||||||
/** Class for handling menus and notifications across the network. Unstable API! */
|
/** Class for handling menus and notifications across the network. Unstable API! */
|
||||||
public class Menus{
|
public class Menus{
|
||||||
private static final Seq<MenuListener> menuListeners = new Seq<>();
|
private static final Seq<MenuListener> menuListeners = new Seq<>();
|
||||||
|
private static final Seq<TextInputListener> textInputListeners = new Seq<>();
|
||||||
|
|
||||||
/** Register a *global* menu listener. If no option is chosen, the option is returned as -1. */
|
/** Register a *global* menu listener. If no option is chosen, the option is returned as -1. */
|
||||||
public static int registerMenu(MenuListener listener){
|
public static int registerMenu(MenuListener listener){
|
||||||
@@ -19,6 +20,12 @@ public class Menus{
|
|||||||
return menuListeners.size - 1;
|
return menuListeners.size - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Register a *global* text input listener. If no text is provided, the text is returned as null. */
|
||||||
|
public static int registerTextInput(TextInputListener listener){
|
||||||
|
textInputListeners.add(listener);
|
||||||
|
return textInputListeners.size - 1;
|
||||||
|
}
|
||||||
|
|
||||||
//do not invoke any of the methods below directly, use Call
|
//do not invoke any of the methods below directly, use Call
|
||||||
|
|
||||||
@Remote(variants = Variant.both)
|
@Remote(variants = Variant.both)
|
||||||
@@ -39,6 +46,27 @@ public class Menus{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Remote(variants = Variant.both)
|
||||||
|
public static void textInput(int textInputId, String title, String message, int textLength, String def, boolean numeric){
|
||||||
|
if(title == null) title = "";
|
||||||
|
|
||||||
|
ui.showTextInput(title, message, textLength, def, numeric, (text) -> {
|
||||||
|
Call.textInputResult(player, textInputId, text);
|
||||||
|
}, () -> {
|
||||||
|
Call.textInputResult(player, textInputId, null);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Remote(targets = Loc.both, called = Loc.both)
|
||||||
|
public static void textInputResult(@Nullable Player player, int textInputId, @Nullable String text){
|
||||||
|
if(player != null){
|
||||||
|
Events.fire(new TextInputEvent(player, textInputId, text));
|
||||||
|
if(textInputId >= 0 && textInputId < textInputListeners.size){
|
||||||
|
textInputListeners.get(textInputId).get(player, text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Remote(variants = Variant.both, unreliable = true)
|
@Remote(variants = Variant.both, unreliable = true)
|
||||||
public static void setHudText(String message){
|
public static void setHudText(String message){
|
||||||
if(message == null) return;
|
if(message == null) return;
|
||||||
@@ -130,4 +158,8 @@ public class Menus{
|
|||||||
public interface MenuListener{
|
public interface MenuListener{
|
||||||
void get(Player player, int option);
|
void get(Player player, int option);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface TextInputListener{
|
||||||
|
void get(Player player, @Nullable String text);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user