control.py 9.4 KB

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