From 5e19014b3e4624677bb9e44ee93c5e5305753bc9 Mon Sep 17 00:00:00 2001 From: MEEPofFaith <54301439+MEEPofFaith@users.noreply.github.com> Date: Tue, 4 Feb 2025 16:15:12 -0800 Subject: [PATCH] #9835 But acually works. (#10462) * Easier access to adding custom buttons to vanilla menu categories. * Initialize in constructor Don't need to wait for `build` to be able to modify * seqn't * Fix null crash on startup * Check if submenu is empty Problem that came up in my test. Might be due to my test being done with js. * Retain original functionality --- .../mindustry/ui/fragments/MenuFragment.java | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/core/src/mindustry/ui/fragments/MenuFragment.java b/core/src/mindustry/ui/fragments/MenuFragment.java index 497b2077e1..296f3c5a89 100644 --- a/core/src/mindustry/ui/fragments/MenuFragment.java +++ b/core/src/mindustry/ui/fragments/MenuFragment.java @@ -28,6 +28,7 @@ public class MenuFragment{ private Button currentMenu; private MenuRenderer renderer; private Seq customButtons = new Seq<>(); + public Seq desktopButtons = null; public void build(Group parent){ renderer = new MenuRenderer(); @@ -187,22 +188,26 @@ public class MenuFragment{ t.defaults().width(width).height(70f); t.name = "buttons"; - buttons(t, - new MenuButton("@play", Icon.play, - new MenuButton("@campaign", Icon.play, () -> checkPlay(ui.planet::show)), - new MenuButton("@joingame", Icon.add, () -> checkPlay(ui.join::show)), - new MenuButton("@customgame", Icon.terrain, () -> checkPlay(ui.custom::show)), - new MenuButton("@loadgame", Icon.download, () -> checkPlay(ui.load::show)) - ), - new MenuButton("@database.button", Icon.menu, - new MenuButton("@schematics", Icon.paste, ui.schematics::show), - new MenuButton("@database", Icon.book, ui.database::show), - new MenuButton("@about.button", Icon.info, ui.about::show) - ), - new MenuButton("@editor", Icon.terrain, () -> checkPlay(ui.maps::show)), steam ? new MenuButton("@workshop", Icon.steam, platform::openWorkshop) : null, - new MenuButton("@mods", Icon.book, ui.mods::show), - new MenuButton("@settings", Icon.settings, ui.settings::show) - ); + if(desktopButtons == null){ + desktopButtons = Seq.with( + new MenuButton("@play", Icon.play, + new MenuButton("@campaign", Icon.play, () -> checkPlay(ui.planet::show)), + new MenuButton("@joingame", Icon.add, () -> checkPlay(ui.join::show)), + new MenuButton("@customgame", Icon.terrain, () -> checkPlay(ui.custom::show)), + new MenuButton("@loadgame", Icon.download, () -> checkPlay(ui.load::show)) + ), + new MenuButton("@database.button", Icon.menu, + new MenuButton("@schematics", Icon.paste, ui.schematics::show), + new MenuButton("@database", Icon.book, ui.database::show), + new MenuButton("@about.button", Icon.info, ui.about::show) + ), + new MenuButton("@editor", Icon.terrain, () -> checkPlay(ui.maps::show)), steam ? new MenuButton("@workshop", Icon.steam, platform::openWorkshop) : null, + new MenuButton("@mods", Icon.book, ui.mods::show), + new MenuButton("@settings", Icon.settings, ui.settings::show) + ); + } + + buttons(t, desktopButtons.toArray(MenuButton.class)); buttons(t, customButtons.toArray(MenuButton.class)); buttons(t, new MenuButton("@quit", Icon.exit, Core.app::exit)); }).width(width).growY(); @@ -250,14 +255,14 @@ public class MenuFragment{ currentMenu = null; fadeOutMenu(); }else{ - if(b.submenu != null){ + if(b.submenu != null && b.submenu.any()){ currentMenu = out[0]; submenu.clearChildren(); fadeInMenu(); //correctly offset the button submenu.add().height((Core.graphics.getHeight() - Core.scene.marginTop - Core.scene.marginBottom - out[0].getY(Align.topLeft)) / Scl.scl(1f)); submenu.row(); - buttons(submenu, b.submenu); + buttons(submenu, b.submenu.toArray()); }else{ currentMenu = null; fadeOutMenu(); @@ -296,7 +301,7 @@ public class MenuFragment{ /** Runnable ran when the button is clicked. Ignored on desktop if {@link #submenu} is not null. */ public final Runnable runnable; /** Submenu shown when this button is clicked. Used instead of {@link #runnable} on desktop. */ - public final @Nullable MenuButton[] submenu; + public final @Nullable Seq submenu; /** Constructs a simple menu button, which behaves the same way on desktop and mobile. */ public MenuButton(String text, Drawable icon, Runnable runnable){ @@ -311,7 +316,7 @@ public class MenuFragment{ this.icon = icon; this.text = text; this.runnable = runnable; - this.submenu = submenu; + this.submenu = submenu != null ? Seq.with(submenu) : null; } /** Comstructs a desktop-only button; used internally. */ @@ -319,7 +324,7 @@ public class MenuFragment{ this.icon = icon; this.text = text; this.runnable = () -> {}; - this.submenu = submenu; + this.submenu = submenu != null ? Seq.with(submenu) : null; } } }