Widget Functions
Functions for creating, managing, and interacting with plugin widgets.
Widget Lifecycle
createWidget
Create a new widget with the specified configuration.
local widgetId = createWidget(config)| Config Property | Type | Required | Default | Description |
|---|---|---|---|---|
| type | string | No | ”canvas” | Widget type: “canvas”, “html”, “svg”, “enhanced-text” |
| renderMode | string | No | - | Alternative to type (for compatibility) |
| x | number | No | 200 | X position |
| y | number | No | 150 | Y position |
| width | number | No | 300 | Widget width |
| height | number | No | 200 | Widget height |
| title | string | No | - | Widget title/name |
| visible | boolean | No | true | Initial visibility |
| zIndex | number | No | 1000 | Z-order (higher = on top) |
| resizable | boolean | No | true | Allow user resizing |
| movable | boolean | No | true | Allow user dragging |
Returns: string - The widget ID
Example:
local mapWidget = createWidget({
type = "canvas",
title = "Map Widget",
x = 100,
y = 100,
width = 400,
height = 300
})
-- Widget is automatically active after creation
-- Draw in init() when canvas is ready
function init()
drawRect(0, 0, 400, 300, "#1a1a1a")
drawText("Map View", 10, 20, "#ffffff")
enddestroyWidget
Destroy a widget and clean up all its resources and event handlers.
destroyWidget(widgetId)| Parameter | Type | Required | Description |
|---|---|---|---|
| widgetId | string | Yes | The widget ID to destroy |
Example:
-- Manually destroy a widget (rarely needed - cleanup is automatic)
destroyWidget(mapWidget)showWidget
Make a widget visible.
showWidget(widgetId)| Parameter | Type | Required | Description |
|---|---|---|---|
| widgetId | string | Yes | The widget ID to show |
Example:
-- Toggle widget visibility
showWidget(statsWidget)hideWidget
Hide a widget (but don’t destroy it).
hideWidget(widgetId)| Parameter | Type | Required | Description |
|---|---|---|---|
| widgetId | string | Yes | The widget ID to hide |
Example:
-- Hide widget when not needed
hideWidget(statsWidget)Widget Manipulation
moveWidget
Move a widget to a new position.
moveWidget(widgetId, x, y)| Parameter | Type | Required | Description |
|---|---|---|---|
| widgetId | string | Yes | The widget ID |
| x | number | Yes | New X position |
| y | number | Yes | New Y position |
Example:
-- Move widget to top-left corner
moveWidget(statsWidget, 10, 10)resizeWidget
Resize a widget to new dimensions.
resizeWidget(widgetId, width, height)| Parameter | Type | Required | Description |
|---|---|---|---|
| widgetId | string | Yes | The widget ID |
| width | number | Yes | New width |
| height | number | Yes | New height |
Example:
-- Make widget larger
resizeWidget(mapWidget, 600, 400)setWidgetZOrder
Set the z-order (stacking order) of a widget.
setWidgetZOrder(widgetId, zIndex)| Parameter | Type | Required | Description |
|---|---|---|---|
| widgetId | string | Yes | The widget ID |
| zIndex | number | Yes | Z-index value (higher = on top) |
Returns: boolean - Success or failure
Example:
-- Bring widget to front
setWidgetZOrder(popupWidget, 2000)
-- Send widget to back
setWidgetZOrder(backgroundWidget, 500)Widget Properties
getWidgetProperty
Get a property value from a widget.
local value = getWidgetProperty(widgetId, property)| Parameter | Type | Required | Description |
|---|---|---|---|
| widgetId | string | Yes | The widget ID |
| property | string | Yes | Property name to retrieve |
Returns: any - The property value
Example:
local visible = getWidgetProperty(statsWidget, "visible")
local width = getWidgetProperty(statsWidget, "width")setWidgetProperty
Set a property value on a widget.
setWidgetProperty(widgetId, property, value)| Parameter | Type | Required | Description |
|---|---|---|---|
| widgetId | string | Yes | The widget ID |
| property | string | Yes | Property name to set |
| value | any | Yes | New property value |
Example:
setWidgetProperty(statsWidget, "title", "Player Stats")
setWidgetProperty(statsWidget, "visible", true)widgetInfo
Get detailed information about a widget.
local value = widgetInfo(widgetId, infoType)| Parameter | Type | Required | Description |
|---|---|---|---|
| widgetId | string | Yes | The widget ID |
| infoType | number | Yes | Information type code (see table below) |
Info Type Codes:
| Code | Description | Returns |
|---|---|---|
| 1 | Original X position | number |
| 2 | Original Y position | number |
| 3 | Current width | number |
| 4 | Current height | number |
| 5 | Original width | number |
| 6 | Original height | number |
| 7 | Is visible | boolean |
| 8 | Is minimized | boolean |
| 9 | Z-order/layer | number |
| 10 | Widget type | string |
| 11 | Last mouse X (relative to widget) | number |
| 12 | Last mouse Y (relative to widget) | number |
| 13 | Mouse event counter | number |
| 14 | Last clicked button | number |
| 15 | Current X position | number |
| 16 | Current Y position | number |
| 17 | Creation timestamp | number |
| 18 | Plugin ID that created it | string |
| 19 | Widget name/title | string |
| 20 | Widget ID | string |
| 21 | Is canvas ready | boolean |
Example:
-- Get current dimensions
local width = widgetInfo(mapWidget, 3)
local height = widgetInfo(mapWidget, 4)
-- Check visibility
local isVisible = widgetInfo(mapWidget, 7)
-- Get mouse position within widget
local mouseX = widgetInfo(mapWidget, 11)
local mouseY = widgetInfo(mapWidget, 12)
-- Check if widget has been resized from original
local origWidth = widgetInfo(mapWidget, 5)
local currWidth = widgetInfo(mapWidget, 3)
if currWidth ~= origWidth then
print("Widget has been resized")
endActive Widget
Canvas widgets automatically become the active widget when created. You only need setActiveWidget() when working with multiple widgets and need to switch between them.
setActiveWidget
Switch the active widget for drawing operations. Only needed when you have multiple widgets.
setActiveWidget(widgetId)
-- or
setActiveWidget(widgetInstance)| Parameter | Type | Required | Description |
|---|---|---|---|
| widget | string or Widget | Yes | Widget ID or widget instance |
Example:
-- First widget is automatically active after creation
local widget1 = createWidget({ type = "canvas", x = 10, y = 10, width = 200, height = 200 })
drawRect(0, 0, 200, 200, "#ff0000") -- Draws on widget1
-- Create second widget (now widget2 is active)
local widget2 = createWidget({ type = "canvas", x = 220, y = 10, width = 200, height = 200 })
drawRect(0, 0, 200, 200, "#00ff00") -- Draws on widget2
-- Switch back to widget1
setActiveWidget(widget1)
drawCircle(100, 100, 50, "#0000ff") -- Draws on widget1getActiveWidget
Get the currently active widget.
local widget = getActiveWidget()Returns: Widget instance or nil
Widget Events
registerWidgetEvent
Register a callback function for widget events (mouse, keyboard, resize, etc.).
registerWidgetEvent(widgetId, eventType, callback)| Parameter | Type | Required | Description |
|---|---|---|---|
| widgetId | string | Yes | The widget ID |
| eventType | string | Yes | Event type (see table below) |
| callback | function | Yes | Function to call when event fires |
Event Types:
| Event | Callback Parameters | Description |
|---|---|---|
| click | x, y, button | Mouse click |
| mousedown | x, y, button | Mouse button pressed |
| mouseup | x, y, button | Mouse button released |
| mousemove | x, y | Mouse moved over widget |
| mouseenter | (none) | Mouse entered widget |
| mouseleave | (none) | Mouse left widget |
| wheel | deltaX, deltaY | Mouse wheel scrolled |
| resize | width, height | Widget was resized |
| keydown | key, code, shift, ctrl, alt | Key pressed |
| keyup | key, code, shift, ctrl, alt | Key released |
Example:
-- Handle mouse clicks
registerWidgetEvent(mapWidget, "click", function(data)
print("Clicked at: " .. data.x .. ", " .. data.y)
print("Button: " .. data.button) -- 1=left, 2=middle, 3=right
-- Draw a marker at click position
drawCircle(data.x, data.y, 5, "#ff0000")
end)
-- Handle mouse movement
registerWidgetEvent(mapWidget, "mousemove", function(data)
-- data.x and data.y are relative to the widget
updateCrosshair(data.x, data.y)
end)
-- Handle resize
registerWidgetEvent(mapWidget, "resize", function(data)
print("New size: " .. data.width .. "x" .. data.height)
redrawMap()
end)
-- Handle keyboard
registerWidgetEvent(mapWidget, "keydown", function(data)
if data.key == "ArrowUp" then
scrollMap(0, -10)
elseif data.key == "ArrowDown" then
scrollMap(0, 10)
end
end)unregisterWidgetEvent
Remove a widget event handler.
unregisterWidgetEvent(widgetId, eventType, callback?)| Parameter | Type | Required | Description |
|---|---|---|---|
| widgetId | string | Yes | The widget ID |
| eventType | string | Yes | Event type to unregister |
| callback | function | No | Specific callback to remove (or all if omitted) |
Example:
-- Remove specific callback
unregisterWidgetEvent(mapWidget, "click", myClickHandler)
-- Remove all click handlers
unregisterWidgetEvent(mapWidget, "click")Text Widget Functions
These functions are for “enhanced-text” type widgets that display scrollable, selectable text.
textWidgetAddLine
Add a line of text to a text widget.
textWidgetAddLine(widgetId, text, color?)| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| widgetId | string | Yes | - | Text widget ID |
| text | string | Yes | - | Text to add |
| color | string | No | ”#ffffff” | Text color |
Example:
local logWidget = createWidget({
type = "enhanced-text",
title = "Combat Log",
x = 10, y = 10,
width = 300, height = 200
})
textWidgetAddLine(logWidget, "Combat started!", "#ff0000")
textWidgetAddLine(logWidget, "You hit the goblin for 15 damage.", "#00ff00")
textWidgetAddLine(logWidget, "The goblin misses you.", "#cccccc")textWidgetAddButton
Add a clickable button to a text widget.
textWidgetAddButton(widgetId, buttonId, text, callback)| Parameter | Type | Required | Description |
|---|---|---|---|
| widgetId | string | Yes | Text widget ID |
| buttonId | string | Yes | Unique ID for this button |
| text | string | Yes | Button label text |
| callback | function | Yes | Function called when clicked |
Example:
textWidgetAddButton(logWidget, "clear_btn", "Clear Log", function()
textWidgetClear(logWidget)
end)
textWidgetAddButton(logWidget, "attack_btn", "Attack!", function()
send("attack goblin")
end)textWidgetClear
Clear all content from a text widget.
textWidgetClear(widgetId)| Parameter | Type | Required | Description |
|---|---|---|---|
| widgetId | string | Yes | Text widget ID |
Example:
textWidgetClear(logWidget)textWidgetSetSelectable
Enable or disable text selection in a text widget.
textWidgetSetSelectable(widgetId, selectable)| Parameter | Type | Required | Description |
|---|---|---|---|
| widgetId | string | Yes | Text widget ID |
| selectable | boolean | Yes | Whether text can be selected |
Example:
-- Allow users to copy text from the widget
textWidgetSetSelectable(logWidget, true)textWidgetSetTransparent
Set whether a text widget has a transparent background.
textWidgetSetTransparent(widgetId, transparent)| Parameter | Type | Required | Description |
|---|---|---|---|
| widgetId | string | Yes | Text widget ID |
| transparent | boolean | Yes | Whether background is transparent |
Example:
-- Make widget overlay without blocking view
textWidgetSetTransparent(logWidget, true)Complete Widget Example
This example shows multiple widgets with proper setActiveWidget usage:
-- Multi-widget example
print("Loading...")
-- Create map canvas
local mapWidget = createWidget({
type = "canvas",
title = "World Map",
x = 100,
y = 100,
width = 400,
height = 300
})
-- Create stats text widget
local statsWidget = createWidget({
type = "enhanced-text",
title = "Character Stats",
x = 510,
y = 100,
width = 200,
height = 300
})
-- Map drawing function
local function drawMap()
setActiveWidget(mapWidget) -- Required: multiple widgets exist
clear("#0a0a1a")
drawText("World Map", 10, 20, "#ffffff")
end
-- Stats update function
local function updateStats()
textWidgetClear(statsWidget)
textWidgetAddLine(statsWidget, "=== Character ===", "#ffff00")
textWidgetAddLine(statsWidget, "HP: 100/100", "#00ff00")
textWidgetAddLine(statsWidget, "MP: 50/50", "#0066ff")
end
-- Handle map clicks
registerWidgetEvent(mapWidget, "click", function(data)
setActiveWidget(mapWidget)
drawCircle(data.x, data.y, 3, "#ffff00")
end)
-- Handle map resize
registerWidgetEvent(mapWidget, "resize", function(data)
drawMap()
end)
-- Initial draw
function init()
drawMap()
updateStats()
end
print("Loaded!")