begui
begui copied to clipboard
A minimal customizable GUI system for Lua.

beGUI is a minimal customizable GUI system for Lua, and fully written in Lua.
Try it in browser.
Index
- Features
- Setup
- Reference
- 1. Principles
- 2. Structures
- beStructures.Percent
- beGUI.percent
- 3. Widget
- beGUI.Widget
- 4. Basic Widgets
- beGUI.Label
- beGUI.MultilineLabel
- beGUI.Url
- beGUI.InputBox
- beGUI.Picture
- beGUI.Button
- beGUI.PictureButton
- beGUI.CheckBox
- beGUI.RadioBox
- beGUI.ComboBox
- beGUI.NumberBox
- beGUI.ProgressBar
- beGUI.Slide
- beGUI.Group
- 5. Container Widgets
- beGUI.List
- beGUI.Draggable
- beGUI.Droppable
- beGUI.Tab
- beGUI.Popup
- beGUI.MessageBox
- beGUI.QuestionBox
- 6. Custom Widget
- beGUI.Custom
- Writing Your Own Widget
- 7. Theme
- 8. Tweening
- beGUI.Tween
- License
Features


"beGUI" implements:
- Placable, resizable, anchorable, and nestable
Widget - Textual
Label,MultilineLabel,Url,InputBox Picture- Clickable
Button,PictureButton CheckBox,RadioBoxComboBoxNumberBoxProgressBar,SlideGroup- Scrollable
List DraggableandDroppableTabPopup,MessageBox,QuestionBoxCustomto make your own update function- And customizable by writing your own widget
- Navigation by key (or custom method)
Play live demo in browser.
Setup
beGUI is originally created to run within the Bitty Engine. The graphics primitives and input API is quite straightforward in Bitty Engine, it's possible to port it to other Lua-based environments with little twist, if that environment does rect(...), tex(...), text(...), mouse(...), etc.
- Clone this repository or download from releases
- Open "src" directly to run or copy everything under "src" to your own projects for Bitty Engine
Reference
1. Principles
beGUI implements a retained mode GUI system, it separates behaviour and appearance into widget classes and theme preference. Widgets are organized in tree hierarchies, each widget can have none or one parent, and none or multiple children. There are two phases of the lib, first to construct the hierarchy, second to update it. It enumerates from root widget to it's descendents during update, beGUI will handle internal states like visibility, click event, etc. then draw it properly with the theme preference during this procedure.
Most of the member functions return the widget object itself, it makes it easier to write internal DSL to construct full hierarchy in a tree style code.
require 'libs/beGUI/beGUI'
require 'libs/beGUI/beTheme'
local widgets = nil
local theme = nil
function setup()
local P = beGUI.percent -- Alias of percent.
widgets = beGUI.Widget.new()
:put(0, 0)
:resize(P(100), P(100))
:addChild(
beGUI.Label.new('beGUI demo')
:setId('label')
:anchor(0, 0)
:put(10, 10)
:resize(100, 23)
)
:addChild(
beGUI.Button.new('Button')
:setId('button')
:anchor(0, 0)
:put(10, 36)
:resize(100, 23)
:on('clicked', function (sender)
local lbl = widgets:find('label')
lbl:setValue('Clicked ' .. tostring(sender))
end)
)
theme = beTheme.default()
end
function update(delta)
cls(Color.new(255, 255, 255))
font(theme['font'].resource)
widgets:update(theme, delta)
font(nil)
end
Each widget has an anchor property which represents for the locating point in its local space, and a position property for either absolute or percentage position in its parent's space relatively. The final position is calculated according to these two properties. An anchor component is typically in range of values from 0.0 to 1.0, but it could be also less than 0.0 or greater than 1.0. A relative position component is typically in range of values from Percent(0) to Percent(100), but it could be also less than Percent(0) or greater than Percent(100).


Resources are splitted into nine grids evenly for flex scaled widgets.

