Skip to content

ui.Component

Overview

Base type common to all user interface elements. The following components inherit from this:

Examples

Positioning and Layout
-- Resize a component to 100x200 interface units and move it to 50 units from the top left of its parent
component:SetPos(50, 50)
component:SetSize(100, 200)

-- Centre the component on its parent and resize it to 300x150 interface units
component:SetPos(0, 0, 0.5, 0.5, -0.5, -0.5)
component:SetSize(300, 150)

-- Resize a component to 30% of the parent's width and 100% of the height, and place it in the centre
component:SetPos(0, 0, 0.3, 1.0, -0.5, 0.0)
component:SetSize(0, 0, 0.3, 1.0)
Styling and Colours
-- Use a specific style
component.stylesheetID = myStyleID
-- Change the colour to red with a slight transparency
component.colour = Vector4.new(1.0, 0.0, 0.0, 0.7)
-- Make it more transparent
component.alpha = component.alpha - 50
-- Set the colour to blue using a hex colour code
component.rgba = 0x0000FF77

Positioning and Layout

Methods

SetX/SetY/SetPos

function Component:SetX(x : Int, parentPercentageX : Float?, selfPercentageX : Float?)
function Component:SetY(y : Int, parentPercentageY : Float?, selfPercentageY : Float?)

function Component:SetPos(
    x : Int, y : Int,
    parentPercentageX : Float?, parentPercentageY : Float?,
    selfPercentageX : Float?, selfPercentageY : Float?)

Moves this component's origin to the provided coordinates, optionally taking into account the parent's dimensions, and this component's own dimensions.

Info

SetPos should be preferred over using SetX and SetY separately as it involves less layout operations.

Parameter Type Description
x Int Absolute horizontal coordinate to move this component to in interface units
y Int Absolute vertical coordinate to move this component to in interface units
parentPercentageX Float? If specified, offsets this component's horizontal origin based on a percentage of its parent's width.
parentPercentageY Float? If specified, offsets this component's vertical origin based on a percentage of its parent's height.
selfPercentageX Float? If specified, offsets this component's horizontal origin based on a percentage of its width.
selfPercentageY Float? If specified, offsets this component's vertical origin based on a percentage of its height.

Example

-- Move the component to 100 units from the left and 50 units from the top
component:SetX(100)
component:SetY(50)

-- Place the component's origin on the parent's center and offset it 50 units to the left
component:SetX(-50, 0.5)
component:SetY(0, 0.5)

-- Right-align the component on its parent, assuming it has a width of 125
component:SetX(-125, 1.0)

-- Right-align the component on its parent, taking into account its actual width
component:SetX(0, 1.0, -1.0)

-- Center the component on its parent
component:SetX(0, 0.5, -0.5)
component:SetY(0, 0.5, -0.5)

-- Right-align the component to the parent's center
component:SetX(0, 0.5, -1.0)
component:SetY(0, 0.5, -0.5)

SetWidth/SetHeight/SetSize

function Component:SetWidth(x : Int, parentPercentageX : Float?)
function Component:SetHeight(y : Int, parentPercentageY : Float?)
function Component:SetSize(x : Int, y : Int, parentPercentageX : Float?, parentPercentageY : Float?)

Sets the width/height for this component, optionally taking into account the parent's dimensions.

Info

SetSize should be preferred over using SetX and SetY separately as it involves less layout operations.

Parameter Type Description
x Int Absolute width in interface units to resize this component to.
y Int Absolute height in interface units to resize this component to.
parentPercentageX Float? If specified, offsets this component's width based on a percentage of its parent's width.
parentPercentageY Float? If specified, offsets this component's height based on a percentage of its parent's height.

Example

-- Resize this component to 100x50
component:SetWidth(100)
component:SetHeight(50)
-- or
component:SetSize(-50, 50)

-- Resize this component to 50% of the parent's dimensions, but make it 50 interface units narrower
component:SetWidth(-50, 0.5)
component:SetHeight(0, 0.5)
-- or
component:SetSize(-50, 0, 0.5, 0.5)

SetAspectRatio

function Component:SetAspectRatio(widthRatio : Int, heightRatio : Int, mode : ui.AspectRatioMode)

