Skip to content

Commit be41b7c

Browse files
authored
Added find_all(as_list=True) + set_current_element (#17)
* Added find_all(as_list=True) + set_current_element Optionally allows the find_all method to return a list with the found elements instead of a generator. Pending: (Docs): Returns should be changed to reflect the new return possibility? * Update bot.py * Update bot.py * Typo + relative import for Box * Typo
1 parent b7e64b7 commit be41b7c

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

botcity/web/bot.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,19 @@ def find_until(self, label, x=None, y=None, width=None, height=None, *,
496496
self.state.element = ele
497497
return ele
498498

499+
def set_current_element(self, element: cv2find.Box):
500+
"""
501+
Changes the current screen element the bot will interact when using click(), move(), and similar methods.
502+
503+
This method is equivalent to self.state.element = element.
504+
505+
Args:
506+
element (Box): A screen element from self.state.element or the find_all(as_list=True) method.
507+
"""
508+
self.state.element = element
509+
499510
def find_all(self, label, x=None, y=None, width=None, height=None, *,
500-
threshold=None, matching=0.9, waiting_time=10000, grayscale=False):
511+
threshold=None, matching=0.9, waiting_time=10000, grayscale=False, as_list: bool = False):
501512
"""
502513
Find all elements defined by label on screen until a timeout happens.
503514
@@ -515,9 +526,13 @@ def find_all(self, label, x=None, y=None, width=None, height=None, *,
515526
Defaults to 10000ms (10s).
516527
grayscale (bool, optional): Whether or not to convert to grayscale before searching.
517528
Defaults to False.
529+
as_list (bool, Optional): If True, returns a list of element coordinates instead of a generator.
530+
Use set_active_element() to be able to interact with the found elements.
531+
This parameter must be True if you intend to run multiple find_all() concurrently.
532+
Defaults to False.
518533
519534
Returns:
520-
elements (collections.Iterable[NamedTuple]): A generator with all element coordinates fount.
535+
elements (collections.Iterable[NamedTuple]): A generator with all element coordinates found.
521536
None if not found.
522537
"""
523538
def deduplicate(elems):
@@ -577,6 +592,12 @@ def find_same(item, items):
577592
if not eles:
578593
continue
579594
eles = deduplicate(list(eles))
595+
596+
# As List
597+
if as_list:
598+
return eles
599+
600+
# As Generator
580601
for ele in eles:
581602
if ele is not None:
582603
self.state.element = ele

0 commit comments

Comments
 (0)