-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathslider.lua
266 lines (225 loc) · 6.87 KB
/
slider.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
--
-- Abstract: slider component
--
-- Version: 0.1
--
-- Sample code is MIT licensed, see http://developer.anscamobile.com/code/license
-- Copyright (C) 2010 ANSCA Inc. All Rights Reserved.
module(..., package.seeall)
local function newSliderHandler( self, event )
local result = true
-- General "onEvent" function overrides onPress and onRelease, if present
local onEvent = self._onEvent
local onPress = self._onPress
local onRelease = self._onRelease
local sliderEvent = { value = self.value }
if (self._id) then
sliderEvent.id = self._id
else
sliderEvent.id = 0
end
local phase = event.phase
if "began" == phase then
if self.thumbOver then
self.thumbDefault.isVisible = false
self.thumbOver.isVisible = true
end
if onEvent then
sliderEvent.phase = "press"
result = onEvent( sliderEvent )
elseif onPress then
result = onPress( event )
end
-- Subsequent touch events will target slider even if they are outside the stageBounds of slider
display.getCurrentStage():setFocus( self, event.id )
self.isFocus = true
elseif self.isFocus then
local bounds = self.stageBounds
local oldValue = self.value
-- find new position of thumb
if self.isVertical then
local y = event.y - self.y
if y < self.thumbMin then
y = self.thumbMin
end
if y > self.thumbMax then
y = self.thumbMax
end
self.thumbDefault.y = y
self.thumbOver.y = y
self.value = (((y - self.thumbMin) / (self.thumbMax - self.thumbMin)) * self.range) + self.minValue
else
local x = event.x - self.x
if x < self.thumbMin then
x = self.thumbMin
end
if x > self.thumbMax then
x = self.thumbMax
end
self.thumbDefault.x = x
self.thumbOver.x = x
self.value = (((x - self.thumbMin) / (self.thumbMax - self.thumbMin)) * self.range) + self.minValue
end
if (self.isInteger) then
sliderEvent.value = math.floor(self.value)
elseif (self.isBoolean) then
if (self.value == 0) then
sliderEvent.value = false
else
sliderEvent.value = true
end
else
sliderEvent.value = tonumber(string.format("%.3f", self.value))
end
if "moved" == phase then
if self.value ~= oldValue then
if onEvent then
sliderEvent.phase = "moved"
result = onEvent( sliderEvent )
end
end
elseif "ended" == phase or "cancelled" == phase then
if self.thumbOver then
self.thumbDefault.isVisible = true
self.thumbOver.isVisible = false
end
if "ended" == phase then
if onEvent then
sliderEvent.phase = "release"
result = onEvent( sliderEvent )
elseif onRelease then
result = onRelease( event )
end
end
-- Allow touch events to be sent normally to the objects they "hit"
display.getCurrentStage():setFocus( self, nil )
self.isFocus = false
end
end
return result
end
-- newSlider( params )
-- where params is a table containing:
-- track - name of track image
-- thumbDefault - name of default thumb image
-- thumbOver - name of thumb over image (optional)
-- minValue - min value (optional, defaults to 0)
-- maxValue - max value (optional, defaults to 100)
-- value - initial value (optional, defaults to minValue)
-- isInteger - true if integer, false if real (continuous value) (defaults to false)
-- isVertical - true if vertical; otherwise is horizontal (defaults to horizontal)
-- onPress - function to call when slider is pressed
-- onRelease - function to call when slider is released
-- onEvent - function to call when an event occurs
--
function newSlider( params )
local slider
slider = display.newGroup()
function slider:getValue()
if (self.isBoolean) then
if not self.value or self.value == 0 then
return false
else
return true
end
elseif (self.isInteger) then
return math.floor(self.value)
else
return tonumber(string.format("%.3f", self.value))
end
end
if params.track then
slider.track = display.newImage( params.track )
slider:insert( slider.track, true )
end
if params.thumbDefault then
slider.thumbDefault = display.newImage( params.thumbDefault )
slider:insert( slider.thumbDefault, true )
end
if params.thumbOver then
slider.thumbOver = display.newImage( params.thumbOver )
slider.thumbOver.isVisible = false
slider:insert( slider.thumbOver, true )
end
if ( params.maxValue ~= nil ) then
slider.maxValue = params.maxValue
else
slider.maxValue = 100
end
if ( params.minValue ~= nil ) then
slider.minValue = params.minValue
else
slider.minValue = 0
end
slider.range = slider.maxValue - slider.minValue
if ( params.value ~= nil ) then
slider.value = params.value
else
slider.value = slider.minValue
end
if ( params.isInteger == true ) then
slider.isInteger = true
else
slider.isInteger = false
end
if ( params.isVertical == true ) then
slider.isVertical = true
else
slider.isVertical = false
end
if ( params.onPress and ( type(params.onPress) == "function" ) ) then
slider._onPress = params.onPress
end
if ( params.onRelease and ( type(params.onRelease) == "function" ) ) then
slider._onRelease = params.onRelease
end
if (params.onEvent and ( type(params.onEvent) == "function" ) ) then
slider._onEvent = params.onEvent
end
if params.isBoolean == true then
slider.isBoolean = true
else
slider.isBoolean = false
end
-- calculate thumb extents
local trackBounds = slider.track.stageBounds
local thumbBounds = slider.thumbDefault.stageBounds
local tempVal
if (type(slider.value) == "boolean") then
if (slider.value) then
tempVal = 1
else
tempVal = 0
end
else
tempVal = slider.value
end
if slider.isVertical then
slider.thumbMin = trackBounds.yMin - thumbBounds.yMin
slider.thumbMax = trackBounds.yMax - thumbBounds.yMax
if (slider.value ~= nil) then
slider.thumbDefault.y = (slider.thumbMax - slider.thumbMin) * ((tempVal - slider.minValue) / slider.range) + slider.thumbMin
slider.thumbOver.y = slider.thumbDefault.y
end
else
slider.thumbMin = trackBounds.xMin - thumbBounds.xMin
slider.thumbMax = trackBounds.xMax - thumbBounds.xMax
if (slider.value ~= nil) then
slider.thumbDefault.x = (slider.thumbMax - slider.thumbMin) * ((tempVal - slider.minValue) / slider.range) + slider.thumbMin
slider.thumbOver.x = slider.thumbDefault.x
end
end
-- Set slider as a table listener by setting a table method and adding the slider as its own table listener for "touch" events
slider.touch = newSliderHandler
slider:addEventListener( "touch", slider )
if params.x then
slider.x = params.x
end
if params.y then
slider.y = params.y
end
if params.id then
slider._id = params.id
end
return slider
end