Class GuiButton

All Implemented Interfaces:
ElementEvents, GuiParent<GuiButton>, TooltipHandler<GuiButton>

public class GuiButton extends GuiElement<GuiButton>
Created by brandon3055 on 28/08/2023
  • Field Details

  • Constructor Details

    • GuiButton

      public GuiButton(@NotNull @NotNull GuiParent<?> parent)
      In its default state this is a blank, invisible element that can fire callbacks when pressed. To make an actual usable button, ether use one of the builtin static create methods, Or add your own elements to make this button look and function in a way that meets your needs.
      Parameters:
      parent - parent GuiParent.
  • Method Details

    • vanilla

      public static GuiButton vanilla(@NotNull @NotNull GuiParent<?> parent, @Nullable @Nullable net.minecraft.network.chat.Component label, Runnable onClick)
      Creates a new gui button that looks and acts exactly like a standard vanilla button.
    • vanilla

      public static GuiButton vanilla(@NotNull @NotNull GuiParent<?> parent, @Nullable @Nullable net.minecraft.network.chat.Component label)
      Creates a new gui button that looks and acts exactly like a standard vanilla button.
    • vanillaAnimated

      public static GuiButton vanillaAnimated(@NotNull @NotNull GuiParent<?> parent, net.minecraft.network.chat.Component label, Runnable onPress)
      Creates a vanilla button with a "press" animation.
    • vanillaAnimated

      public static GuiButton vanillaAnimated(@NotNull @NotNull GuiParent<?> parent, @Nullable @Nullable Supplier<net.minecraft.network.chat.Component> label, Runnable onPress)
      Creates a vanilla button with a "press" animation.
    • vanillaAnimated

      public static GuiButton vanillaAnimated(@NotNull @NotNull GuiParent<?> parent, net.minecraft.network.chat.Component label)
      Creates a vanilla button with a "press" animation.
    • vanillaAnimated

      public static GuiButton vanillaAnimated(@NotNull @NotNull GuiParent<?> parent, @Nullable @Nullable Supplier<net.minecraft.network.chat.Component> label)
      Creates a vanilla button with a "press" animation.
    • flatColourButton

      public static GuiButton flatColourButton(@NotNull @NotNull GuiParent<?> parent, @Nullable @Nullable Supplier<net.minecraft.network.chat.Component> label, Function<Boolean,Integer> buttonColour)
      Super simple button that is just a coloured rectangle with a label.
    • flatColourButton

      public static GuiButton flatColourButton(@NotNull @NotNull GuiParent<?> parent, @Nullable @Nullable Supplier<net.minecraft.network.chat.Component> label, Function<Boolean,Integer> buttonColour, @Nullable @Nullable Function<Boolean,Integer> borderColour)
      Super simple button that is just a coloured rectangle with a label.
    • setLabel

      public GuiButton setLabel(GuiText label)
      When creating buttons with labels, use this method to store a reference to the label in the button fore easy retrival later.
      Parameters:
      label - The button label.
    • getLabel

      public GuiText getLabel()
      Returns:
      The buttons label element, If it has one.
    • setResetHoverOnPress

      public GuiButton setResetHoverOnPress(boolean resetHoverOnPress)
      By default, hover time is reset when button is pressed. THis allows you to disable that functionality to the tooltip will remain open when button is pressed.
    • onClick

      public GuiButton onClick(Runnable onClick)
      This event is fired immediately when this button is left-clicked. This is the logic used by most vanilla gui buttons.
      See Also:
    • onClick

      public GuiButton onClick(Runnable onClick, int mouseButton)
      This event is fired immediately when this button is clicked with the specified mouse button. This is the logic used by most vanilla gui buttons. Note: You can apply one listener per mouse button.
      See Also:
    • onPress

      public GuiButton onPress(Runnable onPress)
      This event is fired when the button is pressed and then released using the left mosue button. The event is only fired if the cursor is still over the button when left click is released. This is the standard logic for most buttons in the world, but not vanillas. Note: You can apply one listener per mouse button.
      See Also:
    • onPress

      public GuiButton onPress(Runnable onPress, int mouseButton)
      This event is fired when the button is pressed and then released using the specified mouse button. The event is only fired if the cursor is still over the button when left click is released. This is the standard logic for most buttons in the world, but not vanillas.
      See Also:
    • setDisabled

      public GuiButton setDisabled(boolean disabled)
      Allows set the disabled status of this button Note: This is not the same as GuiElement.setEnabled(boolean) the "enabled" allows you to completely disable an element. This "disabled" status is specific to GuiButton, When disabled via this method a button is still visible but greyed out / not clickable.
    • setDisabled

      public GuiButton setDisabled(Supplier<Boolean> disabled)
      Allows you to install a suppler that controls the disabled state of this button. Note: This is not the same as GuiElement.setEnabled(Supplier) the "enabled" allows you to completely disable an element. This "disabled" status is specific to GuiButton, When disabled via this method a button is still visible but greyed out / not clickable.
    • setToggleMode

      public GuiButton setToggleMode(@Nullable @Nullable Supplier<Boolean> toggleState)
      Allows this button to be used as a toggle or radio button. This method allows you to install a suppler that controls the current "selected / toggled" state.
      Parameters:
      toggleState - supplier that indicates weather or not this button should currently render as pressed/selected.
    • isDisabled

      public boolean isDisabled()
      Returns:
      the "disabled" status.
      See Also:
    • isPressed

      public boolean isPressed()
      Returns:
      true if this button is currently pressed by the user (left click held down on button)
    • toggleState

      public boolean toggleState()
    • setPressSound

      public GuiButton setPressSound(net.minecraft.core.Holder<net.minecraft.sounds.SoundEvent> pressSound)
      Sets the sound to be played when this button is pressed.
    • setReleaseSound

      public GuiButton setReleaseSound(net.minecraft.core.Holder<net.minecraft.sounds.SoundEvent> releaseSound)
      Sets the sound to be played when this button is released.
    • getPressSound

      public net.minecraft.core.Holder<net.minecraft.sounds.SoundEvent> getPressSound()
    • getReleaseSound

      public net.minecraft.core.Holder<net.minecraft.sounds.SoundEvent> getReleaseSound()
    • mouseClicked

      public boolean mouseClicked(double mouseX, double mouseY, int button)
      Description copied from interface: ElementEvents
      Override this method to implement handling for the mouseClicked event. This event propagates through the entire gui element stack from top to bottom, If eny element consumes the event it will not propagate any further. For rare cases where you need to receive this even if it has been consumed, you can override ElementEvents.mouseClicked(double, double, int, boolean)

      Note: You do not need to call super when overriding this interface method.

      Parameters:
      mouseX - Mouse X position
      mouseY - Mouse Y position
      button - Mouse Button
      Returns:
      true to consume event.
    • mouseReleased

      public boolean mouseReleased(double mouseX, double mouseY, int button, boolean consumed)
      Description copied from interface: ElementEvents
      Root handler for mouseReleased event. This method will always be called for all elements even if the event has already been consumed. There are a few uses for this method, but the fast majority of mouseReleased handling should be implemented via ElementEvents.mouseReleased(double, double, int)

      Note: If overriding this method, do so with caution, You must either return true (if you wish to consume the event) or you must return the result of the super call.

      Parameters:
      mouseX - Mouse X position
      mouseY - Mouse Y position
      button - Mouse Button
      consumed - Will be true if this action has already been consumed.
      Returns:
      true if this event has been consumed.