Source code for bruhanimate.bruheffect.offset_effect

"""
Copyright 2023 Ethan Christensen

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from ..bruhutil import VALID_DIRECTIONS
from ..bruhutil.bruhffer import Buffer
from .base_effect import BaseEffect
from .settings import OffsetSettings


[docs] class OffsetEffect(BaseEffect): """ Class for generating an offset-scrolling background. """
[docs] def __init__( self, buffer: Buffer, background: str, settings: OffsetSettings = None ): """ Initializes the OffsetEffect. Args: buffer (Buffer): Effect buffer to push updates to. background (str): Character or string to use as the background. settings (OffsetSettings, optional): Configuration for the offset effect. Defaults to None. """ super(OffsetEffect, self).__init__(buffer, background) s = settings or OffsetSettings() self.direction = s.direction if s.direction in VALID_DIRECTIONS else "right"
[docs] def set_direction(self, direction: str): """ Updates the scroll direction. Args: direction (str): Direction to scroll ("left" or "right"). """ if direction in VALID_DIRECTIONS: self.direction = direction self.buffer.clear_buffer()
[docs] def render_frame(self, frame_number: int): """ Renders a frame of the offset effect. Args: frame_number (int): The current frame number. """ for y in range(self.buffer.height()): row = ( self.background[y % self.background_length :] + self.background[: y % self.background_length] ) * (self.buffer.width() // self.background_length + self.background_length) if self.direction == "right": self.buffer.put_at(0, y, row[::-1]) else: self.buffer.put_at(0, y, row)