Module technology.sola.engine
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 elementElementType
- 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 Summary
FieldsModifier and TypeFieldDescriptionprotected final List<GuiElement<?,
?>> The list of childrenGuiElement
s.protected GuiElementBounds
Includes only content size.protected final StyleContainer<Style>
TheStyleContainer
for the element. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Creates and initializes a new GuiElement instance. -
Method Summary
Modifier and TypeMethodDescriptionfinal ElementType
Convenience method that callsStyleContainer.addStyle(ConditionalStyle)
for this element'sStyleContainer
wrapping the provided style in aConditionalStyle.always(BaseStyles)
.final ElementType
addStyle
(ConditionalStyle<Style> style) Convenience method that callsStyleContainer.addStyle(ConditionalStyle)
for this element'sStyleContainer
.final ElementType
addStyles
(List<ConditionalStyle<Style>> styles) Convenience method that callsStyleContainer.addStyles(List)
for this element'sStyleContainer
.appendChildren
(GuiElement<?, ?>... children) Adds childGuiElement
s to this element.abstract @Nullable GuiElementDimensions
Calculates content dimensions for this element.events()
<T extends GuiElement<?,
?>>
@Nullable TfindElementById
(String id, Class<T> elementClass) Searches the element tree for an element with the desired id.<T extends GuiElement<?,
?>>
List<T>findElementsByType
(Class<T> elementClass) Searches the element tree for elements with the desired type.protected int
findFocussedChildIndex
(List<GuiElement<?, ?>> children) Utility method to search an array of elements for which one currently has focus and get its index.protected AssetLoaderProvider
Gets theGuiElementBounds
that contains the top, left and full width and height of the element.List<GuiElement<?,
?>> Gets theGuiElementBounds
that contains the top, left of where the content area begins and the width and height of the content area.protected List<GuiElement<?,
?>> @Nullable String
getId()
@Nullable GuiElement<?,
?> void
Invalidates the layout for this element so that it will be recalculated next frame.boolean
isActive()
The active state is for when an element is being interacted with (space key press or mouse pressed).boolean
boolean
boolean
boolean
removeChild
(GuiElement<?, ?> child) Removes a child element from this element.void
Method to render the gui element.protected void
renderChildren
(Renderer renderer) Renders all the element's children.abstract void
renderContent
(Renderer renderer) Method to render the main content of the gui element (not borders, backgrounds, etc.).void
Focuses this element if it is able to have focus.Sets the id of the element.styles()
-
Field Details
-
styleContainer
TheStyleContainer
for the element. -
children
The list of childrenGuiElement
s. -
contentBounds
Includes only content size. Do not manually update this unless you know what you are doing!
-
-
Constructor Details
-
GuiElement
protected GuiElement()Creates and initializes a new GuiElement instance.
-
-
Method Details
-
addStyle
Convenience method that callsStyleContainer.addStyle(ConditionalStyle)
for this element'sStyleContainer
wrapping the provided style in aConditionalStyle.always(BaseStyles)
.- Parameters:
style
- the style to add- Returns:
- this
-
addStyle
Convenience method that callsStyleContainer.addStyle(ConditionalStyle)
for this element'sStyleContainer
.- Parameters:
style
- the style to add- Returns:
- this
-
addStyles
Convenience method that callsStyleContainer.addStyles(List)
for this element'sStyleContainer
.- Parameters:
styles
- the styles to add- Returns:
- this
-
renderContent
Method to render the main content of the gui element (not borders, backgrounds, etc.).- Parameters:
renderer
- theRenderer
instance
-
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
Method to render the gui element. It handles rendering the background and border before then calling therenderContent(Renderer)
method.- Parameters:
renderer
- theRenderer
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
- Returns:
- the
GuiElementEvents
for the element
-
styles
- Returns:
- the
StyleContainer
for the element
-
getParent
- Returns:
- the parent element
-
getBounds
Gets theGuiElementBounds
that contains the top, left and full width and height of the element.- Returns:
- the bounds of the element
-
getContentBounds
Gets theGuiElementBounds
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
- Returns:
- the id of the element
-
setId
Sets the id of the element.- Parameters:
id
- the new id- Returns:
- this
-
findElementById
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 forelementClass
- the class of the element being searched- Returns:
- the element with the desired id or null if not found
-
findElementsByType
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
Removes a child element from this element.- Parameters:
child
- the child element to remove- Returns:
- this
-
getChildren
- Returns:
- an immutable list of children GuiElements
-
appendChildren
Adds childGuiElement
s 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
Renders all the element's children.- Parameters:
renderer
- theRenderer
instance
-
getFocusableChildren
- Returns:
- the list of child elements that are focusable
-
getAssetLoaderProvider
- Returns:
- the
AssetLoaderProvider
instance
-
findFocussedChildIndex
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
-