-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.py
executable file
·75 lines (58 loc) · 2.11 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
#!/usr/bin/python2
import time
def millisecs():
return int(round(time.time() * 1000))
class Animation:
#TODO Remove 'actual' time references, and add arguement for it in the position methods
def __init__(self, positions, times, loop=False):
self.startTime = 0
self.positions = positions
self.times = times
self.loop = loop
self.timeLength = 0
for k,v in enumerate(self.times):
self.timeLength += v
self.reset()
def reset(self):
self.startTime = millisecs()
def getPosition(self):
timeOffset = self.startTime
curTime = millisecs()
if self.loop:
curTime %= self.timeLength
curTime += self.startTime
for k,v in enumerate(self.times):
timeOffset += v
if curTime < timeOffset:
return self.positions[k]
def getPositionInterp(self):
timeOffset = self.startTime
curTime = millisecs()
if self.loop:
curTime %= self.timeLength
curTime += self.startTime
for k,v in enumerate(self.times):
lastTime = timeOffset
timeOffset += v
if curTime < timeOffset:
#Handle first position with no interp
if k == 0:
return self.positions[0]
#Else, interpolate
interp = (curTime-lastTime)/float(timeOffset-lastTime)
return (self.positions[k]-self.positions[k-1])*interp + self.positions[k-1]
def done(self):
return (not self.loop) and (millisecs() >= (self.startTime+self.timeLength))
class Converter:
#BUG If either start and end are same
def __init__(self, fStart, fEnd, tStart, tEnd, limit=True):
self.fStart = fStart
self.fEnd = fEnd
self.tStart = tStart
self.tEnd = tEnd
self.limit = limit
def convert(self, value):
interp = (value-self.fStart)/float(self.fEnd-self.fStart)
if self.limit:
interp = min(1.0, max(0.0, interp))
return interp*(self.tEnd-self.tStart) + self.tStart