Skip to main content

UI

Retained mode and DOM-style API to render interactive user-interfaces, with a Solid.js-inspired UI framework built on top.

It's recommended to either use the out-of-the-box UI framework (or build your own) instead of working with the retained mode API directly. Retained mode has been chosen to significantly reduce the overhead of communicating between Lua and the engine for mostly static UIs.

Constants

UI.SCREEN

Root DOM element rendered to the screen in 2D. Shared by all screenspace UI, so you'll want to further restrict/abstract this if exposed to addons in your game.

UI.SCREEN: Element

Functions

UI.createElement

Creates a new containing element element.

UI.createElement(): Element

Returns

NameTypeDescription
elementElementNo description

UI.createTextElement

Creates a new text element.

UI.createTextElement(textContent: string): TextElement

Parameters

NameTypeDefault ValueDescription
textContentstringRequiredInitial text content of the element.

Returns

NameTypeDescription
elementTextElementNo description

UI.removeElement

Removes the element from its parent. No-op if the element has no parent.

UI.removeElement(child: Element | TextElement)

Parameters

NameTypeDefault ValueDescription
childElement | TextElementRequiredNo description

UI.appendElement

Appends an element to the end of another element's children. Removes the child element from its current parent if it has one.

UI.appendElement(parent: Element, child: Element | TextElement)

Parameters

NameTypeDefault ValueDescription
parentElementRequiredElement to append to.
childElement | TextElementRequiredElement to be appended.

UI.getChildren

Returns a list of the element's children. This is a copy, so will not update if the children of the element change (nor will the element's children change if this list is modified).

UI.getChildren(element: Element): (Element | TextElement)[]

Parameters

NameTypeDefault ValueDescription
elementElementRequiredNo description

Returns

NameTypeDescription
children(Element | TextElement)[]No description

UI.getTextContent

Returns the contents of a text element.

UI.getTextContent(element: TextElement): string

Parameters

NameTypeDefault ValueDescription
elementTextElementRequiredNo description

Returns

NameTypeDescription
textContentstringNo description

UI.setTextContent

Updates the contents of a text element.

UI.setTextContent(element: TextElement, textContent: string)

Parameters

NameTypeDefault ValueDescription
elementTextElementRequiredNo description
textContentstringRequiredNo description

UI.setStyle

Sets a style on the element when in the specified state.

Styles are used in the order Clicked -> Hovered -> Always -> Engine Defaults. The first state as per the order above which has a style set will be used.

UI.setStyle(element: Element, style: UI.Style, value: number | Colour | UI.SizeMode | UI.ChildAlignment | UI.Direction | UI.TextWrapping | UI.TextAlignment, when: UI.StyleMode)

Parameters

NameTypeDefault ValueDescription
elementElementRequiredNo description
styleUI.StyleRequiredNo description
valuenumber | Colour | UI.SizeMode | UI.ChildAlignment | UI.Direction | UI.TextWrapping | UI.TextAlignmentRequiredAccepted type depends on the style being set.
whenUI.StyleModeRequiredIn which state the style will be used.

UI.removeStyle

Removes the style from the element when in the specified state.

Styles are used in the order Clicked -> Hovered -> Always -> Engine Defaults. Removing a style from one of the layers means the first set style in a lower state (as above) will be used instead.

UI.removeStyle(element: Element, style: UI.Style, when: UI.StyleMode)

Parameters

NameTypeDefault ValueDescription
elementElementRequiredNo description
styleUI.StyleRequiredNo description
whenUI.StyleModeRequiredIn which state the style will be removed.

UI.enableDebugger

Enables the Clay debugger.

UI.enableDebugger()

UI.disableDebugger

Disables the Clay debugger.

UI.disableDebugger()

UI.isDebuggerEnabled

Returns whether the Clay debugger is enabled.

UI.isDebuggerEnabled(): boolean

Returns

NameTypeDescription
debuggerEnabledbooleanNo description

UI.addEventListener

Listen to events raised on the element to support user interaction.

