StarRailCopilot/module/base/button.py

249 lines
6.8 KiB
Python
Raw Normal View History

2023-05-16 18:54:03 +00:00
import module.config.server as server
from module.base.decorator import cached_property, del_cached_property
2023-05-14 07:48:34 +00:00
from module.base.resource import Resource
from module.base.utils import *
2023-05-16 18:54:03 +00:00
from module.exception import ScriptError
2023-05-14 07:48:34 +00:00
class Button(Resource):
2023-05-16 18:54:03 +00:00
def __init__(self, file, area, search, color, button):
"""
2023-05-14 07:48:34 +00:00
Args:
2023-05-16 18:54:03 +00:00
file: Filepath to an assets
area: Area to crop template
search: Area to search from, 20px larger than `area` by default
color: Average color of assets
button: Area to click if assets appears on the image
2023-05-14 07:48:34 +00:00
"""
2023-05-16 18:54:03 +00:00
self.file: str = file
self.area: t.Tuple[int, int, int, int] = area
self.search: t.Tuple[int, int, int, int] = search
self.color: t.Tuple[int, int, int] = color
self._button: t.Tuple[int, int, int, int] = button
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
self.resource_add(self.file)
self._button_offset: t.Tuple[int, int] = (0, 0)
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
@property
def button(self):
return area_offset(self._button, self._button_offset)
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
def clear_offset(self):
self._button_offset = (0, 0)
2023-05-14 07:48:34 +00:00
@cached_property
2023-05-16 18:54:03 +00:00
def image(self):
return load_image(self.file, self.area)
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
def resource_release(self):
del_cached_property(self, 'image')
self.clear_offset()
2023-05-14 07:48:34 +00:00
def __str__(self):
2023-05-16 18:54:03 +00:00
return self.file
2023-05-14 07:48:34 +00:00
__repr__ = __str__
def __eq__(self, other):
return str(self) == str(other)
def __hash__(self):
2023-05-16 18:54:03 +00:00
return hash(self.file)
2023-05-14 07:48:34 +00:00
def __bool__(self):
return True
2023-05-16 18:54:03 +00:00
def match_color(self, image, threshold=10) -> bool:
"""
Check if the button appears on the image, using average color
2023-05-14 07:48:34 +00:00
Args:
image (np.ndarray): Screenshot.
threshold (int): Default to 10.
Returns:
bool: True if button appears on screenshot.
"""
2023-05-16 18:54:03 +00:00
color = get_color(image, self.area)
2023-05-14 07:48:34 +00:00
return color_similar(
2023-05-16 18:54:03 +00:00
color1=color,
2023-05-14 07:48:34 +00:00
color2=self.color,
threshold=threshold
)
2023-05-16 18:54:03 +00:00
def match_template(self, image, similarity=0.85) -> bool:
2023-05-14 07:48:34 +00:00
"""
2023-05-16 18:54:03 +00:00
Detects assets by template matching.
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
To Some buttons, its location may not be static, `_button_offset` will be set.
2023-05-14 07:48:34 +00:00
Args:
image: Screenshot.
2023-05-16 18:54:03 +00:00
similarity (float): 0-1.
2023-05-14 07:48:34 +00:00
Returns:
bool.
"""
2023-05-16 18:54:03 +00:00
image = crop(image, self.search, copy=False)
res = cv2.matchTemplate(self.image, image, cv2.TM_CCOEFF_NORMED)
_, sim, _, point = cv2.minMaxLoc(res)
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
self._button_offset = np.array(point) + self.search[:2] - self.area[:2]
return sim > similarity
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
def match_template_color(self, image, similarity=0.85, threshold=30) -> bool:
"""
Template match first, color match then
2023-05-14 07:48:34 +00:00
Args:
image: Screenshot.
2023-05-16 18:54:03 +00:00
similarity (float): 0-1.
threshold (int): Default to 10.
2023-05-14 07:48:34 +00:00
Returns:
2023-05-16 18:54:03 +00:00
2023-05-14 07:48:34 +00:00
"""
2023-05-16 18:54:03 +00:00
matched = self.match_template(image, similarity=similarity)
if not matched:
2023-05-14 07:48:34 +00:00
return False
2023-05-16 18:54:03 +00:00
area = area_offset(self.area, offset=self._button_offset)
color = get_color(image, area)
return color_similar(
color1=color,
color2=self.color,
threshold=threshold
)
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
class ButtonWrapper(Resource):
def __init__(self, name='MULTI_ASSETS', **kwargs):
self.name = name
self.data_buttons = kwargs
self._matched_button: t.Optional[Button] = None
self.resource_add(self.name)
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
def resource_release(self):
del_cached_property(self, 'assets')
self._matched_button = None
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
def __str__(self):
return self.name
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
__repr__ = __str__
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
def __eq__(self, other):
return str(self) == str(other)
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
def __hash__(self):
return hash(self.name)
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
def __bool__(self):
return True
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
@cached_property
def buttons(self) -> t.List[Button]:
for trial in [server.server, 'share', 'cn']:
assets = self.data_buttons.get(trial, None)
if assets is not None:
if isinstance(assets, Button):
return [assets]
elif isinstance(assets, list):
return assets
raise ScriptError(f'ButtonWrapper({self}) on server {server.server} has no fallback button')
def match_color(self, image, threshold=10) -> bool:
for assets in self.buttons:
if assets.match_color(image, threshold=threshold):
self._matched_button = assets
return True
return False
def match_template(self, image, similarity=0.85) -> bool:
for assets in self.buttons:
if assets.match_template(image, similarity=similarity):
self._matched_button = assets
return True
return False
def match_template_color(self, image, similarity=0.85, threshold=30) -> bool:
for assets in self.buttons:
if assets.match_template_color(image, similarity=similarity, threshold=threshold):
self._matched_button = assets
return True
return False
@property
def matched_button(self) -> Button:
if self._matched_button is None:
return self.buttons[0]
2023-05-14 07:48:34 +00:00
else:
2023-05-16 18:54:03 +00:00
return self._matched_button
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
@property
2023-05-30 18:20:45 +00:00
def area(self) -> tuple[int, int, int, int]:
2023-05-16 18:54:03 +00:00
return self.matched_button.area
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
@property
2023-05-30 18:20:45 +00:00
def search(self) -> tuple[int, int, int, int]:
2023-05-16 18:54:03 +00:00
return self.matched_button.search
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
@property
2023-05-30 18:20:45 +00:00
def color(self) -> tuple[int, int, int]:
2023-05-16 18:54:03 +00:00
return self.matched_button.color
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
@property
2023-05-30 18:20:45 +00:00
def button(self) -> tuple[int, int, int, int]:
2023-05-16 18:54:03 +00:00
return self.matched_button.button
2023-05-14 07:48:34 +00:00
2023-05-30 18:20:45 +00:00
@property
2023-06-13 03:36:54 +00:00
def button_offset(self) -> tuple[int, int]:
return self.matched_button._button_offset
@property
2023-05-30 18:20:45 +00:00
def width(self) -> int:
return area_size(self.area)[0]
@property
def height(self) -> int:
return area_size(self.area)[1]
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
class ClickButton:
def __init__(self, button, name='CLICK_BUTTON'):
2023-06-14 16:15:14 +00:00
self.area = button
2023-05-16 18:54:03 +00:00
self.button = button
self.name = name
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
def __str__(self):
return self.name
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
__repr__ = __str__
def __eq__(self, other):
return str(self) == str(other)
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
def __hash__(self):
return hash(self.name)
2023-05-14 07:48:34 +00:00
2023-05-16 18:54:03 +00:00
def __bool__(self):
return True
2023-05-30 18:20:45 +00:00
def match_template(image, template, similarity=0.85):
"""
Args:
image (np.ndarray): Screenshot
template (np.ndarray):
area (tuple): Crop area of image.
offset (int, tuple): Detection area offset.
similarity (float): 0-1. Similarity. Lower than this value will return float(0).
Returns:
bool:
"""
res = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
_, sim, _, point = cv2.minMaxLoc(res)
return sim > similarity