Skip to main content

wisp/scene/
node.rs

1//! Tagged union of scene-graph node variants.
2
3use slotmap::new_key_type;
4
5use crate::scene::container::Container;
6use crate::scene::flex_text::FlexText;
7use crate::scene::graphics::Graphics;
8use crate::scene::mesh::Mesh;
9use crate::scene::sprite::Sprite;
10use crate::scene::text::Text;
11
12new_key_type! {
13    /// Stable handle for a scene-graph node owned by a [`crate::scene::Stage`].
14    pub struct NodeId;
15}
16
17/// Scene-graph node — tagged union over the renderable types.
18#[derive(Debug, Clone)]
19pub enum Node {
20    /// Plain transform-only container (no draw of its own).
21    Container(Container),
22    /// Textured quad.
23    Sprite(Sprite),
24    /// SDF-based vector graphics (rounded rect, ellipse, gradients, …).
25    Graphics(Graphics),
26    /// Bitmap-font text run.
27    Text(Text),
28    /// Late-pass textured quad — renders after every
29    /// [`Graphics`] primitive so axis labels / legends / KPI numbers
30    /// composed via wisp's flexible-text path read on top of the
31    /// chart. See [`FlexText`].
32    FlexText(FlexText),
33    /// Indexed triangle mesh (currently used for the perspective demo).
34    Mesh(Mesh),
35}
36
37impl Node {
38    /// Borrow the underlying [`Container`] (transform + visibility) regardless
39    /// of variant.
40    #[must_use]
41    pub fn container(&self) -> &Container {
42        match self {
43            Self::Container(c) => c,
44            Self::Sprite(s) => &s.container,
45            Self::Graphics(g) => &g.container,
46            Self::Text(t) => &t.container,
47            Self::FlexText(f) => &f.container,
48            Self::Mesh(m) => &m.container,
49        }
50    }
51
52    /// Mutable borrow of the underlying [`Container`].
53    pub fn container_mut(&mut self) -> &mut Container {
54        match self {
55            Self::Container(c) => c,
56            Self::Sprite(s) => &mut s.container,
57            Self::Graphics(g) => &mut g.container,
58            Self::Text(t) => &mut t.container,
59            Self::FlexText(f) => &mut f.container,
60            Self::Mesh(m) => &mut m.container,
61        }
62    }
63}
64
65impl From<Container> for Node {
66    fn from(c: Container) -> Self {
67        Self::Container(c)
68    }
69}
70
71impl From<Sprite> for Node {
72    fn from(s: Sprite) -> Self {
73        Self::Sprite(s)
74    }
75}
76
77impl From<Graphics> for Node {
78    fn from(g: Graphics) -> Self {
79        Self::Graphics(g)
80    }
81}
82
83impl From<Text> for Node {
84    fn from(t: Text) -> Self {
85        Self::Text(t)
86    }
87}
88
89impl From<FlexText> for Node {
90    fn from(f: FlexText) -> Self {
91        Self::FlexText(f)
92    }
93}
94
95impl From<Mesh> for Node {
96    fn from(m: Mesh) -> Self {
97        Self::Mesh(m)
98    }
99}