control.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # from module.base.button import Button
  2. from module.base.decorator import cached_property
  3. from module.base.timer import Timer
  4. from module.base.utils import *
  5. # from module.device.method.hermit import Hermit
  6. # from module.device.method.maatouch import MaaTouch
  7. from module.device.method.minitouch import Minitouch
  8. from module.device.method.adb import Adb
  9. from module.device.method.scrcpy import Scrcpy
  10. from module.device.method.window import Window
  11. from module.logger import logger
  12. class Control(Minitouch, Scrcpy, Window):
  13. def handle_control_check(self, button):
  14. # Will be overridden in Device
  15. pass
  16. @cached_property
  17. def click_methods(self):
  18. return {
  19. 'ADB': self.click_adb,
  20. 'uiautomator2': self.click_uiautomator2,
  21. 'minitouch': self.click_minitouch,
  22. 'window_message': self.click_window_message
  23. # 'Hermit': self.click_hermit,
  24. # 'MaaTouch': self.click_maatouch,
  25. }
  26. def click(self, button, control_check=True):
  27. """
  28. 后面改一改 不用用button的逻辑
  29. :param button:
  30. :param control_check:
  31. :return:
  32. """
  33. if control_check:
  34. self.handle_control_check(button)
  35. x, y = random_rectangle_point(button.button)
  36. x, y = ensure_int(x, y)
  37. logger.info(
  38. 'Click %s @ %s' % (point2str(x, y), button)
  39. )
  40. method = self.click_methods.get(
  41. self.config.script.emulator.control_method,
  42. self.click_adb
  43. )
  44. method(x, y)
  45. def multi_click(self, button, n, interval=(0.1, 0.2)):
  46. """
  47. 也是不能用button的逻辑
  48. :param button:
  49. :param n:
  50. :param interval:
  51. :return:
  52. """
  53. self.handle_control_check(button)
  54. click_timer = Timer(0.1)
  55. for _ in range(n):
  56. remain = ensure_time(interval) - click_timer.current()
  57. if remain > 0:
  58. self.sleep(remain)
  59. click_timer.reset()
  60. self.click(button, control_check=False)
  61. def long_click(self, button, duration=(1, 1.2)):
  62. """
  63. :param button:
  64. :param duration:
  65. :return:
  66. """
  67. self.handle_control_check(button)
  68. x, y = random_rectangle_point(button.button)
  69. x, y = ensure_int(x, y)
  70. duration = ensure_time(duration)
  71. logger.info(
  72. 'Click %s @ %s, %s' % (point2str(x, y), button, duration)
  73. )
  74. method = self.config.script.emulator.control_method
  75. if method == 'minitouch':
  76. self.long_click_minitouch(x, y, duration)
  77. elif method == 'window_message':
  78. self.long_click_window_message(x, y, duration)
  79. elif method == 'uiautomator2':
  80. self.long_click_uiautomator2(x, y, duration)
  81. elif method == 'scrcpy':
  82. self.long_click_scrcpy(x, y, duration)
  83. # elif method == 'MaaTouch':
  84. # self.long_click_maatouch(x, y, duration)
  85. else:
  86. self.swipe_adb((x, y), (x, y), duration)
  87. def swipe(self, p1, p2, duration=(0.1, 0.2), name='SWIPE', distance_check=True):
  88. self.handle_control_check(name)
  89. p1, p2 = ensure_int(p1, p2)
  90. duration = ensure_time(duration)
  91. method = self.config.script.emulator.control_method
  92. if method == 'minitouch':
  93. logger.info('Swipe %s -> %s' % (point2str(*p1), point2str(*p2)))
  94. elif method == 'window_message':
  95. logger.info('Swipe %s -> %s' % (point2str(*p1), point2str(*p2)))
  96. elif method == 'uiautomator2':
  97. logger.info('Swipe %s -> %s, %s' % (point2str(*p1), point2str(*p2), duration))
  98. elif method == 'scrcpy':
  99. logger.info('Swipe %s -> %s' % (point2str(*p1), point2str(*p2)))
  100. # elif method == 'MaaTouch':
  101. # logger.info('Swipe %s -> %s' % (point2str(*p1), point2str(*p2)))
  102. else:
  103. # ADB needs to be slow, or swipe doesn't work
  104. duration *= 2.5
  105. logger.info('Swipe %s -> %s, %s' % (point2str(*p1), point2str(*p2), duration))
  106. if distance_check:
  107. if np.linalg.norm(np.subtract(p1, p2)) < 10:
  108. # Should swipe a certain distance, otherwise AL will treat it as click.
  109. # uiautomator2 should >= 6px, minitouch should >= 5px
  110. logger.info('Swipe distance < 10px, dropped')
  111. return
  112. if method == 'minitouch':
  113. self.swipe_minitouch(p1, p2)
  114. elif method == 'window_message':
  115. self.swipe_window_message(p1, p2)
  116. elif method == 'uiautomator2':
  117. self.swipe_uiautomator2(p1, p2, duration=duration)
  118. elif method == 'scrcpy':
  119. self.swipe_scrcpy(p1, p2)
  120. # elif method == 'MaaTouch':
  121. # self.swipe_maatouch(p1, p2)
  122. else:
  123. self.swipe_adb(p1, p2, duration=duration)
  124. def swipe_vector(self, vector, box=(123, 159, 1175, 628), random_range=(0, 0, 0, 0), padding=15,
  125. duration=(0.1, 0.2), whitelist_area=None, blacklist_area=None, name='SWIPE', distance_check=True):
  126. """Method to swipe.
  127. Args:
  128. box (tuple): Swipe in box (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  129. vector (tuple): (x, y).
  130. random_range (tuple): (x_min, y_min, x_max, y_max).
  131. padding (int):
  132. duration (int, float, tuple):
  133. whitelist_area: (list[tuple[int]]):
  134. A list of area that safe to click. Swipe path will end there.
  135. blacklist_area: (list[tuple[int]]):
  136. If none of the whitelist_area satisfies current vector, blacklist_area will be used.
  137. Delete random path that ends in any blacklist_area.
  138. name (str): Swipe name
  139. distance_check: (bool):
  140. """
  141. p1, p2 = random_rectangle_vector_opted(
  142. vector,
  143. box=box,
  144. random_range=random_range,
  145. padding=padding,
  146. whitelist_area=whitelist_area,
  147. blacklist_area=blacklist_area
  148. )
  149. self.swipe(p1, p2, duration=duration, name=name, distance_check=distance_check)
  150. def drag(self, p1, p2, segments=1, shake=(0, 15), point_random=(-10, -10, 10, 10), shake_random=(-5, -5, 5, 5),
  151. swipe_duration=0.25, shake_duration=0.1, name='DRAG'):
  152. self.handle_control_check(name)
  153. p1, p2 = ensure_int(p1, p2)
  154. logger.info(
  155. 'Drag %s -> %s' % (point2str(*p1), point2str(*p2))
  156. )
  157. method = self.config.script.emulator.control_method
  158. if method == 'minitouch':
  159. self.drag_minitouch(p1, p2, point_random=point_random)
  160. elif method == 'uiautomator2':
  161. self.drag_uiautomator2(
  162. p1, p2, segments=segments, shake=shake, point_random=point_random, shake_random=shake_random,
  163. swipe_duration=swipe_duration, shake_duration=shake_duration)
  164. elif method == 'scrcpy':
  165. self.drag_scrcpy(p1, p2, point_random=point_random)
  166. # elif method == 'MaaTouch':
  167. # self.drag_maatouch(p1, p2, point_random=point_random)
  168. else:
  169. logger.warning(f'Control method {method} does not support drag well, '
  170. f'falling back to ADB swipe may cause unexpected behaviour')
  171. self.swipe_adb(p1, p2, duration=ensure_time(swipe_duration * 2))
  172. # self.click(Button(area=(), color=(), button=area_offset(point_random, p2), name=name))