forked from JackD83/PyMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskHandler.py
84 lines (62 loc) · 2.52 KB
/
TaskHandler.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
import uuid, Common, math
animations = {}
periodic = {}
counter = 0
def addPeriodicTask(time, callback, delay=0):
id = uuid.uuid4()
#min time -> every frame
if(time < 1000 / float(Common.getFPS())):
time = 1000 / float(Common.getFPS())
task = {}
task["delay"] = counter + (float(Common.getFPS()) / 1000 * delay)
task["start"] = counter
task["val"] = float(Common.getFPS()) / 1000.0 * float(time)
task["callback"] = callback
periodic[id] = task
return id
def removePeriodicTask(id):
if(id in periodic):
del periodic[id]
def stopAnimation(id):
if(id in animations):
del animations[id]
def addAnimation(start, target, duration, callback, delay = 0):
if(duration == 0):
return
id = uuid.uuid4()
anim = {}
anim["delay"] = counter + (float(Common.getFPS()) / 1000 * delay)
anim["start"] = start
anim["target"] = target
anim["speed"] = (target- start) / ((float(Common.getFPS()) / 1000) * duration)
anim["current"] = start
anim["callback"] = callback
animations[id] = anim
return id
def updateTasks():
global counter
counter = counter + 1
for anim in animations.copy():
try:
if(counter >= animations[anim]["delay"]):
animations[anim]["current"] = animations[anim]["current"] + animations[anim]["speed"]
finished = False
if(animations[anim]["target"] > animations[anim]["start"]):
finished = animations[anim]["current"] >= animations[anim]["target"]
else:
finished = animations[anim]["current"] <= animations[anim]["target"]
if(finished):
animations[anim]["callback"](animations[anim]["start"], animations[anim]["target"], animations[anim]["current"], True)
del animations[anim]
else:
animations[anim]["callback"](animations[anim]["start"], animations[anim]["target"], animations[anim]["current"], False)
except Exception as identifier:
pass
for task in periodic.copy():
try:
if(counter >= periodic[task]["delay"]):
time = counter - periodic[task]["start"]
if( time != 0 and periodic[task]["val"] != 0 and time % periodic[task]["val"] == 0):
periodic[task]["callback"]()
except Exception as identifier:
pass