X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=music_sampler%2Factions%2Fwait.py;h=31439b80bdb2011cf3a51e730d8d62ecb6290e40;hb=d4217fda2ff3991eb1ee9a9bec6acff751798507;hp=bcee64941cc93c160b0673612146c2740abecc9a;hpb=51f6ce0fdb32061b681d63e8de4d96eb8b59e1e9;p=perso%2FImmae%2FProjets%2FPython%2FMusicSampler.git diff --git a/music_sampler/actions/wait.py b/music_sampler/actions/wait.py index bcee649..31439b8 100644 --- a/music_sampler/actions/wait.py +++ b/music_sampler/actions/wait.py @@ -1,4 +1,5 @@ import threading +import time def run(action, duration=0, music=None, set_wait_id=None, **kwargs): action.mapping.add_wait(action, wait_id=set_wait_id) @@ -8,10 +9,17 @@ def run(action, duration=0, music=None, set_wait_id=None, **kwargs): duration, action.sleep_event.set) + action.sleep_event_initial_duration = duration + action.sleep_event_paused = False + action.sleep_event_left_time = duration + if music is not None: music.wait_end() - action.sleep_event_timer.start() + if duration <= 0 or not action.sleep_event_paused: + action.sleep_event_timer.start() + action.sleep_event_started_time = time.time() + action.sleep_event.wait() def description(action, duration=0, music=None, set_wait_id=None, **kwargs): @@ -34,6 +42,50 @@ def description(action, duration=0, music=None, set_wait_id=None, **kwargs): return _(message).format(*formats) +def pause(action, **kwargs): + if action.sleep_event_paused: + return + + action.sleep_event_paused = True + + if not action.sleep_event_timer.is_alive(): + return + + action.sleep_event_timer.cancel() + + action.sleep_event_left_time = action.sleep_event_left_time\ + - (time.time() - action.sleep_event_started_time) + if action.sleep_event_left_time < 0: + action.sleep_event.set() + +def unpause(action, **kwargs): + if not action.sleep_event_paused: + return + + action.sleep_event_paused = False + + action.sleep_event_timer = threading.Timer( + action.sleep_event_left_time, + action.sleep_event.set) + + action.sleep_event_timer.start() + action.sleep_event_started_time = time.time() + +def reset(action, **kwargs): + action.sleep_event_timer.cancel() + + action.sleep_event_left_time = action.sleep_event_initial_duration + + if action.sleep_event_paused: + return + + action.sleep_event_timer = threading.Timer( + action.sleep_event_left_time, + action.sleep_event.set) + + action.sleep_event_timer.start() + action.sleep_event_started_time = time.time() + def interrupt(action, duration=0, music=None, **kwargs): if action.sleep_event is not None: action.sleep_event.set()