-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreTask.py
More file actions
1136 lines (968 loc) · 44.1 KB
/
reTask.py
File metadata and controls
1136 lines (968 loc) · 44.1 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pynput
import time
import os
import sys
import yaml
import json
import keyboard
from yaml import Loader, Dumper
from pynput.keyboard import Key, KeyCode
from pynput.mouse import Button
from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QGridLayout, QPushButton, QLabel,
QLineEdit, QCheckBox, QGroupBox, QFileDialog,
QMessageBox, QSpinBox, QComboBox)
from PyQt6.QtCore import QTimer, QThread, pyqtSignal
SOLS_ADDON = None
try:
import sols_rng_addon
SOLS_ADDON = sols_rng_addon
except ImportError:
SOLS_ADDON = None
class Config():
def __init__(self):
self.config_dir = os.path.join(os.environ.get('LOCALAPPDATA', ''), 'ReTask')
self.config_file = os.path.join(self.config_dir, 'config.yml')
self.output_dir = os.path.join(self.config_dir, 'output')
self.default_config = {
"recordingHotKey": "F1",
"playbackHotKey": "F3",
"recordingMouse": True,
"defaultOutputFile": os.path.join(self.output_dir, "macro.py"),
"macroName": "macro",
"mouseMovementTracking": False,
"playbackMode": "single",
"loopCount": 1,
"addons": {
"Sols": {
"macroOptimisations": True,
"alignmentHotKey": "F2"
}
}
}
os.makedirs(self.config_dir, exist_ok=True)
os.makedirs(self.output_dir, exist_ok=True)
if not os.path.exists(self.config_file):
self.data = self.default_config.copy()
self.save()
else:
try:
with open(self.config_file, "r") as f:
self.data = yaml.load(f, Loader) or {}
self._ensure_defaults()
except Exception:
self.data = self.default_config.copy()
self.save()
def _ensure_defaults(self):
def merge_dict(target, source):
for key, value in source.items():
if key not in target:
target[key] = value
elif isinstance(value, dict) and isinstance(target[key], dict):
merge_dict(target[key], value)
merge_dict(self.data, self.default_config)
def save(self):
with open(self.config_file, "w") as f:
yaml.dump(self.data, f, Dumper)
class KeyCaptureThread(QThread):
key_captured = pyqtSignal(str)
def __init__(self):
super().__init__()
self.should_stop = False
self.listener = None
def run(self):
self.should_stop = False
def on_key_press(key):
if self.should_stop:
return False
if isinstance(key, Key):
key_name = key.name.upper()
if key_name.startswith('F') and key_name[1:].isdigit():
self.key_captured.emit(f"F{key_name[1:]}")
else:
self.key_captured.emit(key_name.upper())
else:
self.key_captured.emit(str(key).upper().replace("'", ""))
return False
self.listener = pynput.keyboard.Listener(on_press=on_key_press)
self.listener.start()
self.listener.join()
def stop(self):
self.should_stop = True
if self.listener:
self.listener.stop()
class RecordingThread(QThread):
status_changed = pyqtSignal(str)
time_updated = pyqtSignal(float)
recording_finished = pyqtSignal()
def __init__(self, config, sols_addon=False):
super().__init__()
self.config = config
self.sols_addon = sols_addon
self.recording = None
self.should_stop = False
def run(self):
self.should_stop = False
self.status_changed.emit("Recording...")
self.recording = Recording(self.config, self.sols_addon)
start_time = time.perf_counter()
self.recording.start_recording()
while not self.should_stop:
elapsed = time.perf_counter() - start_time
self.time_updated.emit(elapsed)
self.msleep(100)
self.recording.stop_recording()
self.status_changed.emit("Stopped")
self.recording_finished.emit()
def stop(self):
self.should_stop = True
class PlaybackThread(QThread):
status_changed = pyqtSignal(str)
playback_finished = pyqtSignal()
loop_completed = pyqtSignal(int)
def __init__(self, macro_file, playback_mode, loop_count):
super().__init__()
self.macro_file = macro_file
self.playback_mode = playback_mode
self.loop_count = loop_count
self.should_stop = False
self.current_loop = 0
def run(self):
self.should_stop = False
self.current_loop = 0
try:
with open(self.macro_file, 'r') as f:
content = f.read()
macro_start = content.find('run_macro([')
if macro_start == -1:
self.status_changed.emit("Error: Invalid macro file format")
self.playback_finished.emit()
return
macro_start += len('run_macro(')
macro_end = content.rfind('])')
if macro_end == -1:
self.status_changed.emit("Error: Invalid macro file format")
self.playback_finished.emit()
return
macro_str = content[macro_start:macro_end + 1]
macro = eval(macro_str)
if self.playback_mode == "continuous":
while not self.should_stop:
self.current_loop += 1
self.status_changed.emit(f"Playing loop {self.current_loop}")
self._execute_macro(macro)
if self.should_stop:
break
self.loop_completed.emit(self.current_loop)
else:
loops_to_run = self.loop_count if self.playback_mode == "loop" else 1
for i in range(loops_to_run):
if self.should_stop:
break
self.current_loop = i + 1
self.status_changed.emit(f"Playing loop {self.current_loop}/{loops_to_run}")
self._execute_macro(macro)
if not self.should_stop:
self.loop_completed.emit(self.current_loop)
except Exception as e:
self.status_changed.emit(f"Error: {str(e)}")
self.playback_finished.emit()
def _execute_macro(self, macro):
import time
from pynput.keyboard import Key, Controller as KeyboardController
from pynput.mouse import Button, Controller as MouseController
# Wait 2 seconds before starting macro execution
time.sleep(2)
try:
from mousekey import MouseKey
mkey = MouseKey()
except ImportError:
mkey = None
kc = KeyboardController()
mc = MouseController()
pynput_special_keys = {
"Key.alt": Key.alt, "Key.alt_l": Key.alt_l, "Key.alt_r": Key.alt_r,
"Key.backspace": Key.backspace, "Key.caps_lock": Key.caps_lock,
"Key.cmd": Key.cmd, "Key.cmd_l": Key.cmd_l, "Key.cmd_r": Key.cmd_r,
"Key.ctrl": Key.ctrl, "Key.ctrl_l": Key.ctrl_l, "Key.ctrl_r": Key.ctrl_r,
"Key.delete": Key.delete, "Key.down": Key.down, "Key.end": Key.end,
"Key.enter": Key.enter, "Key.esc": Key.esc, "Key.f1": Key.f1,
"Key.f2": Key.f2, "Key.f3": Key.f3, "Key.f4": Key.f4, "Key.f5": Key.f5,
"Key.f6": Key.f6, "Key.f7": Key.f7, "Key.f8": Key.f8, "Key.f9": Key.f9,
"Key.f10": Key.f10, "Key.f11": Key.f11, "Key.f12": Key.f12,
"Key.home": Key.home, "Key.insert": Key.insert, "Key.left": Key.left,
"Key.menu": Key.menu, "Key.num_lock": Key.num_lock,
"Key.page_down": Key.page_down, "Key.page_up": Key.page_up,
"Key.pause": Key.pause, "Key.print_screen": Key.print_screen,
"Key.right": Key.right, "Key.scroll_lock": Key.scroll_lock,
"Key.shift": Key.shift, "Key.shift_l": Key.shift_l, "Key.shift_r": Key.shift_r,
"Key.space": Key.space, "Key.tab": Key.tab, "Key.up": Key.up
}
pynput_special_buttons = {
"Button.left": Button.left,
"Button.right": Button.right,
"Button.middle": Button.middle
}
macro_start_time = time.perf_counter()
for i, action in enumerate(macro):
if self.should_stop:
break
time_difference = action["timestamp"] - (time.perf_counter() - macro_start_time)
"""if time_difference > 0:
start_time = time.perf_counter()
while not self.should_stop:
elapsed_time = time.perf_counter() - start_time
remaining_time = time_difference - elapsed_time
if remaining_time <= 0:
break
if remaining_time > 0.02:
time.sleep(max(remaining_time/2, 0.0001))
if i > 0:
prev_ts = macro[i - 1].get("timestamp", 0)
curr_ts = action.get("timestamp", 0)
delta = curr_ts - prev_ts
if delta > 0:
end_time = time.perf_counter() + delta
while not self.should_stop and time.perf_counter() < end_time:
time.sleep(0.01)"""
if self.should_stop:
break
action_type = action["type"]
match action_type:
case "key_press":
if "Key." in action["key"]:
kc.press(pynput_special_keys[action["key"]])
else:
kc.press(action["key"])
case "key_release":
if "Key." in action["key"]:
kc.release(pynput_special_keys[action["key"]])
else:
kc.release(action["key"])
case "mouse_movement":
if mkey:
mkey.move_to(int(action["x"]), int(action["y"]))
case "mouse_press":
mc.press(pynput_special_buttons[action["button"]])
case "mouse_move_press":
if mkey:
mkey.move_to(int(action["x"]), int(action["y"]))
mc.press(pynput_special_buttons[action["button"]])
case "mouse_release":
mc.release(pynput_special_buttons[action["button"]])
case "mouse_scroll":
if "x" in action and mkey:
mkey.move_to(int(action["x"]), int(action["y"]))
mc.scroll(action["dx"], action["dy"])
case "wait":
duration = action.get("duration", 0)
if duration > 0:
end_time = time.perf_counter() + duration
while not self.should_stop and time.perf_counter() < end_time:
time.sleep(0.01)
def stop(self):
self.should_stop = True
class Recording():
def __init__(self, config, sols_addon=False):
self.keys_pressed = set()
self.buttons_pressed = {}
self.start_time = time.perf_counter()
self.last_mouse_pos = None
self.macro = []
self.sols_addon = sols_addon
self.optimized_time = 0
self.last_action_timestamp = None
self.last_time = None
self.no_keys_pressed = True
self.config = config
self.macro_optimisations = False
self.keyboard_listener = None
self.mouse_listener = None
self.hotkeys_to_ignore = set()
if self.sols_addon and "addons" in self.config.data:
if "Sols" in self.config.data["addons"]:
if self.config.data["addons"]["Sols"].get("macroOptimisations", False):
self.macro_optimisations = True
self._setup_hotkeys_to_ignore()
def _key_to_str(self, key):
if isinstance(key, Key):
return f"Key.{key.name}"
if isinstance(key, KeyCode):
return key.char if key.char is not None else str(key)
s = str(key).replace("'", "")
return s if s.startswith("Key.") else s
def add_action(self, action: dict):
now = time.time()
if self.last_time is not None:
delay = int((now - self.last_time) * 1000) # ms
if delay > 0:
self.macro.append({
"type": "wait",
"duration": delay
})
self.last_time = now
self.macro.append(action)
def timestamp(self):
if self.last_action_timestamp is None:
self.last_action_timestamp = time.perf_counter()
if self.macro_optimisations and self.check_keys_pressed():
self.last_action_timestamp = time.perf_counter()
self.no_keys_pressed = True
elif self.no_keys_pressed:
self.optimized_time += time.perf_counter() - self.last_action_timestamp
self.no_keys_pressed = False
else:
self.no_keys_pressed = False
return (time.perf_counter() - self.start_time) - self.optimized_time
def _setup_hotkeys_to_ignore(self):
self.hotkeys_to_ignore.clear()
recording_hotkey = self.config.data.get("recordingHotKey", "").lower()
playback_hotkey = self.config.data.get("playbackHotKey", "").lower()
if recording_hotkey:
if recording_hotkey.startswith('f') and recording_hotkey[1:].isdigit():
self.hotkeys_to_ignore.add(getattr(Key, recording_hotkey, None))
else:
self.hotkeys_to_ignore.add(recording_hotkey)
if playback_hotkey:
if playback_hotkey.startswith('f') and playback_hotkey[1:].isdigit():
self.hotkeys_to_ignore.add(getattr(Key, playback_hotkey, None))
else:
self.hotkeys_to_ignore.add(playback_hotkey)
if self.config.data.get("addons", {}).get("Sols"):
alignment_hotkey = self.config.data["addons"]["Sols"].get("alignmentHotKey", "").lower()
if alignment_hotkey:
if alignment_hotkey.startswith('f') and alignment_hotkey[1:].isdigit():
self.hotkeys_to_ignore.add(getattr(Key, alignment_hotkey, None))
else:
self.hotkeys_to_ignore.add(alignment_hotkey)
def _should_ignore_key(self, key):
if key in self.hotkeys_to_ignore:
return True
if isinstance(key, Key):
return key in self.hotkeys_to_ignore
return str(key).replace("'", "") in [str(h).replace("'", "") for h in self.hotkeys_to_ignore if h]
def on_key_press(self, key):
if self._should_ignore_key(key):
return
k = self._key_to_str(key)
if k in self.keys_pressed:
return
self.keys_pressed.add(k)
self.add_action({"type": "key_press", "key": k})
def on_key_release(self, key):
if self._should_ignore_key(key):
return
k = self._key_to_str(key)
if k not in self.keys_pressed:
return
self.keys_pressed.remove(k)
self.add_action({"type": "key_release", "key": k})
def on_mouse_move(self, x, y):
if self.last_mouse_pos == [x, y]:
return
self.last_mouse_pos = [x, y]
self.macro.append({
"type": "mouse_movement",
"x": x,
"y": y,
"timestamp": self.timestamp()
})
def on_button_action(self, x, y, button, pressed):
if pressed:
if self.buttons_pressed.get(button, False):
return
self.buttons_pressed[button] = True
if self.last_mouse_pos == [x, y]:
self.macro.append({
"type": "mouse_press",
"button": f"Button.{button.name}",
"timestamp": self.timestamp()
})
else:
self.last_mouse_pos = [x, y]
self.macro.append({
"type": "mouse_move_press",
"x": x,
"y": y,
"button": f"Button.{button.name}",
"timestamp": self.timestamp()
})
elif not pressed and self.buttons_pressed.get(button, False):
self.buttons_pressed[button] = False
self.macro.append({
"type": "mouse_release",
"button": f"Button.{button.name}",
"timestamp": self.timestamp()
})
def on_mouse_scroll(self, x, y, dx, dy):
if self.last_mouse_pos == [x, y]:
self.macro.append({
"type": "mouse_scroll",
"dx": dx,
"dy": dy,
"timestamp": self.timestamp()
})
else:
self.last_mouse_pos = [x, y]
self.macro.append({
"type": "mouse_scroll",
"x": x,
"y": y,
"dx": dx,
"dy": dy,
"timestamp": self.timestamp()
})
def check_keys_pressed(self):
return not self.buttons_pressed and not self.keys_pressed
def start_recording(self):
self.release_all_keys()
self.keyboard_listener = pynput.keyboard.Listener(
on_press=self.on_key_press,
on_release=self.on_key_release
)
if self.config.data["recordingMouse"]:
if self.config.data["mouseMovementTracking"]:
self.mouse_listener = pynput.mouse.Listener(
on_move=self.on_mouse_move,
on_click=self.on_button_action,
on_scroll=self.on_mouse_scroll
)
else:
self.mouse_listener = pynput.mouse.Listener(
on_click=self.on_button_action,
on_scroll=self.on_mouse_scroll
)
self.keyboard_listener.start()
if self.mouse_listener:
self.mouse_listener.start()
def stop_recording(self):
if self.keyboard_listener:
self.keyboard_listener.stop()
if self.mouse_listener:
self.mouse_listener.stop()
self.release_all_keys()
self.save()
def save(self):
base_output_path = self.config.data["defaultOutputFile"]
macro_name = self.config.data.get("macroName", "macro")
# Generate filename based on macro name
dir_path = os.path.dirname(base_output_path)
if macro_name and macro_name.strip() and macro_name != "macro":
base_name = f"{macro_name.strip()}.py"
output_file_path = os.path.join(dir_path, base_name)
# Handle duplicate names by adding a number
counter = 1
while os.path.exists(output_file_path):
base_name = f"{macro_name.strip()}_{counter}.py"
output_file_path = os.path.join(dir_path, base_name)
counter += 1
else:
output_file_path = base_output_path
os.makedirs(os.path.dirname(output_file_path), exist_ok=True)
try:
with open("default_macro_output.py", "r") as template_file:
template_content = template_file.read()
with open(output_file_path, "w") as output_file:
macro_str = str(self.macro).replace("}, ", "},\n")
output_file.write(f"{template_content}\n\nmacro_actions = {macro_str}\n\nif __name__ == \"__main__\":\n run_macro({macro_str})")
print(f"Macro saved as: {output_file_path}")
except Exception as e:
print(f"Error saving macro: {e}")
def release_all_keys(self):
try:
kc = pynput.keyboard.Controller()
keys_to_release = [
Key.f1, Key.f2, Key.f3, Key.f4, Key.f5, Key.f6, Key.f7, Key.f8, Key.f9, Key.f10, Key.f11, Key.f12,
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
Key.alt, Key.alt_gr, Key.backspace, Key.caps_lock, Key.cmd, Key.ctrl, Key.delete, Key.down, Key.end,
Key.enter, Key.esc, Key.home, Key.insert, Key.left, Key.page_down, Key.page_up, Key.right, Key.shift, Key.space, Key.tab, Key.up
]
for key in keys_to_release:
try:
kc.release(key)
except:
pass
mc = pynput.mouse.Controller()
for button in [Button.left, Button.right, Button.middle, Button.x1, Button.x2]:
try:
mc.release(button)
except:
pass
except Exception:
pass
class ReTaskGUI(QMainWindow):
def __init__(self):
super().__init__()
self.config = Config()
self.recording_thread = None
self.playback_thread = None
self.key_capture_thread = None
self.is_recording = False
self.is_playing = False
self.elapsed_time = 0.0
self.timer = QTimer()
self.timer.timeout.connect(self.update_display)
self.capturing_key_for = None
self.global_hotkeys_registered = False
self.init_ui()
self.apply_dark_theme()
self.setup_global_hotkeys()
def init_ui(self):
self.setWindowTitle("ReTask")
self.setFixedSize(500, 750)
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
layout.setSpacing(15)
layout.setContentsMargins(20, 20, 20, 20)
self.create_main_controls(layout)
self.create_settings_panel(layout)
self.create_playback_settings(layout)
if self.config.data.get("addons", {}).get("Sols"):
self.create_sols_settings(layout)
layout.addStretch()
def create_main_controls(self, layout):
main_group = QGroupBox("Recording Controls")
main_layout = QVBoxLayout(main_group)
self.record_button = QPushButton("Start Recording")
self.record_button.setMinimumHeight(50)
self.record_button.clicked.connect(self.toggle_recording)
main_layout.addWidget(self.record_button)
self.playback_button = QPushButton("Start Playback")
self.playback_button.setMinimumHeight(50)
self.playback_button.clicked.connect(self.toggle_playback)
main_layout.addWidget(self.playback_button)
status_layout = QHBoxLayout()
self.status_label = QLabel("Status: Ready")
self.time_label = QLabel("Time: 00:00")
status_layout.addWidget(self.status_label)
status_layout.addStretch()
status_layout.addWidget(self.time_label)
main_layout.addLayout(status_layout)
# Macro name input
name_layout = QHBoxLayout()
name_layout.addWidget(QLabel("Macro Name:"))
self.macro_name_input = QLineEdit(self.config.data.get("macroName", "macro"))
self.macro_name_input.setPlaceholderText("Enter macro name...")
self.macro_name_input.textChanged.connect(lambda: self.save_config(show_message=False))
name_layout.addWidget(self.macro_name_input, 1)
main_layout.addLayout(name_layout)
output_layout = QHBoxLayout()
output_layout.addWidget(QLabel("Output Directory:"))
self.output_path_label = QLabel(os.path.dirname(self.config.data["defaultOutputFile"]))
self.output_path_label.setStyleSheet("QLabel { background-color: #3a3a3a; padding: 5px; border-radius: 3px; }")
self.browse_button = QPushButton("Browse")
self.browse_button.clicked.connect(self.browse_output_directory)
output_layout.addWidget(self.output_path_label, 1)
output_layout.addWidget(self.browse_button)
main_layout.addLayout(output_layout)
layout.addWidget(main_group)
def create_settings_panel(self, layout):
settings_group = QGroupBox("Settings")
settings_layout = QGridLayout(settings_group)
settings_layout.addWidget(QLabel("Recording Hotkey:"), 0, 0)
self.hotkey_input = QLineEdit(self.config.data["recordingHotKey"])
settings_layout.addWidget(self.hotkey_input, 0, 1)
self.capture_recording_key_btn = QPushButton("Set Key")
self.capture_recording_key_btn.clicked.connect(lambda: self.capture_hotkey("recording"))
settings_layout.addWidget(self.capture_recording_key_btn, 0, 2)
settings_layout.addWidget(QLabel("Playback Hotkey:"), 1, 0)
self.playback_hotkey_input = QLineEdit(self.config.data["playbackHotKey"])
settings_layout.addWidget(self.playback_hotkey_input, 1, 1)
self.capture_playback_key_btn = QPushButton("Set Key")
self.capture_playback_key_btn.clicked.connect(lambda: self.capture_hotkey("playback"))
settings_layout.addWidget(self.capture_playback_key_btn, 1, 2)
self.mouse_recording_cb = QCheckBox("Record Mouse Actions")
self.mouse_recording_cb.setChecked(self.config.data["recordingMouse"])
self.mouse_recording_cb.toggled.connect(lambda: self.save_config(show_message=False))
settings_layout.addWidget(self.mouse_recording_cb, 2, 0, 1, 3)
self.mouse_movement_cb = QCheckBox("Track Mouse Movement")
self.mouse_movement_cb.setChecked(self.config.data["mouseMovementTracking"])
self.mouse_movement_cb.toggled.connect(lambda: self.save_config(show_message=False))
settings_layout.addWidget(self.mouse_movement_cb, 3, 0, 1, 3)
button_layout = QHBoxLayout()
self.save_config_button = QPushButton("Save Config")
self.save_config_button.clicked.connect(self.save_config)
self.load_config_button = QPushButton("Load Config")
self.load_config_button.clicked.connect(self.load_config)
button_layout.addWidget(self.save_config_button)
button_layout.addWidget(self.load_config_button)
settings_layout.addLayout(button_layout, 4, 0, 1, 3)
layout.addWidget(settings_group)
def create_playback_settings(self, layout):
playback_group = QGroupBox("Playback Settings")
playback_layout = QGridLayout(playback_group)
playback_layout.addWidget(QLabel("Playback Mode:"), 0, 0)
self.playback_mode_combo = QComboBox()
self.playback_mode_combo.addItems(["Single", "Loop", "Continuous"])
current_mode = self.config.data.get("playbackMode", "single")
mode_index = {"single": 0, "loop": 1, "continuous": 2}.get(current_mode, 0)
self.playback_mode_combo.setCurrentIndex(mode_index)
self.playback_mode_combo.currentTextChanged.connect(self.on_playback_mode_changed)
playback_layout.addWidget(self.playback_mode_combo, 0, 1)
playback_layout.addWidget(QLabel("Loop Count:"), 1, 0)
self.loop_count_spin = QSpinBox()
self.loop_count_spin.setMinimum(1)
self.loop_count_spin.setMaximum(9999)
self.loop_count_spin.setValue(self.config.data.get("loopCount", 1))
self.loop_count_spin.valueChanged.connect(lambda: self.save_config(show_message=False))
playback_layout.addWidget(self.loop_count_spin, 1, 1)
self.loop_count_spin.setEnabled(current_mode == "loop")
layout.addWidget(playback_group)
def create_sols_settings(self, layout):
sols_group = QGroupBox("Sols Addon Settings")
sols_layout = QGridLayout(sols_group)
sols_config = self.config.data["addons"]["Sols"]
self.macro_optimizations_cb = QCheckBox("Sols Alignment")
self.macro_optimizations_cb.setChecked(sols_config.get("macroOptimisations", False))
self.macro_optimizations_cb.toggled.connect(lambda: self.save_config(show_message=False))
sols_layout.addWidget(self.macro_optimizations_cb, 0, 0, 1, 2)
sols_layout.addWidget(QLabel("Alignment Hotkey:"), 1, 0)
self.alignment_hotkey_input = QLineEdit(sols_config.get("alignmentHotKey", ""))
sols_layout.addWidget(self.alignment_hotkey_input, 1, 1)
self.capture_alignment_key_btn = QPushButton("Set Key")
self.capture_alignment_key_btn.clicked.connect(lambda: self.capture_hotkey("alignment"))
sols_layout.addWidget(self.capture_alignment_key_btn, 1, 2)
layout.addWidget(sols_group)
def apply_dark_theme(self):
self.setStyleSheet("""
QMainWindow {
background-color: #2b2b2b;
color: #ffffff;
}
QGroupBox {
font-weight: bold;
border: 2px solid #555555;
border-radius: 8px;
margin-top: 10px;
padding-top: 10px;
background-color: #353535;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 10px;
padding: 0 8px 0 8px;
color: #ffffff;
}
QPushButton {
background-color: #4a4a4a;
border: 1px solid #666666;
border-radius: 6px;
padding: 8px 16px;
color: #ffffff;
font-weight: bold;
}
QPushButton:hover {
background-color: #5a5a5a;
border-color: #777777;
}
QPushButton:pressed {
background-color: #3a3a3a;
}
QPushButton#record_button_recording {
background-color: #d32f2f;
border-color: #f44336;
}
QPushButton#record_button_recording:hover {
background-color: #e53935;
}
QPushButton#playback_button_playing {
background-color: #2e7d32;
border-color: #4caf50;
}
QPushButton#playback_button_playing:hover {
background-color: #388e3c;
}
QLineEdit {
background-color: #3a3a3a;
border: 1px solid #555555;
border-radius: 4px;
padding: 6px;
color: #ffffff;
}
QLineEdit:focus {
border-color: #2196f3;
}
QCheckBox {
color: #ffffff;
spacing: 8px;
}
QCheckBox::indicator {
width: 16px;
height: 16px;
border: 1px solid #555555;
border-radius: 3px;
background-color: #3a3a3a;
}
QCheckBox::indicator:checked {
background-color: #2196f3;
border-color: #1976d2;
}
QComboBox {
background-color: #3a3a3a;
border: 1px solid #555555;
border-radius: 4px;
padding: 6px;
color: #ffffff;
min-width: 100px;
}
QComboBox:focus {
border-color: #2196f3;
}
QComboBox::drop-down {
border: none;
width: 20px;
}
QComboBox::down-arrow {
image: none;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 5px solid #ffffff;
margin-right: 5px;
}
QComboBox QAbstractItemView {
background-color: #3a3a3a;
border: 1px solid #555555;
selection-background-color: #2196f3;
color: #ffffff;
}
QSpinBox {
background-color: #3a3a3a;
border: 1px solid #555555;
border-radius: 4px;
padding: 6px;
color: #ffffff;
}
QSpinBox:focus {
border-color: #2196f3;
}
QLabel {
color: #ffffff;
}
""")
def on_playback_mode_changed(self, mode):
self.loop_count_spin.setEnabled(mode.lower() == "loop")
self.save_config(show_message=False)
def capture_hotkey(self, hotkey_type):
if self.key_capture_thread and self.key_capture_thread.isRunning():
self.key_capture_thread.stop()
self.key_capture_thread.wait()
self.capturing_key_for = hotkey_type
button_map = {
"recording": self.capture_recording_key_btn,
"playback": self.capture_playback_key_btn,
"alignment": self.capture_alignment_key_btn
}
button = button_map.get(hotkey_type)
if button:
button.setText("Press any key...")
button.setEnabled(False)
self.key_capture_thread = KeyCaptureThread()
self.key_capture_thread.key_captured.connect(self.on_key_captured)
self.key_capture_thread.start()
def on_key_captured(self, key):
captured_for = self.capturing_key_for
if self.key_capture_thread:
self.key_capture_thread.stop()
self.key_capture_thread = None
self.capturing_key_for = None
if captured_for:
input_map = {
"recording": self.hotkey_input,
"playback": self.playback_hotkey_input,
"alignment": self.alignment_hotkey_input
}
button_map = {
"recording": self.capture_recording_key_btn,
"playback": self.capture_playback_key_btn,
"alignment": self.capture_alignment_key_btn
}
input_field = input_map.get(captured_for)
button = button_map.get(captured_for)
if input_field:
input_field.setText(key)
if button:
button.setText("Set Key")
button.setEnabled(True)
self.save_config(show_message=False)
def toggle_recording(self):
if not self.is_recording:
self.start_recording()
else:
self.stop_recording()
def toggle_playback(self):
if not self.is_playing:
self.start_playback()
else:
self.stop_playback()
def start_recording(self):
self.is_recording = True
self.elapsed_time = 0.0
self.record_button.setText("Stop Recording")
self.record_button.setObjectName("record_button_recording")
self.record_button.setStyleSheet(self.record_button.styleSheet())
self.status_label.setText("Status: Recording...")
sols_addon = self.config.data.get("addons", {}).get("Sols") is not None
self.recording_thread = RecordingThread(self.config, sols_addon)
self.recording_thread.status_changed.connect(self.update_status)
self.recording_thread.time_updated.connect(self.update_time)
self.recording_thread.recording_finished.connect(self.on_recording_finished)
self.recording_thread.start()
self.timer.start(100)
def stop_recording(self):
if self.recording_thread:
self.recording_thread.stop()
def start_playback(self):
if not os.path.exists(self.config.data["defaultOutputFile"]):
QMessageBox.warning(self, "File Not Found",
f"Macro file not found: {self.config.data['defaultOutputFile']}")
return
self.is_playing = True
self.playback_button.setText("Stop Playback")
self.playback_button.setObjectName("playback_button_playing")
self.playback_button.setStyleSheet(self.playback_button.styleSheet())
mode = self.playback_mode_combo.currentText().lower()
loop_count = self.loop_count_spin.value()
self.playback_thread = PlaybackThread(
self.config.data["defaultOutputFile"],
mode,
loop_count
)
self.playback_thread.status_changed.connect(self.update_status)
self.playback_thread.playback_finished.connect(self.on_playback_finished)
self.playback_thread.loop_completed.connect(self.on_loop_completed)
self.playback_thread.start()
def stop_playback(self):
if self.playback_thread:
self.playback_thread.stop()
def on_playback_finished(self):
self.is_playing = False
self.playback_button.setText("Start Playback")
self.playback_button.setObjectName("")
self.playback_button.setStyleSheet(self.playback_button.styleSheet())
self.status_label.setText("Status: Ready")
def on_loop_completed(self, loop_number):
mode = self.playback_mode_combo.currentText().lower()
if mode == "continuous":
self.status_label.setText(f"Status: Continuous playback - Loop {loop_number} completed")
else:
total_loops = self.loop_count_spin.value()
self.status_label.setText(f"Status: Loop {loop_number}/{total_loops} completed")
def on_recording_finished(self):
self.is_recording = False
self.record_button.setText("Start Recording")
self.record_button.setObjectName("")
self.record_button.setStyleSheet(self.record_button.styleSheet())
self.timer.stop()
QMessageBox.information(self, "Recording Complete",