2. Structures
Structures
These structures are used to help organizing widget layout.
beStructures.Percent
beStructures.Percent denotes relative value instead of absolute for positioning and sizing depending on its parent properties.
Model: require 'libs/beGUI/beGUI_Structures'
- beStructures.
Percent.new(amount): constructs aPercentobjectamount: real number, no limit but often with range of values from 0 to 100
p.__mul(num): multiply thePercentvalue with another numbernum: the number to multiply- returns result number
beGUI.percent
Shortcut to create Percent object.
Model: require 'libs/beGUI/beGUI'
- beGUI.
percent(amount): constructs aPercentobjectamount: real number, no limit but often with range of values from 0 to 100- returns
Percent
3. Widget
Widget
beGUI.Widget
Model: require 'libs/beGUI/beGUI'
-
beGUI.
Widget.new(): constructs aWidgetobject -
widget:setId(id): sets the ID of theWidget; an ID is used to identify aWidgetfrom others for accessingid: ID string- returns
self
-
widget:get(...): gets a childWidgetwith the specific ID sequence...: ID sequence of the full hierarchy path- returns the got
Widgetornil
-
widget:find(id): finds the first matchedWidgetwith the specific IDid: ID string at any level of the hierarchy path- returns the found
Widgetornil
-
widget:visible(): gets the visibility of theWidget- returns boolean for visibility
-
widget:setVisible(val): sets the visibility of theWidgetval: whether it's visible- returns
self
-
widget:capturable(): gets the capturability of theWidget- returns boolean or string for capturability
-
widget:setCapturable(val): sets the capturability of theWidgetval:true,falseor'children'- returns
self
-
widget:anchor(x, y): sets the anchor of theWidget; anchor is used to calculate the offset when placingWidgetx: x position of the anchor in local space as number, typically [0.0, 1.0] for [left, right], but it could be also less than 0.0 or greater than 1.0y: y position of the anchor in local space as number, typically [0.0, 1.0] for [top, bottom], but it could be also less than 0.0 or greater than 1.0- returns
self
-
widget:offset(): gets the offset of theWidget- returns offset
x,yin world space
- returns offset
-
widget:put(x, y): sets the position of theWidgetx: number for absolute position; orPercentfor relative position, typically with range of values fromPercent(0)toPercent(100), but it could be also less thanPercent(0)or greater thanPercent(100)y: number for absolute position; orPercentfor relative position, typically with range of values fromPercent(0)toPercent(100), but it could be also less thanPercent(0)or greater thanPercent(100)- returns
self
-
widget:position(): gets the position of theWidget- returns position
x,yin local space
- returns position
-
widget:worldPosition(): gets the position of theWidgetin world space- returns position
x,yin world space
- returns position
-
widget:resize(width, height): sets the size of theWidgetwidth: number for absolute size; orPercentfor relative size, typically with range of values fromPercent(0.00...n)toPercent(100), but it could be also greater thanPercent(100)height: number for absolute size; orPercentfor relative size, typically with range of values fromPercent(0.00...n)toPercent(100), but it could be also greater thanPercent(100)- returns
self
-
widget:size(): gets the size of theWidget- returns size
width,height
- returns size
-
widget:alpha(): gets the alpha value of theWidget- returns transparency number, with range of values from 0 to 255
-
widget:setAlpha(val): sets the alpha value of theWidgetval: number with range of value from 0 to 255, or nil for default (255)- returns
self
-
widget:getChild(idOrIndex): gets the child with the specific ID or indexidOrIndex: ID string, or index number- returns the found child or nil
-
widget:insertChild(child, index): inserts a child before the specific indexchild: the childWidgetto insert- returns
self
-
widget:addChild(child): adds a child to the end of the children listchild: the childWidgetto add- returns
self
-
widget:removeChild(childOrIdOrIndex): removes a child with the specific child, or its ID or indexchild: childWidget, or its ID string, or index number- returns
self
-
widget:foreachChild(handler): iterates all children, and calls the specific handlerhandler: the children handler in form offunction (child, index) end- returns
self
-
widget:sortChildren(comp): sorts all children with the specific comparercomp: the comparer in form offunction (left, right) end- returns
self
-
widget:getChildrenCount(): gets the count of all children- returns children count number
-
widget:clearChildren(): clears all children- returns
self
- returns
-
widget:openPopup(content): opens a popupcontent: the popup to open- returns
self
-
widget:closePopup(): closes any popup- returns
self
- returns
-
widget:update(theme, delta, event = nil): updates theWidgetand its children recursivelytheme: the theme to draw withdelta: elapsed time since previous updateevent: omit it for common usage, pass a prefilled event to prevent default event
-
widget:on(event, handler): registers the handler of the specific eventevent: event name stringhandler: callback function- returns
self
-
widget:off(event): unregisters the handlers of the specific eventevent: event name string- returns
self
-
widget:navigatable(): gets whether thisWidgetis navigatable- returns
'all'for fully navigatable,nilfor non-navigatable,'children'for children only,'content'for content only
- returns
-
widget:navigate(dir): navigates through widgets, call this to perform key navigation, etc.dir: can be one in'prev','next','press','cancel'
-
widget:queriable(): gets whether thisWidgetis queriable- returns
truefor queriable, otherwisefalse
- returns
-
widget:setQueriable(val): sets whether thisWidgetis queriableval: whether thisWidgetis queriable- returns
self
-
widget:query(x, y): queries aWidgetat the specific positionx: the x position to queryy: the y position to query- returns the queried
Widgetornil
-
widget:captured(): gets whether thisWidgethas captured mouse event.- returns
truefor captured, otherwisefalse
- returns
-
widget:tween(t): schedules a tweening proceduret: the tweening object- returns
self
-
widget:clearTweenings(): clears all tweening procedures- returns
self
- returns
4. Basic Widgets
Basic Widgets
beGUI.Label
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
Label.new(content, alignment = 'left', clip_ = false, theme = nil, shadow = nil): constructs aLabelwith the specific contentcontent: the content stringalignment: one innil,'left','right','center'clip_: whether to clip drawing outside thisWidget's boundstheme: custom themeshadow: shadow theme for shadowed drawing
-
label:getValue(): gets the content text- returns the content string
-
label:setValue(val): sets the content textval: the specific content string- returns
self
-
label:alignment(): gets the alignment- returns the alignment preference string
-
label:setAlignment(val): sets the alignment preferenceval: the specific alignment preference string- returns
self
-
label:clipping(): gets whether to clip drawing outside theWidget's bounds- returns
truefor clipping, otherwisefalse
- returns
-
label:setClipping(val): sets whether to clip drawing outside theWidget's boundsval:trueto clip- returns
self
-
label:setTheme(theme, shadow = nil): sets the themetheme: the custom themeshadow: the custom shadow theme- returns
self
beGUI.MultilineLabel
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
MultilineLabel.new(content, lineHeight = nil, alighment = 'left'): constructs aMultilineLabelwith the specific contentcontent: the content stringlineHeight: the custom line heightalignment: one innil,'left','right','center'
-
multilinelabel:getValue(): gets the content text- returns the content string
-
multilinelabel:setValue(val): sets the content textval: the specific content string- returns
self
-
multilinelabel:lineHeight(): gets the line height- returns the line height
-
multilinelabel:setLineHeight(val): sets the line heightval: the specific line height- returns
self
-
multilinelabel:alignment(): gets the alignment- returns the alignment preference string
-
multilinelabel:setAlignment(val): sets the alignment preference, the preference falls to'left'if had set flex width totrueval: the specific alignment preference string- returns
self
-
multilinelabel:setTheme(theme, widgetTheme): sets the themetheme: the custom font themewidgetTheme: the custom widget theme- returns
self
-
multilinelabel:flexWidth(): gets whether to calculateWidgetwidth automatically- returns
truefor calculating automatically, otherwisefalse
- returns
-
multilinelabel:setFlexWidth(val): sets whether to calculateWidgetwidth automaticallyval:trueto calculate automatically- returns
self
-
multilinelabel:flexHeight(): gets whether to calculateWidgetheight automatically- returns
truefor calculating automatically, otherwisefalse
- returns
-
multilinelabel:setFlexHeight(val): sets whether to calculateWidgetheight automaticallyval:trueto calculate automatically- returns
self
-
multilinelabel:pattern(): gets the word split pattern- returns the word split pattern string
-
multilinelabel:setPattern(val): sets the word split patternval: the specific word split pattern- returns
self
-
multilinelabel:translator(): gets the word translator- returns the word translator
-
multilinelabel:setTranslator(val): sets the word translatorval: the specific word translator- returns
self
beGUI.Url
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
Url.new(content, alighment = 'left', clip_ = false, theme = nil): constructs aUrlwith the specific contentcontent: the content stringalignment: one innil,'left','right','center'clip_: whether to clip drawing outside thisWidget's boundstheme: custom theme
-
url:getValue(): gets the content text- returns the content string
-
url:setValue(val): sets the content textval: the specific content string- returns
self
-
url:alignment(): gets the alignment- returns the alignment preference string
-
url:setAlignment(val): sets the alignment preferenceval: the specific alignment preference string- returns
self
-
url:clipping(): gets whether to clip drawing outside theWidget's bounds- returns
truefor clipping, otherwisefalse
- returns
-
url:setClipping(val): sets whether to clip drawing outside theWidget's boundsval:trueto clip- returns
self
-
url:setTheme(theme): sets the themetheme: the custom theme- returns
self
-
url:on('clicked', function (sender) end): registers an event which will be triggered when theWidgethas been clicked- returns
self
- returns
beGUI.InputBox
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
InputBox.new(content placeholder): constructs an InputBox with the specific contentcontent: the content stringplaceholder: the placeholder string when there's no input yet
-
inputbox:getValue(): gets the content text- returns the content string
-
inputbox:setValue(val): sets the content textval: the specific content string
-
inputbox:setTheme(theme, placeholderTheme): sets the themetheme: the custom font themeplaceholderTheme: the custom placeholder theme- returns
self
-
inputbox:placeholder(): gets the placeholder text- returns the placeholder string
-
inputbox:setPlaceholder(val): sets the placeholder textval: the specific placeholder string- returns
self
-
inputbox:on('changed', function (sender, value) end): registers an event which will be triggered when theWidgetcontent text has been changed- returns
self
- returns
beGUI.Picture
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
Picture.new(content, stretched = false, permeation = false): constructs a Picture with the specific contentcontent: the contentTexturestretched: whether to use 9-grid-based splitting for stretchingpermeation: whether to use permeation correction
-
picture:setValue(content, stretched = false, permeation = false): sets the contentTexturecontent: the contentTexture- returns
self
-
picture:stretched(): gets whether to use 9-grid-based splitting for stretching- returns
truefor 9-grid-based splitting, otherwisefalse
- returns
-
picture:setStretched(val): sets whether to use 9-grid-based splitting for stretchingval:trueto use 9-grid-based splitting for stretching- returns
self
-
picture:permeation(): gets whether to use permeation correction- returns
truefor permeation correction, otherwisefalse
- returns
-
picture:setPermeation(val): sets whether to use permeation correctionval:trueto use permeation correction- returns
self
-
picture:color(): gets the mask color of thePicture- returns the mask color or
nil
- returns the mask color or
-
picture:setColor(val): sets the mask color of thePictureval: the specific mask color- returns
self
beGUI.Button
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
Button.new(content): constructs a Button with the specific contentcontent: the content string
-
button:setValue(content): sets the content textval: the specific content string
-
button:setTheme(theme, themeNormal, themeDown, themeDisabled): sets the themetheme: the custom font themethemeNormal: the custom theme for normal statethemeDown: the custom theme for pressed statethemeDisabled: the custom theme for disabled state- returns
self
-
button:enabled(): gets whether thisWidgetis enabled- returns
truefor enabled, otherwisefalse
- returns
-
button:setEnabled(val): sets whether thisWidgetis enabledval:truefor enabled, otherwisefalse- returns
self
-
button:on('clicked', function (sender) end): registers an event which will be triggered when theWidgethas been clicked- returns
self
- returns
beGUI.PictureButton
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
PictureButton.new(content, repeat_ = false, theme = nil, background = nil): constructs aPictureButtonwith the specific contentcontent: the contentTexturerepeat_: whether to enable repeating eventtheme: the custom themebackground: optional, the custom backgroundTexture
-
picturebutton:setTheme(theme, widgetTheme): sets the themetheme: the custom font themewidgetTheme: the custom widget theme- returns
self
-
picturebutton:enabled(): gets whether thisWidgetis enabled- returns
truefor enabled, otherwisefalse
- returns
-
picturebutton:setEnabled(val): sets whether thisWidgetis enabledval:truefor enabled, otherwisefalse- returns
self
-
picturebutton:on('clicked', function (sender) end): registers an event which will be triggered when theWidgethas been clicked- returns
self
- returns
beGUI.CheckBox
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
CheckBox.new(content, value = false): constructs aCheckBoxwith the specific contentcontent: the content stringvalue: the initial checked state
-
checkbox:getValue(): gets whether thisWidgetis checked- returns
truefor checked, otherwisefalse
- returns
-
checkbox:setValue(val): sets whether thisWidgetis checkedval:truefor checked, otherwisefalse- returns
self
-
checkbox:setContent(val): sets the text content of thisWidgetval: the content string- returns
self
-
checkbox:enabled(): gets whether thisWidgetis enabled- returns
truefor enabled, otherwisefalse
- returns
-
checkbox:setEnabled(val): sets whether thisWidgetis enabledval:truefor enabled, otherwisefalse- returns
self
-
checkbox:on('changed', function (sender, value) end): registers an event which will be triggered when theWidgetchecked state has been changed- returns
self
- returns
beGUI.RadioBox
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
RadioBox.new(content, value = false): constructs aRadioBoxwith the specific contentcontent: the content stringvalue: the initial checked state
-
radiobox:getValue(): gets whether thisWidgetis checked- returns
truefor checked, otherwisefalse
- returns
-
radiobox:setValue(val): sets whether thisWidgetis checked; not recommended to call this manuallyval:truefor checked, otherwisefalse
-
radiobox:setContent(val): sets the text content of thisWidgetval: the content string- returns
self
-
radiobox:on('changed', function (sender, value) end): registers an event which will be triggered when theWidgetchecked state has been changed- returns
self
- returns
beGUI.ComboBox
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
ComboBox.new(content, value = nil): constructs aComboBoxwith the specific contentcontent: list of stringvalue: the selected index number
-
combobox:getItemAt(index): gets the item text at the specific indexindex: the specific index to get- returns got item string or
nil
-
combobox:addItem(item): adds an item string with the specific content textitemthe specific item text to add- returns
self
-
combobox:removeItemAt(index): removes the item at the specific indexindexthe specific index to remove- returns
truefor success, otherwisefalse
-
combobox:clearItems(): clears all items- returns
self
- returns
-
combobox:getValue(): gets the selected index- returns the selected index number
-
combobox:setValue(val): sets the selected indexval: the specific selected index- returns
self
-
combobox:scrollable(): gets whether can scroll the widget by mouse wheel- returns
truefor scrollable, otherwisefalse
- returns
-
combobox:setScrollable(val): sets whether can scroll the widget by mouse wheelval:truefor allowing scrolling with a mouse wheel, otherwisefalse- returns
self
-
combobox:on('changed', function (sender, value) end): registers an event which will be triggered when theWidgetselection state has been changed- returns
self
- returns
beGUI.NumberBox
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
NumberBox.new(value, step, min = nil, max = nil, trim = nil, format = nil): constructs aNumberBoxwith the specific valuevalue: the initial value numberstep: the changing stepmin: the minumum limitmax: the maximum limittrim: optional, used to trim before value settingformat: optional, used to format value for output
-
numberbox:getValue(): gets the value number- returns the value number
-
numberbox:setValue(val): sets the value numberval: the specific value number- returns
self
-
numberbox:getMinValue(): gets the minimum limit number- returns the minimum limit number
-
numberbox:setMinValue(val): sets the minimum limit numberval: the specific minimum limit number- returns
self
-
numberbox:getMaxValue(): gets the maximum limit number- returns the maximum limit number
-
numberbox:setMaxValue(val): sets the maximum limit numberval: the specific maximum limit number- returns
self
-
numberbox:step(): gets the changing step- returns the changing step
-
numberbox:setStep(val): sets the changing stepval: the specific changing step number- returns
self
-
numberbox:trim(): gets the trim function- returns the trim function
-
numberbox:setTrim(val): sets the trim functionval: the specific trim function- returns
self
-
numberbox:format(): gets the format function- returns the format function
-
numberbox:setFormat(val): sets the format functionval: the specific format function- returns
self
-
numberbox:setValueTheme(val): sets the value themeval: the specific theme- returns
self
-
numberbox:scrollable(): gets whether can scroll the widget by mouse wheel- returns
truefor scrollable, otherwisefalse
- returns
-
numberbox:setScrollable(val): sets whether can scroll the widget by mouse wheelval:truefor allowing scrolling with a mouse wheel, otherwisefalse- returns
self
-
numberbox:on('changed', function (sender, value) end): registers an event which will be triggered when theWidgetvalue has been changed- returns
self
- returns
beGUI.ProgressBar
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
ProgressBar.new(max, color, increasing = 'right'): constructs aProgressBarmax: the maximum valuecolor: the color for the completed barincreasing: indicates whether to increase from left to right, or reversed, one in'left','right'
-
progressbar:getValue(): gets the value number- returns the value number
-
progressbar:setValue(val): sets the value numberval: the specific value number- returns
self
-
progressbar:getMaxValue(): gets the maximum limit number- returns the maximum limit number
-
progressbar:setMaxValue(val): sets the maximum limit numberval: the specific maximum limit number- returns
self
-
progressbar:getShadowValue(): gets the shadow value number- returns the value number
-
progressbar:setShadowValue(val): sets the shadow value numberval: the specific shadow value number- returns
self
-
progressbar:setTheme(theme): sets the themetheme: the custom theme- returns
self
-
progressbar:on('changed', function (sender, value, maxValue, shadowValue) end): registers an event which will be triggered when theWidgetvalue has been changed- returns
self
- returns
beGUI.Slide
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
Slide.new(value, min, max): constructs aSlidewith the specific valuevalue: the initial value numbermin: the minimum limit numbermax: the maximum limit number
-
slide:getValue(): gets the value number- returns the value number
-
slide:setValue(val): sets the value numberval: the specific value number- returns
self
-
slide:getMinValue(): gets the minimum limit number- returns the minimum limit number
-
slide:setMinValue(val): sets the minimum limit numberval: the specific minimum limit number- returns
self
-
slide:getMaxValue(): gets the maximum limit number- returns the maximum limit number
-
slide:setMaxValue(val): sets the maximum limit numberval: the specific maximum limit number- returns
self
-
slide:scrollable(): gets whether can scroll the widget by mouse wheel- returns
truefor scrollable, otherwisefalse
- returns
-
slide:setScrollable(val): sets whether can scroll the widget by mouse wheelval:truefor allowing scrolling with a mouse wheel, otherwisefalse- returns
self
-
slide:on('changed', function (sender, value) end): registers an event which will be triggered when theWidgetvalue has been changed- returns
self
- returns
beGUI.Group
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
Group.new(content): constructs aGroupcontent: the content string
-
group:getValue(): gets the content text- returns the content string
-
group:setValue(val): sets the content textval: the specific content string- returns
self
5. Container Widgets
Container Widgets
beGUI.List
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
List.new(withScrollBar = false): constructs aListwithScrollBar: whether to draw scroll bar(s)
-
list:scrollableVertically(): gets whether to allow scrolling vertically- returns
truefor allowing scrolling vertically, otherwisefalse
- returns
-
list:setScrollableVertically(val): sets whether to allow scrolling verticallyval:truefor allowing scrolling vertically, otherwisefalse- returns
self
-
list:scrollableHorizontally(): gets whether to allow scrolling horizontally- returns
truefor allowing scrolling horizontally, otherwisefalse
- returns
-
list:setScrollableHorizontally(val): sets whether to allow scrolling horizontallyval:truefor allowing scrolling horizontally, otherwisefalse- returns
self
-
list:scrollSpeed(): gets the scroll speed- returns the scroll speed
-
list:setScrollSpeed(val): sets the scroll speedval: the specific scroll speed- returns
self
-
list:setTheme(theme): sets the themetheme: the custom theme- returns
self
-
list:scrollable(): gets whether can scroll the widget by mouse wheel- returns
truefor scrollable, otherwisefalse
- returns
-
list:setScrollable(val): sets whether can scroll the widget by mouse wheelval:truefor allowing scrolling with a mouse wheel, otherwisefalse- returns
self
beGUI.Draggable
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
- beGUI.
Draggable.new(): constructs aDraggable
beGUI.Droppable
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
Droppable.new(): constructs aDroppable -
droppable:on('entered', function (sender, draggable) end): registers an event which will be triggered when theWidgethas been entered by aDraggable- returns
self
- returns
-
droppable:on('left', function (sender, draggable) end): registers an event which will be triggered when theWidgethas been left by aDraggable- returns
self
- returns
-
droppable:on('dropping', function (sender, draggable) return droppable end): registers an event which will be triggered when theWidgethas been hovering by aDraggable- returns
self
- returns
-
droppable:on('dropped', function (sender, draggable) end): registers an event which will be triggered when theWidgethas been dropped by aDraggable- returns
self
- returns
-
droppable:on('clicked', function (sender) end): registers an event which will be triggered when theWidgethas been clicked- returns
self
- returns
beGUI.Tab
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
Tab.new(): constructs aTab -
tab:add(title): adds aTabpage with the specific titletitle: theTabpage title to add- returns
self
-
tab:count(): gets the page count of theTabWidget- returns the
Tabpage count
- returns the
-
tab:getValue(): gets the active page index- returns the active page index number
-
tab:setValue(val): sets the active page indexval: the specific page index- returns
self
-
tab:tabSize(): gets the specifiedTabsize- returns the specified
Tabsize
- returns the specified
-
tab:setTabSize(val): sets the specifiedTabsizeval: the specifiedTabsize- returns
self
-
tab:scrollable(): gets whether can scroll the widget by mouse wheel- returns
truefor scrollable, otherwisefalse
- returns
-
tab:setScrollable(val): sets whether can scroll the widget by mouse wheelval:truefor allowing scrolling with a mouse wheel, otherwisefalse- returns
self
-
tab:on('changed', function (sender, value) end): registers an event which will be triggered when theWidgetpage has been switched- returns
self
- returns
beGUI.Popup
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
- beGUI.
Popup.new(): constructs aPopup
beGUI.MessageBox
Model: require 'libs/beGUI/beGUI', implements beGUI.Popup
-
beGUI.
MessageBox.new(closable, title, message, confirm = 'OK'): constructs aMessageBoxclosable:trueto enable the close button,falseto disabletitle: the title textmessage: the message textconfirm: the text for the confirm button
-
messagebox:on('canceled', function (sender) end): registers an event which will be triggered when thePopuphas been canceled- returns
self
- returns
-
messagebox:on('confirmed', function (sender) end): registers an event which will be triggered when thePopuphas been confirmed- returns
self
- returns
beGUI.QuestionBox
Model: require 'libs/beGUI/beGUI', implements beGUI.Popup
-
beGUI.
QuestionBox.new(closable, title, messsage, confirm, deny): constructs aQuestionBoxclosable:trueto enable the close button,falseto disabletitle: the title textmessage: the message textconfirm: the text for the confirm buttondeny: the text for the deny button
-
questionbox:on('canceled', function (sender) end): registers an event which will be triggered when thePopuphas been canceled- returns
self
- returns
-
questionbox:on('confirmed', function (sender) end): registers an event which will be triggered when thePopuphas been confirmed- returns
self
- returns
-
questionbox:on('denied', function (sender) end): registers an event which will be triggered when thePopuphas been denied- returns
self
- returns
6. Custom Widget
Custom Widget
There are two ways to customize your own Widget, one is to use the beWidget.Custom Widget, the other is to write your own Widget class.
beGUI.Custom
The Custom Widget exposes a 'updated' event to let you write short customized update routine in the callback.
Model: require 'libs/beGUI/beGUI', implements beGUI.Widget
-
beGUI.
Custom.new(name = 'Custom'): constructs aCustomWidgetname: the customWidgetname used to perform__tostring
-
custom:name(): gets the customWidgetname- returns the custom
Widgetname
- returns the custom
-
custom:setName(val): sets the customWidgetnameval: the specific customWidgetname- returns
self
-
custom:on('updated', function (sender, x, y, w, h, delta, event) end): registers an event which will be triggered when theWidgethas been updated per frame- returns
self
- returns
Writing Your Own Widget
You can also write your own Widget inheriting from beWidget.Widget.
local beClass = require 'libs/beGUI/beClass'
local beUtils = require 'libs/beGUI/beGUI_Utils'
local beWidget = require 'libs/beGUI/beGUI_Widget'
local MyWidget = beClass.class({
_value = nil, -- Define your fields.
_pressed = false,
ctor = function (self, ...)
beWidget.Widget.ctor(self)
-- Customize your constructor.
end,
__tostring = function (self)
return 'MyWidget'
end,
getValue = function (self) -- Define your properties.
return self._value
end,
setValue = function (self, val)
if self._value == val then
return self
end
self._value = val
self:_trigger('changed', self, self._value)
return self
end,
_update = function (self, theme, delta, dx, dy, event)
-- Ignore if invisible.
if not self.visibility then
return
end
-- Get the offset x, y, which is calculated by this widget's size and its anchor.
local ox, oy = self:offset()
-- Get the position x, y in local space.
local px, py = self:position()
-- Calculate the final position with the delta position, offset and local position,
-- where the delta position (`dx`, `dy`) is from this widget's parent, or 0, 0 for root widget.
local x, y = dx + px + ox, dy + py + oy
-- Get the size width, height of this widget.
local w, h = self:size()
-- The following code detects clicking.
local down = false
if event.context.active and event.context.active ~= self then
self._pressed = false
elseif event.canceled or event.context.dragging then
event.context.active = nil
self._pressed = false
elseif self._pressed then
down = event.mouseDown
else
-- Intersection detection.
down = event.mouseDown and Math.intersects(event.mousePosition, Rect.byXYWH(x, y, w, h))
end
if down and not self._pressed then
event.context.active = self
self._pressed = true
elseif not down and self._pressed then
event.context.active = nil
self._pressed = false
event.context.focus = self
self:_trigger('clicked', self) -- Trigger 'clicked' event by clicking.
elseif event.context.focus == self and event.context.navigated == 'press' then
self:_trigger('clicked', self) -- Trigger 'clicked' event by key navigation.
event.context.navigated = false
end
-- Draw the widget.
local elem = down and theme['button_down'] or theme['button'] -- Using the button theme.
beUtils.tex9Grid(elem, x, y, w, h, nil, self.transparency, nil) -- Draw texture.
beUtils.textCenter(self._value, theme['font'], x, y, w, h, elem.content_offset, self.transparency) -- Draw text.
-- Call base update to update its children.
beWidget.Widget._update(self, theme, delta, dx, dy, event)
end
}, beWidget.Widget)
7. Theme
Theme
Defined in "src/libs/beGUI/beTheme.lua". Widget classes will lookup for image resources, client area, content offset, fonts, colors and all other appearance preferences from it.
8. Tweening
Tweening
beGUI is integrated with a tweening lib adapted from kikito/tween.lua, which allows to create tweening animations.
beGUI.Tween
Model: require 'libs/beGUI/beGUI'
-
beGUI.
Tween.new(duration, subject, target, easing, loop): constructs aTweenobjectduration: the duration in secondssubject: the tweening subjecttarget: the tweening targeteasing: the easing functionloop: whether to loop the tweening
-
tween:reset(): resets theTweenobject- returns
self
- returns
-
tween:set(clock): sets theTweenobject to a specific clock pointclock: the click time point- returns
truefor success, otherwisefalse
-
tween:update(delta): updates theTweenobject with a specific delta time in seconds- returns
truefor success, otherwisefalse
- returns
-
tween:on('changed', function (sender) end): registers an event which will be triggered when theTweenhas been updated- returns
self
- returns
-
tween:on('completed', function (sender) end): registers an event which will be triggered when theTweenhas completed or looped- returns
self
- returns
-
tween:off(event): unregisters the handlers of the specific eventevent: event name string- returns
self
License
beGUI is distributed under the MIT license.