Skip to content

Commit 0f8da57

Browse files
committed
Make play_once and play_many be just one function
Mouse movements weren't captured when moving from right to left and from bottom to top. This kind of movements result in a negative distance and they were not capture due to the sensibility check, which prevents every pixel on a movement to be captured. 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. Make play_once and play_many be just one function Before when many playbacks were needed, play_many was executed in its own thread and created a thread for play_once. But now just a "play_once" thread is created and it calls a playback how many times are needed
1 parent acc0266 commit 0f8da57

2 files changed

Lines changed: 104 additions & 3 deletions

File tree

atbswp/control.py

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ def isinteger(s):
191191

192192
if move == "moveTo":
193193
coordinates = [int(s) for s in parameters.split(", ") if isinteger(s)]
194-
if coordinates[0] - self._lastx < self.mouse_sensibility \
195-
and coordinates[1] - self._lasty < self.mouse_sensibility:
194+
if abs(coordinates[0] - self._lastx) < self.mouse_sensibility \
195+
and abs(coordinates[1] - self._lasty) < self.mouse_sensibility:
196196
return
197197
else:
198198
self._lastx, self._lasty = coordinates
@@ -348,6 +348,105 @@ def update_timer(self, event):
348348
self.timer = self.countdown_dialog.update_ui()
349349

350350

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

@@ -527,3 +626,5 @@ def end(self):
527626

528627
def ended(self):
529628
return self._end.isSet()
629+
630+

atbswp/gui.py

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

205205
# play_button_ctrl
206-
self.pbc = control.PlayCtrl()
206+
self.pbc = control.PlayInterface()
207207
self.Bind(wx.EVT_TOGGLEBUTTON, self.pbc.action, self.play_button)
208208

209209
# compile_button_ctrl

0 commit comments

Comments
 (0)