-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanimation.py
456 lines (362 loc) · 12.3 KB
/
animation.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This application is released under the GNU General Public License
# v3 (or, at your option, any later version). You can find the full
# text of the license under http://www.gnu.org/licenses/gpl.txt.
# By using, editing and/or distributing this software you agree to
# the terms and conditions of this license.
# Thank you for using free software!
class TimeInterpolator:
def fraction(self, fraction):
pass
class Linear:
def fraction(self, fraction):
return fraction
LINEAR = Linear()
class Animation(object):
timer = None
startupDelayTimer = None
stopped = False
startupDelay = 0
steps_count = None
duration = None
fraction = 0.0
targetObject = None
def __init__(self, duration, steps, loop = False):
self.duration = duration
self.steps_count = steps
self.loop = loop
self.endTasks = []
self.transitions = []
self.timeline = TimeLine()
def create(self, targetObject):
anim = Animation(self.duration, self.steps_count, self.loop)
anim.transitions = list(self.transitions)
anim.endTasks = self.endTasks
anim.targetObject = targetObject
return anim
def addLinearTransition(self, attrib, startVal, endVal, timeline):
if isinstance(startVal, list):
self.addTransition(attrib, LinearVectorInterpolator(startVal, endVal), timeline)
else:
self.addTransition(attrib, LinearScalarInterpolator(startVal, endVal), timeline)
def addTransition(self, callback, interpolator, timeline, *params):
assert isinstance(interpolator, Interpolator), "Interpolator object expected, got: %s" % interpolator
self.transitions.append([callback, interpolator, params])
def addTaskOnFinish(self, callback, *params):
self.endTasks.append([callback, params])
def start(self, after_callback, reverse = False):
#print "START ANIMATION"
self.after_callback = after_callback
self.reverse = reverse
self.stop()
if self.reverse:
self.time = self.steps_count
else:
self.time = 0
self.timeline.start()
if self.startupDelay > 0:
self.startupDelayTimer = gobject.timeout_add(self.startupDelay, self.__start)
else:
self.__start()
def __start(self):
self.timer = gobject.timeout_add(self.duration/self.steps_count, self.animation)
def pause(self):
print "############## Pause Animation"
self.timeline.pause()
self.stop()
def stop(self):
if self.timer != None:
gobject.source_remove(self.timer)
if self.startupDelayTimer != None:
gobject.source_remove(self.startupDelayTimer)
def resume(self):
print "############## Resume Animation"
if self.startupDelayTimer != None:
print "resume startup delay"
self.startupDelayTimer = gobject.timeout_add(int(self.startupDelay - self.timeline.getTime()*1000), self.__start)
else:
self.__start()
self.timeline.resume()
def onFinish(self):
for task in self.endTasks:
#task[0](*task[1])
self.call(task[0], *task[1])
def animation(self):
if self.reverse:
self.time -= 1
else:
self.time += 1
if self.time < 0 or self.time > self.steps_count:
if self.loop:
self.start(self.after_callback, self.reverse)
else:
self.onFinish()
return False
self.fraction = self.time/float(self.steps_count)
self.interpolateTransitions()
self.after_callback()
return True
def interpolateTransitions(self):
for transition in self.transitions:
callback = transition[0]
interpolator = transition[1]
params = transition[2]
args = list(params)
value = interpolator.value(self.fraction)
#args.extend(list(value))
#print value
args.append(value)
try:
#callback(*tuple(args))
self.call(callback, *tuple(args))
except Exception, e:
traceback.print_exc()
def call(self, attrib, *params):
if callable(attrib):
attrib(*params)
else:
attr = self.targetObject.__getattribute__(attrib)
if callable(attr):
attr(*params)
else:
self.targetObject.__setattr__(attrib, *params) #TODO params check
class Interpolator(object):
def __init__(self, startVal, endVal):
self.startVal = startVal
self.endVal = endVal
def value(self, fraction):
pass
"""
anim = Animation()
anim.addLinearTransition('attrname', 1.0, 0.0, TimeLine.LINEAR)
anim.addLinearTransition('attrname', [1.0, 1.0], [0.0, 0.0], TimeLine.LINEAR)
anim.addTransition(callback, LinearInterpolator([1.0, 1.0], [0.0, 0.0], extract_values = True), TimeLine.SIN) --> callback(x, y)
anim.addTransition(callback, [1.0, 1.0], [0.0, 0.0], LINEAR) --> callback([x, y])
"""
import gobject
import traceback
class TimeLine(object):
elapsed = 0
real = 0.0
stopped = True
def start(self):
self.elapsed = 0
self.real = gobject.get_current_time()
self.stopped = False
def setTime(self, time):
print "SET TIME %f" % time
self.elapsed = time
self.real = gobject.get_current_time()
def pause(self):
if not self.stopped:
self.elapsed += (gobject.get_current_time() - self.real)*1000
self.stopped = True
def resume(self):
if self.stopped:
self.real = gobject.get_current_time()
self.stopped = False
def getTime(self):
if self.stopped:
return self.elapsed
return self.elapsed+(gobject.get_current_time() - self.real)*1000
class CompositeAnimation(object):
timer = None
startupDelayTimer = None
time = 0
stopped = False
startupDelay = 0
_delay_start = 0
_delay_elapsed = 0
_step_start = 0
_step_elapsed = 0
steps_count = None
duration = None
fraction = 0.0
def __init__(self, steps, duration, loop = False):
self.steps_count = steps
self.duration = duration
self.loop = loop
self.endTasks = []
self.transitions = []
self.timeline = TimeLine()
#self.isRunning = False
def addTransition(self, callback, interpolator, *params):
assert isinstance(interpolator, Interpolator), "Interpolator object expected, got: %s" % interpolator
self.transitions.append([callback, interpolator, params])
def addTaskOnFinish(self, callback, *params):
self.endTasks.append([callback, params])
def pause(self):
print "############## Pause Animation"
self.timeline.pause()
self.stop()
if self.timer != None:
self._step_elapsed += (gobject.get_current_time() - self._step_start) * 1000
print "pause, elapsed %f" % self._step_elapsed
if self.startupDelayTimer != None:
self._delay_elapsed += (gobject.get_current_time() - self._delay_start) * 1000
def resume(self):
self.timeline.resume()
print "############## Resume Animation"
print self.timer
if self.timer != None:
self._step_start = gobject.get_current_time()
print "resume main animation"
#print "elapsed %f" % self._step_elapsed
#print "wait %d" % int(self.duration/self.steps_count - self._step_elapsed)
print self.fraction
self.timer = gobject.timeout_add(int(self.duration/self.steps_count), self.animation)
remaining_time = self.duration/self.steps_count - self._step_elapsed
#if remaining_time > 5:
#self.timer = gobject.timeout_add(int(remaining_time), self.animation)
#else:
# self.animation()
if self.startupDelayTimer != None:
print "resume startup delay"
self._delay_start = gobject.get_current_time()
self.startupDelayTimer = gobject.timeout_add(int(self.startupDelay - self._delay_elapsed), self.__start)
def stop(self):
if self.timer != None:
#print "STOP MAIN ANIMATION"
gobject.source_remove(self.timer)
if self.startupDelayTimer != None:
gobject.source_remove(self.startupDelayTimer)
def start(self, after_callback, reverse = False):
#print "START ANIMATION"
self.after_callback = after_callback
self.stop()
self.reverse = reverse
if self.reverse:
self.time = self.steps_count
else:
self.time = 0
if self.startupDelay > 0:
#gobject.timeout_add(self.startupDelay, lambda: self.minimize("paused") if not self.playing and (gobject.get_current_time() - self.lastStop) > 4.8 else 1)
#gobject.timeout_add(self.startupDelay, lambda: self.timer = gobject.timeout_add(self.duration/self.steps_count, self.animation, reverse))
self.startupDelayTimer = gobject.timeout_add(self.startupDelay, self.__start)
self._delay_elapsed = 0
self._delay_start = gobject.get_current_time()
else:
self.__start()
def __start(self):
self._step_start = 0
self.timeline.start()
self.timer = gobject.timeout_add(self.duration/self.steps_count, self.animation)
def onFinish(self):
for task in self.endTasks:
task[0](*task[1])
def animation(self):
self._step_elapsed = 0
self._step_start = gobject.get_current_time()
if self.reverse:
self.time -= 1
else:
self.time += 1
if self.time < 0 or self.time > self.steps_count:
if self.loop:
self.start(self.after_callback, self.reverse)
else:
self.onFinish()
return False
self.fraction = self.time/float(self.steps_count)
self.interpolateTransitions()
self.after_callback()
return True
def interpolateTransitions(self):
for transition in self.transitions:
callback = transition[0]
interpolator = transition[1]
params = transition[2]
args = list(params)
value = interpolator.value(self.fraction)
#args.extend(list(value))
#print value
args.append(value)
try:
callback(*tuple(args))
except Exception, e:
traceback.print_exc()
print args
class TemplateAnimation(object):
transitions = None
def __init__(self, steps, duration, loop = False):
self.steps = steps
self.duration = duration
self.loop = loop
self.transitions = []
def createAnimation(self, *objects):
anim = CompositeAnimation(self.steps, self.duration, self.loop)
for obj in objects:
for transition in self.transitions:
if callable(obj.__getattribute__(transition[0])):
anim.addTransition(obj.__getattribute__(transition[0]), transition[1], transition[2])
else:
params = list(transition[2])
params.insert(transition[0], 0)
anim.addTransition(obj.__setattr__, transition[1], transition[2])
print obj
return anim
def addTransition(self, attrName, interpolator, *params):
self.transitions.append([attrName, interpolator, params])
"""
class GenericAnimation(CompositeAnimation)
objects = None
def start(self, objectsList, after_callback, reverse = False):
self.objects = objectsList
CompositeAnimation.start(self, after_callback, reverse = False)
def addTransition(self, attrName, interpolator, *params):
self.transitions.append([attrName, interpolator, params])
def interpolateTransitions(self, fraction):
for transition in self.transitions:
attrName = transition[0]
interpolator = transition[1]
params = transition[2]
if
args = list(params)
args.extend(list(interpolator.value(fraction)))
callback(*tuple(args))
"""
import math
class Interpolator:
def value(self, fraction):
pass
class LinearVectorInterpolator(Interpolator):
def __init__(self, startValue, endValue):
self.startValue = startValue#[].extend(startValue)
self.endValue = endValue
def value(self, fraction):
result = []
i = 0
for val in self.startValue:
result.append(val+(self.endValue[i]-val)*fraction)
i+=1
return result
class LinearScalarInterpolator(Interpolator):
def __init__(self, startValue, endValue):
self.startValue = startValue
self.endValue = endValue
def value(self, fraction):
return self.startValue+(self.endValue-self.startValue)*fraction
class BezierInterpolator(Interpolator):
def __init__(self, p1, p2, c1, c2, asList = True):
self.p1 = []
self.p1.extend(p1)
#list(p1)
self.p2 = list(p2)
self.c1 = c1
self.c2 = c2
self.resultAsList = asList
def linearIteraion(self, dist):
pass
def value(self, t):
B0 = math.pow(1.0 - t, 3)
B1 = 3.0 * t * math.pow(1.0 - t, 2)
B2 = 3.0 * t * t * (1.0 - t)
B3 = math.pow(t, 3)
x = self.p1[0] * B0 + self.c1[0] * B1 + self.c2[0] * B2 + self.p2[0] * B3
y = self.p1[1] * B0 + self.c1[1] * B1 + self.c2[1] * B2 + self.p2[1] * B3
if self.resultAsList:
return [x, y]
else:
return x, y
#return [int(x), int(y)]