Sets the target aspect ratio the component should follow, and how it should be applied. Can also be used to disable aspect ratio scaling.

Info

Invalid width and height ratio values will be replaced with 1 when calling this function.

Parameter Type Description
widthRatio Int Target width aspect ratio
heightRatio Int Target height aspect ratio
mode ui.AspectRatioMode Indicates how the component scales based on its target aspect ratio

Example

-- Set the component to a standard 16:9 aspect ratio, scaling its width
component:SetAspectRatio(16, 9, ui.AspectRatioMode.SCALE_WIDTH)

-- Disable aspect ratio scaling
component:SetAspectRatio(1, 1, ui.AspectRatioMode.NONE)

Destroy

function Component:Destroy()

Destroys this component, along with any other components that might have been attached to it.

Note

Plugins can't delete components belonging to other plugins.

MoveToFront/MoveToBack

function Component:MoveToFront()
function Component:MoveToBack()

Moves this component to the front/back of the render order, causing it to draw on top/below of components that have the same parent.

UpdateLayout

function Component:UpdateLayout()

Triggers a layout update on this component.

Info

You should generally never need to call this directly, as it is automatically called whenever a layout modifying operation (eg. SetSize) occurs on a component.

Properties

Name Type Description
position ui.Pos Gets or sets this component's origin.
size ui.Size Gets or sets the dimensions for this component.
isSafeAreaRelative Bool Whether or not layout operations involving this component take into account the device's safe areas.
x Int (Read-only) Current horizontal position for this component relative to its parent.
y Int (Read-only) Current vertical position for this component relative to its parent.
width Int (Read-only) Current width for this component.
height Int (Read-only) Current height for this component.
xyLocal Vector2 (Read-only) Current XY position for this component relative to its parent.
xyGlobal Vector2 (Read-only) Current absolute XY position for this component.

Handling Events

Components can set as listeners for certain events, allowing developers to trigger actions in response. These range from events triggered on that component, such as mouse clicks, to global events, such as variable transmissions.

ui.Hook

The ui.Hook table contains a list of all potential event hooks that can be attached to a component.

Associated Enumerations

ui.SelectionChangeEvent

Identifies the source of a ONSELECTIONCHANGED event on a component's entry.

Value Description
DESELECTED This entry has been deselected by the user.
SELECTED This entry has been selected by the user.
ERROR_MAX_SELECTED This entry cannot be selected as the maximum number of entries are already selected.
ERROR_NOT_SELECTABLE This entry cannot be selected as it's disabled.
Component State Change Events
Name Parameters Description
ONBUTTONPRESSED
Supported by ui.Button, ui.Check
self : Component<T>
currentValue : Int
previousValue : Int
Triggered whenever the user interacts with this button/check.
Non-toggle buttons will always receive 1 and 0 for current/previous values respectively.
ONCONTENTCHANGED
Supported by ui.InputField
self : Component<T>
reason : ui.InputFieldActionResult
newContents : String
Triggered whenever a user interacts with an input field component in a way that may affect its contents.
ONSELECTIONCHANGED
Supported by ui.Carousel, ui.PagedLayer, ui.Slider
self : Component<T>
entryID : Int
selectedStatus : Int

entryID represents the component's entry that's had its selected status changed, while a selectedStatus of 1 indicates this entry has been selected and 0 indicates it has been deselected.
ONSELECTIONCHANGED
Supported by ui.ComboField, ui.List, ui.RadioGroup
self : Component<T>
entryID : Int
selectionEventType : ui.SelectionChangeEvent

