Class GuiElement<Style extends BaseStyles,ElementType extends GuiElement<Style,ElementType>>

java.lang.Object
technology.sola.engine.graphics.gui.GuiElement<Style,ElementType>
Type Parameters:
Style - the style type for the element
ElementType - this element's type, so it can be used for method chaining
Direct Known Subclasses:
BaseInputGuiElement, ImageGuiElement, SectionGuiElement, SpriteGuiElement, TextGuiElement

@NullMarked public abstract class GuiElement<Style extends BaseStyles,ElementType extends GuiElement<Style,ElementType>> extends Object
GuiElement is the base class for all elements that can be rendered in a GuiDocument.
  • Field Details

  • Constructor Details

    • GuiElement

      protected GuiElement()
      Creates and initializes a new GuiElement instance.
  • Method Details

    • addStyle

      public final ElementType addStyle(Style style)
      Convenience method that calls StyleContainer.addStyle(ConditionalStyle) for this element's StyleContainer wrapping the provided style in a ConditionalStyle.always(BaseStyles).
      Parameters:
      style - the style to add
      Returns:
      this
    • addStyle

      public final ElementType addStyle(ConditionalStyle<Style> style)
      Convenience method that calls StyleContainer.addStyle(ConditionalStyle) for this element's StyleContainer.
      Parameters:
      style - the style to add
      Returns:
      this
    • addStyles

      public final ElementType addStyles(List<ConditionalStyle<Style>> styles)
      Convenience method that calls StyleContainer.addStyles(List) for this element's StyleContainer.
      Parameters:
      styles - the styles to add
      Returns:
      this
    • renderContent

      public abstract void renderContent(Renderer renderer)
      Method to render the main content of the gui element (not borders, backgrounds, etc.).
      Parameters:
      renderer - the Renderer instance
    • calculateContentDimensions

      public abstract @Nullable GuiElementDimensions calculateContentDimensions()
      Calculates content dimensions for this element. If its dimensions are derived from only its children then it should return null.
      Returns:
      the dimensions of the content of this element or null if only children dimensions matter
    • render

      public void render(Renderer renderer)
      Method to render the gui element. It handles rendering the background and border before then calling the renderContent(Renderer) method.
      Parameters:
      renderer - the Renderer instance
    • isHovered

      public boolean isHovered()
      Returns:
      true if the element is currently hovered
    • isActive

      public boolean isActive()
      The active state is for when an element is being interacted with (space key press or mouse pressed).
      Returns:
      true if element is currently active
    • isFocussed

      public boolean isFocussed()
      Returns:
      true if element currently has keyboard focus
    • requestFocus

      public void requestFocus()
      Focuses this element if it is able to have focus.
    • isFocusable

      public boolean isFocusable()
      Returns:
      true if the element is currently focusable
    • events

      public GuiElementEvents events()
      Returns:
      the GuiElementEvents for the element
    • styles

      public StyleContainer<Style> styles()
      Returns:
      the StyleContainer for the element
    • getParent

      public @Nullable GuiElement<?,?> getParent()
      Returns:
      the parent element
    • getBounds

      public GuiElementBounds getBounds()
      Gets the GuiElementBounds that contains the top, left and full width and height of the element.
      Returns:
      the bounds of the element
    • getContentBounds

      public GuiElementBounds getContentBounds()
      Gets the GuiElementBounds that contains the top, left of where the content area begins and the width and height of the content area.
      Returns:
      the content bounds of the element
    • getId

      public @Nullable String getId()
      Returns:
      the id of the element
    • setId

      public ElementType setId(String id)
      Sets the id of the element.
      Parameters:
      id - the new id
      Returns:
      this
    • findElementById

      public <T extends GuiElement<?, ?>> @Nullable T findElementById(String id, Class<T> elementClass)
      Searches the element tree for an element with the desired id. Returns null if not found.
      Type Parameters:
      T - the element type
      Parameters:
      id - the id of the element to search for
      elementClass - the class of the element being searched
      Returns:
      the element with the desired id or null if not found
    • findElementsByType

      public <T extends GuiElement<?, ?>> List<T> findElementsByType(Class<T> elementClass)
      Searches the element tree for elements with the desired type.
      Type Parameters:
      T - the element type
      Parameters:
      elementClass - the class of the elements being searched
      Returns:
      the list of elements with the desired type
    • removeChild

      public ElementType removeChild(GuiElement<?,?> child)
      Removes a child element from this element.
      Parameters:
      child - the child element to remove
      Returns:
      this
    • getChildren

      public List<GuiElement<?,?>> getChildren()
      Returns:
      an immutable list of children GuiElements
    • appendChildren

      public ElementType appendChildren(GuiElement<?,?>... children)
      Adds child GuiElements to this element. How or if they will be rendered depends on the implementing element.
      Parameters:
      children - the child elements to add
      Returns:
      this
    • isLayoutChanged

      public boolean isLayoutChanged()
      Returns:
      true if the layout of this element has been changed
    • invalidateLayout

      public void invalidateLayout()
      Invalidates the layout for this element so that it will be recalculated next frame.
    • renderChildren

      protected void renderChildren(Renderer renderer)
      Renders all the element's children.
      Parameters:
      renderer - the Renderer instance
    • getFocusableChildren

      protected List<GuiElement<?,?>> getFocusableChildren()
      Returns:
      the list of child elements that are focusable
    • getAssetLoaderProvider

      protected AssetLoaderProvider getAssetLoaderProvider()
      Returns:
      the AssetLoaderProvider instance
    • findFocussedChildIndex

      protected int findFocussedChildIndex(List<GuiElement<?,?>> children)
      Utility method to search an array of elements for which one currently has focus and get its index.
      Parameters:
      children - the list of elements to search
      Returns:
      the index of the element with focus or -1 if not found