-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
checkbox.lua
125 lines (101 loc) · 2.89 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
--=========== Copyright © 2019, Planimeter, All rights reserved. ===========--
--
-- Purpose: Checkbox class
--
--==========================================================================--
class "gui.checkbox" ( "gui.button" )
local checkbox = gui.checkbox
function checkbox:checkbox( parent, name, text )
gui.button.button( self, parent, name, text )
self.width = nil
self.height = nil
self:setPadding( 4, 0, 3 )
self:setBorderWidth( 0 )
self.text:setDisplay( "inline-block" )
self.text:set( text )
self.text:setMarginLeft( 34 )
self.icon = self:getScheme( "checkbox.icon" )
self.checked = false
end
function checkbox:draw()
self:drawCheck()
self:drawBorder()
self:drawLabel()
gui.box.draw( self )
end
function checkbox:drawCheck()
if ( not self:isChecked() ) then
return
end
local color = self:getScheme( "checkbox.iconColor" )
if ( self:isDisabled() ) then
color = self:getScheme( "checkbox.disabled.iconColor" )
end
love.graphics.setColor( color )
local height = self:getHeight()
local x = math.round( height / 2 - self.icon:getWidth() / 2 )
local y = math.round( height / 2 - self.icon:getHeight() / 2 )
love.graphics.draw( self.icon, x, y )
end
function checkbox:drawBorder()
local mouseover = ( self.mouseover or self:isChildMousedOver() )
local color = self:getScheme( "checkbox.borderColor" )
if ( not self:isDisabled() ) then
if ( self.mousedown and mouseover ) then
color = self:getScheme( "checkbox.mousedown.borderColor" )
elseif ( self.mousedown or mouseover or self.focus ) then
color = self:getScheme( "checkbox.mouseover.borderColor" )
end
end
love.graphics.setColor( color )
local lineWidth = 1
love.graphics.setLineWidth( lineWidth )
local height = self:getHeight()
love.graphics.rectangle(
"line",
lineWidth / 2,
lineWidth / 2,
height - lineWidth,
height - lineWidth
)
end
function checkbox:drawLabel()
local color = self:getScheme( "checkbox.textColor" )
if ( self:isDisabled() ) then
color = self:getScheme( "checkbox.disabled.textColor" )
end
self.text:setColor( color )
end
accessor( checkbox, "checked", "is" )
function checkbox:keypressed( key, scancode, isrepeat )
if ( not self.focus or self:isDisabled() ) then
return
end
if ( key == "return"
or key == "kpenter"
or key == "space" ) then
self:setChecked( not self:isChecked() )
self:onClick()
end
end
function checkbox:mousereleased( x, y, button, istouch )
local mouseover = ( self.mouseover or self:isChildMousedOver() )
if ( ( self.mousedown and mouseover ) and not self:isDisabled() ) then
self:setChecked( not self:isChecked() )
self:onClick()
end
if ( self.mousedown ) then
self.mousedown = false
self:invalidate()
end
end
function checkbox:onCheckedChanged( checked )
end
function checkbox:setChecked( checked )
if ( self.checked == checked ) then
return
end
self.checked = checked
self:onCheckedChanged( self.checked )
self:invalidate()
end