-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathui_radio.lua
99 lines (81 loc) · 2.13 KB
/
ui_radio.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
-- radio button
local log = require 'log'
local Widget = require 'ui_widget'
local Util = require 'util'
local Radio = {}
Radio.__index = Radio
setmetatable(Radio, {__index = Widget})
function Radio.new(o)
assert(o.text)
assert(o.var)
assert(o.val)
local fname = 'assets/icons/radio_button_checked.png'
local imageData = love.image.newImageData(fname)
if not imageData then
log.error('could not load', fname)
else
o.imgWidth = imageData:getWidth() * _G.UI_SCALE
o.imgHeight = imageData:getHeight() * _G.UI_SCALE
o.imgChecked = love.graphics.newImage(imageData)
end
fname = 'assets/icons/radio_button_unchecked.png'
imageData = love.image.newImageData(fname)
if not imageData then
log.error('could not load', fname)
else
o.imgUnchecked = love.graphics.newImage(imageData)
end
o.baizeCmd = 'toggleRadio'
o.param = o
o.enabled = true
-- check and img will be updated when drawer is shown by showSettingsDrawer
-- supply some values for now, so layout works
o.checked = true
o.img = o.imgChecked
return setmetatable(o, Radio)
end
function Radio:setChecked(checked)
if checked then
self.checked = true
self.img = self.imgChecked
else
self.checked = false
self.img = self.imgUnchecked
end
end
function Radio:draw()
local cx, cy, cw, ch = self.parent:screenRect()
local wx, wy, ww, wh = self:screenRect()
-- TODO consider using scissors
if wy < cy then
return
end
if wy + wh > cy + ch then
return
end
love.graphics.setFont(self.parent.font)
if self.enabled then
local mx, my = love.mouse.getPosition()
if self.baizeCmd and Util.inRect(mx, my, self:screenRect()) then
Util.setColorFromName('UiForeground')
if love.mouse.isDown(1) then
wx = wx + 2
wy = wy + 2
end
else
Util.setColorFromName('UiForeground')
end
else
Util.setColorFromName('UiGrayedOut')
end
love.graphics.draw(self.img, wx, wy, 0, _G.UI_SCALE, _G.UI_SCALE)
if self.text then
love.graphics.print(self.text, wx + self.imgWidth + 8, wy + 3)
end
if _G.SETTINGS.debug then
Util.setColorFromName('UiGrayedOut')
love.graphics.setLineWidth(1)
love.graphics.rectangle('line', wx, wy, ww, wh)
end
end
return Radio