-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle.lua
More file actions
49 lines (41 loc) · 1.14 KB
/
toggle.lua
File metadata and controls
49 lines (41 loc) · 1.14 KB
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
gui.toggle = class("gui.toggle", component)
function gui.toggle:created(offImg, onImg, makeButton)
-- you can accept and set parameters here
self.offImg = offImg
self.onImg = onImg
self.state = false
self.entity.sprite = self.offImg
if makeButton then
button = self.entity:add(gui.button)
button.style.pressedColor = color(255)
self.entity.sprite = offImg
self.entity.onTapped = function(button)
self.state = not self.state
if self.state == true then
self.entity.sprite = self.onImg
self.entity:dispatch('turnedOn', self)
else
self.entity.sprite = self.offImg
self.entity:dispatch('turnedOff', self)
end
end
end
end
function gui.toggle:flip()
self:set(not self.state)
end
function gui.toggle:set(value)
if value then
self:turnOn()
else
self:turnOff()
end
end
function gui.toggle:turnOff()
self.state = false
self.entity.sprite = self.offImg
end
function gui.toggle:turnOn()
self.state = true
self.entity.sprite = self.onImg
end