UI
Create canvases, labels and buttons, update them and hit-test screen positions.
Namespace: caffeine.ui
createCanvas#
Lua: caffeine.ui.createCanvas
createCanvas([w, h]) -> integer
Creates a UI canvas entity.
| Parameters | Type | Description |
|---|---|---|
w | number | Width (default 1280). |
h | number | Height (default 720). |
Returns: integer. Canvas entity id, or 0 without a UI system or world.
Example
local canvas = caffeine.ui.createCanvas(1280, 720)
createLabel#
Lua: caffeine.ui.createLabel
createLabel(parent, text, pos) -> integer
Creates a text label under a parent.
| Parameters | Type | Description |
|---|---|---|
parent | integer | Parent canvas or widget id. |
text | string | Label text. |
pos | table | {x, y} position. |
Returns: integer. Label entity id, or 0.
Example
local l = caffeine.ui.createLabel(canvas, 'Score: 0', { x = 10, y = 10 })
createButton#
Lua: caffeine.ui.createButton
createButton(parent, text, pos[, w, h]) -> integer
Creates a button under a parent.
| Parameters | Type | Description |
|---|---|---|
parent | integer | Parent canvas or widget id. |
text | string | Button text. |
pos | table | {x, y} position. |
w | number | Width (default 120). |
h | number | Height (default 40). |
Returns: integer. Button entity id, or 0.
Example
local b = caffeine.ui.createButton(canvas, 'Play', { x = 20, y = 60 })
hitTest#
Lua: caffeine.ui.hitTest
hitTest(sx, sy) -> integer
Finds the UI element at a screen position.
| Parameters | Type | Description |
|---|---|---|
sx | number | Screen x. |
sy | number | Screen y. |
Returns: integer. UI entity id under the point, or 0.
Example
local hit = caffeine.ui.hitTest(100, 80)
setLabel#
Lua: caffeine.ui.setLabel
setLabel(entity, text)
Changes a label's text. Does nothing when the entity has no label.
| Parameters | Type | Description |
|---|---|---|
entity | integer | Entity id. |
text | string | New text (up to 255 characters). |
Returns: none
Example
caffeine.ui.setLabel(l, 'Score: 10')
setProgress#
Lua: caffeine.ui.setProgress
setProgress(entity, normalized)
Sets a progress bar between its min and max. Does nothing when the entity has no progress bar.
| Parameters | Type | Description |
|---|---|---|
entity | integer | Entity id. |
normalized | number | 0..1; clamped. |
Returns: none
Example
caffeine.ui.setProgress(bar, 0.5)