From 500d811d15628a1588ff2f90132946c9f0bfb98e Mon Sep 17 00:00:00 2001 From: Gavan Fantom Date: Fri, 22 Aug 2025 22:11:37 +0100 Subject: [PATCH] Implement Display events --- display.py | 27 +++++++++++++++++++++++++++ events.py | 19 ++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/display.py b/display.py index ac1561f..bf9b7d3 100755 --- a/display.py +++ b/display.py @@ -29,3 +29,30 @@ class DisplayLyrics(DisplayContent): multiline=True, batch=obj.audiencebatch)) +class DisplayImage(DisplayContent): + def __init__(self, image, background=None): + self._image = image + self._background = background + + def create(self, obj, width, height, x, y): + + super().create(obj, width, height, x, y) + + scale = min(width / self._image.width, height / self._image.height) + + if self._background is not None: + print("Background is {}".format(self._background)) + rectangle = pyglet.shapes.Rectangle(x=x + width//2, y=y + height//2, + width=width, height=height, + color=self._background, + batch=obj.audiencebatch) + rectangle.anchor_x = width//2 + rectangle.anchor_y = height//2 + obj.audiencecontent.append(rectangle) + + sprite = pyglet.sprite.Sprite(img=self._image, + x=x + width//2, y=y + height//2, + batch=obj.audiencebatch) + sprite.scale = scale + obj.audiencecontent.append(sprite) + diff --git a/events.py b/events.py index 67f8900..4c7f818 100644 --- a/events.py +++ b/events.py @@ -8,6 +8,8 @@ import display import numpy as np +import pyglet.image + class Event: def __init__(self, at='start'): self._at = timings.Timing(at) @@ -46,10 +48,25 @@ class Event: pass class Display(Event): - def __init__(self, media, *args, **kwargs): + def __init__(self, media, background=None, *args, **kwargs): super().__init__(*args, **kwargs) self._media = media + self._background = background + self._image = pyglet.image.load(self._media) + self._image.anchor_x = self._image.width // 2 + self._image.anchor_y = self._image.height // 2 + def start(self): + self._audiencecontent.new_content(display.DisplayImage(self._image, self._background)) + print("Started displaying image") + + def stop(self): + self._audiencecontent.new_content(display.DisplayContent()) + print("Stopped displaying image") + + def notify(self, message, *args, **kwargs): + if message == 'newdisplay': + self._eventrunner.finished(self) class Playback(Event): def __init__(self, media, device=None, channels=[1,2], tempo=None, beats=None, leadin=0, loop=False, gain=0, *args, **kwargs):