-
Notifications
You must be signed in to change notification settings - Fork 2
/
checkbox.lua
139 lines (113 loc) · 3.16 KB
/
checkbox.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
--
-- CheckBox Widget.
-- @copyright Jefferson Gonzalez
-- @license MIT
--
local style = require "core.style"
local Widget = require "widget"
---@class widget.checkbox : widget
---@overload fun(parent?:widget, label?:string):widget.checkbox
---@field private checked boolean
local CheckBox = Widget:extend()
---Constructor
---@param parent widget
---@param label string
function CheckBox:new(parent, label)
CheckBox.super.new(self, parent)
self.type_name = "widget.checkbox"
self.checked = false
self:set_label(label or "")
self.animating = false
self.animating_color = style.caret
end
---Set the checkbox label and recalculates the widget size.
---@param text string
function CheckBox:set_label(text)
CheckBox.super.set_label(self, text)
local _, _, bw, _ = self:get_box_rect()
local font = self:get_font()
local size = self:get_size()
size.x = font:get_width(self.label) + bw + (style.padding.x / 2)
size.y = font:get_height()
end
---Change the status of the checkbox.
---@param checked boolean
function CheckBox:set_checked(checked)
self.checked = checked
self:on_change(self.checked)
end
---Get the status of the checkbox.
---@return boolean
function CheckBox:is_checked()
return self.checked
end
---Called when the checkbox is (un)checked.
---@param checked boolean
function CheckBox:on_checked(checked) end
function CheckBox:on_mouse_enter(...)
CheckBox.super.on_mouse_enter(self, ...)
self.hover_text = style.accent
self.hover_back = style.dim
end
function CheckBox:on_mouse_leave(...)
CheckBox.super.on_mouse_leave(self, ...)
self.hover_text = nil
self.hover_back = nil
end
function CheckBox:on_click()
self.checked = not self.checked
self:on_checked(self.checked)
self:on_change(self.checked)
self.animating = true
self.animating_color = {table.unpack(style.caret)}
local target_color = {table.unpack(style.caret)}
if self.checked then
self.animating_color[4] = 0
target_color[4] = 255
else
self.animating_color[4] = 255
target_color[4] = 0
end
self:animate(self.animating_color, {table.unpack(target_color)}, {
on_complete = function()
self.animating = false
end
})
end
function CheckBox:get_box_rect()
local size = 1.6
local font = self:get_font()
local fh = font:get_height() / size
return
self.position.x,
self.position.y + (fh / (size * 2)),
font:get_width("x") + 4,
fh
end
function CheckBox:update_size_position()
CheckBox.super.update_size_position(self)
self:set_label(self.label)
end
function CheckBox:draw()
if not self:is_visible() then return false end
local bx, by, bw, bh = self:get_box_rect()
self:draw_border(bx, by, bw, bh)
renderer.draw_rect(
bx, by, bw, bh,
self.hover_back or self.background_color or style.background
)
if self.animating then
renderer.draw_rect(bx + 2, by + 2, bw-4, bh-4, self.animating_color)
elseif self.checked then
renderer.draw_rect(bx + 2, by + 2, bw-4, bh-4, style.caret)
end
renderer.draw_text(
self:get_font(),
self.label,
self.position.x + bw + (style.padding.x / 2),
self.position.y,
self.hover_text or self.foreground_color or style.text
)
return true
end
return CheckBox