-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoggle.lua
198 lines (149 loc) · 6.46 KB
/
toggle.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
--[[ ########## Toggle Class ########## ]--
Version 1.0
Written and supported by Adam Buchweitz and Crawl Space Games
http://crawlspacegames.com
http://twitter.com/crawlspacegames
For inquiries about Crawl Space, email us at
For support with this class, email me at
Copyright (C) 2011 Crawl Space Games - All Rights Reserved
:: FEATURES ::
This toggle is a simple toggle switch. It is capable of switching
between two images, with or without dynamic resolution, and can also
switch between two strings.
To toggle images, the only parameters needed are on and off images.
To use dynamic resolution images, simply supply additional parameters
for width and height.
To toggle strings, you have the option of supplying a font, a text
size, and a text color.
The toggle switches via touch, but you can also set the state manually,
and retrieve its state at any time.
Declare a default value with a 'state' parameter, and the method to
fire with a 'callback' parameter.
:: USAGE ::
ToggleClass.new( params )
:: EXAPMLE - Switch between two images ::
local toggle = require "toggle"
local myToggle = toggle.new( { on = "path/to/imageOn.png", off = "path/to/imageOff.png", callback = myFunction } )
:: EXAPMLE - Switch between two images with dynamic resolution, and default the state to off ::
local toggle = require "toggle"
local params = {
on = "path/to/imageOn.png",
onWidth = 100,
onHeight = 50,
off = "path/to/imageOff.png",
offWidth = 100,
offHeight = 50,
state = false,
callback = myFunction
}
local myToggle = toggle.new( params )
:: EXAPMLE - Switch between two strings with a custom font, size, and color ::
local toggle = require "toggle"
local params = {
onText = "On",
offText = "Off",
font = "Helvetica",
textSize = "24",
textColorOn = { 255, 0, 0 }
textColorOff = { 0, 255, 0 }
callback = myFunction
}
local myToggle = toggle.new( params )
:: EXAPMLE - Change the VISUAL state manually, this does NOT fire the attached callback ::
myToggle.setState(true)
:: EXAPMLE - Change the state manually and fire its attached callback ::
myToggle.setState(true, true)
]]
local ToggleClass = {}
ToggleClass.new = function(params)
local params = params
if not params then
error("You must supply parameters to make a new Toggle")
return false
end
local toggle = display.newGroup()
toggle.state = tostring(params.state) ~= "false"
local onImg
if params.onWidth and params.onHeight then
onImg = display.newImageRect(params.on, params.onWidth, params.onHeight)
onImg:setReferencePoint(params.referencePoint or display.TopLeftReferencePoint)
elseif params.onText then
params.textColorOn = params.textColorOn or {255,255,255}
onImg = display.newText(params.onText, 0, 0, params.font, params.textSize)
onImg:setReferencePoint(params.referencePoint or display.TopLeftReferencePoint)
onImg:setTextColor(params.textColorOn[1],params.textColorOn[2],params.textColorOn[3])
else
onImg = display.newImage(params.on)
onImg:setReferencePoint(params.referencePoint or display.TopLeftReferencePoint)
end
onImg.x, onImg.y = 0, 0
local offImg
if params.offWidth and params.offHeight then
offImg = display.newImageRect(params.off, params.offWidth, params.offHeight)
offImg:setReferencePoint(params.referencePoint or display.TopLeftReferencePoint)
elseif params.offText then
params.textColorOff = params.textColorOff or params.textColorOn
offImg = display.newText(params.offText, 0, 0, params.font, params.textSize)
offImg:setReferencePoint(params.referencePoint or display.TopLeftReferencePoint)
offImg:setTextColor(params.textColorOff[1],params.textColorOff[2],params.textColorOff[3])
else
offImg = display.newImage(params.off)
offImg:setReferencePoint(params.referencePoint or display.TopLeftReferencePoint)
end
offImg.x, offImg.y = 0, 0
if not toggle.state then
onImg.isVisible = false
else
offImg.isVisible = false
end
toggle:insert(onImg)
toggle:insert(offImg)
toggle.touch = function(self, event)
if event.phase == "began" then
if toggle.state == true then
toggle.state = false
onImg.isVisible = false
offImg.isVisible = true
else
toggle.state = true
onImg.isVisible = true
offImg.isVisible = false
end
if params.callback then params.callback( toggle ) end
end
return false
end
if onImg.text then
local onImgRect = display.newRect(0, 0, onImg.contentWidth*1.25, onImg.contentHeight * 0.6)
onImgRect:setReferencePoint(params.referencePoint or display.TopLeftReferencePoint)
onImgRect.x, onImgRect.y = onImg.x, onImg.y
onImgRect:setFillColor(0,0,0,0)
local offImgRect = display.newRect(0, 0, offImg.contentWidth*1.25, offImg.contentHeight * 0.6)
offImgRect:setReferencePoint(params.referencePoint or display.TopLeftReferencePoint)
offImgRect.x, offImgRect.y = offImg.x, offImg.y
offImgRect:setFillColor(0,0,0,0)
local rectGroup = display.newGroup(onImgRect, offImgRect)
rectGroup:setReferencePoint(params.referencePoint or display.TopLeftReferencePoint)
rectGroup.y = rectGroup.y + rectGroup.contentHeight * 0.2
rectGroup:addEventListener("touch", toggle)
toggle:insert(rectGroup)
onImgRect, offImgRect, rectGroup = nil, nil, nil
else
toggle:addEventListener("touch", toggle)
end
toggle.setState = function( bool, fireCallback )
toggle.state = tostring(bool) ~= "false"
if toggle.state then
onImg.isVisible = true
offImg.isVisible = false
else
onImg.isVisible = false
offImg.isVisible = true
end
if fireCallback and params.callback then params.callback( toggle ) end
end
return toggle
end
return ToggleClass