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 childrenGuiElements.protected GuiElementBoundsIncludes only content size.protected final StyleContainer<Style>TheStyleContainerfor the element. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates and initializes a new GuiElement instance. -
Method Summary
Modifier and TypeMethodDescriptionfinal ElementTypeConvenience method that callsStyleContainer.addStyle(ConditionalStyle)for this element'sStyleContainerwrapping the provided style in aConditionalStyle.always(BaseStyles).final ElementTypeaddStyle(ConditionalStyle<Style> style) Convenience method that callsStyleContainer.addStyle(ConditionalStyle)for this element'sStyleContainer.final ElementTypeaddStyles(List<ConditionalStyle<Style>> styles) Convenience method that callsStyleContainer.addStyles(List)for this element'sStyleContainer.appendChildren(GuiElement<?, ?>... children) Adds childGuiElements to this element.abstract @Nullable GuiElementDimensionsCalculates 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 intfindFocussedChildIndex(List<GuiElement<?, ?>> children) Utility method to search an array of elements for which one currently has focus and get its index.protected AssetLoaderProviderGets theGuiElementBoundsthat contains the top, left and full width and height of the element.Gets theGuiElementBoundsthat contains the top, left of where the content area begins and the width and height of the content area.protected List<GuiElement<?,?>> @Nullable StringgetId()GuiElement<?,?> voidInvalidates the layout for this element so that it will be recalculated next frame.booleanisActive()The active state is for when an element is being interacted with (space key press or mouse pressed).booleanChecks to see if thisGuiElementis currently attached to theGuiDocumentand is part of rendering.booleanbooleanbooleanbooleanremoveChild(GuiElement<?, ?> child) Removes a child element from this element.voidMethod to render the gui element.protected voidrenderChildren(Renderer renderer) Renders all the element's children.abstract voidrenderContent(Renderer renderer) Method to render the main content of the gui element (not borders, backgrounds, etc.).voidFocuses this element if it is able to have focus.abstract ElementTypeself()Sets the id of the element.voidsetVirtualKeyboardVisible(boolean visible) Sets the visibility of the virtual keyboard.styles()
-
Field Details
-
styleContainer
TheStyleContainerfor the element. -
children
The list of childrenGuiElements. -
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'sStyleContainerwrapping 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- theRendererinstance
-
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
-
self
- Returns:
- A correctly typed reference to this
GuiElement
-
render
Method to render the gui element. It handles rendering the background and border before then calling therenderContent(Renderer)method.- Parameters:
renderer- theRendererinstance
-
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 this element is currently active
-
isFocussed
public boolean isFocussed()- Returns:
- true if this element currently has keyboard focus
-
requestFocus
public void requestFocus()Focuses this element if it is able to have focus. -
setVirtualKeyboardVisible
public void setVirtualKeyboardVisible(boolean visible) Sets the visibility of the virtual keyboard.- Parameters:
visible- whether the virtual keyboard should be visible or not
-
isFocusable
public boolean isFocusable()- Returns:
- true if the element is currently focusable
-
events
- Returns:
- the
GuiElementEventsfor the element
-
styles
- Returns:
- the
StyleContainerfor the element
-
getParent
- Returns:
- the parent element
-
getBounds
Gets theGuiElementBoundsthat contains the top, left and full width and height of the element.- Returns:
- the bounds of the element
-
getContentBounds
Gets theGuiElementBoundsthat 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
-
appendChildren
Adds childGuiElements 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. -
isAttached
public boolean isAttached()Checks to see if thisGuiElementis currently attached to theGuiDocumentand is part of rendering.- Returns:
- true if attached
-
renderChildren
Renders all the element's children.- Parameters:
renderer- theRendererinstance
-
getFocusableChildren
- Returns:
- the list of child elements that are focusable
-
getAssetLoaderProvider
- Returns:
- the
AssetLoaderProviderinstance
-
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
-