Class TElement

java.lang.Object
com.thecsdev.common.scene.Node<TElement>
com.thecsdev.commonmc.api.client.gui.TElement
All Implemented Interfaces:
INode<TElement>, INodeBounded<TElement,Bounds2i>, INodeRenderable<TElement, TGuiGraphics>, Iterable<TElement>, Collection<TElement>
Direct Known Subclasses:
TBlockStateElement, TClickableWidget, TContextMenu, TEntityElement, TFillColorElement, THoverScrollElement, TItemStackElement, TLabelElement, TPanelElement, TScreen, TSeparatorElement, TStretchedTextElement, TTextureElement, TTooltip, TWindowElement

@Environment(CLIENT) @Reflected(AccessorTElement.class) @Virtual public class TElement extends Node<TElement> implements INodeBounded<TElement,Bounds2i>, INodeRenderable<TElement, TGuiGraphics>
Represents a GUI element in TCDCommons's GUI system.
  • Field Details

  • Constructor Details

    • TElement

      public TElement()
  • Method Details

    • getSelf

      @NotNull public final @NotNull TElement getSelf()
      Description copied from class: Node
      Returns this Node, cast to its concrete type.

      Override this method in your subclasses and return this. For example:

      public class MyNode extends Node<MyNode>
      {
          protected MyNode getSelf() { return this; }
      }
      
      Specified by:
      getSelf in class Node<TElement>
      Returns:
      this Node instance, cast to the type parameter E.
    • getBaseType

      @NotNull public final @NotNull Class<TElement> getBaseType()
      Description copied from class: Node
      Returns the Class object that represents the base type of this node.

      The base type is the "root" Class in an Node hierarchy, from which all other specific Node types in a scene graph are derived. For example, if LightNode, MeshNode, and CameraNode all extend a common Node class, then Node is the base type for that hierarchy.

      Override this method in your subclasses and return the class literal of your subclass. For example:

      public class MyNode extends Node<MyNode>
      {
          public Class<MyNode> getBaseType() { return MyNode.class; }
      }
      
      Specified by:
      getBaseType in interface INode<TElement>
      Specified by:
      getBaseType in class Node<TElement>
      See Also:
    • renderCallback

      @Virtual public void renderCallback(@NotNull @NotNull TGuiGraphics pencil)
      Description copied from interface: INodeRenderable
      Callback method that is invoked when this Node is being rendered.
      Specified by:
      renderCallback in interface INodeRenderable<TElement, TGuiGraphics>
      Parameters:
      pencil - The rendering context.
    • postRenderCallback

      @Virtual public void postRenderCallback(@NotNull @NotNull TGuiGraphics pencil)
      Description copied from interface: INodeRenderable
      Callback method that is invoked after this Node and all of their children have been rendered.
      Specified by:
      postRenderCallback in interface INodeRenderable<TElement, TGuiGraphics>
      Parameters:
      pencil - The rendering context.
    • toString

      @Virtual public String toString()
      Overrides:
      toString in class Object
    • getBounds

      @NotNull public final @NotNull Bounds2i getBounds()
      Description copied from interface: INodeBounded
      Returns the bounding box of this Node.
      Specified by:
      getBounds in interface INodeBounded<TElement,Bounds2i>
    • getContentBounds

      @NotNull public final @NotNull Bounds2i getContentBounds()
      Returns the Bounds2i that encapsulates all child bounding boxes as one. The returned bounding box shall represent the exact space all children's bounding boxes collectively occupy at the moment. This is done using each child's getBounds().

      Does NOT return null. If there are no children, returns a Bounds2i instance that has this TElement's position but zero size (aka width and height of 0).

    • getClient

      @NonExtendable @Nullable public @Nullable net.minecraft.client.Minecraft getClient()
      Returns the Minecraft client instance that last opened this TElement's (grand/)parent TScreen, if any.
    • boundsProperty

      public final NotNullProperty<Bounds2i> boundsProperty()
      The NotNullProperty that holds the bounding box of this TElement.
    • screenProperty

      public final ObjectProperty<TScreen> screenProperty()
      Returns the ObjectProperty for this TElement's parent/grandparent TScreen element.
      API Note:
      Read only. Owned by TElement.TElementPropertyAccessor.
    • visibleProperty

      public final BooleanProperty visibleProperty()
      Returns the BooleanProperty that controls the visibility of this TElement. Invisible elements do not render and cannot be interacted with.
    • clipsDescendantsProperty

      public final BooleanProperty clipsDescendantsProperty()
      Returns the BooleanProperty that controls whether this TElement will clip (hide) any part of its descendant elements that would extend beyond its rectangular boundaries.
    • focusableProperty

      public final BooleanProperty focusableProperty()
      Returns the BooleanProperty that controls whether this TElement should be receiving focus via user input such as mouse and keyboard.
      API Note:
      Not to be confused with isFocusable(). This property is about telling the TElement if it should be focusable, and isFocusable() is about the TElement making the final decision.
    • hoverableProperty

      public final BooleanProperty hoverableProperty()
      Returns the BooleanProperty that controls whether this TElement should be detected as hovered by the mouse cursor.
      API Note:
      Not to be confused with isHoverable(). This property is about telling the TElement if it should be hoverable, and isHoverable() is about the TElement making the final decision.
    • tooltipProperty

      public final ObjectProperty<Function<@NotNull TElement, @NotNull TElement>> tooltipProperty()
      The ObjectProperty that holds a supplier Function for the tooltip TElement that is to be rendered as this TElement's tooltip.

      Reason Functions are used is for optimization, as creating a TElement instance is much more expensive than a supplier Function.

      The first argument of the Function is this TElement instance, and the Function is to return a new TElement instance acting as the tooltip.

      While the function itself can be set to null, the TElement it returns must not be null!

      API Note:
      The tooltip element is only rendered visually, should serve no functional purpose, and should not be a child of another element.
    • contextMenuProperty

      public final ObjectProperty<Function<@NotNull TElement, @Nullable TContextMenu>> contextMenuProperty()
      The ObjectProperty that holds a supplier Function for the context menu TContextMenu that is to be shown when this TElement is right-clicked.

      Reason Functions are used is for optimization, as creating a TContextMenu instance is much more expensive than a supplier Function.

      The first argument of the Function is this TElement instance, and the Function is to return a new TContextMenu instance acting as the context menu.

      The function itself can be set to null, and the TContextMenu it returns may also be null.

    • isVisible

      public final boolean isVisible()
      Returns true if this TElement is fully visible and is not being hidden by a parent TElement either.
    • isHovered

      public final boolean isHovered()
      Returns true if this TElement is currently being hovered by the mouse cursor.
      API Note:
      Returns false if screenProperty()'s value is null.
    • isFocused

      public final boolean isFocused()
      Returns true if this TElement is currently the focused element of the TScreen.
      API Note:
      Returns false if screenProperty()'s value is null.
    • isHoveredOrFocused

      public final boolean isHoveredOrFocused()
    • isFocusable

      @Virtual public boolean isFocusable()
      Returns true if this TElement should be receiving focus via user input such as mouse and keyboard.
      See Also:
      API Note:
      Overrides should take focusableProperty() into account.
    • isHoverable

      @Virtual public boolean isHoverable()
      Returns true if this TElement should be detected as hovered by the mouse cursor. When false, mouse hovering over this element will "go though" this element, thus hovering an element below it instead.
      See Also:
      API Note:
      Overrides should take hoverableProperty() into account.
    • getTooltip

      @Nullable public final @Nullable TElement getTooltip()
      Returns the tooltip TElement that should be rendered whenever this TElement is hovered.
      If tooltipProperty()'s value is null, a tooltip element is sought in this element's (grand/)parents.
    • invalidateTooltipCache

      public final void invalidateTooltipCache()
      Invalidates the cached currentTooltip value. This cached value is supplied by the tooltipProperty(), and is held for optimization purposes.

      Call this in the event a property change requires updating the tooltip.

    • move

      public final void move(int dX, int dY)
      Moves this TElement and all of its (grand/)children.
      Parameters:
      dX - The amount to move in the X axis.
      dY - The amount to move in the Y axis.
    • moveChildren

      public final void moveChildren(int dX, int dY)
      Moves all of this TElement's (grand/)children.
      Parameters:
      dX - The amount to move in the X axis.
      dY - The amount to move in the Y axis.
    • moveTo

      public final void moveTo(int x, int y)
      Moves this TElement to the given position. This affects children as well, by moving them based on the difference between the old and new position.
      Parameters:
      x - New X position.
      y - New Y position.
    • setBounds

      public final void setBounds(int x, int y, int width, int height)
      Sets the value of boundsProperty(). This does not move children or affect their bounding boxes.
      Parameters:
      x - The new X position.
      y - The new Y position.
      width - The new width.
      height - The new height.
    • setBounds

      public final void setBounds(@NotNull @NotNull Bounds2i bounds)
      Sets the value of boundsProperty(). This does not move children or affect their bounding boxes.
      Parameters:
      bounds - The new Bounds2i.
    • setBounds

      public final void setBounds(@NotNull @NotNull UDim2 position, @NotNull @NotNull UDim2 size) throws NullPointerException, IllegalStateException
      Sets the value of boundsProperty(), using UDims to calculate the new bounds relative to parent bounds. This does not move children or affect their bounding boxes.
      Parameters:
      position - New position.
      size - New size.
      Throws:
      NullPointerException - If an argument is null.
      IllegalStateException - If Node.getParent() is null.
    • findElementAt

      @Nullable public final @Nullable TElement findElementAt(int screenX, int screenY)
      Attempts to find a TElement at the given screen-space position.
      Parameters:
      screenX - Screen-space X position.
      screenY - Screen-space Y position.
      API Note:
      Usually used internally for TScreen.hoveredElementProperty() value calculation. This method assumes this TElement does not clip descendants.
    • forEachVisible

      public final void forEachVisible(@NotNull @NotNull Consumer<TElement> action, boolean recursive) throws NullPointerException
      Calls Consumer.accept(Object) for each child TElement whose isVisible() returns true.
      Parameters:
      action - The action to perform.
      recursive - Whether to call the Consumer recursively for (grand/)children.
      Throws:
      NullPointerException - If the argument is null.
    • getCursor

      @Contract(pure=true) @Virtual @NotNull public @NotNull CursorType getCursor()
      Returns CursorType that represents the visual look the user's mouse cursor should have when this TElement is hovered.
    • clearAndInit

      public final void clearAndInit()
      (Re/)initializes this TElement and its children.

      If this TElement instance's Class overrides initCallback(); Node.clear() is called and then initCallback() is called.

      Once initialized, recursively calls this method for each child, and then invokes eInitialized.

      API Note:
      Automatically called by an initializing TScreen.
    • initCallback

      @Reflected @Virtual protected void initCallback()
      Callback method that is invoked when this TElement is initializing. You may override this method to create and add child TElements.
      See Also:
      API Note:
      Automatically called by an initializing TScreen.
    • inputCallback

      @Virtual public boolean inputCallback(@NotNull TInputContext.InputDiscoveryPhase phase, @NotNull @NotNull TInputContext context) throws NullPointerException
      A callback method that is invoked when the user makes an input.
      Parameters:
      phase - The input discovery phase.
      context - Information about the user's input.
      Returns:
      true if this TElement handled the input.
      Throws:
      NullPointerException - If an argument is null.
    • tickCallback

      @Virtual protected void tickCallback()
      Callback method that is invoked automatically every GUI tick, if and after Node.getParent() ticks.

      For performance optimization reasons, this is invoked if this element is contained within its parent's getBounds().

      Note that this is regardless of what isVisible() returns. The only requirements are the element being "within parent bounds" and the parent having ticked.

    • hoverGainedCallback

      @Reflected @Virtual protected void hoverGainedCallback()
      A callback method that is invoked whenever this TElement becomes hovered.
      See Also:
    • hoverLostCallback

      @Reflected @Virtual protected void hoverLostCallback()
      A callback method that is invoked whenever this TElement stops being hovered.
      See Also:
    • focusGainedCallback

      @Reflected @Virtual protected void focusGainedCallback()
      A callback method that is invoked whenever this TElement gains focus.
      See Also:
    • focusLostCallback

      @Reflected @Virtual protected void focusLostCallback()
      A callback method that is invoked whenever this TElement loses focus.
      See Also:
    • dragStartCallback

      @Reflected @Virtual protected void dragStartCallback()
      A callback method that is invoked whenever this TElement starts being dragged.
      See Also:
    • dragEndCallback

      @Reflected @Virtual protected void dragEndCallback()
      A callback method that is invoked whenever this TElement stops being dragged.
      See Also:
    • addRel

      public final void addRel(TElement child)
      Adds the child via Node.add(Node) and repositions it by this element's bounds using move(int, int).
      Parameters:
      child - The child to add and reposition relative to this element.
    • removeRel

      public final void removeRel(TElement child)
      Removes the child via Node.remove(Node) and repositions it by the inverse of this element's bounds using move(int, int).
      Parameters:
      child - The child to remove and reposition relative to this element.
    • showContextMenu

      @Nullable public final @Nullable TContextMenu showContextMenu()
      Shows the context menu for this TElement, if available in contextMenuProperty().
      Returns:
      The shown TContextMenu, or null if none was shown.