Skip to content

Commit

Permalink
fix: 适配新版maafw v2.0.0b4,修复一些小问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Sep 20, 2024
1 parent 96fa3c7 commit 883a751
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
41 changes: 14 additions & 27 deletions src/MaaDebugger/maafw/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Callable, List, Optional

from maa.controller import AdbController, Win32Controller
from maa.tasker import Tasker, RecognitionDetail
from maa.tasker import Tasker, RecognitionDetail, NotificationHandler
from maa.resource import Resource
from maa.toolkit import Toolkit, AdbDevice, DesktopWindow
from PIL import Image
Expand All @@ -14,7 +14,10 @@

class MaaFW:

tasker: Tasker
resource: Resource | None
controller: AdbController | Win32Controller | None
tasker: Tasker | None
notification_handler: NotificationHandler | None

def __init__(self):
Toolkit.init_option("./")
Expand Down Expand Up @@ -84,12 +87,16 @@ def run_task(self, entry: str, pipeline_override: dict = {}) -> bool:
if not self.tasker:
self.tasker = Tasker(notification_handler=self.notification_handler)

if not self.resource or not self.controller:
print("Resource or Controller not initialized")
return False

self.tasker.bind(self.resource, self.controller)
if not self.tasker.inited:
print("Failed to init MaaFramework instance")
return False

return self.tasker.post_pipeline(entry, pipeline_override).wait()
return self.tasker.post_pipeline(entry, pipeline_override).wait().succeeded()

@asyncify
def stop_task(self):
Expand All @@ -112,38 +119,18 @@ def screencap(self, capture: bool = True) -> Optional[Image.Image]:
return cvmat_to_image(im)

@asyncify
def click(self, x, y) -> None:
def click(self, x, y) -> bool:
if not self.controller:
return None
return False

self.controller.post_click(x, y).wait()
return self.controller.post_click(x, y).wait().succeeded()

@asyncify
def get_reco_detail(self, reco_id: int) -> Optional[RecognitionDetail]:
if not self.tasker:
return None

return self.tasker._get_recognition_detail(reco_id)

def _tasker_callback(self, msg: str, detail: dict, arg):
if msg == "Task.Debug.ListToRecognize":
self.screenshotter.refresh(False)
if self.on_next_list_starting:
self.on_next_list_starting(detail["current"], detail["list"])

elif msg == "Task.Debug.MissAll":
if self.on_miss_all:
self.on_miss_all(detail["current"], detail["list"])

elif msg == "Task.Debug.RecognitionResult":
reco = detail["recognition"]
reco_id = reco["reco_id"]
name = reco["name"]
hit = reco["box"] is not None

if self.on_recognition_succeeded:
self.on_recognition_succeeded(reco_id, name, hit)

return self.tasker.get_recognition_detail(reco_id)

# class Screenshotter(threading.Thread):
class Screenshotter:
Expand Down
3 changes: 2 additions & 1 deletion src/MaaDebugger/webpage/index_page/master_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ async def on_click_detect():
return

devices_select.value = next(iter(options))
on_change_devices_select(devices_select.value)
GlobalStatus.ctrl_detecting = Status.SUCCEEDED

def on_change_devices_select(e):
Expand Down Expand Up @@ -223,7 +224,7 @@ async def on_click_connect():
async def on_click_detect():
GlobalStatus.ctrl_detecting = Status.RUNNING

windows = await maafw.detect_win32hwnd("", window_name_input.value)
windows = await maafw.detect_win32hwnd(window_name_input.value)
options = {}
for w in windows:
options[hex(w.hwnd)] = hex(w.hwnd) + " " + w.window_name
Expand Down
10 changes: 6 additions & 4 deletions src/MaaDebugger/webpage/reco_page/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ async def reco_page(reco_id: int):

ui.separator()

details: RecognitionDetail = await maafw.get_reco_detail(reco_id)
details: RecognitionDetail | None = await maafw.get_reco_detail(reco_id)
if not details:
ui.markdown("## Not Found")
return

ui.markdown(f"#### Hit: {str(details.box)}")
ui.markdown(f"#### {details.algorithm}")
# ui.markdown(f"#### Box: {str(details.box)}")
ui.markdown(f"#### {details.best_result}")

for draw in details.draws:
for draw in details.draw_images:
ui.image(cvmat_to_image(draw)).props("fit=scale-down")

ui.json_editor({"content": {"json": details.detail}, "readOnly": True})
ui.json_editor({"content": {"json": details.raw_detail}, "readOnly": True})

0 comments on commit 883a751

Please sign in to comment.