control.py 9.5 KB

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