opus/sys/modules/opus/ui/components/Checkbox.lua

73 lines
1.6 KiB
Lua
Raw Normal View History

local class = require('opus.class')
local UI = require('opus.ui')
2019-02-06 04:03:57 +00:00
UI.Checkbox = class(UI.Window)
UI.Checkbox.defaults = {
UIElement = 'Checkbox',
nochoice = 'Select',
checkedIndicator = UI.extChars and '\4' or 'X',
leftMarker = UI.extChars and '\124' or '[',
rightMarker = UI.extChars and '\124' or ']',
2019-02-06 04:03:57 +00:00
value = false,
textColor = 'white',
backgroundColor = 'black',
backgroundFocusColor = 'lightGray',
2020-04-23 05:37:12 +00:00
event = 'checkbox_change',
2019-02-06 04:03:57 +00:00
height = 1,
2019-02-12 22:03:50 +00:00
width = 3,
2019-02-06 04:03:57 +00:00
accelerators = {
space = 'checkbox_toggle',
mouse_click = 'checkbox_toggle',
}
}
function UI.Checkbox:layout()
2019-11-17 05:12:02 +00:00
self.width = self.label and #self.label + 4 or 3
UI.Window.layout(self)
2019-11-17 05:12:02 +00:00
end
2019-02-06 04:03:57 +00:00
function UI.Checkbox:draw()
2019-11-17 05:12:02 +00:00
local bg = self.focused and self.backgroundFocusColor or self.backgroundColor
2019-02-06 04:03:57 +00:00
local x = 1
if self.label then
2019-11-17 05:12:02 +00:00
self:write(1, 1, self.label, self.labelBackgroundColor)
2019-02-06 04:03:57 +00:00
x = #self.label + 2
end
self:write(x, 1, self.leftMarker, self.backgroundColor, self.textColor)
self:write(x + 1, 1, not self.value and ' ' or self.checkedIndicator, bg)
self:write(x + 2, 1, self.rightMarker, self.backgroundColor, self.textColor)
end
function UI.Checkbox:focus()
self:draw()
end
2019-02-22 08:52:47 +00:00
function UI.Checkbox:setValue(v)
2019-11-17 05:12:02 +00:00
self.value = not not v
2019-02-22 08:52:47 +00:00
end
2019-02-06 04:03:57 +00:00
function UI.Checkbox:reset()
self.value = false
2019-11-17 05:12:02 +00:00
self:draw()
2019-02-06 04:03:57 +00:00
end
function UI.Checkbox:eventHandler(event)
if event.type == 'checkbox_toggle' then
self.value = not self.value
2020-04-23 05:37:12 +00:00
self:emit({ type = self.event, checked = self.value, element = self })
2019-02-06 04:03:57 +00:00
self:draw()
return true
end
end
function UI.Checkbox.example()
2019-11-17 05:12:02 +00:00
return UI.Window {
ex1 = UI.Checkbox {
label = 'test',
x = 2, y = 2,
},
ex2 = UI.Checkbox {
x = 2, y = 4,
},
}
end