From 4be7cf5c0ee9d0f52658ad213baa21e112a0e7d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D1=80=D0=BA=D0=BD=D0=B5=D1=81=D1=81=233729?= <79508138+Darkness6030@users.noreply.github.com> Date: Sat, 25 Mar 2023 20:50:25 +0300 Subject: [PATCH] Call.textInput (#8355) * Update Menus.java * Update EventType.java * Update UI.java * WHY * WHY x2 * fix * :skull: --- core/src/mindustry/core/UI.java | 33 +++++++++++++++----------- core/src/mindustry/game/EventType.java | 17 +++++++++++-- core/src/mindustry/ui/Menus.java | 32 +++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/core/src/mindustry/core/UI.java b/core/src/mindustry/core/UI.java index a4650d16a7..5783c6f617 100644 --- a/core/src/mindustry/core/UI.java +++ b/core/src/mindustry/core/UI.java @@ -261,28 +261,32 @@ public class UI implements ApplicationListener, Loadable{ }); } - public void showTextInput(String titleText, String dtext, int textLength, String def, boolean inumeric, Cons confirmed){ + public void showTextInput(String titleText, String text, int textLength, String def, boolean numbers, Cons 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 confirmed){ - showTextInput(titleText, text, textLength, def, false, confirmed); + public void showTextInput(String title, String text, int textLength, String def, Cons confirmed){ + showTextInput(title, text, textLength, def, false, confirmed); + } + + public void showTextInput(String title, String text, int textLength, String def, boolean numeric, Cons 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(); diff --git a/core/src/mindustry/game/EventType.java b/core/src/mindustry/game/EventType.java index cf44cea851..fab1fd8b6f 100644 --- a/core/src/mindustry/game/EventType.java +++ b/core/src/mindustry/game/EventType.java @@ -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; } } diff --git a/core/src/mindustry/ui/Menus.java b/core/src/mindustry/ui/Menus.java index 96e2ffdba2..4da4713f73 100644 --- a/core/src/mindustry/ui/Menus.java +++ b/core/src/mindustry/ui/Menus.java @@ -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 menuListeners = new Seq<>(); + private static final Seq 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); + } }