|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from typing import Union, Tuple |
| 4 | + |
| 5 | +from .driver import Driver |
| 6 | +from .proto import SwipeDirection |
| 7 | + |
| 8 | + |
| 9 | +class SwipeExt(object): |
| 10 | + def __init__(self, d: Driver): |
| 11 | + self._d = d |
| 12 | + |
| 13 | + def __call__(self, |
| 14 | + direction: Union[SwipeDirection, str], |
| 15 | + scale: float = 0.8, |
| 16 | + box: Union[Tuple, None] = None, |
| 17 | + speed=2000): |
| 18 | + """ |
| 19 | + Args: |
| 20 | + direction (str): one of "left", "right", "up", "bottom" or SwipeDirection.LEFT |
| 21 | + scale (float): percent of swipe, range (0, 1.0] |
| 22 | + box (Tuple): None or (x1, x1, y1, x2, y2) |
| 23 | + speed (int, optional): The swipe speed in pixels per second. Default is 2000. Range: 200-40000. If not within the range, set to default value of 2000. |
| 24 | + Raises: |
| 25 | + ValueError |
| 26 | + """ |
| 27 | + def _swipe(_from, _to): |
| 28 | + self._d.swipe(_from[0], _from[1], _to[0], _to[1], speed=speed) |
| 29 | + |
| 30 | + if scale <= 0 or scale > 1.0 or not isinstance(scale, (float, int)): |
| 31 | + raise ValueError("scale must be in range (0, 1.0]") |
| 32 | + |
| 33 | + if box: |
| 34 | + x1, y1, x2, y2 = self._validate_and_convert_box(box) |
| 35 | + else: |
| 36 | + x1, y1 = 0, 0 |
| 37 | + x2, y2 = self._d.display_size |
| 38 | + |
| 39 | + width, height = x2 - x1, y2 - y1 |
| 40 | + |
| 41 | + h_offset = int(width * (1 - scale) / 2) |
| 42 | + v_offset = int(height * (1 - scale) / 2) |
| 43 | + |
| 44 | + if direction == SwipeDirection.LEFT: |
| 45 | + start = (x2 - h_offset, y1 + height // 2) |
| 46 | + end = (x1 + h_offset, y1 + height // 2) |
| 47 | + elif direction == SwipeDirection.RIGHT: |
| 48 | + start = (x1 + h_offset, y1 + height // 2) |
| 49 | + end = (x2 - h_offset, y1 + height // 2) |
| 50 | + elif direction == SwipeDirection.UP: |
| 51 | + start = (x1 + width // 2, y2 - v_offset) |
| 52 | + end = (x1 + width // 2, y1 + v_offset) |
| 53 | + elif direction == SwipeDirection.DOWN: |
| 54 | + start = (x1 + width // 2, y1 + v_offset) |
| 55 | + end = (x1 + width // 2, y2 - v_offset) |
| 56 | + else: |
| 57 | + raise ValueError("Unknown SwipeDirection:", direction) |
| 58 | + |
| 59 | + _swipe(start, end) |
| 60 | + |
| 61 | + def _validate_and_convert_box(self, box: Tuple) -> Tuple[int, int, int, int]: |
| 62 | + """ |
| 63 | + Validate and convert the box coordinates if necessay. |
| 64 | +
|
| 65 | + Args: |
| 66 | + box (Tuple): The box coordinates as a tuple (x1, y1, x2, y2). |
| 67 | +
|
| 68 | + Returns: |
| 69 | + Tuple[int, int, int, int]: The validated and converted box coordinates. |
| 70 | + """ |
| 71 | + if not isinstance(box, tuple) or len(box) != 4: |
| 72 | + raise ValueError("Box must be a tuple of length 4.") |
| 73 | + x1, y1, x2, y2 = box |
| 74 | + if not (x1 >= 0 and y1 >= 0 and x2 > 0 and y2 > 0): |
| 75 | + raise ValueError("Box coordinates must be greater than 0.") |
| 76 | + if not (x1 < x2 and y1 < y2): |
| 77 | + raise ValueError("Box coordinates must satisfy x1 < x2 and y1 < y2.") |
| 78 | + |
| 79 | + from .driver import Point |
| 80 | + p1: Point = self._d._to_abs_pos(x1, y1) |
| 81 | + p2: Point = self._d._to_abs_pos(x2, y2) |
| 82 | + x1, y1, x2, y2 = p1.x, p1.y, p2.x, p2.y |
| 83 | + |
| 84 | + return x1, y1, x2, y2 |
0 commit comments