2021-03-23 14:15:41 +00:00
|
|
|
use crate::{
|
|
|
|
color::ColorU,
|
|
|
|
fonts::{FontId, GlyphId},
|
|
|
|
geometry::{rect::RectF, vector::Vector2F},
|
|
|
|
};
|
2021-03-21 15:19:12 +00:00
|
|
|
|
2021-03-19 19:31:25 +00:00
|
|
|
pub struct Scene {
|
|
|
|
scale_factor: f32,
|
|
|
|
layers: Vec<Layer>,
|
|
|
|
active_layer_stack: Vec<usize>,
|
|
|
|
}
|
|
|
|
|
2021-03-23 02:54:52 +00:00
|
|
|
#[derive(Default, Debug)]
|
2021-03-21 04:15:04 +00:00
|
|
|
pub struct Layer {
|
2021-03-19 19:31:25 +00:00
|
|
|
clip_bounds: Option<RectF>,
|
|
|
|
quads: Vec<Quad>,
|
2021-03-22 17:05:16 +00:00
|
|
|
shadows: Vec<Shadow>,
|
2021-03-23 14:15:41 +00:00
|
|
|
glyphs: Vec<Glyph>,
|
2021-03-19 19:31:25 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 04:15:04 +00:00
|
|
|
#[derive(Default, Debug)]
|
2021-03-19 19:31:25 +00:00
|
|
|
pub struct Quad {
|
|
|
|
pub bounds: RectF,
|
|
|
|
pub background: Option<ColorU>,
|
|
|
|
pub border: Border,
|
2021-03-21 17:38:23 +00:00
|
|
|
pub corner_radius: f32,
|
2021-03-19 19:31:25 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 02:54:52 +00:00
|
|
|
#[derive(Debug)]
|
2021-03-22 17:05:16 +00:00
|
|
|
pub struct Shadow {
|
|
|
|
pub bounds: RectF,
|
|
|
|
pub corner_radius: f32,
|
|
|
|
pub sigma: f32,
|
|
|
|
pub color: ColorU,
|
|
|
|
}
|
|
|
|
|
2021-03-23 14:15:41 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Glyph {
|
|
|
|
pub font_id: FontId,
|
|
|
|
pub font_size: f32,
|
|
|
|
pub glyph_id: GlyphId,
|
|
|
|
pub origin: Vector2F,
|
|
|
|
pub color: ColorU,
|
|
|
|
}
|
|
|
|
|
2021-03-21 04:15:04 +00:00
|
|
|
#[derive(Clone, Copy, Default, Debug)]
|
2021-03-19 19:31:25 +00:00
|
|
|
pub struct Border {
|
|
|
|
pub width: f32,
|
|
|
|
pub color: Option<ColorU>,
|
|
|
|
pub top: bool,
|
|
|
|
pub right: bool,
|
|
|
|
pub bottom: bool,
|
|
|
|
pub left: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Scene {
|
|
|
|
pub fn new(scale_factor: f32) -> Self {
|
|
|
|
Scene {
|
|
|
|
scale_factor,
|
|
|
|
layers: vec![Layer::default()],
|
|
|
|
active_layer_stack: vec![0],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 15:19:12 +00:00
|
|
|
pub fn scale_factor(&self) -> f32 {
|
|
|
|
self.scale_factor
|
|
|
|
}
|
|
|
|
|
2021-03-21 04:15:04 +00:00
|
|
|
pub fn layers(&self) -> &[Layer] {
|
|
|
|
self.layers.as_slice()
|
2021-03-19 19:31:25 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 17:55:53 +00:00
|
|
|
pub fn push_layer(&mut self) {
|
|
|
|
let ix = self.layers.len();
|
|
|
|
self.layers.push(Layer::default());
|
|
|
|
self.active_layer_stack.push(ix);
|
|
|
|
}
|
2021-03-21 04:15:04 +00:00
|
|
|
|
2021-03-22 17:55:53 +00:00
|
|
|
pub fn pop_layer(&mut self) {
|
|
|
|
assert!(self.active_layer_stack.len() > 1);
|
|
|
|
self.active_layer_stack.pop();
|
|
|
|
}
|
2021-03-21 04:15:04 +00:00
|
|
|
|
2021-03-19 19:31:25 +00:00
|
|
|
pub fn push_quad(&mut self, quad: Quad) {
|
|
|
|
self.active_layer().push_quad(quad)
|
|
|
|
}
|
|
|
|
|
2021-03-22 17:05:16 +00:00
|
|
|
pub fn push_shadow(&mut self, shadow: Shadow) {
|
|
|
|
self.active_layer().push_shadow(shadow)
|
|
|
|
}
|
|
|
|
|
2021-03-23 14:15:41 +00:00
|
|
|
pub fn push_glyph(&mut self, glyph: Glyph) {
|
|
|
|
self.active_layer().push_glyph(glyph)
|
|
|
|
}
|
|
|
|
|
2021-03-19 19:31:25 +00:00
|
|
|
fn active_layer(&mut self) -> &mut Layer {
|
|
|
|
&mut self.layers[*self.active_layer_stack.last().unwrap()]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Layer {
|
|
|
|
fn push_quad(&mut self, quad: Quad) {
|
|
|
|
self.quads.push(quad);
|
|
|
|
}
|
2021-03-21 04:15:04 +00:00
|
|
|
|
|
|
|
pub fn quads(&self) -> &[Quad] {
|
|
|
|
self.quads.as_slice()
|
|
|
|
}
|
2021-03-22 17:05:16 +00:00
|
|
|
|
|
|
|
fn push_shadow(&mut self, shadow: Shadow) {
|
|
|
|
self.shadows.push(shadow);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shadows(&self) -> &[Shadow] {
|
|
|
|
self.shadows.as_slice()
|
|
|
|
}
|
2021-03-23 14:15:41 +00:00
|
|
|
|
|
|
|
fn push_glyph(&mut self, glyph: Glyph) {
|
|
|
|
self.glyphs.push(glyph);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn glyphs(&self) -> &[Glyph] {
|
|
|
|
self.glyphs.as_slice()
|
|
|
|
}
|
2021-03-19 19:31:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Border {
|
|
|
|
pub fn new(width: f32, color: impl Into<ColorU>) -> Self {
|
|
|
|
Self {
|
|
|
|
width,
|
|
|
|
color: Some(color.into()),
|
|
|
|
top: false,
|
|
|
|
left: false,
|
|
|
|
bottom: false,
|
|
|
|
right: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn all(width: f32, color: impl Into<ColorU>) -> Self {
|
|
|
|
Self {
|
|
|
|
width,
|
|
|
|
color: Some(color.into()),
|
|
|
|
top: true,
|
|
|
|
left: true,
|
|
|
|
bottom: true,
|
|
|
|
right: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn top(width: f32, color: impl Into<ColorU>) -> Self {
|
|
|
|
let mut border = Self::new(width, color);
|
|
|
|
border.top = true;
|
|
|
|
border
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn left(width: f32, color: impl Into<ColorU>) -> Self {
|
|
|
|
let mut border = Self::new(width, color);
|
|
|
|
border.left = true;
|
|
|
|
border
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bottom(width: f32, color: impl Into<ColorU>) -> Self {
|
|
|
|
let mut border = Self::new(width, color);
|
|
|
|
border.bottom = true;
|
|
|
|
border
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn right(width: f32, color: impl Into<ColorU>) -> Self {
|
|
|
|
let mut border = Self::new(width, color);
|
|
|
|
border.right = true;
|
|
|
|
border
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_sides(mut self, top: bool, left: bool, bottom: bool, right: bool) -> Self {
|
|
|
|
self.top = top;
|
|
|
|
self.left = left;
|
|
|
|
self.bottom = bottom;
|
|
|
|
self.right = right;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn all_sides(&self) -> bool {
|
|
|
|
self.top && self.left && self.bottom && self.right
|
|
|
|
}
|
|
|
|
}
|