Skip to main content

shapes_stage/
shapes_stage.rs

1//! A stage in the graphics model drawn with the shape helpers: a panel, an
2//! orbiting dot, a sweeping line, a progress bar and a label. It animates at
3//! the viewer's paced rate on mains power and once a second when saving
4//! power, and follows the viewer's theme colours.
5//!
6//! Manifest surfaces: `[{ "id": "stage", "anchor": "stage", "model": "pixels" }]`.
7#![no_std]
8
9use standard_plugin::prelude::*;
10use standard_plugin::{Power, libm, view};
11
12#[standard_plugin::ui]
13struct ShapesStage {
14    stage: Surface<Pixels>,
15}
16
17impl UiPlugin for ShapesStage {
18    fn activate(_cx: &mut Context) -> Self {
19        Self {
20            stage: Surface::new("stage"),
21        }
22    }
23
24    fn frame(&mut self, frame: &Frame) {
25        let (w, h) = self.stage.size();
26        if w == 0 || h == 0 {
27            // No pixel geometry yet: a resize will follow.
28            return;
29        }
30        let theme = view::theme();
31        let (fg, bg, accent) = (
32            Rgba::hex(theme.fg),
33            Rgba::hex(theme.bg),
34            Rgba::hex(theme.accent),
35        );
36        let t = (frame.now_ms() % 4000) as f32 / 4000.0;
37
38        // A full repaint: nothing is carried forward from the last frame.
39        self.stage.clear(bg);
40        let panel = Area::new(8, 8, w.saturating_sub(16), h.saturating_sub(16));
41        self.stage
42            .fill_rounded_rect(panel, 12, Rgba::hex(theme.recede_bg));
43        self.stage
44            .rounded_rect(panel, 12, Rgba::hex(theme.recede_fg));
45
46        let (cx, cy) = (w as f32 / 2.0, h as f32 / 2.0);
47        let radius = (w.min(h) / 4) as f32;
48        let angle = t * core::f32::consts::TAU;
49        // `no_std` has no `f32::sin`: the SDK re-exports `libm`. The dot
50        // moves between pixels; the shapes antialias it there.
51        let dot = (
52            cx + radius * libm::cosf(angle),
53            cy + radius * libm::sinf(angle),
54        );
55        self.stage
56            .circle((cx, cy), radius, Rgba::hex(theme.recede_fg));
57        self.stage
58            .line((cx as i32, cy as i32), (dot.0 as i32, dot.1 as i32), accent);
59        self.stage.fill_circle(dot, 6.0, accent);
60
61        let bar = Area::new(24, h as i32 - 40, w.saturating_sub(48), 8);
62        self.stage
63            .bar(bar, t, accent, Rgba::hex(theme.recede_fg).with_alpha(96));
64        self.stage.label(24, 24, "shapes", fg);
65        let _ = self.stage.commit();
66
67        match frame.power() {
68            Power::Mains => frame.request_frame(),
69            Power::SavePower => frame.wake_after(1000),
70        }
71    }
72}