-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ace.Slide.lua
283 lines (259 loc) · 6.74 KB
/
Ace.Slide.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
--[[
*************************************************************
* This script is developed by Arturs Sosins aka ar2rsawseen, http://appcodingeasy.com
* Feel free to distribute and modify code, but keep reference to its creator
*
* Gideros AceSlide object creates a sliding element to switch different objects.
* Great for providing and input to choose levels or packages in mobile games
* You can navigate through objects by using provided function or
* allowing users to drag and drop to switch content.
*
* For more information, examples and online documentation visit:
* http://appcodingeasy.com/Gideros-Mobile/Easy-input-for-choosing-packages-or-levels-in-Gideros-Mobile
**************************************************************
]]--
module("AceSlide", package.seeall)
--configuration options
local conf = {
orientation = "horizontal",
spacing = 100,
parent = stage,
speed = 3,
unfocusedAlpha = 0.75,
easing = nil,
allowDrag = true,
dragOffset = 2
}
local store
local total = 1
local cur = 1
local scrW
local scrH
local coord
local offset = 0
local function findClosest(current)
local closest = cur
local distance = 100000000
for i = 1, #coord do
if i ~= cur then
local newdist = math.abs(coord[i]-current)
if distance > newdist then
distance = newdist
closest = i
end
end
end
return closest
end
local function falseButtons(elem)
local num = elem:getNumChildren()
if num > 0 then
for i = 1, num do
local sprite = elem:getChildAt(i)
if sprite.upState and sprite.downState then
sprite.focus = false
else
falseButtons(sprite)
end
end
end
end
local function onMouseDown(self, event)
if store:hitTestPoint(event.x, event.y) then
store.isFocus = true
store.x0 = event.x
store.y0 = event.y
store.startX = event.x
store.startY = event.y
--event:stopPropagation()
end
end
local function onMouseMove(self, event)
if store.isFocus then
if(conf.orientation == "horizontal") then
local dx = event.x - store.x0
store:setX(store:getX() + dx)
store.x0 = event.x
else
local dy = event.y - store.y0
store:setY(store:getY() + dy)
store.y0 = event.y
end
event:stopPropagation()
end
end
local function onMouseUp(self, event)
if store.isFocus then
if(conf.orientation == "horizontal") then
if(store.x0 - store.startX < -conf.dragOffset or store.x0 - store.startX > conf.dragOffset) then
if store.x0 - store.startX < -conf.dragOffset and cur == total then
gotoItem(cur)
elseif store.x0 - store.startX > conf.dragOffset and cur == 1 then
gotoItem(cur)
else
gotoItem(findClosest(store:getX()))
end
falseButtons(store)
event:stopPropagation()
else
gotoItem(cur)
end
else
if(store.y0 - store.startY < -conf.dragOffset or store.y0 - store.startY > conf.dragOffset) then
if store.y0 - store.startY < -conf.dragOffset and cur == total then
gotoItem(cur)
elseif store.y0 - store.startY > conf.dragOffset and cur == 1 then
gotoItem(cur)
else
gotoItem(findClosest(store:getY()))
end
falseButtons(store)
event:stopPropagation()
else
gotoItem(cur)
end
end
store.isFocus = false
end
end
function init(config)
total = 1
cur = 1
offset = 0
if config then
--copying configuration
for key,value in pairs(config) do
conf[key]= value
end
end
coord = {}
scrH = application:getContentHeight()
scrW = application:getContentWidth()
store = Sprite.new()
end
function add(elem, selected)
if conf.allowDrag then
elem:addEventListener(Event.MOUSE_DOWN, onMouseDown, elem)
elem:addEventListener(Event.MOUSE_MOVE, onMouseMove, elem)
elem:addEventListener(Event.MOUSE_UP, onMouseUp, elem)
end
store:addChild(elem)
total = store:getNumChildren()
if selected then
cur = total
end
end
function addButton(image, image_pushed, callback, selected)
local button_default = Bitmap.new(Texture.new(image))
local button_pushed = Bitmap.new(Texture.new(image_pushed))
local button = Button.new(button_default, button_pushed)
button:addEventListener("click", callback)
if conf.allowDrag then
button:addEventListener(Event.MOUSE_DOWN, onMouseDown, button)
button:addEventListener(Event.MOUSE_MOVE, onMouseMove, button)
button:addEventListener(Event.MOUSE_UP, onMouseUp, button)
end
store:addChild(button)
total = store:getNumChildren()
if selected then
cur = total
end
end
function applyToAll(callback)
for i = 1, total do
callback(store:getChildAt(i))
end
end
function show()
local last = 0
for i = 1, store:getNumChildren() do
if i == 1 then
local sprite = store:getChildAt(i)
sprite:setAlpha(conf.unfocusedAlpha)
coord[i] = last
if(conf.orientation == "horizontal") then
last = (scrW/2)-(sprite:getWidth()/2)
offset = last
sprite:setPosition(last, (scrH/2)-(sprite:getHeight()/2))
last = last + sprite:getWidth() + conf.spacing
else
last = (scrH/2)-(sprite:getHeight()/2)
offset = last
sprite:setPosition((scrW/2)-(sprite:getWidth()/2), last)
last = last + sprite:getHeight() + conf.spacing
end
else
local sprite = store:getChildAt(i)
sprite:setAlpha(conf.unfocusedAlpha)
coord[i] = -(last-offset)
if(conf.orientation == "horizontal") then
sprite:setPosition(last, (scrH/2)-(sprite:getHeight()/2))
last = last + sprite:getWidth() + conf.spacing
else
sprite:setPosition((scrW/2)-(sprite:getWidth()/2), last)
last = last + sprite:getHeight() + conf.spacing
end
end
end
--dirty little fix
local fix = Sprite.new()
store:addChild(fix)
if conf.allowDrag then
fix:addEventListener(Event.MOUSE_DOWN, onMouseDown, fix)
fix:addEventListener(Event.MOUSE_MOVE, onMouseMove, fix)
fix:addEventListener(Event.MOUSE_UP, onMouseUp, fix)
end
store:getChildAt(cur):setAlpha(1)
jumptoItem(cur)
conf.parent:addChild(store)
end
function gotoItem(ind)
local target = coord[ind]
local curpos
if(conf.orientation == "horizontal") then
curpos = store:getX()
else
curpos = store:getY()
end
local switchTime = (conf.speed*math.abs(curpos-target))+1
local animate = {}
if(conf.orientation == "horizontal") then
animate.x = target
else
animate.y = target
end
store:getChildAt(cur):setAlpha(conf.unfocusedAlpha)
cur = ind
local tween = GTween.new(store, switchTime/1000, animate, {delay = 0, ease = conf.easing})
tween.dispatchEvents = true
tween:addEventListener("complete", function()
store:getChildAt(cur):setAlpha(1)
end)
end
function jumptoItem(ind)
if(conf.orientation == "horizontal") then
store:setPosition(coord[ind],0)
else
store:setPosition(0,coord[ind])
end
end
function nextItem()
if cur ~= total then
gotoItem(cur + 1)
else
gotoItem(total)
end
end
function prevItem()
if cur ~= 1 then
gotoItem(cur - 1)
else
gotoItem(1)
end
end
function firstItem()
gotoItem(1)
end
function lastItem()
gotoItem(total)
end