from .draw_ops import OpsSet
from .drawable import Drawable
[docs]
class DrawableCache:
"""
A cache management class for storing and retrieving drawable objects and their corresponding operation sets.
It caches the state of a drawable object after a choice of particular event, or at initialization.
Provides methods to:
- Store and retrieve drawable objects and their computed operation sets, at different key frames
- Check for existing drawable operation sets
- Calculate bounding boxes for multiple drawables
Attributes:
cache (dict[str, OpsSet]): A mapping of drawable IDs to their computed operation sets
drawables (dict[str, Drawable]): A mapping of drawable IDs to their drawable objects
"""
def __init__(self, background_color: tuple[float, float, float] = (1, 1, 1)):
self.cache: dict[str, OpsSet] = {}
self.drawables: dict[str, Drawable] = {}
self.background_color: tuple[float, float, float] = background_color
[docs]
def get_cachekey(self, drawable_id: str, event_id: str | None = None):
if event_id is None:
return f"{drawable_id}__init"
else:
return f"{drawable_id}__{event_id}"
[docs]
def set_drawable_opsset(self, drawable: Drawable, opsset: OpsSet | None = None):
cachekey = self.get_cachekey(drawable.id)
self.drawables[cachekey] = drawable
if opsset is None:
opsset = drawable.draw()
self.cache[cachekey] = opsset # calculate opsset and store
[docs]
def set_drawable_event_opsset(self, drawable_id: str, event_id: str, opsset: OpsSet):
cachekey = self.get_cachekey(drawable_id, event_id)
self.cache[cachekey] = opsset
[docs]
def get_drawable(self, drawable_id: str) -> Drawable:
return self.drawables[drawable_id]
[docs]
def exists_in_cache(self, drawable_id: str, event_id: str | None = None) -> bool:
cachekey = self.get_cachekey(drawable_id, event_id)
return cachekey in self.cache
[docs]
def get_drawable_opsset(self, drawable_id: str, event_id: str | None = None) -> OpsSet:
cachekey = self.get_cachekey(drawable_id, event_id)
return self.cache.get(cachekey, OpsSet(initial_set=[]))
[docs]
def calculate_bounding_box(self, drawables: list[Drawable]):
"""
Calculates the bounding box for a list of drawables
stored in the cache
"""
from .drawable import DrawableGroup
merge_opsset = OpsSet(initial_set=[])
def _collect(item: Drawable) -> None:
if isinstance(item, DrawableGroup):
for elem in item.elements:
_collect(elem)
elif self.exists_in_cache(item.id):
merge_opsset.extend(self.get_drawable_opsset(item.id))
else:
opsset = item.draw()
self.set_drawable_opsset(item, opsset)
merge_opsset.extend(opsset)
for drawable in drawables:
_collect(drawable)
return merge_opsset.get_bbox()
[docs]
class GroupFrameCache:
"""
Per-frame cache for group animation transforms. Must be reset at the start
of each frame via reset(). Stores two slots per (group, event):
- pre-transform: the assembled group OpsSet before event.apply()
- transformed: the result of event.apply(group_opsset, progress)
"""
def __init__(self) -> None:
self._pretransform: dict[tuple[str, str], OpsSet] = {}
self._transformed: dict[tuple[str, str, int], OpsSet] = {}
[docs]
def reset(self):
self._pretransform.clear()
self._transformed.clear()