Class WWidget

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

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

    Properties
    Modifier and Type
    Property
    Description
    public java.lang.Boolean
    Returns whether the user is hovering over this widget.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
    The height of this widget, defaults to 18 pixels.
    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 narrations of this widget to a narration builder.
    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
    Tests whether this widget receives mouse hovering status.
    boolean
    Checks whether this widget can be resized using setSize(int, int).
    int
    Gets the absolute X coordinate of this widget.
    int
    Gets the absolute Y coordinate of this widget.
    Returns the focus model of this widget.
    int
     
    Gets the host of this widget.
    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.
    Returns whether the user is hovering over this widget.
    static boolean
    Tests if the provided key code is an activation key for widgets.
    boolean
    Tests whether this widget has focus.
    final boolean
    Returns whether the user is hovering over this widget.
    boolean
    Returns whether this widget can be narrated.
    boolean
    isWithinBounds(int x, int y)
    Checks whether a location is within this widget's bounds.
    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.
    onKeyPressed(int ch, int key, int modifiers)
    Notifies this widget that a key has been pressed.
    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 horizontalAmount, double verticalAmount)
    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(DrawContext context, 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(DrawContext context, 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
    Sets the host of this widget and all its children without creating peers.
    final void
    setHovered(boolean hovered)
    Sets the hovered property.
    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.
    boolean
    Checks if this widget should be rendered in dark mode.
    void
    Executes a client-side tick for this widget.
    void
    Creates component peers 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

      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 horizontalAmount, double verticalAmount)
      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)
      horizontalAmount - The scrolled horizontal amount. Positive values are right and negative values are left.
      verticalAmount - The scrolled vertical 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 InputResult 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
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • onKeyPressed

      @Environment(CLIENT) public InputResult onKeyPressed(int ch, int key, int modifiers)
      Notifies this widget that a key has been pressed.
      Parameters:
      key - the GLFW scancode of the key
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • onKeyReleased

      @Environment(CLIENT) public InputResult onKeyReleased(int ch, int key, int modifiers)
      Notifies this widget that a key has been released
      Parameters:
      key - the GLFW scancode of the key
      Returns:
      InputResult.PROCESSED if the event is handled, InputResult.IGNORED otherwise.
    • 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:
    • requestFocus

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

      public void releaseFocus()
      If this widget has a host, releases this widget's focus.
      See Also:
    • 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
    • paint

      @Environment(CLIENT) public void paint(DrawContext context, int x, int y, int mouseX, int mouseY)
      Paints this widget.
      Parameters:
      context - the draw context
      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(DrawContext context, int x, int y, int tX, int tY)
      Internal method to render tooltip data. This requires an overridden addTooltip method to insert data into the tooltip - without this, the method returns early because of no work.
      Parameters:
      context - the draw context
      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 and initializes animation data for this Widget and all its children. The host screen handler must clear any heavyweight peers from its records before this method is called.

      This method must be called on the root panel of any screen once the widgets have been initialized.

      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:
    • setHost

      public void setHost(GuiDescription host)
      Sets the host of this widget and all its children without creating peers.
      Parameters:
      host - the new host
      Since:
      2.1.0
      See Also:
    • 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.
    • getFocusModel

      @Nullable public @Nullable FocusModel<?> getFocusModel()
      Returns the focus model of this widget. The focus model provides the focusable areas of this widget, and handles switching through them.

      If this widget can focus, it should return a nonnull focus model. The default implementation returns FocusModel.simple(this) when the widget can be focused.

      Returns:
      the focus model, or null if not available
      Since:
      7.0.0
    • onShown

      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

      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
    • canHover

      public boolean canHover()
      Tests whether this widget receives mouse hovering status.
      Returns:
      true if this widget receives hovering status, false otherwise
      Since:
      4.2.0
    • hoveredProperty

      public ObservableProperty<Boolean> hoveredProperty()
      Returns whether the user is hovering over this widget. The result is an observable property that can be modified and listened to.

      This property takes into account isWithinBounds(int, int) to check if the cursor is within the bounds, as well as canHover() to enable hovering at all.

      Hovering is used by LibGui itself mostly for narration support. For rendering, it might be preferable that you check the mouse coordinates in paint() directly. That lets you react to different parts of the widget being hovered over.

      Returns:
      the hovered property
      Since:
      4.2.0
      See Also:
    • isHovered

      public final boolean isHovered()
      Returns whether the user is hovering over this widget. This is equivalent to calling hoveredProperty().get().
      Returns:
      true if this widget is hovered, false otherwise
      Since:
      4.2.0
    • setHovered

      public final void setHovered(boolean hovered)
      Sets the hovered property. This is equivalent to calling hoveredProperty().set(hovered).
      Parameters:
      hovered - the new value; true if hovered, false otherwise
      Since:
      4.2.0
    • isNarratable

      public boolean isNarratable()
      Returns whether this widget can be narrated.
      Returns:
      whether this widget can be narrated
      Since:
      4.2.0
      See Also:
    • addNarrations

      @Environment(CLIENT) public void addNarrations(NarrationMessageBuilder builder)
      Adds the narrations of this widget to a narration builder. Narrations will only apply if this widget is narratable.

      The widget needs to be focusable or hoverable, and also be focused/hovered for narrations to be added.

      Parameters:
      builder - the narration builder, cannot be null
      Since:
      4.2.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
    • shouldRenderInDarkMode

      @Environment(CLIENT) public boolean shouldRenderInDarkMode()
      Checks if this widget should be rendered in dark mode.

      If the widget has a host that forces dark mode, the forced value is used. Otherwise, this method returns LibGui.isDarkMode().

      Painting should respect this value for general-purpose widgets intended for use in multiple different GUIs.

      Returns:
      true if this widget should be rendered in dark mode, false otherwise
      Since:
      7.1.0