-
Notifications
You must be signed in to change notification settings - Fork 0
/
Checkbox.lua
62 lines (51 loc) · 1.72 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
-- Checkbox.lua
-- Constants
local uncheckedImage = "GFX/checkOFF.png"
local checkedImage = "GFX/checkON.png"
-- Checkbox Class
local Checkbox = {}
Checkbox.__index = Checkbox
function Checkbox:new(params)
local self = setmetatable({}, Checkbox)
self.id = params.id
self.x = params.x
self.y = params.y
self.parentGroup = params.parentGroup
self.isChecked = false
self.displayObject = display.newImage(self.parentGroup, uncheckedImage)
self.displayObject.x = self.x
self.displayObject.y = self.y
self.displayObject:scale(0.2, 0.2)
self.displayObject.id = self.id
self.displayObject.checkbox = self -- Reference back to the checkbox object
self.displayObject:addEventListener("touch", function(event) return self:toggleCheckbox(event, params.getSelectedImage()) end)
return self
end
function Checkbox:setCheckedState(state, selectedImage)
self.isChecked = state
local imagePath = state and checkedImage or uncheckedImage
self.displayObject.fill = {type = "image", filename = imagePath}
if selectedImage then
if state then
if self.id == "checkboxFlipX" then
selectedImage.xScale = -1
else
selectedImage.yScale = -1
end
else
if self.id == "checkboxFlipX" then
selectedImage.xScale = 1
else
selectedImage.yScale = 1
end
end
end
end
function Checkbox:toggleCheckbox(event, selectedImage)
local checkbox = event.target.checkbox
if event.phase == "ended" and selectedImage then
checkbox:setCheckedState(not checkbox.isChecked, selectedImage)
end
return true
end
return Checkbox