scene.rs

  1use crate::{color::ColorU, geometry::rect::RectF};
  2pub struct Scene {
  3    scale_factor: f32,
  4    layers: Vec<Layer>,
  5    active_layer_stack: Vec<usize>,
  6}
  7
  8#[derive(Default)]
  9pub struct Layer {
 10    clip_bounds: Option<RectF>,
 11    quads: Vec<Quad>,
 12}
 13
 14#[derive(Default, Debug)]
 15pub struct Quad {
 16    pub bounds: RectF,
 17    pub background: Option<ColorU>,
 18    pub border: Border,
 19    pub corder_radius: f32,
 20}
 21
 22#[derive(Clone, Copy, Default, Debug)]
 23pub struct Border {
 24    pub width: f32,
 25    pub color: Option<ColorU>,
 26    pub top: bool,
 27    pub right: bool,
 28    pub bottom: bool,
 29    pub left: bool,
 30}
 31
 32impl Scene {
 33    pub fn new(scale_factor: f32) -> Self {
 34        Scene {
 35            scale_factor,
 36            layers: vec![Layer::default()],
 37            active_layer_stack: vec![0],
 38        }
 39    }
 40
 41    pub fn layers(&self) -> &[Layer] {
 42        self.layers.as_slice()
 43    }
 44
 45    // pub fn push_layer(&mut self, clip_bounds: Option<RectF>) {
 46
 47    // }
 48
 49    // pub fn pop_layer(&mut self) {
 50    //     assert!(self.active_layer_stack.len() > 1);
 51    //     self.active_layer_stack.pop();
 52    // }
 53
 54    pub fn push_quad(&mut self, quad: Quad) {
 55        self.active_layer().push_quad(quad)
 56    }
 57
 58    fn active_layer(&mut self) -> &mut Layer {
 59        &mut self.layers[*self.active_layer_stack.last().unwrap()]
 60    }
 61}
 62
 63impl Layer {
 64    fn push_quad(&mut self, quad: Quad) {
 65        self.quads.push(quad);
 66    }
 67
 68    pub fn quads(&self) -> &[Quad] {
 69        self.quads.as_slice()
 70    }
 71}
 72
 73impl Border {
 74    pub fn new(width: f32, color: impl Into<ColorU>) -> Self {
 75        Self {
 76            width,
 77            color: Some(color.into()),
 78            top: false,
 79            left: false,
 80            bottom: false,
 81            right: false,
 82        }
 83    }
 84
 85    pub fn all(width: f32, color: impl Into<ColorU>) -> Self {
 86        Self {
 87            width,
 88            color: Some(color.into()),
 89            top: true,
 90            left: true,
 91            bottom: true,
 92            right: true,
 93        }
 94    }
 95
 96    pub fn top(width: f32, color: impl Into<ColorU>) -> Self {
 97        let mut border = Self::new(width, color);
 98        border.top = true;
 99        border
100    }
101
102    pub fn left(width: f32, color: impl Into<ColorU>) -> Self {
103        let mut border = Self::new(width, color);
104        border.left = true;
105        border
106    }
107
108    pub fn bottom(width: f32, color: impl Into<ColorU>) -> Self {
109        let mut border = Self::new(width, color);
110        border.bottom = true;
111        border
112    }
113
114    pub fn right(width: f32, color: impl Into<ColorU>) -> Self {
115        let mut border = Self::new(width, color);
116        border.right = true;
117        border
118    }
119
120    pub fn with_sides(mut self, top: bool, left: bool, bottom: bool, right: bool) -> Self {
121        self.top = top;
122        self.left = left;
123        self.bottom = bottom;
124        self.right = right;
125        self
126    }
127
128    fn all_sides(&self) -> bool {
129        self.top && self.left && self.bottom && self.right
130    }
131}