From 3bf2d682ae0b4875b9d357793a557440b02d3b6f Mon Sep 17 00:00:00 2001 From: MistEO Date: Fri, 22 Mar 2024 18:18:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E4=B8=80=E4=BA=9Bpy?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/binding/Python/maa/controller.py | 69 +++++++++++++++++++++++++ source/binding/Python/maa/library.pyi | 28 ++++++++++ 2 files changed, 97 insertions(+) diff --git a/source/binding/Python/maa/controller.py b/source/binding/Python/maa/controller.py index e6e694a7d..d201fe5cf 100644 --- a/source/binding/Python/maa/controller.py +++ b/source/binding/Python/maa/controller.py @@ -9,6 +9,7 @@ from .define import * from .future import Future from .library import Library +from .buffer import ImageBuffer __all__ = [ "AdbController", @@ -73,6 +74,34 @@ def connected(self) -> bool: return bool(Library.framework.MaaControllerConnected(self._handle)) + async def screencap(self) -> Optional[numpy.ndarray]: + """ + screencap. + + :return: image + """ + if not await self.post_screencap().wait(): + return None + + image_buffer = ImageBuffer() + ret = Library.framework.MaaControllerGetImage( + self._handle, image_buffer.c_handle + ) + if not ret: + return None + return image_buffer.get() + + def post_screencap(self) -> Future: + maaid = Library.framework.MaaControllerPostScreencap(self._handle) + return Future(maaid, self._status) + + async def click(self, x: int, y: int) -> bool: + return await self.post_click(x, y).wait() + + def post_click(self, x: int, y: int) -> Future: + maaid = Library.framework.MaaControllerPostClick(self._handle, x, y) + return Future(maaid, self._status) + def _status(self, maaid: int) -> MaaStatus: return Library.framework.MaaControllerStatus(self._handle, maaid) @@ -110,6 +139,46 @@ def _set_api_properties(): Library.framework.MaaControllerConnected.restype = MaaBool Library.framework.MaaControllerConnected.argtypes = [MaaControllerHandle] + Library.framework.MaaControllerPostClick.restype = MaaCtrlId + Library.framework.MaaControllerPostClick.argtypes = [ + MaaControllerHandle, + c_int32, + c_int32, + ] + + Library.framework.MaaControllerPostSwipe.restype = MaaCtrlId + Library.framework.MaaControllerPostSwipe.argtypes = [ + MaaControllerHandle, + c_int32, + c_int32, + c_int32, + c_int32, + c_int32, + ] + + Library.framework.MaaControllerPostPressKey.restype = MaaCtrlId + Library.framework.MaaControllerPostPressKey.argtypes = [ + MaaControllerHandle, + c_int32, + ] + + Library.framework.MaaControllerPostInputText.restype = MaaCtrlId + Library.framework.MaaControllerPostInputText.argtypes = [ + MaaControllerHandle, + MaaStringView, + ] + + Library.framework.MaaControllerPostScreencap.restype = MaaCtrlId + Library.framework.MaaControllerPostScreencap.argtypes = [ + MaaControllerHandle, + ] + + Library.framework.MaaControllerGetImage.restype = MaaBool + Library.framework.MaaControllerGetImage.argtypes = [ + MaaControllerHandle, + MaaImageBufferHandle, + ] + class AdbController(Controller): def __init__( diff --git a/source/binding/Python/maa/library.pyi b/source/binding/Python/maa/library.pyi index c669b30cd..d785f64f2 100644 --- a/source/binding/Python/maa/library.pyi +++ b/source/binding/Python/maa/library.pyi @@ -180,6 +180,34 @@ class _Framework: ) -> MaaStatus: ... @staticmethod def MaaControllerConnected(ctrl: MaaControllerHandle) -> MaaBool: ... + @staticmethod + def MaaControllerPostClick( + ctrl: MaaControllerHandle, x: c_int32, y: c_int32 + ) -> MaaCtrlId: ... + @staticmethod + def MaaControllerPostSwipe( + ctrl: MaaControllerHandle, + x1: c_int32, + y1: c_int32, + x2: c_int32, + y2: c_int32, + duration: c_int32, + ) -> MaaCtrlId: ... + @staticmethod + def MaaControllerPostPressKey( + ctrl: MaaControllerHandle, keycode: c_int32 + ) -> MaaCtrlId: ... + @staticmethod + def MaaControllerPostInputText( + ctrl: MaaControllerHandle, text: MaaStringView + ) -> MaaCtrlId: ... + @staticmethod + def MaaControllerPostScreencap(ctrl: MaaControllerHandle) -> MaaCtrlId: ... + @staticmethod + def MaaControllerGetImage( + ctrl: MaaControllerHandle, + out_image: MaaImageBufferHandle, + ) -> MaaBool: ... # AdbController @staticmethod def MaaAdbControllerCreateV2(