Source code for RWESharp.Loaders.Prop

from __future__ import annotations
from PySide6.QtGui import QImage, QColor, QPixmap
from PySide6.QtCore import QPoint, QSize, QObject, Signal
from dataclasses import dataclass, field
from RWESharp.info import PATH_COLLECTIONS_PROPS
import os


[docs] @dataclass(frozen=True) class Prop: item: dict name: str type: str repeatl: list[int] description: str images: list[QImage] colorTreatment: str vars: int # color: str color: QColor cat: QPoint tags: list[str] err: bool category: PropCategory notes: list[str] size: QSize layerExceptions: list = field(default=list) beveltable: None | list[int] = None rope: bool = False long: bool = False @property def colorable(self): return len(self.repeatl) > 1
[docs] def get(self, key, default=None): return self.item.get(key, default)
def __getitem__(self, item): return self.item.get(item)
[docs] def copy(self): return Prop(self.item, self.name, self.type, self.repeatl, self.description, self.images, self.colorTreatment, self.vars, self.color, self.cat, self.tags, self.err, self.category, self.notes, self.size, self.layerExceptions, self.beveltable, self.rope, self.long)
[docs] @dataclass class PropCategory: name: str color: QColor props: list[Prop]
[docs] def find_prop(self, name) -> Prop | None: for i in self.props: if i.name == name: return i return None
def __repr__(self): return f"<Prop category {self.name} with {len(self.props)} prop(s)>"
[docs] class Props(QObject): default = Prop({}, "None", "standard", [1], "No Description", [QImage(10, 10, QImage.Format.Format_RGBA64)], "none", 1, QColor(255, 0, 0), QPoint(0, 0), [], True, None, [], QSize(1, 1)) propschanged = Signal() @property def categories(self) -> list[PropCategory]: return [*self._categories, *self.custom_categories]
[docs] def find_prop(self, name) -> Prop | None: for i in self.categories: tile = i.find_prop(name) if tile is not None: return tile return None
[docs] def find_category(self, name) -> PropCategory | None: for i in self.categories: if i.name == name: return i return None
[docs] def all_props(self) -> list[Prop]: t = [i.props for i in self.categories] newt = [] for i in t: newt = [*newt, *i] return newt
def __getitem__(self, item): if isinstance(item, str): return self.find_prop(item)
[docs] def add_custom_props(self): self.custom_categories = [] for i in os.listdir(PATH_COLLECTIONS_PROPS): fullpath = os.path.join(PATH_COLLECTIONS_PROPS, i) if not os.path.isfile(fullpath): continue with open(fullpath) as f: addedprops = [] lines = [t.replace("\n", "") for t in f.readlines()] if len(lines) < 1: continue color = QColor(*[int(v) for v in lines[0].split()]) for tile in lines[1:]: found = self.find_prop(tile) if found is None: continue addedprops.append(found) self.custom_categories.append(PropCategory(i, color, addedprops)) self.propschanged.emit()
[docs] def __init__(self, categories): super().__init__() self._categories: list[PropCategory] = categories self.custom_categories = [] self.add_custom_props()