entryID represents the component's entry that's had its selected status changed.
ONSELECTIONCHANGED
Supported by ui.RadialProgressOverlay
self : Component<T>
curValue : Float
progressPc
Triggered whenever this radial's progress changes.
progressPc is received as a 0..1 number representing 0 to 100%.
ONCUTSCENEFINISHED
Supported by ui.CutsceneLayer
self : Component<T>
reason : ui.CutscenePlaybackEndReason
Triggered when a cutscene finishes playing on a cutscene layer component.
Mouse and Keyboard Events
Name Parameters Description
ONCLICK self : Component<T>
x : Int
y : Int
Triggered when the user clicks the left mouse button or taps on this component.
ONCLICKREPEAT self : Component<T>
x : Int
y : Int
Triggered when the user clicks the left mouse button or taps on this component.
ONRELEASE self : Component<T>
x : Int
y : Int
Triggered when the user stops clicking on this component or stops a tap gesture.
ONHOLD self : Component<T>
x : Int
y : Int
Triggered while the user is clicking on this component.
ONMOUSEOVER self : Component<T>
x : Int
y : Int
Triggered when the cursor enters this component's region.
ONMOUSEREPEAT self : Component<T>
x : Int
y : Int
Triggered while the cursor is over this component's region.
ONMOUSELEAVE self : Component<T>
x : Int
y : Int
Triggered when the cursor leaves this component's region.
ONDRAG self : Component<T>
dragStartX : Int
dragStartY : Int
Triggered when the user starts dragging this component.
ONDRAGCOMPLETE self : Component<T>
dragDestination : Component<T>
dragEndX : Int
dragEndY : Int
Triggered when the user finishes dragging this component.
ONSCROLLWHEEL self : Component<T>
scrollWheelYDelta : Int
Triggered when the user moves the mouse's scrollwheel.
ONKEY self : Component<T>
keyCode : Int
keyChar : Int
Triggered when the user inputs a key on their keyboard.
Touch Gesture Events

These events are only triggered when using a touchscreen.

Name Parameters Description
ONSWIPEVERTICAL self : Component<T>
swipeCurrentX : Int
swipeCurrentY : Int
Triggered whenever a horizontal swipe motion occurs.
ONSWIPEHORIZONTAL self : Component<T>
swipeCurrentX : Int
swipeCurrentY : Int
Triggered whenever a vertical swipe motion occurs.
ONSWIPECOMPLETE self : Component<T>
Triggered when a swipe gesture terminates.
ONPINCHHORIZONTAL self : Component<T>
direction : Int
Triggered whenever a horizontal pinch motion occurs.
Direction is -1 for inwards pinch gestures, and 1 for outwards.
ONPINCHVERTICAL self : Component<T>
direction : Int
Triggered whenever a vertical pinch motion occurs.
Direction is -1 for inwards pinch gestures, and 1 for outwards.
ONPINCHCOMPLETE self : Component<T>
Triggered when a pinch gesture terminates.
Controller Events

Warning

Controller support is experimental and any usage of these events should be avoided.

Name Parameters Description
ONGAMEPADBUTTON self : Component<T>
buttonID : Int
buttonValue : Int
ONGAMEPADBUTTONHELD self : Component<T>
buttonID : Int
ONGAMEPADAXIS self : Component<T>
axisID : Int
axisValue : Int
ONGAMEPADTRIGGER self : Component<T>
triggerID : Int
triggerValue : Int
Action Events

These events are associated with actions that the local player is taking in the game world.

Name Parameters Description
ONOP self : Component<T>
opID : Int
opBaseName : String
Triggered whenever the user interacts with something in the game world, or with a UI component that has ops specified for its interactions.
ONOPT self : Component<T>
target : Component<T>
Triggered when the user selects another UI component while in target selection mode.
ONTARGETENTER self : Component<T> Triggered when the component enters target selection mode.
ONTARGETLEAVE self : Component<T> Triggered when the component leaves target selection mode.
Transmission Events

These events represent data updates received from the server which may need to be reflected on the user interface.

