Last Update: 2023-07-06
Widget
Widgets are UI elements that can be used to create user interfaces in Don't Starve Together. They handle rendering, input, and other UI-related functionality.
Overview
Widgets are the building blocks of the game's user interface. They can be combined hierarchically to create complex UI layouts. Each widget handles its own rendering, input events, and animations.
Common Widget Structure
local MyWidget = Class(Widget, function(self, name)
Widget._ctor(self, name or "MyWidget")
-- Initialize widget properties
self.bg = self:AddChild(Image("images/ui.xml", "panel.tex"))
self.bg:SetScale(2, 2)
self.text = self:AddChild(Text(BODYTEXTFONT, 20))
self.text:SetString("Hello World")
self.text:SetPosition(0, 0)
-- Set up callbacks
self:SetOnClick(function() print("Widget clicked!") end)
end)
function MyWidget:OnGainFocus()
self.bg:SetTint(1, 1, 0, 1)
return true
end
function MyWidget:OnLoseFocus()
self.bg:SetTint(1, 1, 1, 1)
return true
end
return MyWidget
Widget Hierarchy
Widgets can be organized in a parent-child hierarchy:
local parent = Widget("ParentWidget")
local child = parent:AddChild(Widget("ChildWidget"))
Children inherit properties like visibility and focus from their parents.
Key Methods
AddChild(widget)
: Adds a child widgetRemoveChild(widget)
: Removes a child widgetSetPosition(x, y, z)
: Sets the widget's positionSetScale(x, y, z)
: Sets the widget's scaleSetRotation(angle)
: Sets the widget's rotationSetTint(r, g, b, a)
: Sets the widget's color tintSetOnClick(fn)
: Sets a click handler functionSetFocus()
: Gives focus to this widgetClearFocus()
: Removes focus from this widgetShow()
: Makes the widget visibleHide()
: Makes the widget invisibleScaleTo(from, to, time, fn)
: Animates the widget's scaleMoveTo(from, to, time, fn)
: Animates the widget's position
Common Event Handlers
OnControl(control, down)
: Called when a control is pressed/releasedOnMouseButton(button, down, x, y)
: Called on mouse eventsOnRawKey(key, down)
: Called on keyboard eventsOnTextInput(text)
: Called when text is enteredOnGainFocus()
: Called when the widget gains focusOnLoseFocus()
: Called when the widget loses focus
Common Widget Types
Text
: Displays textImage
: Displays an imageButton
: Clickable buttonTextButton
: Button with textImageButton
: Button with an imageMenu
: Group of optionsScrollableList
: Scrollable list of itemsTextEdit
: Editable text field
Related Systems
- UI Screen system
- Animation system