1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
//! Support for scrolling arbitrary static images horizontally.
//!
//! [`ScrollingStatics`] can be used for static slices of any type implementing
//! [`Render`].
//!
//! # Example
//!
//! ```ignore
//! use microbit::display::nonblocking::{Display, Frame, MicrobitFrame};
//! use microbit_text::scrolling::Animate;
//! use microbit_text::scrolling_text::ScrollingStaticText;
//! use microbit_text::image::BitImage;
//! const BLANK: BitImage = BitImage::blank();
//! const HEART: BitImage = BitImage::new(&[
//! [0, 1, 0, 1, 0],
//! [1, 0, 1, 0, 1],
//! [1, 0, 0, 0, 1],
//! [0, 1, 0, 1, 0],
//! [0, 0, 1, 0, 0],
//! ]);
//! let mut display = MicrobitDisplay::new(...);
//! let mut scroller = ScrollingStatics::default();
//! let frame = MicrobitFrame::default();
//! scroller.set_images(&[&HEART, &BLANK, &HEART]);
//! while !scroller.is_finished() {
//! // every 50ms or so
//! scroller.tick();
//! frame.set(scroller);
//! display.show_frame(frame);
//! }
//! ```
//!
//! [`Render`]: tiny_led_matrix::Render
use tiny_led_matrix::Render;
use crate::scrolling::{Animate, ScrollingState, Scrollable};
/// A [`Scrollable`] displaying a static slice of arbitrary images.
///
/// The underlying images can be any sized type that implements [`Render`].
#[derive(Copy, Clone)]
pub struct ScrollingStatics<T: Render + 'static> {
images: &'static [T],
state: ScrollingState,
}
impl<T: Render + 'static> ScrollingStatics<T> {
/// Specifies the images to be displayed.
///
/// This also resets the animation to the beginning.
pub fn set_images(&mut self, images: &'static [T]) {
self.images = images;
self.reset();
}
}
impl<T: Render + 'static> Default for ScrollingStatics<T> {
fn default() -> ScrollingStatics<T> {
ScrollingStatics {
images: &[],
state: Default::default(),
}
}
}
impl<T: Render + 'static> Scrollable for ScrollingStatics<T> {
type Subimage = T;
fn length(&self) -> usize {
self.images.len()
}
fn state(&self) -> &ScrollingState {
&self.state
}
fn state_mut(&mut self) -> &mut ScrollingState {
&mut self.state
}
fn subimage(&self, index: usize) -> &T {
&self.images[index]
}
}
impl<T: Render + 'static> Render for ScrollingStatics<T> {
fn brightness_at(&self, x: usize, y: usize) -> u8 {
self.current_brightness_at(x, y)
}
}