-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrollViewUtil.lua
321 lines (274 loc) · 9.49 KB
/
scrollViewUtil.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
-- util.lua
--
-- Version 1.0
--
-- Copyright (C) 2010 ANSCA Inc. All Rights Reserved.
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in the
-- Software without restriction, including without limitation the rights to use, copy,
-- modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
-- and to permit persons to whom the Software is furnished to do so, subject to the
-- following conditions:
--
-- The above copyright notice and this permission notice shall be included in all copies
-- or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
-- INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
-- PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
-- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.
module(..., package.seeall)
-- set some global values for width and height of the screen
local screenW, screenH = display.contentWidth, display.contentHeight
local viewableScreenW, viewableScreenH = display.viewableContentWidth, display.viewableContentHeight
local screenOffsetW, screenOffsetH = display.contentWidth - display.viewableContentWidth, display.contentHeight - display.viewableContentHeight
--------------------------------------------
-- Wrap text
function wrap(str, limit, indent, indent1)
indent = indent or ""
indent1 = indent1 or indent
limit = limit or 72
local here = 1-#indent1
return indent1..str:gsub("(%s+)()(%S+)()",
function(sp, st, word, fi)
if fi-here > limit then
here = st - #indent
return "\n"..indent..word
end
end)
end
function explode(div,str)
if (div=='') then return false end
local pos,arr = 0,{}
-- for each divider found
for st,sp in function() return string.find(str,div,pos,true) end do
table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
pos = sp + 1 -- Jump past current divider
end
table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
return arr
end
function wrappedText(str, limit, size, font, color, indent, indent1)
str = explode("\n", str)
size = tonumber(size) or 12
color = color or {255, 255, 255}
font = font or "Helvetica"
--apply line breaks using the wrapping function
local i = 1
local strFinal = ""
while i <= #str do
strW = wrap(str[i], limit, indent, indent1)
strFinal = strFinal.."\n"..strW
i = i + 1
end
str = strFinal
--search for each line that ends with a line break and add to an array
local pos, arr = 0, {}
for st,sp in function() return string.find(str,"\n",pos,true) end do
table.insert(arr,string.sub(str,pos,st-1))
pos = sp + 1
end
table.insert(arr,string.sub(str,pos))
--iterate through the array and add each item as a display object to the group
local g = display.newGroup()
local i = 1
while i <= #arr do
local t = display.newText( arr[i], 0, 0, font, size )
t:setTextColor( color[1], color[2], color[3] )
t.x = math.floor(t.width/2)
t.y = math.floor((size*1.3)*(i-1))
g:insert(t)
i = i + 1
end
return g
end
--[[
-- USAGE:
local myText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc euismod justo sapien, at sollicitudin lacus. Quisque vestibulum commodo felis id posuere."
local myTextObject = wrappedText(myText, 46)
--local myTextObject = wrappedText(myText, 46, 16)
--local myTextObject = wrappedText(myText, 46, 16, {255, 0, 0})
local myGroup = display.newGroup()
myGroup:insert( myTextObject )
]]--
--------------------------------------------
-- XML Parser
function parseargs(s)
local arg = {}
string.gsub(s, "(%w+)=([\"'])(.-)%2", function (w, _, a)
arg[w] = a
end)
return arg
end
function collect(s)
local stack = {}
local top = {}
table.insert(stack, top)
local ni,c,label,xarg, empty
local i, j = 1, 1
while true do
ni,j,c,label,xarg, empty = string.find(s, "<(%/?)([%w:]+)(.-)(%/?)>", i)
if not ni then break end
local text = string.sub(s, i, ni-1)
if not string.find(text, "^%s*$") then
table.insert(top, text)
end
if empty == "/" then -- empty element tag
table.insert(top, {label=label, xarg=parseargs(xarg), empty=1})
elseif c == "" then -- start tag
top = {label=label, xarg=parseargs(xarg)}
table.insert(stack, top) -- new level
else -- end tag
local toclose = table.remove(stack) -- remove top
top = stack[#stack]
if #stack < 1 then
error("nothing to close with "..label)
end
if toclose.label ~= label then
error("trying to close "..toclose.label.." with "..label)
end
table.insert(top, toclose)
end
i = j+1
end
local text = string.sub(s, i)
if not string.find(text, "^%s*$") then
table.insert(stack[#stack], text)
end
if #stack > 1 then
error("unclosed "..stack[stack.n].label)
end
return stack[1]
end
------------------------------
function aLink(e)
local t = e.target
if (t.link) then
print(e.target.link)
system.openURL( e.target.link )
else
print("no link found")
end
end
--------------------------------------------
-- Helper function for newLink utility function below
local function newLinkHandler( self, event )
local result = true
local default = self[1]
local over = self[2]
local t = self.parent
local phase = event.phase
print ("newButtonHandler: "..phase)
if over then
linkCurrentDefault = default
linkCurrentRollover = over
end
local function showHighlight (event)
local timePassed = event.time - startTime
print("timePassed: "..timePassed)
if timePassed > 100 then
print("highlight")
linkCurrentDefault.isVisible = false
linkCurrentRollover.isVisible = true
Runtime:removeEventListener( "enterFrame", showHighlight )
end
end
if "began" == phase then
t.startPos = event.y
t.prevPos = event.y
t.delta, t.velocity = 0, 0
if t.tween then transition.cancel(t.tween) end
Runtime:removeEventListener("enterFrame", t )
self.prevTime = 0
self.prevY = 0
if over then
startTime = event.time
Runtime:addEventListener( "enterFrame", showHighlight )
end
local onPress = self._onPress
if onPress then
result = onPress( event )
end
-- Subsequent touch events will target button even if they are outside the stageBounds of button
display.getCurrentStage():setFocus( self )
self.isFocus = true
elseif self.isFocus then
if "moved" == phase then
local bottomLimit = screenH - t.height - t.bottom
t.delta = event.y - t.prevPos
t.prevPos = event.y
if ( t.y > t.top or t.y < bottomLimit ) then
t.y = t.y + t.delta/2
else
t.y = t.y + t.delta
end
--Track velocity while the user is moving the view
local timePassed = event.time - t.prevTime
t.prevTime = t.prevTime + timePassed
if t.prevY then
t.velocity = (t.y - t.prevY)/timePassed
end
t.prevY = t.y
if over then
Runtime:removeEventListener( "enterFrame", showHighlight )
-- the over image should only be visible while the finger is within button's stageBounds
default.isVisible = true --not isWithinBounds
over.isVisible = false --isWithinBounds
end
elseif "ended" == phase or "cancelled" == phase then
local dragDistance = event.y - t.startPos
t.lastTime = event.time
Runtime:addEventListener("enterFrame", t )
local bounds = self.stageBounds
local x,y = event.x,event.y
local isWithinBounds =
bounds.xMin <= x and bounds.xMax >= x and bounds.yMin <= y and bounds.yMax >= y
if over then
Runtime:removeEventListener( "enterFrame", showHighlight )
default.isVisible = true
over.isVisible = false
end
-- Only consider this a "click", if the user lifts their finger inside button's stageBounds
if isWithinBounds and (dragDistance < 10 and dragDistance > -10 ) then
result = self._onRelease( event )
end
-- Allow touch events to be sent normally to the objects they "hit"
display.getCurrentStage():setFocus( nil )
self.isFocus = false
end
end
return result
end
function newLink( params )
local button
if params.default and params.onRelease then
button = display.newGroup()
local default = display.newImage( params.default )
button:insert( default, true )
if params.over then
local over = display.newImage( params.over )
over.isVisible = false
button:insert( over, true )
end
if type( params.onPress ) == "function" then
button._onPress = params.onPress
end
if type( params.onRelease ) == "function" then
button._onRelease = params.onRelease
end
button.buttonID = params.buttonID
-- Set button as a table listener by setting a table method and adding the button as its own table listener for "touch" events
button.touch = newLinkHandler
button:addEventListener( "touch", button )
end
if params.x then
button.x = params.x
end
if params.y then
button.y = params.y
end
return button
end