use super::{
entity::{calculate_hash, Entity},
resource::{Handle, SharedOwnership},
FillShader,
};
use crate::platform::prelude::*;
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Layer {
Bottom,
Top,
}
impl Layer {
pub const fn from_updates_frequently(updates_frequently: bool) -> Self {
match updates_frequently {
false => Self::Bottom,
true => Self::Top,
}
}
}
pub struct Scene<P, I, L> {
rectangle: Handle<P>,
background: Option<FillShader>,
bottom_hash: u64,
bottom_layer_changed: bool,
bottom_layer: Vec<Entity<P, I, L>>,
top_layer: Vec<Entity<P, I, L>>,
}
impl<P: SharedOwnership, I: SharedOwnership, L: SharedOwnership> Scene<P, I, L> {
pub fn new(rectangle: Handle<P>) -> Self {
Self {
rectangle,
background: None,
bottom_hash: calculate_hash::<P, I, L>(&None, &[]),
bottom_layer_changed: false,
bottom_layer: Vec::new(),
top_layer: Vec::new(),
}
}
pub const fn background(&self) -> &Option<FillShader> {
&self.background
}
pub const fn bottom_layer_changed(&self) -> bool {
self.bottom_layer_changed
}
pub fn bottom_layer(&self) -> &[Entity<P, I, L>] {
&self.bottom_layer
}
pub fn top_layer(&self) -> &[Entity<P, I, L>] {
&self.top_layer
}
pub fn rectangle(&self) -> Handle<P> {
self.rectangle.share()
}
pub fn set_background(&mut self, background: Option<FillShader>) {
self.background = background;
}
pub fn bottom_layer_mut(&mut self) -> &mut Vec<Entity<P, I, L>> {
&mut self.bottom_layer
}
pub fn top_layer_mut(&mut self) -> &mut Vec<Entity<P, I, L>> {
&mut self.top_layer
}
pub fn clear(&mut self) {
self.bottom_layer.clear();
self.top_layer.clear();
}
pub fn recalculate_if_bottom_layer_changed(&mut self) {
let new_hash = calculate_hash(&self.background, &self.bottom_layer);
self.bottom_layer_changed = new_hash != self.bottom_hash;
self.bottom_hash = new_hash;
}
pub fn layer_mut(&mut self, layer: Layer) -> &mut Vec<Entity<P, I, L>> {
match layer {
Layer::Bottom => &mut self.bottom_layer,
Layer::Top => &mut self.top_layer,
}
}
}