-
Notifications
You must be signed in to change notification settings - Fork 3
/
sequence.lua
529 lines (417 loc) · 12.1 KB
/
sequence.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
--[[
class to create simple animations using easing as building blocks
To create a simple sequence:
animation = sequence.new():from(0):to(1,2.0,"outQuad"):mirror():start()
In your game loop
sequence.update()
currentValue = animation:get()
Add hooks or callback
animation = sequence.new():from(0):to(1,2.0,"outQuad"):callback(function() print("end animation") end):mirror()
]]--
import 'CoreLibs/easing'
sequence = {}
sequence.__index = sequence
-- private member
local _easings = playdate.easingFunctions
if not _easings then
print("sequence warning: easing function not found. Don't forget to call import 'CoreLibs/easing'")
return
end
local _runningSequences = table.create(32,0)
local _previousUpdateTime = playdate.getCurrentTimeMilliseconds()
-- create a new sequence
function sequence.new()
local new_sequence = {
-- runtime values
time = 0, -- in ms
cachedResultTimestamp = nil,
cachedResult = 0,
previousUpdateEasingIndex = nil,
isRunning = false,
duration = 0, -- in ms
loopType = false,
easings = table.create(4, 0),
easingCount = 0,
callbacks = nil,
}
return setmetatable(new_sequence, sequence)
end
-- put a low pacing to slow down all animations, great for tweaking
function sequence.update( pacing )
pacing = pacing or 1
local currentTime = playdate.getCurrentTimeMilliseconds()
local deltaTime = math.floor((currentTime-_previousUpdateTime) * pacing)
_previousUpdateTime = currentTime
for index = #_runningSequences, 1, -1 do
local seq = _runningSequences[index]
if seq.isRunning == true then
local previousTime = seq.time
seq.time = seq.time + deltaTime
seq.cachedResultTimestamp = nil
seq:triggerCallbacks( previousTime, seq.time )
if seq:isDone() then
seq.isRunning = false
end
end
if seq.isRunning == false then
table.remove(_runningSequences, index)
end
end
end
function sequence.print()
local seqs = sequence.getRunningSequencesDbg()
print("Sequences running:", #seqs)
for index, seq in pairs(seqs) do
print(" Sequence", index, seq)
end
end
function sequence:clear()
self:stop()
self.time = 0
self.duration = 0
self.loopType = false
self.easingCount = 0
self.cachedResultTimestamp = nil
self.cachedResult = 0
self.previousUpdateEasingIndex = nil
self.callbacks = {}
end
-- Reinitialize the sequence
function sequence:from( from )
from = from or 0
-- release all easings
self:clear()
-- setup first empty easing at the beginning of the sequence
local newEasing = self:newEasing()
newEasing.timestamp = 0 -- in ms
newEasing.from = from -- in ms
newEasing.to = from -- in ms
newEasing.duration = 0 -- in ms
newEasing.fn = _easings.flat
return self
end
function sequence:to( to, duration, easingFunction, ... )
if not self then return end
-- default parameters
to = to or 0
duration = toMilliseconds(duration) or 300
easingFunction = easingFunction or _easings.inOutQuad
if type(easingFunction)=="string" then
easingFunction = _easings[easingFunction] or _easings.inOutQuad
end
local lastEasing = self.easings[self.easingCount]
local newEasing = self:newEasing()
-- setup first empty easing at the beginning of the sequence
newEasing.timestamp = lastEasing.timestamp + lastEasing.duration
newEasing.from = lastEasing.to
newEasing.to = to
newEasing.duration = duration
newEasing.fn = easingFunction
newEasing.params = {...}
-- update overall sequence infos
self.duration = self.duration + duration
return self
end
function sequence:set( value )
if not self then return end
local lastEasing = self.easings[self.easingCount]
local newEasing = self:newEasing()
-- setup first empty easing at the beginning of the sequence
newEasing.timestamp = lastEasing.timestamp + lastEasing.duration
newEasing.from = value
newEasing.to = value
newEasing.duration = 0
newEasing.fn = _easings.flat
return self
end
-- @repeatCount: number of times the last easing as to be duplicated
-- @mirror: bool, does the repeating easings have to be mirrored (yoyo effect)
function sequence:again( repeatCount, mirror )
if not self then return end
repeatCount = repeatCount or 1
local previousEasing = self.easings[self.easingCount]
for i = 1, repeatCount do
local newEasing = self:newEasing()
-- setup first empty easing at the beginning of the sequence
newEasing.timestamp = previousEasing.timestamp + previousEasing.duration
newEasing.duration = previousEasing.duration
newEasing.fn = previousEasing.fn
newEasing.params = previousEasing.params
if mirror then
newEasing.from = previousEasing.to
newEasing.to = previousEasing.from
else
newEasing.from = previousEasing.from
newEasing.to = previousEasing.to
end
-- update overall sequence infos
self.duration = self.duration + newEasing.duration
previousEasing = newEasing
end
return self
end
function sequence:sleep( duration )
if not self then return end
duration = toMilliseconds(duration) or 500
if duration==0 then
return self
end
local lastEasing = self.easings[self.easingCount]
local new_easing = self:newEasing()
-- setup first empty easing at the beginning of the sequence
new_easing.timestamp = lastEasing.timestamp + lastEasing.duration
new_easing.from = lastEasing.to
new_easing.to = lastEasing.to
new_easing.duration = duration
new_easing.fn = _easings.flat
-- update overall sequence infos
self.duration = self.duration + duration
return self
end
function sequence:callback( fn, timeOffset )
if not self then return end
timeOffset = toMilliseconds(timeOffset) or 0
local lastEasing = self.easings[self.easingCount]
local cb = self:newCallback()
cb.timestamp = lastEasing.timestamp + lastEasing.duration + timeOffset
cb.fn = fn
return self
end
function sequence:loop()
self.loopType = "loop"
return self
end
function sequence:mirror()
self.loopType = "mirror"
return self
end
function sequence:newEasing()
self.easingCount = self.easingCount + 1
return self:getEasingByIndex(self.easingCount)
end
function sequence:newCallback()
local newCallback = {
fn = nil,
timestamp = nil,
}
table.insert( self.callbacks, newCallback)
return newCallback
end
function sequence:getEasingByIndex( index )
local easing = self.easings[index]
if type(easing)=="table" then
easing.params = nil
easing.callback = nil
return easing
end
local new_easing = {
timestamp = 0,
from = 0,
to = 0,
duration = 0,
params = nil,
fn = _easings.flat
}
self.easings[index] = new_easing
return new_easing
end
function sequence:getEasingByTime( clampedTime )
if self:isEmpty() then
print("sequence warning: empty animation")
return nil
end
local easingIndex = self.previousUpdateEasingIndex or 1
local foundEasing = false
while easingIndex>=1 and easingIndex<=self.easingCount do
local easing = self.easings[easingIndex]
if clampedTime < easing.timestamp then
easingIndex = easingIndex - 1
elseif clampedTime > (easing.timestamp+easing.duration) then
easingIndex = easingIndex + 1
elseif clampedTime == (easing.timestamp+easing.duration) then
-- if the time is in between two easings, we prioritize the highest index (if it exists)
if self.easings[easingIndex + 1] then
easingIndex = easingIndex + 1
else
foundEasing = true
end
else
foundEasing = true
end
if foundEasing then
self.previousUpdateEasingIndex = easingIndex
return easing, easingIndex
end
end
-- we didn't the correct part
print("sequence warning: couldn't find sequence part. clampedTime probably out of bound.", clampedTime, self.duration)
return self.easings[1]
end
function sequence:get( time )
if not self then return nil end
if self:isEmpty() then
return 0
end
time = toMilliseconds(time) or self.time
-- try to get cached result
if self.cachedResultTimestamp==time then
return self.cachedResult
end
-- we calculate and cache the result
local clampedTime = self:getClampedTime(time)
local easing = self:getEasingByTime(clampedTime)
local result
if easing.duration == 0 then
result = easing.to
else
result = easing.fn(clampedTime-easing.timestamp, easing.from, easing.to-easing.from, easing.duration, table.unpack(easing.params or {}))
end
-- cache
self.cachedResultTimestamp = clampedTime
self.cachedResult = result
return result
end
function sequence:triggerCallbacks( startTime, endTime )
if #self.callbacks==0 then
return
end
if endTime<=startTime then
return
end
local deltaTime = endTime - startTime
local triggerCallbacksClampedTimeRange = function( clampedStart, clampedEnd)
local isForward = true
if clampedStart>clampedEnd then
clampedStart, clampedEnd = clampedEnd, clampedStart
isForward = false
end
for index, cbObject in pairs(self.callbacks) do
local doTrigger = false
if cbObject.timestamp>clampedStart and cbObject.timestamp<clampedEnd then
doTrigger = true
elseif isForward and cbObject.timestamp==clampedEnd then
doTrigger = true
elseif isForward==false and cbObject.timestamp==clampedStart then
doTrigger = true
elseif clampedStart==0 and cbObject.timestamp==0 and isForward then
doTrigger = true
end
if doTrigger and type(cbObject.fn)=="function" then
cbObject.fn()
end
end
end
-- most straightforward case: no loop
if not self.loopType then
local startTimeClamped = self:getClampedTime( startTime )
triggerCallbacksClampedTimeRange(startTimeClamped, startTimeClamped+deltaTime)
return
end
--
-- now we handle loops
-- probably rare case but we have to handle it
if deltaTime>self.duration then
triggerCallbacksClampedTimeRange(0, self.duration)
end
local startTimeClamped, isForward = self:getClampedTime( startTime )
if isForward then
endTime = startTimeClamped + deltaTime
else
endTime = startTimeClamped - deltaTime
end
if endTime<0 then
triggerCallbacksClampedTimeRange(0, math.max(startTimeClamped, self:getClampedTime( endTime )))
elseif endTime>self.duration then
if self.loopType=="loop" then
triggerCallbacksClampedTimeRange(startTimeClamped, self.duration)
triggerCallbacksClampedTimeRange(0, self:getClampedTime( endTime ))
else
triggerCallbacksClampedTimeRange(math.min(startTimeClamped, self:getClampedTime( endTime )), self.duration)
end
else
triggerCallbacksClampedTimeRange(startTimeClamped, endTime)
end
end
-- get the time clamped in the sequence duration
-- manage time using loop setting
function sequence:getClampedTime( time )
time = time or self.time
local isForward = true
-- time is looped
if self.loopType=="loop" then
return math.floor(time%self.duration), isForward
-- time is mirrored / yoyo
elseif self.loopType=="mirror" then
time = time%(self.duration*2)
if time>self.duration then
isForward = false
time = self.duration + self.duration - time
end
return math.floor(time), isForward
end
-- time is normally clamped
return math.clamp(time, 0, self.duration), isForward
end
function sequence:addRunning()
if self:isEmpty() or self.isRunning then
return
end
table.insert(_runningSequences, self)
self.isRunning = true
end
function sequence:removeRunning()
-- _runningSequences table will be updated in the next sequence.update()
self.isRunning = false
end
function sequence:start()
self:addRunning()
return self
end
function sequence:stop()
self:removeRunning()
self.time = 0
self.cachedResultTimestamp = nil
self.previousUpdateEasingIndex = nil
return self
end
function sequence:pause()
self:removeRunning()
return self
end
function sequence:restart()
self.time = 0
self.cachedResultTimestamp = nil
self.previousUpdateEasingIndex = nil
self:start()
return self
end
function sequence:isDone()
return self.time>=self.duration and (not self.loopType)
end
function sequence:isEmpty()
return self.easingCount==0
end
function sequence.getRunningSequencesDbg()
local result = {}
for index, seq in pairs(_runningSequences) do
if seq.isRunning then
table.insert( result, seq)
end
end
return result
end
-- new easing function
function _easings.flat(t, b, c, d)
return b
end
math.clamp = math.clamp or function(a, min, max)
if min > max then
min, max = max, min
end
return math.max(min, math.min(max, a))
end
-- convert a floating point second to a rounded int millisecond
function toMilliseconds(seconds)
if seconds==nil then return nil end
return math.floor(1000*seconds)
end