Skip to Content
API ReferenceWidget Functions

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 PropertyTypeRequiredDefaultDescription
typestringNo”canvas”Widget type: “canvas”, “html”, “svg”, “enhanced-text”
renderModestringNo-Alternative to type (for compatibility)
xnumberNo200X position
ynumberNo150Y position
widthnumberNo300Widget width
heightnumberNo200Widget height
titlestringNo-Widget title/name
visiblebooleanNotrueInitial visibility
zIndexnumberNo1000Z-order (higher = on top)
resizablebooleanNotrueAllow user resizing
movablebooleanNotrueAllow 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") end

destroyWidget

Destroy a widget and clean up all its resources and event handlers.

destroyWidget(widgetId)
ParameterTypeRequiredDescription
widgetIdstringYesThe widget ID to destroy

Example:

-- Manually destroy a widget (rarely needed - cleanup is automatic) destroyWidget(mapWidget)

showWidget

Make a widget visible.

showWidget(widgetId)
ParameterTypeRequiredDescription
widgetIdstringYesThe widget ID to show

Example:

-- Toggle widget visibility showWidget(statsWidget)

hideWidget

Hide a widget (but don’t destroy it).

hideWidget(widgetId)
ParameterTypeRequiredDescription
widgetIdstringYesThe 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)
ParameterTypeRequiredDescription
widgetIdstringYesThe widget ID
xnumberYesNew X position
ynumberYesNew Y position

Example:

-- Move widget to top-left corner moveWidget(statsWidget, 10, 10)

resizeWidget

Resize a widget to new dimensions.

resizeWidget(widgetId, width, height)
ParameterTypeRequiredDescription
widgetIdstringYesThe widget ID
widthnumberYesNew width
heightnumberYesNew height

Example:

-- Make widget larger resizeWidget(mapWidget, 600, 400)

setWidgetZOrder

Set the z-order (stacking order) of a widget.

setWidgetZOrder(widgetId, zIndex)
ParameterTypeRequiredDescription
widgetIdstringYesThe widget ID
zIndexnumberYesZ-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)
ParameterTypeRequiredDescription
widgetIdstringYesThe widget ID
propertystringYesProperty 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)
ParameterTypeRequiredDescription
widgetIdstringYesThe widget ID
propertystringYesProperty name to set
valueanyYesNew property value

Example:

setWidgetProperty(statsWidget, "title", "Player Stats") setWidgetProperty(statsWidget, "visible", true)

widgetInfo

Get detailed information about a widget.

local value = widgetInfo(widgetId, infoType)
ParameterTypeRequiredDescription
widgetIdstringYesThe widget ID
infoTypenumberYesInformation type code (see table below)

Info Type Codes:

CodeDescriptionReturns
1Original X positionnumber
2Original Y positionnumber
3Current widthnumber
4Current heightnumber
5Original widthnumber
6Original heightnumber
7Is visibleboolean
8Is minimizedboolean
9Z-order/layernumber
10Widget typestring
11Last mouse X (relative to widget)number
12Last mouse Y (relative to widget)number
13Mouse event counternumber
14Last clicked buttonnumber
15Current X positionnumber
16Current Y positionnumber
17Creation timestampnumber
18Plugin ID that created itstring
19Widget name/titlestring
20Widget IDstring
21Is canvas readyboolean

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") end

Active 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)
ParameterTypeRequiredDescription
widgetstring or WidgetYesWidget 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 widget1

getActiveWidget

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)
ParameterTypeRequiredDescription
widgetIdstringYesThe widget ID
eventTypestringYesEvent type (see table below)
callbackfunctionYesFunction to call when event fires

Event Types:

EventCallback ParametersDescription
clickx, y, buttonMouse click
mousedownx, y, buttonMouse button pressed
mouseupx, y, buttonMouse button released
mousemovex, yMouse moved over widget
mouseenter(none)Mouse entered widget
mouseleave(none)Mouse left widget
wheeldeltaX, deltaYMouse wheel scrolled
resizewidth, heightWidget was resized
keydownkey, code, shift, ctrl, altKey pressed
keyupkey, code, shift, ctrl, altKey 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?)
ParameterTypeRequiredDescription
widgetIdstringYesThe widget ID
eventTypestringYesEvent type to unregister
callbackfunctionNoSpecific 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?)
ParameterTypeRequiredDefaultDescription
widgetIdstringYes-Text widget ID
textstringYes-Text to add
colorstringNo”#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)
ParameterTypeRequiredDescription
widgetIdstringYesText widget ID
buttonIdstringYesUnique ID for this button
textstringYesButton label text
callbackfunctionYesFunction 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)
ParameterTypeRequiredDescription
widgetIdstringYesText widget ID

Example:

textWidgetClear(logWidget)

textWidgetSetSelectable

Enable or disable text selection in a text widget.

textWidgetSetSelectable(widgetId, selectable)
ParameterTypeRequiredDescription
widgetIdstringYesText widget ID
selectablebooleanYesWhether 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)
ParameterTypeRequiredDescription
widgetIdstringYesText widget ID
transparentbooleanYesWhether 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!")
Last updated on