model-converter/src/scene.rs

59 lines
1.4 KiB
Rust
Raw Permalink Normal View History

2018-04-17 10:45:44 +02:00
//! This module contains everything related to scenes.
use std::slice::{Iter, IterMut};
2018-04-18 10:26:40 +02:00
use std::vec::IntoIter;
2018-06-13 14:55:41 +02:00
use std::rc::Rc;
2018-04-17 10:45:44 +02:00
use std::cell::RefCell;
use model::Model;
/// Represents a 3D scene with models.
2018-04-18 10:26:40 +02:00
pub struct Scene {
2018-06-13 14:55:41 +02:00
models: Vec<Rc<RefCell<Model>>>,
2018-04-18 10:26:40 +02:00
}
impl Scene {
/// Creates a new empty scene.
pub fn new() -> Scene {
Scene {
models: vec![],
}
}
/// Adds a model to the scene.
2018-06-13 14:55:41 +02:00
pub fn push(&mut self, model: Rc<RefCell<Model>>) {
2018-04-18 10:26:40 +02:00
self.models.push(model);
}
2018-06-13 14:55:41 +02:00
/// Converts the model to a Rc<RefCell<Model>> and adds it to the scene.
2018-04-18 10:26:40 +02:00
pub fn emplace(&mut self, model: Model) {
2018-06-13 14:55:41 +02:00
self.models.push(Rc::new(RefCell::new(model)));
2018-04-18 10:26:40 +02:00
}
/// Returns an iterator to the models of the scene.
2018-06-13 14:55:41 +02:00
pub fn iter(&self) -> Iter<Rc<RefCell<Model>>> {
2018-04-18 10:26:40 +02:00
self.models.iter()
}
/// Returns a mutable iterator to the model of the scene.
2018-06-13 14:55:41 +02:00
pub fn iter_mut(&mut self) -> IterMut<Rc<RefCell<Model>>> {
self.models.iter_mut()
}
2018-04-18 10:26:40 +02:00
}
impl IntoIterator for Scene {
2018-06-13 14:55:41 +02:00
type IntoIter = IntoIter<Rc<RefCell<Model>>>;
type Item = Rc<RefCell<Model>>;
2018-04-18 10:26:40 +02:00
fn into_iter(self) -> Self::IntoIter {
self.models.into_iter()
}
}
2018-04-18 11:49:27 +02:00
impl From<Vec<Model>> for Scene {
fn from(input: Vec<Model>) -> Scene {
Scene {
2018-06-13 14:55:41 +02:00
models: input.into_iter().map(|x| Rc::new(RefCell::new(x))).collect(),
2018-04-18 11:49:27 +02:00
}
}
}