now engine shutdowns properly when quiting, corrected logic for window show

This commit is contained in:
Adam Saudagar 2020-10-18 04:42:48 +05:30
parent fafb6ea952
commit 69edc75c16
4 changed files with 43 additions and 28 deletions

View File

@ -23,16 +23,16 @@ class WindowClient:
self.crop = crop self.crop = crop
self.scale = scale self.scale = scale
self.show_name = show_name self.show_name = show_name
self.showing = False
if len(WindowClient.clients) == 0: if len(WindowClient.clients) == 0:
window_server.start() window_server.start()
WindowClient.clients.append(self) WindowClient.clients.append(self)
def __del__(self): def destory(self):
WindowClient.clients.remove(self) WindowClient.clients.remove(self)
if len(WindowClient.clients) == 0: if len(WindowClient.clients) == 0:
window_server.stop() window_server.stop()
logging.info("window server stopped")
@staticmethod @staticmethod
def running(): def running():
@ -86,7 +86,7 @@ class WindowClient:
else: else:
return func(img) return func(img)
def show(self, resize=None, func=None): def show(self, to_show, resize=None, func=None):
""" """
Displays the processed image for debugging purposes Displays the processed image for debugging purposes
:param ready_img: send ready image, just show the `ready_img` directly :param ready_img: send ready image, just show the `ready_img` directly
@ -100,6 +100,10 @@ class WindowClient:
logging.warning("You need to assign a name first") logging.warning("You need to assign a name first")
return return
if not to_show:
cv2.destroyWindow(self.show_name)
return
img = self.processed_image(func) img = self.processed_image(func)
if img is None: if img is None:
@ -108,5 +112,4 @@ class WindowClient:
if resize is not None: if resize is not None:
img = imutils.resize(img, width=resize) img = imutils.resize(img, width=resize)
cv2.imshow(self.show_name, img) cv2.imshow(self.show_name, img)
cv2.waitKey(25)
self.showing = True

View File

@ -77,11 +77,6 @@ def loop():
def loop_end(): def loop_end():
cv2.waitKey(25) cv2.waitKey(25)
from fishy.engine.common.window import WindowClient
for c in WindowClient.clients:
if not c.showing:
cv2.destroyWindow(c.show_name)
def run(): def run():
# todo use config # todo use config

View File

@ -92,7 +92,16 @@ class FullAuto(IEngine):
self.fisher = SemiFisherEngine(None) self.fisher = SemiFisherEngine(None)
self.controls = Controls(self.get_controls()) self.controls = Controls(self.get_controls())
@property
def show(self):
return config.get("show_window_full_auto", False)
@show.setter
def show(self, x):
config.set("show_window_full_auto", x)
def update_crop(self): def update_crop(self):
self.show = True
self.crop = get_crop_coods(self.window) self.crop = get_crop_coods(self.window)
config.set("full_auto_crop", self.crop) config.set("full_auto_crop", self.crop)
self.window.crop = self.crop self.window.crop = self.crop
@ -118,12 +127,16 @@ class FullAuto(IEngine):
self.gui.bot_started(True) self.gui.bot_started(True)
while self.start and WindowClient.running(): while self.start and WindowClient.running():
self.window.show(func=image_pre_process) self.window.show(self.show, func=image_pre_process)
cv2.waitKey(25) if not self.show:
time.sleep(0.1)
self.gui.bot_started(False) self.gui.bot_started(False)
self.controls.unassign_keys() self.controls.unassign_keys()
self.window.show(False)
logging.info("Quit") logging.info("Quit")
self.window.destory()
self.fisher.toggle_start()
def get_coods(self): def get_coods(self):
return get_values_from_image(self.window.processed_image(func=image_pre_process), self._tesseract_dir) return get_values_from_image(self.window.processed_image(func=image_pre_process), self._tesseract_dir)
@ -163,7 +176,6 @@ class FullAuto(IEngine):
if from_angle is None: if from_angle is None:
_, _, from_angle = self.get_coods() _, _, from_angle = self.get_coods()
if target_angle < 0: if target_angle < 0:
target_angle = 360 + target_angle target_angle = 360 + target_angle
while target_angle > 360: while target_angle > 360:
@ -234,32 +246,36 @@ class FullAuto(IEngine):
def move_to_target(): def move_to_target():
self.move_to(config.get("target")) self.move_to(config.get("target"))
def rotate_to(): def rotate_to_90():
self.rotate_to(90) self.rotate_to(90)
def toggle_show():
self.show = not self.show
controls = [ controls = [
{ ("MAIN", {
Key.RIGHT: Player(self).start_route, Key.RIGHT: Player(self).start_route,
Key.UP: Calibrate(self).callibrate, Key.UP: Calibrate(self).callibrate,
Key.LEFT: self.update_crop, Key.LEFT: Recorder(self).start_recording,
Key.DOWN: change_state Key.DOWN: change_state
}, }),
{ ("COODS", {
Key.RIGHT: print_coods, Key.RIGHT: print_coods,
Key.UP: Recorder(self).start_recording, Key.UP: self.update_crop,
Key.LEFT: helper.empty_function, Key.LEFT: toggle_show,
Key.DOWN: change_state Key.DOWN: change_state
}, }),
{ ("TEST1", {
Key.RIGHT: set_target, Key.RIGHT: set_target,
Key.UP: rotate_to, Key.UP: rotate_to_90,
Key.LEFT: move_to_target, Key.LEFT: move_to_target,
Key.DOWN: change_state Key.DOWN: change_state
} })
] ]
return controls return controls
class Controls: class Controls:
def __init__(self, controls, first=0): def __init__(self, controls, first=0):
self.current_menu = first - 1 self.current_menu = first - 1
@ -270,8 +286,8 @@ class Controls:
if self.current_menu == len(self.controls): if self.current_menu == len(self.controls):
self.current_menu = 0 self.current_menu = 0
help_str = "CONTROLS" help_str = F"CONTROLS: {self.controls[self.current_menu][0]}"
for key, func in self.controls[self.current_menu].items(): for key, func in self.controls[self.current_menu][1].items():
hotkey.set_hotkey(key, func) hotkey.set_hotkey(key, func)
help_str += f"\n{key.value}: {func.__name__}" help_str += f"\n{key.value}: {func.__name__}"
logging.info(help_str) logging.info(help_str)
@ -279,7 +295,7 @@ class Controls:
def unassign_keys(self): def unassign_keys(self):
keys = [] keys = []
for c in self.controls: for c in self.controls:
for k in c.keys(): for k in c[1].keys():
if k not in keys: if k not in keys:
hotkey.free_key(k) hotkey.free_key(k)

View File

@ -37,7 +37,7 @@ class SemiFisherEngine(IEngine):
self.gui.bot_started(True) self.gui.bot_started(True)
logging.info("Starting the bot engine, look at the fishing hole to start fishing") logging.info("Starting the bot engine, look at the fishing hole to start fishing")
Thread(target=self._wait_and_check).start() Thread(target=self._wait_and_check).start()
while self.start: while self.start and WindowClient.running():
capture = self.fishPixWindow.get_capture() capture = self.fishPixWindow.get_capture()
if capture is None: if capture is None:
@ -53,6 +53,7 @@ class SemiFisherEngine(IEngine):
logging.info("Fishing engine stopped") logging.info("Fishing engine stopped")
self.gui.bot_started(False) self.gui.bot_started(False)
fishing_event.unsubscribe() fishing_event.unsubscribe()
self.fishPixWindow.destory()
def _wait_and_check(self): def _wait_and_check(self):
time.sleep(10) time.sleep(10)