feat: add state loop methods for screenshots and hierarchy dumping

This commit is contained in:
2026-07-06 22:40:53 +08:00
parent 30b77d2dbe
commit 3d32cccc7e
+59
View File
@@ -69,6 +69,65 @@ class ModuleBase:
pool = ThreadPoolExecutor(1)
return pool
def loop(self, skip_first=True):
"""
A syntactic sugar to start a state loop
Args:
skip_first (bool): Usually to be True to reuse the previous screenshot
Yields:
np.ndarray: screenshot
Examples:
for _ in self.loop():
if self.appear(...):
break
if self.appear_then_click(...):
continue
"""
while 1:
if skip_first:
skip_first = False
else:
self.device.screenshot()
yield self.device.image
def loop_hierarchy(self, skip_first=True):
"""
A syntactic sugar to start a hierarchy state loop
Args:
skip_first (bool): Usually to be True to reuse the previous hierarchy
Yields:
etree._Element: hierarchy
"""
while 1:
if skip_first:
skip_first = False
else:
self.device.dump_hierarchy()
yield self.device.hierarchy
def loop_screenshot_hierarchy(self, skip_first=True):
"""
A syntactic sugar to start a state loop that takes screenshots and dump hierarchy
Args:
skip_first (bool): Usually to be True to reuse the previous screenshot
Yields:
tuple[np.ndarray, etree._Element]: screenshot, hierarchy
"""
while 1:
if skip_first:
skip_first = False
else:
self.device.screenshot()
self.device.dump_hierarchy()
yield self.device.image, self.device.hierarchy
def match_template(self, button, interval=0, similarity=0.85):
"""
Args: