Skip to content

Commit

Permalink
feat: 新增一些py接口
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Mar 23, 2024
1 parent 5222c64 commit 3bf2d68
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
69 changes: 69 additions & 0 deletions source/binding/Python/maa/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .define import *
from .future import Future
from .library import Library
from .buffer import ImageBuffer

__all__ = [
"AdbController",
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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__(
Expand Down
28 changes: 28 additions & 0 deletions source/binding/Python/maa/library.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 3bf2d68

Please sign in to comment.