Fabric API

Fabric API

106M Downloads

Screen API ButtonList broken since 21w20a

jackassmc opened this issue ยท 4 comments

commented

21w20a changed buttons to be a "drawable child" of screen, added to a screen by calling addDrawableChild.

   protected <T extends Element & Drawable & Selectable> T addDrawableChild(T drawableElement) {
      this.drawables.add((Drawable)drawableElement);
      return this.addSelectableChild(drawableElement);
   }

   protected <T extends Element & Selectable> T addSelectableChild(T child) {
      this.children.add(child);
      this.selectables.add((Selectable)child);
      return child;
   }

The port of the screen API to 1.17-pre1 assumes buttons to be a "selectable child", not handling this.drawables leading to buttons being clickable and functioning but invisible.

While looking into this I found out that the tab order is handled by the order of this.children, the render order by the order of this.drawables while this.selectable has something to do with narration.

commented

@jackassmc wait, where are these? The screen api doesn't add buttons. Where are these buttons added incorrectly?

commented

The screen API allows adding buttons using this:

/**
* Gets all of a screen's button widgets.
* The provided list allows for addition and removal of buttons from the screen.
* This method should be preferred over adding buttons directly to a screen's {@link Screen#children() child elements}.
*
* @return a list of all of a screen's buttons
*/
public static List<ClickableWidget> getButtons(Screen screen) {

But it looks like the backing implementation (Fabric API's ButtonList) does not add them to all needed lists.

commented

There's also an example on how to add buttons that uses the button list:

* <p>For example, to add a button to the title screen, the following code could be used:
* <pre>{@code
* ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
* if (screen instanceof TitleScreen) {
* Screens.getButtons(screen).add(new ButtonWidget(...));
* }
* });
* }</pre>

commented

The testmod also changes a button and adds two new ones:

final List<ClickableWidget> buttons = Screens.getButtons(screen);


The notes about list orders are because I'm new to Java and got confused if the proper order of this.children and this.selectables is maintained when setting and adding buttons. Could somebody tell me please, it's in here: https://github.com/FabricMC/fabric/blob/b7ab612143d0de808476ea6039740c123c109231/fabric-screen-api-v1/src/main/java/net/fabricmc/fabric/impl/client/screen/ButtonList.java
Edit: I think I found another bug:

should use a translated index (but for children instead of selectables).