kotonebot 0.5.0__py3-none-any.whl → 0.7.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. kotonebot/__init__.py +39 -39
  2. kotonebot/backend/bot.py +312 -312
  3. kotonebot/backend/color.py +525 -525
  4. kotonebot/backend/context/__init__.py +3 -3
  5. kotonebot/backend/context/context.py +1002 -1002
  6. kotonebot/backend/context/task_action.py +183 -183
  7. kotonebot/backend/core.py +86 -129
  8. kotonebot/backend/debug/entry.py +89 -89
  9. kotonebot/backend/debug/mock.py +78 -78
  10. kotonebot/backend/debug/server.py +222 -222
  11. kotonebot/backend/debug/vars.py +351 -351
  12. kotonebot/backend/dispatch.py +227 -227
  13. kotonebot/backend/flow_controller.py +196 -196
  14. kotonebot/backend/image.py +36 -5
  15. kotonebot/backend/loop.py +222 -208
  16. kotonebot/backend/ocr.py +535 -535
  17. kotonebot/backend/preprocessor.py +103 -103
  18. kotonebot/client/__init__.py +9 -9
  19. kotonebot/client/device.py +369 -529
  20. kotonebot/client/fast_screenshot.py +377 -377
  21. kotonebot/client/host/__init__.py +43 -43
  22. kotonebot/client/host/adb_common.py +101 -107
  23. kotonebot/client/host/custom.py +118 -118
  24. kotonebot/client/host/leidian_host.py +196 -196
  25. kotonebot/client/host/mumu12_host.py +353 -353
  26. kotonebot/client/host/protocol.py +214 -214
  27. kotonebot/client/host/windows_common.py +73 -58
  28. kotonebot/client/implements/__init__.py +65 -70
  29. kotonebot/client/implements/adb.py +89 -89
  30. kotonebot/client/implements/nemu_ipc/__init__.py +11 -11
  31. kotonebot/client/implements/nemu_ipc/external_renderer_ipc.py +284 -284
  32. kotonebot/client/implements/nemu_ipc/nemu_ipc.py +327 -327
  33. kotonebot/client/implements/remote_windows.py +188 -188
  34. kotonebot/client/implements/uiautomator2.py +85 -85
  35. kotonebot/client/implements/windows/__init__.py +1 -0
  36. kotonebot/client/implements/windows/print_window.py +133 -0
  37. kotonebot/client/implements/windows/send_message.py +324 -0
  38. kotonebot/client/implements/{windows.py → windows/windows.py} +175 -176
  39. kotonebot/client/protocol.py +69 -69
  40. kotonebot/client/registration.py +24 -24
  41. kotonebot/client/scaler.py +467 -0
  42. kotonebot/config/base_config.py +103 -96
  43. kotonebot/config/config.py +61 -0
  44. kotonebot/config/manager.py +36 -36
  45. kotonebot/core/__init__.py +13 -0
  46. kotonebot/core/entities/base.py +182 -0
  47. kotonebot/core/entities/compound.py +75 -0
  48. kotonebot/core/entities/ocr.py +117 -0
  49. kotonebot/core/entities/template_match.py +198 -0
  50. kotonebot/devtools/__init__.py +42 -0
  51. kotonebot/devtools/cli/__init__.py +6 -0
  52. kotonebot/devtools/cli/main.py +53 -0
  53. kotonebot/{tools → devtools}/mirror.py +354 -354
  54. kotonebot/devtools/project/project.py +41 -0
  55. kotonebot/devtools/project/scanner.py +202 -0
  56. kotonebot/devtools/project/schema.py +99 -0
  57. kotonebot/devtools/resgen/__init__.py +42 -0
  58. kotonebot/devtools/resgen/codegen.py +331 -0
  59. kotonebot/devtools/resgen/core.py +94 -0
  60. kotonebot/devtools/resgen/parsers.py +360 -0
  61. kotonebot/devtools/resgen/utils.py +158 -0
  62. kotonebot/devtools/resgen/validation.py +115 -0
  63. kotonebot/devtools/web/dist/assets/bootstrap-icons-BOrJxbIo.woff +0 -0
  64. kotonebot/devtools/web/dist/assets/bootstrap-icons-BtvjY1KL.woff2 +0 -0
  65. kotonebot/devtools/web/dist/assets/ext-language_tools-CD021WJ2.js +2577 -0
  66. kotonebot/devtools/web/dist/assets/index-B_m5f2LF.js +2836 -0
  67. kotonebot/devtools/web/dist/assets/index-BlEDyGGa.css +9 -0
  68. kotonebot/devtools/web/dist/assets/language-client-C9muzqaq.js +128 -0
  69. kotonebot/devtools/web/dist/assets/mode-python-CtHp76XS.js +476 -0
  70. kotonebot/devtools/web/dist/icons/symbol-class.svg +3 -0
  71. kotonebot/devtools/web/dist/icons/symbol-file.svg +3 -0
  72. kotonebot/devtools/web/dist/icons/symbol-method.svg +3 -0
  73. kotonebot/devtools/web/dist/index.html +25 -0
  74. kotonebot/devtools/web/server/__init__.py +0 -0
  75. kotonebot/devtools/web/server/rest_api.py +217 -0
  76. kotonebot/devtools/web/server/server.py +85 -0
  77. kotonebot/errors.py +76 -76
  78. kotonebot/interop/win/__init__.py +13 -9
  79. kotonebot/interop/win/_mouse.py +310 -310
  80. kotonebot/interop/win/message_box.py +313 -313
  81. kotonebot/interop/win/reg.py +37 -37
  82. kotonebot/interop/win/shake_mouse.py +224 -0
  83. kotonebot/interop/win/shortcut.py +43 -43
  84. kotonebot/interop/win/task_dialog.py +513 -513
  85. kotonebot/interop/win/window.py +89 -0
  86. kotonebot/logging/__init__.py +2 -2
  87. kotonebot/logging/log.py +17 -17
  88. kotonebot/primitives/__init__.py +19 -17
  89. kotonebot/primitives/geometry.py +1067 -862
  90. kotonebot/primitives/visual.py +143 -63
  91. kotonebot/ui/file_host/sensio.py +36 -36
  92. kotonebot/ui/file_host/tmp_send.py +54 -54
  93. kotonebot/ui/pushkit/__init__.py +3 -3
  94. kotonebot/ui/pushkit/image_host.py +88 -88
  95. kotonebot/ui/pushkit/protocol.py +13 -13
  96. kotonebot/ui/pushkit/wxpusher.py +54 -54
  97. kotonebot/ui/user.py +148 -148
  98. kotonebot/util.py +436 -436
  99. {kotonebot-0.5.0.dist-info → kotonebot-0.7.0.dist-info}/METADATA +84 -82
  100. kotonebot-0.7.0.dist-info/RECORD +109 -0
  101. {kotonebot-0.5.0.dist-info → kotonebot-0.7.0.dist-info}/WHEEL +1 -1
  102. kotonebot-0.7.0.dist-info/entry_points.txt +2 -0
  103. {kotonebot-0.5.0.dist-info → kotonebot-0.7.0.dist-info}/licenses/LICENSE +673 -673
  104. kotonebot/client/implements/adb_raw.py +0 -163
  105. kotonebot-0.5.0.dist-info/RECORD +0 -71
  106. /kotonebot/{tools → devtools/project}/__init__.py +0 -0
  107. {kotonebot-0.5.0.dist-info → kotonebot-0.7.0.dist-info}/top_level.txt +0 -0