UI.addEventListener(element: Element, eventName: 'mouseDown' | 'mouseUp', callback: function): function

Parameters

NameTypeDefault ValueDescription
elementElementRequiredNo description
eventName'mouseDown' | 'mouseUp'RequiredNo description
callbackfunctionRequiredNo description

Returns

NameTypeDescription
callbackfunctionCallback that was passed in.

UI.removeEventListener

Remove an event listener from the element.

UI.removeEventListener(element: Element, eventName: 'mouseDown' | 'mouseUp', callback: function)

Parameters

NameTypeDefault ValueDescription
elementElementRequiredNo description
eventName'mouseDown' | 'mouseUp'RequiredNo description
callbackfunctionRequiredCallback that was passed to addEventListener.

UI.createComponent

Creates a component from the given function. This sets up the component function as stateful and normalises its parameters.

UI.createComponent(componentFn: fun(props: table, children: fun(): (Element | TextElement)[]): (fun(): (Element | TextElement)[])[] | fun(): (fun(): (Element | TextElement)[])[]): fun(props?: table, children?: (fun(): (Element | TextElement)[])[]): fun(): (Element | TextElement)[]

Parameters

NameTypeDefault ValueDescription
componentFnfun(props: table, children: fun(): (Element | TextElement)[]): (fun(): (Element | TextElement)[])[] | fun(): (fun(): (Element | TextElement)[])[]RequiredNo description

Returns

NameTypeDescription
componentfun(props?: table, children?: (fun(): (Element | TextElement)[])[]): fun(): (Element | TextElement)[]The normalised component function.

UI.render

Resolves the component tree and appends all root nodes to the given element. Should be called only once when initialising the UI.

UI.render(component: fun(props?: table, children?: (fun(): (Element | TextElement)[])[]): fun(): (Element | TextElement)[], element: Element)

Parameters

NameTypeDefault ValueDescription
componentfun(props?: table, children?: (fun(): (Element | TextElement)[])[]): fun(): (Element | TextElement)[]RequiredRoot component of the application.
elementElementRequiredUI element to mount the components to.

UI.toString

Stringifies the element and its children into a format resembling HTML/XML Useful for debugging and snapshot testing.

UI.toString(element: Element | TextElement, indent?: integer): string

Parameters

NameTypeDefault ValueDescription
elementElement | TextElementRequiredNo description
indentinteger?nilNumber of spaces to indent all lines by. Defaults to 0.

Returns

NameTypeDescription
elementMarkupstringNo description

Enums

Style

Style properties of Elements.

Value
Width
MinWidth
MaxWidth
WidthMode
Height
MinHeight
MaxHeight
HeightMode
PaddingLeft
PaddingRight
PaddingTop
PaddingBottom
Gap
AlignmentX
AlignmentY
Direction
BackgroundColour
CornerRadiusTopLeft
CornerRadiusTopRight
CornerRadiusBottomLeft
CornerRadiusBottomRight
BorderColour
BorderLeft
BorderRight
BorderTop
BorderBottom
BorderBetween
TextColour
FontSize
LetterSpacing
LineHeight
TextWrapping
TextAlignment

StyleMode

When to apply a given style. Styles apply in the order Clicked -> Hovered -> Always -> Engine Defaults.

Value
Always
Hovered
Clicked

SizeMode

Determines how the Element will respond to changes in width/height by its parent and children. FixedPercent fixes the Element's width/height to a percentage (0-1) of the parent's size, using the first of Width, MaxWidth or MinWidth which is defined (and *Height for height).

Value
Shrink
Grow
FixedPercent

ChildAlignment

Alignment of children on a given axis relative to this Element's content box.

Value
Start
Centre
End

Direction

Direction that children are laid out in this Element.

Value
Horizontal
Vertical

TextWrapping

Where in the string it's valid to insert line breaks in order to prevent overflow.

Value
Words
Newlines
None

TextAlignment

Alignment of TextElements which are the immediate child of this Element.

Value
Left
Centre
Right