1use crate::id::{MaterialId, MeshId, NodeId, SkeletonId, TileMapId};
2use crate::transform::Transform;
3use serde::{Deserialize, Serialize};
4use std::collections::BTreeMap;
5
6#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
7pub enum NodeKind {
8 Empty,
9 MeshInstance {
10 mesh: MeshId,
11 #[serde(default, skip_serializing_if = "Option::is_none")]
12 material: Option<MaterialId>,
13 #[serde(default, skip_serializing_if = "Option::is_none")]
15 skeleton_node: Option<NodeId>,
16 },
17 SkeletonInstance {
19 skeleton: SkeletonId,
20 },
21 TileMapInstance {
23 tilemap: TileMapId,
24 },
25 Camera {
26 fov_deg: f32,
27 },
28 Light {
29 intensity: f32,
30 },
31}
32
33#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
34pub struct Node {
35 pub name: String,
36 #[serde(default, skip_serializing_if = "Option::is_none")]
37 pub parent: Option<NodeId>,
38 pub children: Vec<NodeId>,
39 pub transform: Transform,
40 pub visible: bool,
41 pub kind: NodeKind,
42 #[serde(default, skip_serializing_if = "crate::motion::Motion::is_none")]
44 pub motion: crate::motion::Motion,
45}
46
47impl Default for Node {
48 fn default() -> Self {
49 Self {
50 name: String::new(),
51 parent: None,
52 children: Vec::new(),
53 transform: Transform::default(),
54 visible: true,
55 kind: NodeKind::Empty,
56 motion: crate::motion::Motion::None,
57 }
58 }
59}
60
61#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
63pub struct Scene {
64 pub nodes: BTreeMap<NodeId, Node>,
65 pub roots: Vec<NodeId>,
66 pub(crate) next_node: u32,
67}
68
69impl Scene {
70 pub fn new() -> Self {
71 Self::default()
72 }
73
74 pub fn alloc_node_id(&mut self) -> NodeId {
75 let id = NodeId(self.next_node);
76 self.next_node += 1;
77 id
78 }
79
80 pub fn insert(&mut self, parent: Option<NodeId>, node: Node) -> NodeId {
82 let id = self.alloc_node_id();
83 let mut node = node;
84 node.parent = parent;
85 if let Some(p) = parent {
86 if let Some(pn) = self.nodes.get_mut(&p) {
87 pn.children.push(id);
88 }
89 } else {
90 self.roots.push(id);
91 }
92 self.nodes.insert(id, node);
93 id
94 }
95
96 pub fn get(&self, id: NodeId) -> Option<&Node> {
97 self.nodes.get(&id)
98 }
99
100 pub fn get_mut(&mut self, id: NodeId) -> Option<&mut Node> {
101 self.nodes.get_mut(&id)
102 }
103
104 pub fn remove(&mut self, id: NodeId) -> Option<Node> {
105 let node = self.nodes.remove(&id)?;
106 match node.parent {
108 Some(p) => {
109 if let Some(pn) = self.nodes.get_mut(&p) {
110 pn.children.retain(|c| *c != id);
111 }
112 }
113 None => self.roots.retain(|r| *r != id),
114 }
115 for c in &node.children {
117 if let Some(cn) = self.nodes.get_mut(c) {
118 cn.parent = None;
119 }
120 self.roots.push(*c);
121 }
122 Some(node)
123 }
124
125 pub fn remove_subtree(&mut self, id: NodeId) {
127 let children: Vec<NodeId> = self.get(id).map(|n| n.children.clone()).unwrap_or_default();
128 for c in children {
129 self.remove_subtree(c);
130 }
131 self.remove(id);
132 }
133
134 pub fn world_transform(&self, id: NodeId) -> Transform {
136 let Some(node) = self.get(id) else {
137 return Transform::default();
138 };
139 match node.parent {
140 Some(p) => self.world_transform(p).then(&node.transform),
141 None => node.transform,
142 }
143 }
144
145 pub fn walk(&self, mut f: impl FnMut(NodeId, &Node)) {
147 fn rec(scene: &Scene, id: NodeId, f: &mut impl FnMut(NodeId, &Node)) {
148 if let Some(n) = scene.get(id) {
149 f(id, n);
150 for c in n.children.clone() {
151 rec(scene, c, f);
152 }
153 }
154 }
155 for r in self.roots.clone() {
156 rec(self, r, &mut f);
157 }
158 }
159}