37 lines
951 B
Python
37 lines
951 B
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from core.models import Scene
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class IconSearchResult:
|
|
found: bool
|
|
name: str
|
|
x: float | None = None
|
|
y: float | None = None
|
|
reason: str | None = None
|
|
|
|
def to_dict(self) -> dict[str, object]:
|
|
return {
|
|
"found": self.found,
|
|
"name": self.name,
|
|
"x": self.x,
|
|
"y": self.y,
|
|
"reason": self.reason,
|
|
}
|
|
|
|
|
|
def find_icon(scene: Scene, name: str) -> IconSearchResult:
|
|
query = name.casefold()
|
|
for element in scene.elements:
|
|
if element.type not in {"image", "icon", "button"}:
|
|
continue
|
|
label = (element.text or element.id).casefold()
|
|
if query in label:
|
|
x, y = element.center
|
|
return IconSearchResult(found=True, name=name, x=x, y=y)
|
|
return IconSearchResult(found=False, name=name, reason="not found")
|
|
|