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