Skip to content

Commit 1874c78

Browse files
committed
Re-use AutoSplit reference from __init__
1 parent de64398 commit 1874c78

12 files changed

+1492
-1488
lines changed

src/AutoControlledThread.py

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
from typing import TYPE_CHECKING
2-
3-
from PySide6 import QtCore
4-
5-
import error_messages
6-
import user_profile
7-
8-
if TYPE_CHECKING:
9-
from AutoSplit import AutoSplit
10-
11-
12-
class AutoControlledThread(QtCore.QThread):
13-
def __init__(self, autosplit: "AutoSplit"):
14-
self.autosplit = autosplit
15-
super().__init__()
16-
17-
@QtCore.Slot()
18-
def run(self):
19-
while True:
20-
try:
21-
line = input()
22-
except RuntimeError:
23-
self.autosplit.show_error_signal.emit(error_messages.stdin_lost)
24-
break
25-
except EOFError:
26-
continue
27-
match line:
28-
# This is for use in a Development environment
29-
case "kill":
30-
self.autosplit.closeEvent()
31-
break
32-
case "start":
33-
self.autosplit.start_auto_splitter()
34-
case "split" | "skip":
35-
self.autosplit.skip_split_signal.emit()
36-
case "undo":
37-
self.autosplit.undo_split_signal.emit()
38-
case "reset":
39-
self.autosplit.reset_signal.emit()
40-
# TODO: Not yet implemented in AutoSplit Integration
41-
# case 'pause':
42-
# self.pause_signal.emit()
43-
case line:
44-
if line.startswith("settings"):
45-
# Allow for any split character between "settings" and the path
46-
user_profile.load_settings(self.autosplit, line[9:])
1+
from typing import TYPE_CHECKING
2+
3+
from PySide6 import QtCore
4+
5+
import error_messages
6+
import user_profile
7+
8+
if TYPE_CHECKING:
9+
from AutoSplit import AutoSplit
10+
11+
12+
class AutoControlledThread(QtCore.QThread):
13+
def __init__(self, autosplit: "AutoSplit"):
14+
self._autosplit_ref = autosplit
15+
super().__init__()
16+
17+
@QtCore.Slot()
18+
def run(self):
19+
while True:
20+
try:
21+
line = input()
22+
except RuntimeError:
23+
self._autosplit_ref.show_error_signal.emit(error_messages.stdin_lost)
24+
break
25+
except EOFError:
26+
continue
27+
match line:
28+
# This is for use in a Development environment
29+
case "kill":
30+
self._autosplit_ref.closeEvent()
31+
break
32+
case "start":
33+
self._autosplit_ref.start_auto_splitter()
34+
case "split" | "skip":
35+
self._autosplit_ref.skip_split_signal.emit()
36+
case "undo":
37+
self._autosplit_ref.undo_split_signal.emit()
38+
case "reset":
39+
self._autosplit_ref.reset_signal.emit()
40+
# TODO: Not yet implemented in AutoSplit Integration
41+
# case 'pause':
42+
# self.pause_signal.emit()
43+
case line:
44+
if line.startswith("settings"):
45+
# Allow for any split character between "settings" and the path
46+
user_profile.load_settings(self._autosplit_ref, line[9:])

src/AutoSplit.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def __init__(self): # noqa: PLR0915
9595
self.split_image_number = 0
9696
self.split_images_and_loop_number: list[tuple[AutoSplitImage, int]] = []
9797
self.split_groups: list[list[int]] = []
98-
self.capture_method = CaptureMethodBase(None)
98+
self.capture_method = CaptureMethodBase(self)
9999
self.is_running = False
100100

