Skip to content

Commit 2fab34a

Browse files
committed
Separe PlayCtrl in gui and functionality classes
Previously playCtrl had part of gui handling, and functionality due to untoggle the play button when playback stops.
1 parent 7637cd3 commit 2fab34a

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

atbswp/control.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,116 @@ def update_timer(self, event):
353353
self.timer = self.countdown_dialog.update_ui()
354354

355355

356+
class PlayControl:
357+
"""Control class for playback functionality"""
358+
359+
def __init__(self, count=1, infinite=False):
360+
self.count = count
361+
self.infinite = infinite
362+
self._stop_locks = [True, True] # [once, many]
363+
self._current_count = 0
364+
self._play_once_thread = None
365+
self._play_many_thread = None
366+
367+
def _play_once(self, capture, append_function=None):
368+
for line in capture:
369+
if self._stop_locks[0]: break
370+
exec(line)
371+
self._stop_locks[0] = True
372+
if append_function:
373+
append_function()
374+
375+
def _play_many(self, capture, append_function=None):
376+
self.current_count = 0
377+
while (self.current_count < self.count or self.infinite)\
378+
and not self._stop_locks[1]:
379+
self._stop_locks[0] = False
380+
self._play_once_thread = self._start_thread(self._play_once, capture)
381+
self._play_once_thread.join()
382+
self.current_count += 1
383+
self._stop_locks = [True, True]
384+
if append_function:
385+
append_function()
386+
387+
def _start_thread(self, target, *args, daemon=False):
388+
thread = Thread(target=target, args=(*args,))
389+
thread.daemon = daemon
390+
thread.start()
391+
return thread
392+
393+
def set_config(self, count, infinite):
394+
"""Set the configuration to the next playback"""
395+
self.count = count
396+
self.infinite = infinite
397+
398+
def get_current_count(self):
399+
"""Returns the current playback time"""
400+
return current_count
401+
402+
def is_stoped(self):
403+
return self._stop_locks[0] and self._stop_locks[1]
404+
405+
def play(self, capture, append_function=None):
406+
"""Starts playback according with configs setted
407+
408+
:param capture: list of commands to be executed
409+
:param append_function: a function to be executed after
410+
a playback
411+
"""
412+
if self.infinite or self.count > 1:
413+
self._stop_locks = [False, False]
414+
self._start_thread(self._play_many, capture,
415+
append_function)
416+
else:
417+
self._stop_locks[0] = False
418+
self._start_thread(self._play_once, capture,
419+
append_function)
420+
421+
def stop(self):
422+
self._stop_locks = [True, True]
423+
424+
425+
class PlayInterface:
426+
def __init__(self):
427+
self.play_ctrl = PlayControl()
428+
self.capture = None
429+
self._config_was_updated = False
430+
self._toggle_button = None
431+
432+
def _load_capture_file(self):
433+
if TMP_PATH is None or not os.path.isfile(TMP_PATH):
434+
wx.LogError("No capture loaded")
435+
self._toggle_button.Value = False
436+
return False
437+
with open(TMP_PATH, 'r') as f:
438+
self.capture = f.readlines()
439+
return True
440+
441+
def _set_config(self):
442+
if self._config_was_updated: return
443+
count = settings.CONFIG.getint('DEFAULT', 'Repeat Count')
444+
infinite = settings.CONFIG.getboolean('DEFAULT', 'Infinite Playback')
445+
self.play_ctrl.set_config(count, infinite)
446+
self._config_was_updated = False
447+
448+
def _stop(self):
449+
self._toggle_button.Value = False
450+
self.play_ctrl.stop()
451+
self._config_was_updated = False
452+
settings.save_config()
453+
454+
def action(self, event):
455+
self._toggle_button = event.GetEventObject()
456+
self._toggle_button.Parent.panel.SetFocus()
457+
if self._toggle_button.Value:
458+
if self.play_ctrl.is_stoped():
459+
if not self._load_capture_file(): return
460+
self._set_config()
461+
self.play_ctrl.play(self.capture, self._stop)
462+
else:
463+
self._stop()
464+
465+
356466
class PlayCtrl:
357467
"""Control class for the play button."""
358468

@@ -532,3 +642,5 @@ def end(self):
532642

533643
def ended(self):
534644
return self._end.isSet()
645+
646+

atbswp/gui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def __add_bindings(self):
201201
self.Bind(wx.EVT_TOGGLEBUTTON, self.rbc.action, self.record_button)
202202

203203
# play_button_ctrl
204-
self.pbc = control.PlayCtrl()
204+
self.pbc = control.PlayInterface()
205205
self.Bind(wx.EVT_TOGGLEBUTTON, self.pbc.action, self.play_button)
206206

207207
# compile_button_ctrl

0 commit comments

Comments
 (0)