45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
from abc import ABC
|
|
from pathlib import Path
|
|
from typing import Optional, Any
|
|
|
|
import pyglet
|
|
|
|
|
|
class Style(ABC):
|
|
"""
|
|
This class represent a style that can be attached to a widget.
|
|
All property of the class will be loaded into a pyglet image.
|
|
|
|
If the property is associated to only a Path, a simple image will be loaded.
|
|
If the property is associated to a list of Path, an animation will be loaded.
|
|
"""
|
|
|
|
def __init_subclass__(cls, **kwargs):
|
|
atlas = pyglet.image.atlas.TextureAtlas()
|
|
|
|
for name, args in cls.__dict__.items():
|
|
if name.startswith("_"): continue
|
|
|
|
if isinstance(args, Path): # if this is a normal path for a normal image
|
|
path = args
|
|
texture = atlas.add(pyglet.image.load(path))
|
|
|
|
elif isinstance(args, tuple): # if this is a tuple for an animation
|
|
paths, duration, loop = args
|
|
|
|
textures = map(lambda path: atlas.add(pyglet.image.load(path)), paths)
|
|
texture = pyglet.image.Animation.from_image_sequence(textures, duration, loop)
|
|
|
|
else:
|
|
raise ValueError(f"Invalid type : {type(args)}")
|
|
|
|
setattr(cls, name, texture)
|
|
|
|
setattr(cls, "_atlas", atlas)
|
|
|
|
@classmethod
|
|
def get(cls, item: str, default: Any = None) -> Optional[pyglet.image.AbstractImage]:
|
|
return getattr(cls, item, default)
|
|
|
|
def __class_getitem__(cls, item: str) -> Optional[pyglet.image.AbstractImage]:
|
|
return cls.get(item)
|