101101
# Last loaded settings empty and last successful loaded settings file path to None until we try to load them
@@ -249,7 +249,7 @@ def __update_live_image_details(self, capture: MatLike | None, called_from_timer
249249
if called_from_timer:
250250
if self.is_running or self.start_image:
251251
return
252-
capture, _ = self.capture_method.get_frame(self)
252+
capture, _ = self.capture_method.get_frame()
253253

254254
# Update title from target window or Capture Device name
255255
capture_region_window_label = (
@@ -387,7 +387,7 @@ def __take_screenshot(self):
387387
screenshot_index += 1
388388

389389
# Grab screenshot of capture region
390-
capture, _ = self.capture_method.get_frame(self)
390+
capture, _ = self.capture_method.get_frame()
391391
if not is_valid_image(capture):
392392
error_messages.region()
393393
return
@@ -782,7 +782,7 @@ def gui_changes_on_reset(self, safe_to_reload_start_image: bool = False):
782782

783783
def __get_capture_for_comparison(self):
784784
"""Grab capture region and resize for comparison."""
785-
capture, is_old_image = self.capture_method.get_frame(self)
785+
capture, is_old_image = self.capture_method.get_frame()
786786

787787
# This most likely means we lost capture
788788
# (ie the captured window was closed, crashed, lost capture device, etc.)
@@ -792,9 +792,9 @@ def __get_capture_for_comparison(self):
792792
self.live_image.setText("Waiting for capture device...")
793793
else:
794794
self.live_image.setText("Trying to recover window...")
795-
recovered = self.capture_method.recover_window(self.settings_dict["captured_window_title"], self)
795+
recovered = self.capture_method.recover_window(self.settings_dict["captured_window_title"])
796796
if recovered:
797-
capture, _ = self.capture_method.get_frame(self)
797+
capture, _ = self.capture_method.get_frame()
798798

799799
self.__update_live_image_details(capture)
800800
return capture, is_old_image
@@ -868,7 +868,7 @@ def exit_program() -> NoReturn:
868868
if self.update_auto_control:
869869
# self.update_auto_control.terminate() hangs in PySide6
870870
self.update_auto_control.quit()
871-
self.capture_method.close(self)
871+
self.capture_method.close()
872872
if event is not None:
873873
event.accept()
874874
if self.is_auto_controlled:

src/capture_method/BitBltCaptureMethod.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ctypes
22
import ctypes.wintypes
3-
from typing import TYPE_CHECKING, cast
3+
from typing import cast
44

55
import numpy as np
66
import pywintypes
@@ -13,9 +13,6 @@
1313
from capture_method.CaptureMethodBase import CaptureMethodBase
1414
from utils import BGRA_CHANNEL_COUNT, get_window_bounds, is_valid_hwnd, try_delete_dc
1515

16-
if TYPE_CHECKING:
17-
from AutoSplit import AutoSplit
18-
1916
# This is an undocumented nFlag value for PrintWindow
2017
PW_RENDERFULLCONTENT = 0x00000002
2118

@@ -32,12 +29,12 @@ class BitBltCaptureMethod(CaptureMethodBase):
3229
_render_full_content = False
3330

3431
@override
35-
def get_frame(self, autosplit: "AutoSplit") -> tuple[MatLike | None, bool]:
36-
selection = autosplit.settings_dict["capture_region"]
37-
hwnd = autosplit.hwnd
32+
def get_frame(self) -> tuple[MatLike | None, bool]:
33+
selection = self._autosplit_ref.settings_dict["capture_region"]
34+
hwnd = self._autosplit_ref.hwnd
3835
image: MatLike | None = None
3936

40-
if not self.check_selected_region_exists(autosplit):
37+
if not self.check_selected_region_exists():
4138
return None, False
4239

4340
# If the window closes while it's being manipulated, it could cause a crash
@@ -77,9 +74,9 @@ def get_frame(self, autosplit: "AutoSplit") -> tuple[MatLike | None, bool]:
7774
return image, False
7875

7976
@override
80-
def recover_window(self, captured_window_title: str, autosplit: "AutoSplit"):
77+
def recover_window(self, captured_window_title: str):
8178
hwnd = win32gui.FindWindow(None, captured_window_title)
8279
if not is_valid_hwnd(hwnd):
8380
return False
84-
autosplit.hwnd = hwnd
85-
return self.check_selected_region_exists(autosplit)
81+
self._autosplit_ref.hwnd = hwnd
82+
return self.check_selected_region_exists()
Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
1-
from typing import TYPE_CHECKING
2-
3-
from cv2.typing import MatLike
4-
5-
from utils import is_valid_hwnd
6-
7-
if TYPE_CHECKING:
8-
from AutoSplit import AutoSplit
9-
10-
11-
class CaptureMethodBase:
12-
name = "None"
13-
short_description = ""
14-
description = ""
15-
16-
def __init__(self, autosplit: "AutoSplit | None"):
17-
# Some capture methods don't need an initialization process
18-
pass
19-
20-
def reinitialize(self, autosplit: "AutoSplit"):
21-
self.close(autosplit)
22-
self.__init__(autosplit) # type: ignore[misc]
23-
24-
def close(self, autosplit: "AutoSplit"):
25-
# Some capture methods don't need an initialization process
26-
pass
27-
28-
def get_frame(self, autosplit: "AutoSplit") -> tuple[MatLike | None, bool]: # noqa: PLR6301
29-
"""
30-
Captures an image of the region for a window matching the given
31-
parameters of the bounding box.
32-
33-
@return: The image of the region in the window in BGRA format
34-
"""
35-
return None, False
36-
37-
def recover_window(self, captured_window_title: str, autosplit: "AutoSplit") -> bool: # noqa: PLR6301
38-
return False
39-
40-
def check_selected_region_exists(self, autosplit: "AutoSplit") -> bool: # noqa: PLR6301
41-
return is_valid_hwnd(autosplit.hwnd)
1+
from typing import TYPE_CHECKING
2+
3+
from cv2.typing import MatLike
4+
5+
from utils import is_valid_hwnd
6+
7+
if TYPE_CHECKING:
8+
from AutoSplit import AutoSplit
9+
10+
11+
class CaptureMethodBase:
12+
name = "None"
13+
short_description = ""
14+
description = ""
15+
16+
_autosplit_ref: "AutoSplit"
17+
18+
def __init__(self, autosplit: "AutoSplit"):
19+
# Some capture methods don't need an initialization process
20+
self._autosplit_ref = autosplit
21+
22+
def reinitialize(self):
23+
self.close()
24+
self.__init__(self._autosplit_ref) # type: ignore[misc]
25+
26+
def close(self):
27+
# Some capture methods don't need an initialization process
28+
pass
29+
30+
def get_frame(self) -> tuple[MatLike | None, bool]: # noqa: PLR6301
31+
"""
32+
Captures an image of the region for a window matching the given
33+
parameters of the bounding box.
34+
35+
@return: The image of the region in the window in BGRA format
36+
"""
37+
return None, False
38+
39+
def recover_window(self, captured_window_title: str) -> bool: # noqa: PLR6301
40+
return False
41+
42+
def check_selected_region_exists(self) -> bool:
43+
return is_valid_hwnd(self._autosplit_ref.hwnd)

0 commit comments

Comments
 (0)