矩形缓冲区 / Rectangle buffer 用于在 Python 和 C API 之间传递矩形数据(x, y, width, height)。 Used to pass rectangle data (x, y, width, height) between Python and C API.
| 532 | |
| 533 | |
| 534 | class RectBuffer: |
| 535 | """矩形缓冲区 / Rectangle buffer |
| 536 | |
| 537 | 用于在 Python 和 C API 之间传递矩形数据(x, y, width, height)。 |
| 538 | Used to pass rectangle data (x, y, width, height) between Python and C API. |
| 539 | """ |
| 540 | |
| 541 | _handle: MaaRectHandle |
| 542 | _own: bool |
| 543 | |
| 544 | def __init__(self, c_handle: Optional[MaaRectHandle] = None): |
| 545 | self._set_api_properties() |
| 546 | |
| 547 | if c_handle: |
| 548 | self._handle = c_handle |
| 549 | self._own = False |
| 550 | else: |
| 551 | self._handle = Library.framework().MaaRectCreate() |
| 552 | self._own = True |
| 553 | |
| 554 | if not self._handle: |
| 555 | raise RuntimeError("Failed to create rect buffer.") |
| 556 | |
| 557 | def __del__(self): |
| 558 | if self._handle and self._own: |
| 559 | Library.framework().MaaRectDestroy(self._handle) |
| 560 | |
| 561 | def get(self) -> Rect: |
| 562 | """获取矩形数据 / Get rectangle data |
| 563 | |
| 564 | Returns: |
| 565 | Rect: 矩形对象 (x, y, width, height) / Rectangle object (x, y, width, height) |
| 566 | """ |
| 567 | x = Library.framework().MaaRectGetX(self._handle) |
| 568 | y = Library.framework().MaaRectGetY(self._handle) |
| 569 | w = Library.framework().MaaRectGetW(self._handle) |
| 570 | h = Library.framework().MaaRectGetH(self._handle) |
| 571 | |
| 572 | return Rect(x, y, w, h) |
| 573 | |
| 574 | def set(self, value: RectType) -> bool: |
| 575 | """设置矩形数据 / Set rectangle data |
| 576 | |
| 577 | Args: |
| 578 | value: 矩形数据,可以是 Rect、tuple、list 或 numpy.ndarray |
| 579 | Rectangle data, can be Rect, tuple, list, or numpy.ndarray |
| 580 | |
| 581 | Returns: |
| 582 | bool: 是否成功 / Whether successful |
| 583 | |
| 584 | Raises: |
| 585 | ValueError: 如果数据格式不正确 |
| 586 | TypeError: 如果类型不支持 |
| 587 | """ |
| 588 | if isinstance(value, numpy.ndarray): # pyright: ignore[reportUnnecessaryIsInstance] |
| 589 | if value.ndim != 1: |
| 590 | raise ValueError("value must be a 1D array") |
| 591 | if value.shape[0] != 4: |
no outgoing calls
no test coverage detected