-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.py
45 lines (34 loc) · 1.19 KB
/
Timer.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
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
class Timer:
def __init__(self, ms_duration, is_repeat, n_times=None):
self.is_started = False
self.is_repeat = is_repeat
self.n_times = n_times
self.ms_duration = ms_duration
self.on_timer = None
self.after_timer = None
def timer(self, _=None):
self.on_timer()
if self.n_times:
self.n_times -= 1
if (self.is_repeat and self.is_started) or \
(self.n_times and self.is_started):
glutTimerFunc(self.ms_duration, self.timer, 0)
else:
self.is_started = False
if self.after_timer:
self.after_timer()
def start(self, on_timer, after=None):
self.on_timer = on_timer
self.after_timer = after
if not self.is_started:
glutTimerFunc(self.ms_duration, self.timer, None)
self.is_started = True
def change_duration(self, ms_duration):
self.stop()
self.ms_duration = ms_duration
self.start(self.on_timer)
def stop(self):
self.is_started = False