Class GuiTextField

All Implemented Interfaces:
BackgroundRender, ElementEvents, GuiParent<GuiTextField>, TooltipHandler<GuiTextField>

public class GuiTextField extends GuiElement<GuiTextField> implements BackgroundRender
TODO, Re write this, Its currently mostly pulled from the TextField in Gui v2

Created by brandon3055 on 03/09/2023

  • Constructor Details

    • GuiTextField

      public GuiTextField(@NotNull @NotNull GuiParent<?> parent)
  • Method Details

    • create

      public static GuiTextField.TextField create(GuiElement<?> parent, int backgroundColour, int borderColour, int textColour)
      Creates a simple text box with a simple bordered background. Using colours 0xFF000000, 0xFFFFFFFF, 0xE0E0E0 will get you a text box identical to the one in a command block
    • setOnEditComplete

      public GuiTextField setOnEditComplete(Runnable onEditComplete)
      Called when the user clicks outside the text box, or when they press enter
    • setEnterPressed

      public GuiTextField setEnterPressed(Runnable onEnterPressed)
      Called when the user presses enter key (Including numpad enter) with the text box ion focus
    • setTextState

      public GuiTextField setTextState(TextState textState)
      The TextState is an accessor for the current text value. It simply contains string getter and setter methods. You can use this to link the text field to some external value.
    • getTextState

      public TextState getTextState()
    • setTextColor

      public GuiTextField setTextColor(Supplier<Integer> textColor)
    • setTextColor

      public GuiTextField setTextColor(int textColor)
    • setShadow

      public GuiTextField setShadow(Supplier<Boolean> shadow)
      Should the text be rendered with a shadow?
    • setShadow

      public GuiTextField setShadow(boolean shadow)
      Should the text be rendered with a shadow?
    • setSuggestion

      public GuiTextField setSuggestion(net.minecraft.network.chat.Component suggestion)
      Set the "suggestion" text that is displayed when the text field is empty.
    • setSuggestion

      public GuiTextField setSuggestion(@Nullable @Nullable Supplier<net.minecraft.network.chat.Component> suggestion)
      Set the "suggestion" text that is displayed when the text field is empty.
    • setSuggestionColour

      public GuiTextField setSuggestionColour(Supplier<Integer> suggestionColour)
      Set the colour of the suggestion text.
    • setSuggestionColour

      public GuiTextField setSuggestionColour(int suggestionColour)
    • setSuggestionShadow

      public GuiTextField setSuggestionShadow(Supplier<Boolean> suggestionShadow)
    • setSuggestionShadow

      public GuiTextField setSuggestionShadow(boolean suggestionShadow)
    • setFilter

      public GuiTextField setFilter(Predicate<String> filter)
      Allows you to apply a fielder to this text field. Whenever this field's value is updated, If the new value does not pass this filter It will not be applied.
    • setFormatter

      public GuiTextField setFormatter(BiFunction<String,Integer,net.minecraft.util.FormattedCharSequence> formatter)
      Formats the current text value for display to the user. The integer is the current display start position within the current field value.
    • setCanLoseFocus

      public GuiTextField setCanLoseFocus(boolean canLoseFocus)
      If set to false, it will not be possible for the text field to lose focus via normal means. Focus can still be set via setFocus(boolean)
    • setCanLoseFocus

      public GuiTextField setCanLoseFocus(Supplier<Boolean> canLoseFocus)
      If set to false, it will not be possible for the text field to lose focus via normal means. Focus can still be set via setFocus(boolean)
    • setFocusable

      public GuiTextField setFocusable(boolean focusable)
      If false, It will not be possible to focus this element by clicking on it.
    • setFocusable

      public GuiTextField setFocusable(Supplier<Boolean> focusable)
      If false, It will not be possible to focus this element by clicking on it.
    • setEditable

      public GuiTextField setEditable(boolean editable)
      If false, It will not be possible for the user to edit the value of this text field.
    • setEditable

      public GuiTextField setEditable(Supplier<Boolean> editable)
      If false, It will not be possible for the user to edit the value of this text field.
    • setMaxLength

      public GuiTextField setMaxLength(int newWidth)
      Sets the maximum allowed text length
    • setValue

      public GuiTextField setValue(String newValue)
      Note, Initial value should be set after element is constrained, If element width is zero when set, nothing will render until the field is updated. TODO, I need to fix this. Element size should be able to change dynamically without things breaking.
    • getValue

      public String getValue()
    • getHighlighted

      public String getHighlighted()
    • insertText

      public void insertText(String text)
    • deleteWords

      public void deleteWords(int i)
    • deleteChars

      public void deleteChars(int i1)
    • getWordPosition

      public int getWordPosition(int i)
    • moveCursor

      public void moveCursor(int pos)
    • moveCursorTo

      public void moveCursorTo(int pos, boolean notify)
    • moveCursorTo

      public void moveCursorTo(int pos)
    • setCursorPosition

      public void setCursorPosition(int pos)
    • moveCursorToStart

      public void moveCursorToStart()
    • moveCursorToEnd

      public void moveCursorToEnd(boolean notify)
    • moveCursorToEnd

      public void moveCursorToEnd()
    • isEditable

      public boolean isEditable()
    • setFocus

      public void setFocus(boolean focused)
    • isFocused

      public boolean isFocused()
    • getCursorPosition

      public int getCursorPosition()
    • setHighlightPos

      public void setHighlightPos(int newPos)
    • keyPressed

      public boolean keyPressed(int key, int scancode, int modifiers)
      Description copied from interface: ElementEvents
      Override this method to implement handling for the keyPressed 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.keyPressed(int, int, int, boolean)

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

      Specified by:
      keyPressed in interface ElementEvents
      Parameters:
      key - the keyboard key that was pressed.
      scancode - the system-specific scancode of the key
      modifiers - bitfield describing which modifier keys were held down.
      Returns:
      true to consume event.
    • canConsumeInput

      public boolean canConsumeInput()
    • keyReleased

      public boolean keyReleased(int key, int scancode, int modifiers, boolean consumed)
      Description copied from interface: ElementEvents
      Root handler for keyReleased 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 keyReleased handling should be implemented via ElementEvents.keyReleased(int, int, 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.

      Specified by:
      keyReleased in interface ElementEvents
      Parameters:
      key - the keyboard key that was released.
      scancode - the system-specific scancode of the key
      modifiers - bitfield describing which modifier keys were held down.
      consumed - Will be true if this action has already been consumed.
      Returns:
      true if this event has been consumed.
    • charTyped

      public boolean charTyped(char charTyped, int charCode)
      Description copied from interface: ElementEvents
      Override this method to implement handling for the charTyped 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.charTyped(char, int, boolean)

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

      Specified by:
      charTyped in interface ElementEvents
      Parameters:
      charTyped - The character typed.
      charCode - bitfield describing which modifier keys were held down.
      Returns:
      true to consume event.
    • mouseClicked

      public boolean mouseClicked(double mouseX, double mouseY, int button, boolean consumed)
      Description copied from interface: ElementEvents
      Root handler for mouseClick 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 mouseClick handling should be implemented via ElementEvents.mouseClicked(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.

      Specified by:
      mouseClicked in interface ElementEvents
      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.
    • getBackgroundDepth

      public double getBackgroundDepth()
      Description copied from interface: BackgroundRender
      Specifies the z depth of the background content. After BackgroundRender.renderBackground(GuiRender, double, double, float) is called, the PoseStack will be translated by this amount in the z direction before any assigned child elements are rendered. Recommended minimum depth is 0.01 or 0.035 if this element renders text. (text shadows are rendered with a 0.03 offset)
      Specified by:
      getBackgroundDepth in interface BackgroundRender
      Returns:
      the z height of the background content.
    • renderBackground

      public void renderBackground(GuiRender render, double mouseX, double mouseY, float partialTicks)
      Description copied from interface: BackgroundRender
      Used to render content behind this elements child elements. When rendering element content, always use the PoseStack available via the provided GuiRender Where applicable, always use push/pop to ensure the stack is returned to its original state after your rendering is complete.
      Specified by:
      renderBackground in interface BackgroundRender
      Parameters:
      render - Contains gui context information as well as essential render methods/utils including the PoseStack.
    • tick

      public void tick(double mouseX, double mouseY)
      Description copied from class: GuiElement
      Called every tick to update the element. Note this is called regardless of weather or not the element is actually enabled.
      Overrides:
      tick in class GuiElement<GuiTextField>
      Parameters:
      mouseX - Current mouse X position
      mouseY - Current mouse Y position