Call.textInput (#8355)

* Update Menus.java

* Update EventType.java

* Update UI.java

* WHY

* WHY x2

* fix

* 💀
This commit is contained in:
Даркнесс#3729
2023-03-25 20:50:25 +03:00
committed by GitHub
parent 58eef49284
commit 4be7cf5c0e
3 changed files with 66 additions and 16 deletions

View File

@@ -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){
Core.input.getTextInput(new TextInput(){{
this.title = (titleText.startsWith("@") ? Core.bundle.get(titleText.substring(1)) : titleText);
this.text = def;
this.numeric = inumeric;
this.numeric = numbers;
this.maxLength = textLength;
this.accepted = confirmed;
this.allowEmpty = false;
}});
}else{
new Dialog(titleText){{
cont.margin(30).add(dtext).padRight(6f);
TextFieldFilter filter = inumeric ? TextFieldFilter.digitsOnly : (f, c) -> true;
cont.margin(30).add(text).padRight(6f);
TextFieldFilter filter = numbers ? TextFieldFilter.digitsOnly : (f, c) -> true;
TextField field = cont.field(def, t -> {}).size(330f, 50f).get();
field.setFilter((f, c) -> field.getText().length() < textLength && filter.acceptChar(f, c));
buttons.defaults().size(120, 54).pad(4);
buttons.button("@cancel", this::hide);
buttons.button("@cancel", () -> {
closed.run();
hide();
});
buttons.button("@ok", () -> {
confirmed.get(field.getText());
hide();
}).disabled(b -> field.getText().isEmpty());
keyDown(KeyCode.enter, () -> {
String text = field.getText();
if(!text.isEmpty()){
@@ -290,9 +294,10 @@ public class UI implements ApplicationListener, Loadable{
hide();
}
});
keyDown(KeyCode.escape, this::hide);
keyDown(KeyCode.back, this::hide);
closeOnBack(closed);
show();
Core.scene.setKeyboardFocus(field);
field.setCursorPosition(def.length());
}};
@@ -303,8 +308,12 @@ public class UI implements ApplicationListener, Loadable{
showTextInput(title, text, 32, def, confirmed);
}
public void showTextInput(String titleText, String text, int textLength, String def, Cons<String> confirmed){
showTextInput(titleText, text, textLength, def, false, confirmed);
public void showTextInput(String title, String text, int textLength, String def, Cons<String> 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){
@@ -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. */
public void showMenu(String title, String message, String[][] options, Intc callback){
new Dialog("[accent]" + title){{
new Dialog(title){{
setFillParent(true);
removeChild(titleTable);
cont.add(titleTable).width(400f);
getStyle().titleFontColor = Color.white;
title.getStyle().fontColor = Color.white;
title.setStyle(title.getStyle());
cont.row();
cont.image().width(400f).pad(2).colspan(2).height(4f).color(Pal.accent).bottom();
cont.row();

View File

@@ -161,7 +161,7 @@ public class EventType{
this.host = host;
}
}
public static class ClientServerConnectEvent{
public final String ip;
public final int port;
@@ -179,8 +179,21 @@ public class EventType{
public MenuOptionChooseEvent(Player player, int menuId, int option){
this.player = player;
this.option = option;
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;
}
}

View File

@@ -12,6 +12,7 @@ import static mindustry.Vars.*;
/** Class for handling menus and notifications across the network. Unstable API! */
public class Menus{
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. */
public static int registerMenu(MenuListener listener){
@@ -19,6 +20,12 @@ public class Menus{
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
@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)
public static void setHudText(String message){
if(message == null) return;
@@ -130,4 +158,8 @@ public class Menus{
public interface MenuListener{
void get(Player player, int option);
}
public interface TextInputListener{
void get(Player player, @Nullable String text);
}
}