@@ -1,311 +1,311 @@
1
- import math
2
- import time
3
- from typing import Callable, Literal, TypedDict, overload, Tuple
4
-
5
- import mouse
6
- from typing_extensions import Unpack
7
-
8
- from kotonebot.primitives import Point
9
-
10
- MouseButton = Literal['left', 'right', 'middle']
11
- TweenFunc = Callable[[float], float]
12
- Tween = Literal['linear', 'ease_in', 'ease_out', 'ease_in_out'] | TweenFunc
13
-
14
- # https://stackoverflow.com/a/76554895
15
- def high_precision_sleep(duration):
16
- start_time = time.perf_counter()
17
- while True:
18
- elapsed_time = time.perf_counter() - start_time
19
- remaining_time = duration - elapsed_time
20
- if remaining_time <= 0:
21
- break
22
- if remaining_time > 0.02: # Sleep for 5ms if remaining time is greater
23
- time.sleep(max(remaining_time/2, 0.0001)) # Sleep for the remaining time or minimum sleep interval
24
- else:
25
- pass
26
-
27
- class AnimationParams(TypedDict, total=False):
28
- duration: float
29
- """动画持续时间,单位为秒。
30
-
31
- 动画实际持续时间可能会略大于此值,具体取决于系统以及 delay_func 的实现。
32
- """
33
- speed: float
34
- """动画速度,单位为像素/秒。"""
35
- steps: int
36
- """动画步数。"""
37
- tween: Tween
38
- """插值函数。
39
-
40
- 可选 'linear', 'ease_in', 'ease_out', 'ease_in_out',默认为 'ease_in_out'。
41
- 也可以是一个函数,其输入为动画进度,输出为动画值。
42
- """
43
- delay_func: Callable[[float], None] | None
44
- """延时函数。默认为 time.sleep。
45
-
46
- 可选,如果提供了此参数,则会在每个动画点之间调用此函数。
47
- """
48
- user_interrupt: Callable[[], bool | None] | Literal[True] | None
49
- """可选,用户中断函数,默认为 None。
50
-
51
- 若提供此参数,那么在动画执行时会检测用户输入,如果用户尝试移动鼠标,
52
- 会自动终止动画(传入 True)或调用此函数(传入 Callable,根据返回值决定是否继续)。
53
- """
54
- user_interrupt_threshold: float | None
55
- """可选,用户中断阈值,单位为像素,默认为 None,表示使用全局参数。
56
-
57
- 如果提供此参数,则在检测用户中断时,仅当移动鼠标的距离大于此值时才会触发终止。
58
- """
59
-
60
- default_speed = 3000
61
- animation_args: AnimationParams = {
62
- 'steps': 100,
63
- 'tween': 'ease_in_out',
64
- 'delay_func': high_precision_sleep,
65
- 'user_interrupt': None,
66
- 'user_interrupt_threshold': 30,
67
- }
68
- """全局动画参数。"""
69
-
70
- # https://easings.net
71
- def _tween_linear(t: float) -> float:
72
- return t
73
-
74
- def _tween_ease_in(t: float) -> float:
75
- return t * t
76
-
77
- def _tween_ease_out(t: float) -> float:
78
- return t * (2 - t)
79
-
80
- def _tween_ease_in_out(t: float) -> float:
81
- return t * t * (3 - 2 * t)
82
-
83
- _TWEEN_FUNCTIONS = {
84
- 'linear': _tween_linear,
85
- 'ease_in': _tween_ease_in,
86
- 'ease_out': _tween_ease_out,
87
- 'ease_in_out': _tween_ease_in_out,
88
- }
89
-
90
- def _get_animated_points(start_point: Point, end_point: Point, steps: int, tween: Tween):
91
- if isinstance(tween, str):
92
- tween_func = _TWEEN_FUNCTIONS.get(tween, _tween_linear)
93
- else:
94
- tween_func = tween
95
- for i in range(steps + 1):
96
- progress = i / steps
97
- eased_progress = tween_func(progress)
98
- x = start_point.x + (end_point.x - start_point.x) * eased_progress
99
- y = start_point.y + (end_point.y - start_point.y) * eased_progress
100
- yield Point(int(x), int(y))
101
-
102
- def do_tween(start: Point, end: Point, args: AnimationParams, *, skip_first: bool = True):
103
- """从起点到终点根据输入的动画参数进行插值,并自动进行延时。
104
-
105
- :param start: 开始位置。
106
- :param end: 结束位置。
107
- :param args: 动画参数,详见 :class:`.AnimationParams`。
108
- :param skip_first: 是否跳过第一个点。默认为 True。
109
- :raises ValueError: 输入的 speed 为非正数时抛出。
110
- :raises ValueError: 同时提供 speed 与 duration 参数时抛出。
111
- :return: 一个迭代器,包含所有插值的中间点。
112
- """
113
- duration = args.get('duration')
114
- speed = args.get('speed')
115
- steps = args.get('steps', animation_args.get('steps'))
116
- tween = args.get('tween', animation_args.get('tween'))
117
- delay_func = args.get('delay_func', animation_args.get('delay_func'))
118
- user_interrupt = args.get('user_interrupt', animation_args.get('user_interrupt'))
119
- user_interrupt_threshold = args.get('user_interrupt_threshold', animation_args.get('user_interrupt_threshold'))
120
- assert steps is not None and tween is not None and delay_func is not None
121
-
122
- def _speed_to_duration(speed: float) -> float:
123
- return math.sqrt((end.x - start.x)**2 + (end.y - start.y)**2) / speed
124
-
125
- if duration is None and speed is not None:
126
- # 设置了速度,但没有设置时长,则计算时长
127
- if speed <= 0:
128
- raise ValueError('speed must be positive')
129
- duration = _speed_to_duration(speed)
130
- elif duration is not None and speed is None:
131
- # 设置了时长,但没有设置速度,则计算速度
132
- pass
133
- elif duration is not None and speed is not None:
134
- # 两个都设置
135
- raise ValueError('duration and speed cannot be set at the same time')
136
- else:
137
- # 两个都没有设置,使用默认速度
138
- duration = _speed_to_duration(default_speed)
139
-
140
- delay = duration / steps if steps > 0 else 0
141
- print(duration, steps, delay)
142
-
143
- point_iterator = _get_animated_points(start, end, steps, tween)
144
- if skip_first:
145
- next(point_iterator, None)
146
-
147
- # 调用函数 (drag/move) 负责设置鼠标位置。
148
- # 在每次迭代中,我们检查当前鼠标位置是否与我们在“上一次”迭代中生成的位置匹配。
149
- # 我们无法在此处知道初始鼠标位置,因此我们从第二个点开始检查。
150
-
151
- iterator = iter(point_iterator)
152
- try:
153
- prev_pos = next(iterator)
154
- yield prev_pos
155
- delay_func(delay)
156
- except StopIteration:
157
- return
158
-
159
- for pt in iterator:
160
- # 检测用户中断
161
- if user_interrupt:
162
- pos = get_pos()
163
- should_interrupt = False
164
- if user_interrupt_threshold is not None:
165
- if pos.distance_to(prev_pos) > user_interrupt_threshold:
166
- should_interrupt = True
167
- else:
168
- if pos != prev_pos:
169
- should_interrupt = True
170
-
171
- if should_interrupt:
172
- # 鼠标被用户移动,中断动画。
173
- if user_interrupt is True:
174
- return
175
- if callable(user_interrupt):
176
- if not user_interrupt():
177
- return
178
-
179
- yield pt
180
- prev_pos = pt
181
- delay_func(delay)
182
-
183
- @overload
184
- def set_pos(p: Point) -> None:
185
- """移动光标到指定位置。
186
-
187
- :param p: 坐标,Point 实例。
188
- """
189
-
190
- @overload
191
- def set_pos(p: Tuple[int, int]) -> None:
192
- """移动光标到指定位置。
193
-
194
- :param p: 坐标,二元 tuple[int, int]。
195
- """
196
-
197
- @overload
198
- def set_pos(p: int, y: int) -> None:
199
- """移动光标到指定位置。
200
-
201
- :param p: 坐标,x 坐标。
202
- :param y: 坐标,y 坐标。
203
- """
204
-
205
- def set_pos(p, y: int | None = None):
206
- if y is None:
207
- # 可能是 Point 或二元 tuple
208
- if isinstance(p, Point):
209
- x = int(p.x)
210
- y = int(p.y)
211
- else:
212
- # 假定为 (x, y)
213
- _x, _y = p
214
- x = int(_x)
215
- y = int(_y)
216
- else:
217
- x = int(p)
218
- y = int(y)
219
-
220
- mouse.move(x, y)
221
-
222
-
223
- def get_pos() -> Point:
224
- x, y = mouse.get_position()
225
- return Point(x, y)
226
-
227
-
228
- def down(button: MouseButton):
229
- mouse.press(button)
230
-
231
-
232
- def up(button: MouseButton):
233
- mouse.release(button)
234
-
235
-
236
- def click(button: MouseButton, *, duration: float = 0.1):
237
- """模拟鼠标点击。
238
-
239
- :param button: 必填,鼠标按钮。
240
- :param duration: 可选,点击持续时间。默认为 0.1。
241
- """
242
- down(button)
243
- time.sleep(duration)
244
- up(button)
245
-
246
-
247
- def drag(
248
- start: Point,
249
- end: Point,
250
- *,
251
- button: MouseButton | None = 'left',
252
- **kargs: Unpack[AnimationParams],
253
- ):
254
- """模拟鼠标拖拽。
255
- 参数中与动画相关的部分详见 :class:`.AnimationParams`。
256
-
257
- :param start: 必填,起点。
258
- :param end: 必填,终点。
259
- :param button: 可选,拖拽使用的鼠标按钮。默认为 `left`。None 表示不按下任何鼠标按钮,相当于只移动光标。
260
- :param duration: 可选,动画持续时间。
261
- :param speed: 可选,动画速度。
262
- :param steps: 可选,动画步数。
263
- :param tween: 可选,动画曲线。
264
- :param delay_func: 可选,延时函数。详见 :class:`.AnimationParams`。
265
- :param user_interrupt: 可选,用户中断函数。详见 :class:`.AnimationParams`。
266
- :param user_interrupt_threshold: 可选,用户中断阈值。详见 :class:`.AnimationParams`。
267
- """
268
- if button:
269
- set_pos(start)
270
- time.sleep(0.02)
271
- down(button)
272
- time.sleep(0.02)
273
-
274
- try:
275
- for p in do_tween(start, end, kargs):
276
- set_pos(p)
277
- pass
278
- finally:
279
- if button:
280
- up(button)
281
-
282
- def move(
283
- start: Point,
284
- end: Point,
285
- /,
286
- **kargs: Unpack[AnimationParams],
287
- ):
288
- """模拟鼠标移动。
289
- 参数中与动画相关的部分详见 :class:`kotonebot.interop.win.AnimationParams`。
290
-
291
- :param start: 必填,起点。
292
- :param end: 必填,终点。
293
- :param duration: 可选,动画持续时间。
294
- :param speed: 可选,动画速度。
295
- :param steps: 可选,动画步数。
296
- :param tween: 可选,动画曲线。
297
- :param delay_func: 可选,延时函数。详见 :class:`.AnimationParams`。
298
- :param user_interrupt: 可选,用户中断函数。详见 :class:`.AnimationParams`。
299
- :param user_interrupt_threshold: 可选,用户中断阈值。详见 :class:`.AnimationParams`。
300
- """
301
- drag(start, end, button=None, **kargs)
302
-
303
- __all__ = [
304
- 'set_pos',
305
- 'get_pos',
306
- 'down',
307
- 'up',
308
- 'click',
309
- 'drag',
310
- 'move',
1
+ import math
2
+ import time
3
+ from typing import Callable, Literal, TypedDict, overload, Tuple
4
+
5
+ import mouse
6
+ from typing_extensions import Unpack
7
+
8
+ from kotonebot.primitives import Point
9
+
10
+ MouseButton = Literal['left', 'right', 'middle']
11
+ TweenFunc = Callable[[float], float]
12
+ Tween = Literal['linear', 'ease_in', 'ease_out', 'ease_in_out'] | TweenFunc
13
+
14
+ # https://stackoverflow.com/a/76554895
15
+ def high_precision_sleep(duration):
16
+ start_time = time.perf_counter()
17
+ while True:
18
+ elapsed_time = time.perf_counter() - start_time
19
+ remaining_time = duration - elapsed_time
20
+ if remaining_time <= 0:
21
+ break
22
+ if remaining_time > 0.02: # Sleep for 5ms if remaining time is greater
23
+ time.sleep(max(remaining_time/2, 0.0001)) # Sleep for the remaining time or minimum sleep interval
24
+ else:
25
+ pass
26
+
27
+ class AnimationParams(TypedDict, total=False):
28
+ duration: float
29
+ """动画持续时间,单位为秒。
30
+
31
+ 动画实际持续时间可能会略大于此值,具体取决于系统以及 delay_func 的实现。
32
+ """
33
+ speed: float
34
+ """动画速度,单位为像素/秒。"""
35
+ steps: int
36
+ """动画步数。"""
37
+ tween: Tween
38
+ """插值函数。
39
+
40
+ 可选 'linear', 'ease_in', 'ease_out', 'ease_in_out',默认为 'ease_in_out'。
41
+ 也可以是一个函数,其输入为动画进度,输出为动画值。
42
+ """
43
+ delay_func: Callable[[float], None] | None
44
+ """延时函数。默认为 time.sleep。
45
+
46
+ 可选,如果提供了此参数,则会在每个动画点之间调用此函数。
47
+ """
48
+ user_interrupt: Callable[[], bool | None] | Literal[True] | None
49
+ """可选,用户中断函数,默认为 None。
50
+
51
+ 若提供此参数,那么在动画执行时会检测用户输入,如果用户尝试移动鼠标,
52
+ 会自动终止动画(传入 True)或调用此函数(传入 Callable,根据返回值决定是否继续)。
53
+ """
54
+ user_interrupt_threshold: float | None
55
+ """可选,用户中断阈值,单位为像素,默认为 None,表示使用全局参数。
56
+
57
+ 如果提供此参数,则在检测用户中断时,仅当移动鼠标的距离大于此值时才会触发终止。
58
+ """
59
+
60
+ default_speed = 3000
61
+ animation_args: AnimationParams = {
62
+ 'steps': 100,
63
+ 'tween': 'ease_in_out',
64
+ 'delay_func': high_precision_sleep,
65
+ 'user_interrupt': None,
66
+ 'user_interrupt_threshold': 30,
67
+ }
68
+ """全局动画参数。"""
69
+
70
+ # https://easings.net
71
+ def _tween_linear(t: float) -> float:
72
+ return t
73
+
74
+ def _tween_ease_in(t: float) -> float:
75
+ return t * t
76
+
77
+ def _tween_ease_out(t: float) -> float:
78
+ return t * (2 - t)
79
+
80
+ def _tween_ease_in_out(t: float) -> float:
81
+ return t * t * (3 - 2 * t)
82
+
83
+ _TWEEN_FUNCTIONS = {
84
+ 'linear': _tween_linear,
85
+ 'ease_in': _tween_ease_in,
86
+ 'ease_out': _tween_ease_out,
87
+ 'ease_in_out': _tween_ease_in_out,
88
+ }
89
+
90
+ def _get_animated_points(start_point: Point, end_point: Point, steps: int, tween: Tween):
91
+ if isinstance(tween, str):
92
+ tween_func = _TWEEN_FUNCTIONS.get(tween, _tween_linear)
93
+ else:
94
+ tween_func = tween
95
+ for i in range(steps + 1):
96
+ progress = i / steps
97
+ eased_progress = tween_func(progress)
98
+ x = start_point.x + (end_point.x - start_point.x) * eased_progress
99
+ y = start_point.y + (end_point.y - start_point.y) * eased_progress
100
+ yield Point(int(x), int(y))
101
+
102
+ def do_tween(start: Point, end: Point, args: AnimationParams, *, skip_first: bool = True):
103
+ """从起点到终点根据输入的动画参数进行插值,并自动进行延时。
104
+
105
+ :param start: 开始位置。
106
+ :param end: 结束位置。
107
+ :param args: 动画参数,详见 :class:`.AnimationParams`。
108
+ :param skip_first: 是否跳过第一个点。默认为 True。
109
+ :raises ValueError: 输入的 speed 为非正数时抛出。
110
+ :raises ValueError: 同时提供 speed 与 duration 参数时抛出。
111
+ :return: 一个迭代器,包含所有插值的中间点。
112
+ """
113
+ duration = args.get('duration')
114
+ speed = args.get('speed')
115
+ steps = args.get('steps', animation_args.get('steps'))
116
+ tween = args.get('tween', animation_args.get('tween'))
117
+ delay_func = args.get('delay_func', animation_args.get('delay_func'))
118
+ user_interrupt = args.get('user_interrupt', animation_args.get('user_interrupt'))
119
+ user_interrupt_threshold = args.get('user_interrupt_threshold', animation_args.get('user_interrupt_threshold'))
120
+ assert steps is not None and tween is not None and delay_func is not None
121
+
122
+ def _speed_to_duration(speed: float) -> float:
123
+ return math.sqrt((end.x - start.x)**2 + (end.y - start.y)**2) / speed
124
+
125
+ if duration is None and speed is not None:
126
+ # 设置了速度,但没有设置时长,则计算时长
127
+ if speed <= 0:
128
+ raise ValueError('speed must be positive')
129
+ duration = _speed_to_duration(speed)
130
+ elif duration is not None and speed is None:
131
+ # 设置了时长,但没有设置速度,则计算速度
132
+ pass
133
+ elif duration is not None and speed is not None:
134
+ # 两个都设置
135
+ raise ValueError('duration and speed cannot be set at the same time')
136
+ else:
137
+ # 两个都没有设置,使用默认速度
138
+ duration = _speed_to_duration(default_speed)
139
+
140
+ delay = duration / steps if steps > 0 else 0
141
+ print(duration, steps, delay)
142
+
143
+ point_iterator = _get_animated_points(start, end, steps, tween)
144
+ if skip_first:
145
+ next(point_iterator, None)
146
+
147
+ # 调用函数 (drag/move) 负责设置鼠标位置。
148
+ # 在每次迭代中,我们检查当前鼠标位置是否与我们在“上一次”迭代中生成的位置匹配。
149
+ # 我们无法在此处知道初始鼠标位置,因此我们从第二个点开始检查。
150
+
151
+ iterator = iter(point_iterator)
152
+ try:
153
+ prev_pos = next(iterator)
154
+ yield prev_pos
155
+ delay_func(delay)
156
+ except StopIteration:
157
+ return
158
+
159
+ for pt in iterator:
160
+ # 检测用户中断
161
+ if user_interrupt:
162
+ pos = get_pos()
163
+ should_interrupt = False
164
+ if user_interrupt_threshold is not None:
165
+ if pos.distance_to(prev_pos) > user_interrupt_threshold:
166
+ should_interrupt = True
167
+ else:
168
+ if pos != prev_pos:
169
+ should_interrupt = True
170
+
171
+ if should_interrupt:
172
+ # 鼠标被用户移动,中断动画。
173
+ if user_interrupt is True:
174
+ return
175
+ if callable(user_interrupt):
176
+ if not user_interrupt():
177
+ return
178
+
179
+ yield pt
180
+ prev_pos = pt
181
+ delay_func(delay)
182
+
183
+ @overload
184
+ def set_pos(p: Point) -> None:
185
+ """移动光标到指定位置。
186
+
187
+ :param p: 坐标,Point 实例。
188
+ """
189
+
190
+ @overload
191
+ def set_pos(p: Tuple[int, int]) -> None:
192
+ """移动光标到指定位置。
193
+
194
+ :param p: 坐标,二元 tuple[int, int]。
195
+ """
196
+
197
+ @overload
198
+ def set_pos(p: int, y: int) -> None:
199
+ """移动光标到指定位置。
200
+
201
+ :param p: 坐标,x 坐标。
202
+ :param y: 坐标,y 坐标。
203
+ """
204
+
205
+ def set_pos(p, y: int | None = None):
206
+ if y is None:
207
+ # 可能是 Point 或二元 tuple
208
+ if isinstance(p, Point):
209
+ x = int(p.x)
210
+ y = int(p.y)
211
+ else:
212
+ # 假定为 (x, y)
213
+ _x, _y = p
214
+ x = int(_x)
215
+ y = int(_y)
216
+ else:
217
+ x = int(p)
218
+ y = int(y)
219
+
220
+ mouse.move(x, y)
221
+
222
+
223
+ def get_pos() -> Point:
224
+ x, y = mouse.get_position()
225
+ return Point(x, y)
226
+
227
+
228
+ def down(button: MouseButton):
229
+ mouse.press(button)
230
+
231
+
232
+ def up(button: MouseButton):
233
+ mouse.release(button)
234
+
235
+
236
+ def click(button: MouseButton, *, duration: float = 0.1):
237
+ """模拟鼠标点击。
238
+
239
+ :param button: 必填,鼠标按钮。
240
+ :param duration: 可选,点击持续时间。默认为 0.1。
241
+ """
242
+ down(button)
243
+ time.sleep(duration)
244
+ up(button)
245
+
246
+
247
+ def drag(
248
+ start: Point,
249
+ end: Point,
250
+ *,
251
+ button: MouseButton | None = 'left',
252
+ **kargs: Unpack[AnimationParams],
253
+ ):
254
+ """模拟鼠标拖拽。
255
+ 参数中与动画相关的部分详见 :class:`.AnimationParams`。
256
+
257
+ :param start: 必填,起点。
258
+ :param end: 必填,终点。
259
+ :param button: 可选,拖拽使用的鼠标按钮。默认为 `left`。None 表示不按下任何鼠标按钮,相当于只移动光标。
260
+ :param duration: 可选,动画持续时间。
261
+ :param speed: 可选,动画速度。
262
+ :param steps: 可选,动画步数。
263
+ :param tween: 可选,动画曲线。
264
+ :param delay_func: 可选,延时函数。详见 :class:`.AnimationParams`。
265
+ :param user_interrupt: 可选,用户中断函数。详见 :class:`.AnimationParams`。
266
+ :param user_interrupt_threshold: 可选,用户中断阈值。详见 :class:`.AnimationParams`。
267
+ """
268
+ if button:
269
+ set_pos(start)
270
+ time.sleep(0.02)
271
+ down(button)
272
+ time.sleep(0.02)
273
+
274
+ try:
275
+ for p in do_tween(start, end, kargs):
276
+ set_pos(p)
277
+ pass
278
+ finally:
279
+ if button:
280
+ up(button)
281
+
282
+ def move(
283
+ start: Point,
284
+ end: Point,
285
+ /,
286
+ **kargs: Unpack[AnimationParams],
287
+ ):
288
+ """模拟鼠标移动。
289
+ 参数中与动画相关的部分详见 :class:`kotonebot.interop.win.AnimationParams`。
290
+
291
+ :param start: 必填,起点。
292
+ :param end: 必填,终点。
293
+ :param duration: 可选,动画持续时间。
294
+ :param speed: 可选,动画速度。
295
+ :param steps: 可选,动画步数。
296
+ :param tween: 可选,动画曲线。
297
+ :param delay_func: 可选,延时函数。详见 :class:`.AnimationParams`。
298
+ :param user_interrupt: 可选,用户中断函数。详见 :class:`.AnimationParams`。
299
+ :param user_interrupt_threshold: 可选,用户中断阈值。详见 :class:`.AnimationParams`。
300
+ """
301
+ drag(start, end, button=None, **kargs)
302
+
303
+ __all__ = [
304
+ 'set_pos',
305
+ 'get_pos',
306
+ 'down',
307
+ 'up',
308
+ 'click',
309
+ 'drag',
310
+ 'move',
311
311
  ]