Name Parameters Description
ONVARPTRANSMIT self : Component<T> Triggered when the game client receives a server transmission for player variables.
ONINVTRANSMIT self : Component<T> Triggered when the game client receives a server transmission for inventories.
ONSTATTRANSMIT self : Component<T> Triggered when the game client receives a server transmission for stats.
ONVARCTRANSMIT self : Component<T> Triggered when the game client receives a server transmission for client variables.
ONVARCSTRTRANSMIT self : Component<T> Triggered when the game client receives a server transmission for client string variables.
ONVARCLANTRANSMIT self : Component<T> Triggered when the game client receives a server transmission for clan variables.
ONCHATTRANSMIT self : Component<T> Triggered when the game client receives a server transmission for chat messages.
ONFRIENDTRANSMIT self : Component<T> Triggered when the game client receives a server transmission for friends data.
ONCLANTRANSMIT self : Component<T> Triggered when the game client receives a server transmission for clan data.
ONCLANSETTINGSTRANSMIT self : Component<T> Triggered when the game client receives a server transmission for clan settings data.
ONCLANCHANNELTRANSMIT self : Component<T> Triggered when the game client receives a server transmission for clan chat messages.
ONRELATIONSHIPSETTRANSMIT self : Component<T> Triggered when the game client receives a server transmission for friends/clans/ignore lists.
ONMISCTRANSMIT self : Component<T> Triggered when the game client receives a server transmission for miscellaneous data.
ONSTOCKTRANSMIT self : Component<T> Triggered when the game client receives a server transmission for stock data.
ONPLAYERGROUPTRANSMIT self : Component<T> Triggered when the game client receives a server transmission involving the current player group state.
ONPLAYERGROUPVARPTRANSMIT self : Component<T> Triggered when the game client receives a server transmission for the current group's player variable.
ONCAMERAUPDATETRANSMIT self : Component<T> Triggered when the game client receives a server transmission involving camera changes.
Miscellaneous Events
Name Parameters Description
ONTIMER self : Component<T> Triggered once per logic tick.
Prefer using PluginLogic over this as it's a more general solution.
ONDIALOGABORT self : Component<T> Triggered manually by the server.
ONSUBCHANGE self : Component<T> Triggered whenever a sub-interface loads.
ONCAMFINISHED self : Component<T> Triggered whenever a server-controlled camera motion finishes.
ONRESIZE self : Component<T> Triggered whenever this component has its dimensions changed.

Subscribe

function Component:Subscribe(hook : ui.Hook, callback : function (...) -> Bool?)
function Component:Subscribe(hook : ui.Hook, id : String, callback : function (...) -> Bool?)

Subscribes a listener for an event hook, setting a callback to be triggered whenever this event occurs, and optionally assigning it an ID allowing it to be referenced to in the future.

Info

IDs are specific to an event hook type, the component they're attached to, and the current plugin, so the same ID can be used across different components or events, and doesn't need to be unique per plugin as well.

Limitations

  • Calling Subscribe multiple times using the same event hook type and ID will instead replace the existing callback.
  • Plugins can't remove/replace callbacks subscribed by other plugins.
Parameter Type Description
hook ui.Hook The event hook to listen for.
callback function (...) -> Bool? The callback to trigger on this event, with parameters varying based on the event hook type.
Can return a boolean indicating whether or not to continue processing listeners afterwards. If omitted, the callback is treated as if it returned true, resulting in other listeners still getting triggered.
id String Name used to refer to this listener.
Used when attempting to remove a listener by its ID via Unsubscribe.

Example

Subscribing multiple listeners
local myComponent = ...
myComponent:Subscribe(ui.Hook.ONCLICK, "a", function () log("Hello") return true end)
-- Or
myComponent:Subscribe(ui.Hook.ONCLICK, "b", function () log("World") return true end)

-- Clicks yield "Hello World"
Replacing listeners
local myComponent = ...
myComponent:Subscribe(ui.Hook.ONCLICK, "a", function () log("Hello") return true end)
-- Or
myComponent:Subscribe(ui.Hook.ONCLICK, "a", function () log("World") return true end)

-- Clicks yield "World"
Prevent additional listeners from triggering
local myComponent = ...
myComponent:Subscribe(ui.Hook.ONCLICK, "a", function () log("Hello") return false end)
-- Or
myComponent:Subscribe(ui.Hook.ONCLICK, "b", function () log("World") return true end)

-- Clicks yield "Hello"

Unsubscribe

function Component:Unsubscribe(hook : ui.Hook)
function Component:Unsubscribe(hook : ui.Hook, id : String)

Removes the listener set for the specified hook with the provided ID, or the default listener for that hook if omitted, from that component.

Parameter Type Description
hook ui.Hook The event hook to listen for.
id String Name used to refer to this listener.
Used when attempting to remove a listener by its ID via Unsubscribe.

Example

local myComponent = ...
myComponent:Subscribe(ui.Hook.ONCLICK, "myHookName", function () log("Hello World") end)
-- Later on
myComponent:Unsubscribe(ui.Hook.ONCLICK, "myHookName")

