Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier access to adding custom buttons to existing vanilla menu categories. #9835

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 29 additions & 20 deletions core/src/mindustry/ui/fragments/MenuFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ public class MenuFragment{
private Button currentMenu;
private MenuRenderer renderer;
private Seq<MenuButton> customButtons = new Seq<>();
public MenuButton[] desktopButtons;
MEEPofFaith marked this conversation as resolved.
Show resolved Hide resolved

public MenuFragment(){
if(!mobile){
desktopButtons = new MenuButton[]{
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)
};
}else{
desktopButtons = null;
}
}

public void build(Group parent){
renderer = new MenuRenderer();
Expand Down Expand Up @@ -187,22 +211,7 @@ private void buildDesktop(){
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)
);
buttons(t, desktopButtons);
buttons(t, customButtons.toArray(MenuButton.class));
buttons(t, new MenuButton("@quit", Icon.exit, Core.app::exit));
}).width(width).growY();
Expand Down Expand Up @@ -257,7 +266,7 @@ private void buttons(Table t, MenuButton... buttons){
//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();
Expand Down Expand Up @@ -296,7 +305,7 @@ public static class MenuButton{
/** 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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how I feel about a signature change here, this might crash mods on startup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a mods needing to access submenu directly since adding submenu buttons is already handled by the constructor.
I think it'll be fine.

public final @Nullable Seq<MenuButton> submenu;

/** Constructs a simple menu button, which behaves the same way on desktop and mobile. */
public MenuButton(String text, Drawable icon, Runnable runnable){
Expand All @@ -311,15 +320,15 @@ public MenuButton(String text, Drawable icon, Runnable runnable, MenuButton... s
this.icon = icon;
this.text = text;
this.runnable = runnable;
this.submenu = submenu;
this.submenu = Seq.with(submenu);
}

/** Comstructs a desktop-only button; used internally. */
MenuButton(String text, Drawable icon, MenuButton... submenu){
this.icon = icon;
this.text = text;
this.runnable = () -> {};
this.submenu = submenu;
this.submenu = Seq.with(submenu);
}
}
}