Class WWidget

java.lang.Object
io.github.cottonmc.cotton.gui.widget.WWidget
Direct Known Subclasses:
WAbstractSlider, WBar, WButton, WDynamicLabel, WItem, WItemSlot, WKirbSprite, WLabel, WPanel, WScrollBar, WSprite, WText, WTextField, WToggleButton

public class WWidget extends Object
The base class for all widgets.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
    The height of this widget, defaults to 18 pixels.
    protected @Nullable GuiDescription
    The containing GuiDescription of this widget.
    protected @Nullable WPanel
    The containing panel of this widget.
    protected int
    The width of this widget, defaults to 18 pixels.
    protected int
    The X coordinate of this widget relative to its parent.
    protected int
    The Y coordinate of this widget relative to its parent.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds the default background painters to this widget and all children.
    void
    Adds lines to this widget's tooltip.
    boolean
    Tests whether this widget can have the focus in the GUI.
    boolean
    Checks whether this widget can be resized using setSize(int, int).
    void
    Deprecated.
    All widget peers should be added in validate(GuiDescription).
    @Nullable WWidget
    cycleFocus​(boolean lookForwards)
    Cycles the focus inside this widget.
    int
    Gets the absolute X coordinate of this widget.
    int
    Gets the absolute Y coordinate of this widget.
    int
     
    @Nullable GuiDescription
    Gets the host of this widget.
    @Nullable WPanel
    Gets the parent panel of this widget.
    int
     
    int
    Gets the X coordinate of this widget relative to its parent.
    int
    Gets the Y coordinate of this widget relative to its parent.
    hit​(int x, int y)
    Find the most specific child node at this location.
    static boolean
    isActivationKey​(int ch)
    Tests if the provided key code is an activation key for widgets.
    boolean
    Tests whether this widget has focus.
    boolean
    isWithinBounds​(int x, int y)
    Checks whether a location is within this widget's bounds.
    void
    onCharTyped​(char ch)
    Notifies this widget that a character has been typed.
    onClick​(int x, int y, int button)
    Notifies this widget that the mouse has been pressed and released, both while inside its bounds.
    void
    Notifies this widget that it has gained focus
    void
    Notifies this widget that it has lost focus
    void
    Notifies this widget that it won't be drawn and hides any visible peers of itself and its children.
    void
    onKeyPressed​(int ch, int key, int modifiers)
    Notifies this widget that a key has been pressed.
    void
    onKeyReleased​(int ch, int key, int modifiers)
    Notifies this widget that a key has been released
    onMouseDown​(int x, int y, int button)
    Notifies this widget that the mouse has been pressed while inside its bounds
    onMouseDrag​(int x, int y, int button, double deltaX, double deltaY)
    Notifies this widget that the mouse has been moved while pressed and inside its bounds.
    onMouseMove​(int x, int y)
    Notifies this widget that the mouse has been moved while inside its bounds.
    onMouseScroll​(int x, int y, double amount)
    Notifies this widget that the mouse has been scrolled inside its bounds.
    onMouseUp​(int x, int y, int button)
    Notifies this widget that the mouse has been released while inside its bounds
    void
    Notifies this widget that it is visible and shows any hidden peers of itself and its children.
    void
    paint​(net.minecraft.client.util.math.MatrixStack matrices, int x, int y, int mouseX, int mouseY)
    Paints this widget.
    void
    If this widget has a host, releases this widget's focus.
    void
    renderTooltip​(net.minecraft.client.util.math.MatrixStack matrices, int x, int y, int tX, int tY)
    Internal method to render tooltip data.
    void
    If this widget has a host, requests the focus from the host.
    void
    setHost​(@Nullable GuiDescription host)
    Sets the host of this widget without creating peers.
    void
    setLocation​(int x, int y)
    Sets the location of this widget relative to its parent.
    void
    setParent​(WPanel parent)
    Sets the parent panel of this widget.
    void
    setSize​(int x, int y)
    Sets the size of this widget.
    void
    Executes a client-side tick for this widget.
    void
    Creates component peers, lays out children, and initializes animation data for this Widget and all its children.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • parent

      @Nullable protected @Nullable WPanel parent
      The containing panel of this widget. Can be null if this widget is the root panel or a HUD widget.
    • x

      protected int x
      The X coordinate of this widget relative to its parent.
    • y

      protected int y
      The Y coordinate of this widget relative to its parent.
    • width

      protected int width
      The width of this widget, defaults to 18 pixels.
    • height

      protected int height
      The height of this widget, defaults to 18 pixels.
    • host

      @Nullable protected @Nullable GuiDescription host
      The containing GuiDescription of this widget. Can be null if this widget is a HUD widget.
  • Constructor Details

    • WWidget

      public WWidget()
  • Method Details

    • setLocation

      public void setLocation(int x, int y)
      Sets the location of this widget relative to its parent.
      Parameters:
      x - the new X coordinate
      y - the new Y coordinate
    • setSize

      public void setSize(int x, int y)
      Sets the size of this widget.

      Overriding methods may restrict one of the dimensions to be a constant value, for example super.setSize(x, 20).

      Parameters:
      x - the new width
      y - the new height
    • getX

      public int getX()
      Gets the X coordinate of this widget relative to its parent.
      Returns:
      the X coordinate
    • getY

      public int getY()
      Gets the Y coordinate of this widget relative to its parent.
      Returns:
      the Y coordinate
    • getAbsoluteX

      public int getAbsoluteX()
      Gets the absolute X coordinate of this widget.
      Returns:
      the absolute X coordinate
    • getAbsoluteY

      public int getAbsoluteY()
      Gets the absolute Y coordinate of this widget.
      Returns:
      the absolute Y coordinate
    • getWidth

      public int getWidth()
    • getHeight

      public int getHeight()
    • canResize

      public boolean canResize()
      Checks whether this widget can be resized using setSize(int, int).
      Returns:
      true if this widget can be resized, false otherwise
    • getParent

      @Nullable public @Nullable WPanel getParent()
      Gets the parent panel of this widget.
      Returns:
      the parent, or null if this widget has no parent
      Since:
      2.0.0
    • setParent

      public void setParent(WPanel parent)
      Sets the parent panel of this widget.
      Parameters:
      parent - the new parent
    • onMouseDown

      @Environment(CLIENT) public InputResult onMouseDown(int x, int y, int button)
      Notifies this widget that the mouse has been pressed while inside its bounds
      Parameters:
      x - The X coordinate of the event, in widget-space (0 is the left edge of this widget)
      y - The Y coordinate of the event, in widget-space (0 is the top edge of this widget)
      button - The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click)
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • onMouseDrag

      @Environment(CLIENT) public InputResult onMouseDrag(int x, int y, int button, double deltaX, double deltaY)
      Notifies this widget that the mouse has been moved while pressed and inside its bounds.
      Parameters:
      x - The X coordinate of the event, in widget-space (0 is the left edge of this widget)
      y - The Y coordinate of the event, in widget-space (0 is the top edge of this widget)
      button - The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click)
      deltaX - The amount of dragging on the X axis
      deltaY - The amount of dragging on the Y axis
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
      Since:
      1.5.0
    • onMouseUp

      @Environment(CLIENT) public InputResult onMouseUp(int x, int y, int button)
      Notifies this widget that the mouse has been released while inside its bounds
      Parameters:
      x - The X coordinate of the event, in widget-space (0 is the left edge of this widget)
      y - The Y coordinate of the event, in widget-space (0 is the top edge of this widget)
      button - The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click)
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • onClick

      @Environment(CLIENT) public InputResult onClick(int x, int y, int button)
      Notifies this widget that the mouse has been pressed and released, both while inside its bounds.
      Parameters:
      x - The X coordinate of the event, in widget-space (0 is the left edge of this widget)
      y - The Y coordinate of the event, in widget-space (0 is the top edge of this widget)
      button - The mouse button that was used. Button numbering is consistent with LWJGL Mouse (0=left, 1=right, 2=mousewheel click)
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • onMouseScroll

      @Environment(CLIENT) public InputResult onMouseScroll(int x, int y, double amount)
      Notifies this widget that the mouse has been scrolled inside its bounds.
      Parameters:
      x - The X coordinate of the event, in widget-space (0 is the left edge of this widget)
      y - The Y coordinate of the event, in widget-space (0 is the top edge of this widget)
      amount - The scrolled amount. Positive values are up and negative values are down.
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • onMouseMove

      @Environment(CLIENT) public InputResult onMouseMove(int x, int y)
      Notifies this widget that the mouse has been moved while inside its bounds.
      Parameters:
      x - The X coordinate of the event, in widget-space (0 is the left edge of this widget)
      y - The Y coordinate of the event, in widget-space (0 is the top edge of this widget)
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
      Since:
      1.5.0
    • onCharTyped

      @Environment(CLIENT) public void onCharTyped(char ch)
      Notifies this widget that a character has been typed. This method is subject to key repeat, and may be called for characters that do not directly have a corresponding keyboard key.
      Parameters:
      ch - the character typed
    • onKeyPressed

      @Environment(CLIENT) public void onKeyPressed(int ch, int key, int modifiers)
      Notifies this widget that a key has been pressed.
      Parameters:
      key - the GLFW scancode of the key
    • onKeyReleased

      @Environment(CLIENT) public void onKeyReleased(int ch, int key, int modifiers)
      Notifies this widget that a key has been released
      Parameters:
      key - the GLFW scancode of the key
    • onFocusGained

      public void onFocusGained()
      Notifies this widget that it has gained focus
    • onFocusLost

      public void onFocusLost()
      Notifies this widget that it has lost focus
    • isFocused

      public boolean isFocused()
      Tests whether this widget has focus.
      Returns:
      true if this widget widget has focus, false otherwise
      See Also:
      GuiDescription.isFocused(WWidget)
    • requestFocus

      public void requestFocus()
      If this widget has a host, requests the focus from the host.
      See Also:
      GuiDescription.requestFocus(WWidget)
    • releaseFocus

      public void releaseFocus()
      If this widget has a host, releases this widget's focus.
      See Also:
      GuiDescription.releaseFocus(WWidget)
    • canFocus

      public boolean canFocus()
      Tests whether this widget can have the focus in the GUI.
      Returns:
      true if this widget can be focused, false otherwise
    • createPeers

      @Deprecated public void createPeers(GuiDescription c)
      Deprecated.
      All widget peers should be added in validate(GuiDescription).
      Creates "heavyweight" component peers
      Parameters:
      c - the top-level Container that will hold the peers
    • paint

      @Environment(CLIENT) public void paint(net.minecraft.client.util.math.MatrixStack matrices, int x, int y, int mouseX, int mouseY)
      Paints this widget.
      Parameters:
      matrices - the rendering matrix stack
      x - this widget's X coordinate on the screen
      y - this widget's Y coordinate on the screen
      mouseX - the X coordinate of the cursor
      mouseY - the X coordinate of the cursor
      Since:
      2.0.0
    • isWithinBounds

      public boolean isWithinBounds(int x, int y)
      Checks whether a location is within this widget's bounds.

      The default implementation checks that X and Y are at least 0 and below the width and height of this widget.

      Parameters:
      x - the X coordinate
      y - the Y coordinate
      Returns:
      true if the location is within this widget, false otherwise
    • renderTooltip

      @Environment(CLIENT) public void renderTooltip(net.minecraft.client.util.math.MatrixStack matrices, int x, int y, int tX, int tY)
      Internal method to render tooltip data. This requires an overriden addTooltip method to insert data into the tooltip - without this, the method returns early, because no work
      Parameters:
      x - the X coordinate of this widget on screen
      y - the Y coordinate of this widget on screen
      tX - the X coordinate of the tooltip
      tY - the Y coordinate of the tooltip
    • validate

      public void validate(GuiDescription host)
      Creates component peers, lays out children, and initializes animation data for this Widget and all its children. The host container must clear any heavyweight peers from its records before this method is called.
      Parameters:
      host - the host GUI description
    • getHost

      @Nullable public final @Nullable GuiDescription getHost()
      Gets the host of this widget.
      Returns:
      the host
      Since:
      2.1.0
      See Also:
      host
    • setHost

      public void setHost(@Nullable @Nullable GuiDescription host)
      Sets the host of this widget without creating peers.
      Parameters:
      host - the new host
      Since:
      2.1.0
      See Also:
      host
    • addTooltip

      @Environment(CLIENT) public void addTooltip(TooltipBuilder tooltip)
      Adds lines to this widget's tooltip. If the lines remain empty after this call, no tooltip will be drawn.
      Parameters:
      tooltip - the builder to add tooltip lines to
    • hit

      public WWidget hit(int x, int y)
      Find the most specific child node at this location. For non-panel widgets, returns this widget.
    • tick

      @Environment(CLIENT) public void tick()
      Executes a client-side tick for this widget.
    • cycleFocus

      @Nullable public @Nullable WWidget cycleFocus(boolean lookForwards)
      Cycles the focus inside this widget.

      If this widget is not focusable, returns null.

      Parameters:
      lookForwards - whether this should cycle forwards (true) or backwards (false)
      Returns:
      the next focused widget, or null if should exit to the parent panel
      Since:
      2.0.0
    • onShown

      @Beta public void onShown()
      Notifies this widget that it is visible and shows any hidden peers of itself and its children.
      Since:
      3.0.0
    • onHidden

      @Beta public void onHidden()
      Notifies this widget that it won't be drawn and hides any visible peers of itself and its children.

      The default implementation releases this widget's focus if it is focused. Overriding implementations might want to do this as well.

      Since:
      3.0.0
    • addPainters

      @Environment(CLIENT) public void addPainters()
      Adds the default background painters to this widget and all children.

      Always called before GuiDescription.addPainters() to allow users to modify painters.

      Since:
      3.0.0
    • isActivationKey

      @Environment(CLIENT) public static boolean isActivationKey(int ch)
      Tests if the provided key code is an activation key for widgets.

      The activation keys are Enter, keypad Enter, and Space.

      Parameters:
      ch - the key code
      Returns:
      whether the key is an activation key
      Since:
      2.0.0