UnsubscribeAll

function Component:UnsubscribeAll(hook : ui.Hook)

Removes all event listeners of the specified type from this component associated with the current plugin.

Parameter Type Description
hook ui.Hook The event hook to clear the listeners for.

Visual Properties

Name Type Description
alpha Float Controls how opaque this component appears, ranges from 0 (fully transparent) to 1 (opaque).
rgb Uint Hexadecimal colour code representing the Red, Green, and Blue tints for this component.
rgba Uint Hexadecimal colour code representing the Red, Green, Blue, And Alpha tints for this component.
colour Vector4 Red, Green, Blue, and Alpha tints represented as a 4-channel vector, with each channel limited to a 0..1 range.
On assignment, values outside of this range will be clamped.
hidden Bool Controls whether this component is visible.
This only applies to Lua-side visibility, and cannot be used to force built-in UI components to become visible.
visible Bool (Read-only) Indicates whether this component is visible or not.
This state refers only to the internal state of this component. For the global visibility state see visibleGlobal.
Differs from hidden in that this also takes into account built-in UI component visibility.
visibleGlobal Bool (Read-only) Indicates whether this component is visible within the UI component tree.
For example, will return false if any parent of this component is not visible.
stylesheetID Int ID for the StyleSheet currently in use by this component, or -1 if no StyleSheet is active.
aspectRatioProportion Float (Read-only)
The aspect ratio width value divided by the height value.
For example, a 16:10 ratio resolves to a proportion of 1.6.
aspectRatioWidth Int (Read-only)
The target width value used for aspect ratio calculations.
Note this value is always 1 or higher, even when aspect ratio scaling is disabled.
aspectRatioHeight Int (Read-only)
The target height value used for aspect ratio calculations.
Note this value is always 1 or higher, even when aspect ratio scaling is disabled.
aspectRatioMode ui.AspectRatioMode (Read-only)
Mode used when determining how the aspect ratio should be applied to this component, if at all.

State Properties

The following properties relate to how the user interacts with the component and how the component reacts to said interactions.

Name Type Description
enabled Bool Controls whether this component responds to inputs and events.
clickthrough Bool Controls whether mouse interactions on this component block interactions on components below it.
isMouseOver Bool (Read-only) Whether the cursor is currently over this component or not.
isClicked Bool (Read-only) Whether the user has clicked on this component recently or not.
opConfig ui.ComponentOpConfig Identifies the action to perform when the component is interacted with.
dragConfig ui.ComponentDragConfig Determines how the component reacts to drag actions.
swipeConfig ui.ComponentSwipeConfig Determines how the component reacts to swipe gestures.
pinchConfig ui.ComponentPinchConfig Determines how the component reacts to pinch gestures.
cursorConfig ui.ComponentCursorConfig Specifies which cursors to use, if any, when interacting with this component.
paramsConfig ui.ComponentParamsConfig Additional parameters attached to this component.

Interface Properties

Name Type Description
type ui.ComponentType (Read-only) ID identifying the type of this component.
parent ui.Component? (Read-Only) The immediate parent of this component within the UI component tree.
Returns nil if there is no parent.
parentStatic ui.Component? (Read-Only) The static parent of this component within the UI component tree.
Returns nil if there is no static parent.
parentInterface Interface (Read-Only) Interface in which this component resides.
interfaceID Int (Read-only) ID identifying the interface in which this component resides.
staticID Int (Read-only) ID identifying the static component on which this component resides.
categoryID Int (Read-only) ID identifying the dynamic ID category this component's using, or -1 if this is a static component.
dynamicID Int (Read-only) ID identifying the dynamic ID category this component's using, or -1 if this is a static component.

Restricted Components

In some cases, components may be flagged as restricted, preventing certain operations from working from the Lua-side, or resulting in them yielding default values instead.

Components can be intentionally tagged as restricted through the Component:FlagRestricted() function, with the restricted status being verifiable via component.isRestricted.

Warning

Once restricted, a component cannot become unrestricted afterwards.

Warning

Flagging a component as restricted is only possible if the current plugin is its creator.

Limitations on restricted components

  • Attempting to retrieve the content of InputField and Label components returns an empty string.
  • InputField:Setup cannot be called on a restricted component