scene.rs

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