RASCSI/python/ctrlboard/src/menu/transition.py
dependabot[bot] 52e4a92aec
Bump black from 22.8.0 to 24.3.0 in /python/web (#1444)
* Bump black from 22.8.0 to 24.3.0 in /python/web

Bumps [black](https://github.com/psf/black) from 22.8.0 to 24.3.0.
- [Release notes](https://github.com/psf/black/releases)
- [Changelog](https://github.com/psf/black/blob/main/CHANGES.md)
- [Commits](https://github.com/psf/black/compare/22.8.0...24.3.0)

---
updated-dependencies:
- dependency-name: black
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>

* Github CI: Backend web test runner use python 3.9.19

* Reformat python sources with black

* Docker: Bump to python 3.9-slim image for pytest

* Bump pytest version to 8.1.1

* Bump more library versions, and freeze them

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Daniel Markstedt <daniel@mindani.net>
2024-03-22 00:19:13 -07:00

68 lines
2.9 KiB
Python

"""Module providing implementations for menu transitions."""
from abc import abstractmethod
from PIL import Image
# pylint: disable=too-few-public-methods
class Transition:
"""Class provides the interface for menu transitions. Must be subclassed."""
def __init__(self, disp):
self.disp = disp
@abstractmethod
def perform(self, start_image: Image, end_image: Image, transition_attributes=None) -> None:
"""Signature for an abstract transition. Must be implemented in subclasses."""
class PushTransition(Transition):
"""Class implementing a push left/right transition."""
PUSH_LEFT_TRANSITION = "push_left"
PUSH_RIGHT_TRANSITION = "push_right"
def __init__(self, disp):
super().__init__(disp)
self.transition_attributes = None
def perform(self, start_image: Image, end_image: Image, transition_attributes=None):
"""Performs a push transition to the left or right depending on the
transition attribute 'direction'."""
direction = {}
self.transition_attributes = transition_attributes
if transition_attributes is not None and transition_attributes != {}:
direction = transition_attributes["direction"]
transition_image = Image.new("1", (self.disp.width, self.disp.height))
if direction == PushTransition.PUSH_LEFT_TRANSITION:
self.perform_push_left(end_image, start_image, transition_image)
elif direction == PushTransition.PUSH_RIGHT_TRANSITION:
self.perform_push_right(end_image, start_image, transition_image)
else:
self.disp.image(end_image)
self.disp.show()
def perform_push_left(self, end_image, start_image, transition_image):
"""Implements a push left transition. Is called by perform depending on the transition
attribute 'direction'."""
for x_pos in range(0, 128, self.transition_attributes["transition_speed"]):
left_region = start_image.crop((x_pos, 0, 128, 64))
right_region = end_image.crop((0, 0, x_pos, 64))
transition_image.paste(left_region, (0, 0, 128 - x_pos, 64))
transition_image.paste(right_region, (128 - x_pos, 0, 128, 64))
self.disp.display(transition_image)
self.disp.display(end_image)
def perform_push_right(self, end_image, start_image, transition_image):
"""Implements a push right transition. Is called by perform depending on the transition
attribute 'direction'."""
for x_pos in range(0, 128, self.transition_attributes["transition_speed"]):
left_region = start_image.crop((0, 0, 128 - x_pos, 64))
right_region = end_image.crop((128 - x_pos, 0, 128, 64))
transition_image.paste(left_region, (x_pos, 0, 128, 64))
transition_image.paste(right_region, (0, 0, x_pos, 64))
self.disp.display(transition_image)
self.disp.display(end_image)