covalent/scene/
mod.rs

1//! A scene is essentially everything that the user can see or hear, and anything that interacts with that.
2
3mod node;
4pub use node::*;
5
6use std::sync::{RwLock, Arc, Weak};
7use crate::events::EventHandlers;
8
9/// The scene contains everything that the user can see or hear, and anything that interacts with that.
10/// Covalent will automatically render everything in this scene according to the active render pipeline.
11///
12/// The scene should mostly be borrowed immutably to allow for more concurrency, many of its fields
13/// are internally mutable.
14pub struct Scene {
15    self_ref: Weak<RwLock<Scene>>,
16    nodes: Vec<Arc<RwLock<Node>>>,
17    pub events: EventHandlers
18}
19
20impl Scene {
21    pub fn new() -> Arc<RwLock<Scene>> {
22        let scene = Arc::new(RwLock::new(Scene {
23            self_ref: Weak::new(),
24            nodes: Vec::new(),
25            events: EventHandlers::default()
26        }));
27        scene.write().unwrap().self_ref = Arc::downgrade(&scene);
28        scene
29    }
30
31    /// Creates a new node and adds it to the scene.
32    pub fn new_node(&mut self) -> Arc<RwLock<Node>> {
33        let n = Node::default(Weak::upgrade(&self.self_ref).unwrap());
34        self.nodes.push(Arc::clone(&n));
35        n
36    }
37
38    /// Iterates over all the 3D nodes in the scene.
39    pub fn iter_3d(&self) -> impl Iterator<Item=&Arc<RwLock<Node>>> {
40        self.nodes.iter()
41    }
42}