#Adding Custom Widgets To Existing Minecraft Screens

1 messages · Page 1 of 1 (latest)

rose horizon
#

So I do this to add a button to a GameMenuScreen:

ScreenEvents.AFTER_INIT.register(
    (client, screen, sw, sh) -> {
        if (!(screen instanceof GameMenuScreen)) return;

        Screens.getButtons(screen).add(
                ButtonWidget
                        .builder(...)
                        .dimensions(...)
                        .build()
        );
    }
);

But what about other widgets? Version: 1.21.8.

vale phoenix
#

yes thats what to do to add

#

ScreenEvents.AFTER_INIT.register((client, screen, sw, sh) -> {
if (!(screen instanceof GameMenuScreen)) return;

// add a button
ButtonWidget myButton = ButtonWidget.builder(Text.of("Click Me"), btn -> {
    System.out.println("Button clicked!");
})
.dimensions(10, 10, 100, 20) // x, y, width, height
.build();

screen.addDrawableChild(myButton);

// add a text field
TextFieldWidget textField = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, 10, 40, 150, 20, Text.of(""));
screen.addDrawableChild(textField);

});

rose horizon
#

Well that doesn't work in 1.21.8 because
<T extends Element & Drawable & Selectable> T addDrawableChild(T drawableElement)
has protected access in Screen so it can only be accessed from the subclass of Screen. But when you are adding a widget onto an existing screen this is not a case.

tall apex
#

Screens.getButtons(screen).add(<...>)

vale phoenix
#

then i think

#

ScreenEvents.AFTER_INIT.register((client, screen, sw, sh) -> {
if (!(screen instanceof GameMenuScreen)) return;

TextFieldWidget textField = new TextFieldWidget(
    client.textRenderer,
    10, 40, 150, 20,
    Text.of("example")
);

Screens.getChildren(screen).add(textField);
Screens.getDrawables(screen).add(textField);

});

rose horizon
vale phoenix
#

yeah

tall apex
#

well good that .getButtons returns a List<ClickableWidget> and not a List<ButtonWidget> then lol

rose horizon
#

It isn't ClickableWidget then

tall apex
#

then make it a ClickableWidget

rose horizon
#

Like my widget is a scrollable list with a text field

tall apex
#

how is it a workaround if ClickableWidget literally implements Drawable, Element, Widget, Selectable

#

all of the methods from Widget are (iirc) handled by ClickableWidget so its literally a drop in replacement

rose horizon
tall apex
#

if your widget is scrollable then its clickable; .getButtons returns a List<ClickableWidget> and not List<ButtonWidget>. i don't see a problem except for the weird method name

rose horizon
#

Ok I'll use it for my widget but what if I have a widget that isn't clickable like a texture or a text?

tall apex
#

AbstractTextWidget and IconWidget extend ClickableWidget

#

the yarn name for ClickableWidget is misleading anyway

#

its used for non clickable widgets aswell. i consider the mojmap name, AbstractWidget, better

rose horizon
#

Ok so you can render basically anything with ClickableWidget?

tall apex
#

yes