sciart_core/generator.rs
1use crate::{Canvas, Ctx, Size};
2
3/// Where a scene belongs (CORE-R77, ADR-0013). Sky scenes are sorted by setting (Voyages,
4/// then Earth & Sky, then Sun & Planets, then Deep Cosmos); the rest by which of physics (P),
5/// chemistry (C) and biology (B) its model needs. Variant order is display order only.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum Category {
8 /// Deep space: galaxies, clusters, black holes, the early universe.
9 DeepCosmos,
10 /// The Sun, and planets, moons and comets (including exoplanets).
11 SunPlanets,
12 /// The sky as seen from Earth's surface or atmosphere.
13 EarthSky,
14 /// Human spacecraft and missions.
15 Voyages,
16 /// P: a physical law (forces, flow, fields, waves, statistical or quantum mechanics,
17 /// deterministic chaos).
18 Physics,
19 /// C: chemical kinetics, molecular structure or a phase change (incl. ordering).
20 Chemistry,
21 /// B: an organism, tissue, cell or artificial life.
22 Biology,
23 /// P×C.
24 PhysicalChemistry,
25 /// P×B.
26 Biophysics,
27 /// C×B.
28 Biochemistry,
29 /// P×C×B.
30 LivingMatter,
31}
32
33impl Category {
34 /// Every category, in display order.
35 pub const ALL: [Category; 11] = [
36 Category::DeepCosmos,
37 Category::SunPlanets,
38 Category::EarthSky,
39 Category::Voyages,
40 Category::Physics,
41 Category::Chemistry,
42 Category::Biology,
43 Category::PhysicalChemistry,
44 Category::Biophysics,
45 Category::Biochemistry,
46 Category::LivingMatter,
47 ];
48
49 /// The label in kebab case (`&` dropped), printed by `sciart list` (CLI-R4), so the
50 /// machine id and the name people see never drift apart.
51 pub const fn id(self) -> &'static str {
52 match self {
53 Category::DeepCosmos => "deep-cosmos",
54 Category::SunPlanets => "sun-planets",
55 Category::EarthSky => "earth-sky",
56 Category::Voyages => "voyages",
57 Category::Physics => "forces-waves",
58 Category::Chemistry => "reactions",
59 Category::Biology => "living-systems",
60 Category::PhysicalChemistry => "states-of-matter",
61 Category::Biophysics => "physics-of-life",
62 Category::Biochemistry => "molecules-of-life",
63 Category::LivingMatter => "life-from-matter",
64 }
65 }
66
67 /// Display name, as written in SPEC-002's scene headings (GEN-R15).
68 pub const fn label(self) -> &'static str {
69 match self {
70 Category::DeepCosmos => "Deep Cosmos",
71 Category::SunPlanets => "Sun & Planets",
72 Category::EarthSky => "Earth & Sky",
73 Category::Voyages => "Voyages",
74 Category::Physics => "Forces & Waves",
75 Category::Chemistry => "Reactions",
76 Category::Biology => "Living Systems",
77 Category::PhysicalChemistry => "States of Matter",
78 Category::Biophysics => "Physics of Life",
79 Category::Biochemistry => "Molecules of Life",
80 Category::LivingMatter => "Life from Matter",
81 }
82 }
83}
84
85#[derive(Debug)]
86pub struct Meta {
87 pub name: &'static str,
88 pub category: Category,
89 pub description: &'static str,
90 /// `Phenomenon · mechanism`, drawn by `Stage` (CORE-R71; content rules GEN-R12).
91 pub title: &'static str,
92 /// Every caption [`Generator::caption`] can return; empty for a single-act scene.
93 pub captions: &'static [&'static str],
94}
95
96/// A scene (CORE-R14). Call protocol (CORE-R15), guaranteed by the runtime:
97/// `resize` before the first `update`/`render` and on every size change;
98/// `render` receives a cleared canvas of the last resized size.
99///
100/// Contract (GEN-R4..R10): never panic for any size `>= 1x1`, rebuild size-dependent
101/// state in `resize`, use only `ctx.rng` and `dt`/`ctx.time()`.
102pub trait Generator: Send {
103 fn meta(&self) -> &'static Meta;
104 fn resize(&mut self, size: Size, ctx: &mut Ctx);
105 fn update(&mut self, dt: f64, ctx: &mut Ctx);
106
107 /// Called by `Stage::render` exactly once right before every `render` (CORE-R101):
108 /// the place to rasterise simulation state into the scene's buffers once per drawn
109 /// frame rather than once per step (the runtime takes about two 60 Hz steps per
110 /// 30 FPS frame; GEN-R20). Must not advance the simulation or draw from `ctx.rng`,
111 /// and must not allocate (GEN-R11). Default: nothing.
112 fn prepare(&mut self) {}
113
114 fn render(&self, canvas: &mut Canvas);
115
116 /// The live act's caption, which replaces the title's mechanism; an entry of
117 /// `meta().captions` (CORE-R71).
118 fn caption(&self) -> Option<&'static str> {
119 None
120 }
121}