1//! Draw and stack layers of graphical primitives.
2use crate::core::{Rectangle, Transformation};
34/// A layer of graphical primitives.
5///
6/// Layers normally dictate a set of primitives that are
7/// rendered in a specific order.
8pub trait Layer: Default {
9/// Creates a new [`Layer`] with the given bounds.
10fn with_bounds(bounds: Rectangle) -> Self;
1112/// Flushes and settles any pending group of primitives in the [`Layer`].
13 ///
14 /// This will be called when a [`Layer`] is finished. It allows layers to efficiently
15 /// record primitives together and defer grouping until the end.
16fn flush(&mut self);
1718/// Resizes the [`Layer`] to the given bounds.
19fn resize(&mut self, bounds: Rectangle);
2021/// Clears all the layers contents and resets its bounds.
22fn reset(&mut self);
23}
2425/// A stack of layers used for drawing.
26#[derive(Debug)]
27pub struct Stack<T: Layer> {
28 layers: Vec<T>,
29 transformations: Vec<Transformation>,
30 previous: Vec<usize>,
31 current: usize,
32 active_count: usize,
33}
3435impl<T: Layer> Stack<T> {
36/// Creates a new empty [`Stack`].
37pub fn new() -> Self {
38Self {
39 layers: vec![T::default()],
40 transformations: vec![Transformation::IDENTITY],
41 previous: vec![],
42 current: 0,
43 active_count: 1,
44 }
45 }
4647/// Returns a mutable reference to the current [`Layer`] of the [`Stack`], together with
48 /// the current [`Transformation`].
49#[inline]
50pub fn current_mut(&mut self) -> (&mut T, Transformation) {
51let transformation = self.transformation();
5253 (&mut self.layers[self.current], transformation)
54 }
5556/// Returns the current [`Transformation`] of the [`Stack`].
57#[inline]
58pub fn transformation(&self) -> Transformation {
59self.transformations.last().copied().unwrap()
60 }
6162/// Pushes a new clipping region in the [`Stack`]; creating a new layer in the
63 /// process.
64pub fn push_clip(&mut self, bounds: Rectangle) {
65self.previous.push(self.current);
6667self.current = self.active_count;
68self.active_count += 1;
6970let bounds = bounds * self.transformation();
7172if self.current == self.layers.len() {
73self.layers.push(T::with_bounds(bounds));
74 } else {
75self.layers[self.current].resize(bounds);
76 }
77 }
7879/// Pops the current clipping region from the [`Stack`] and restores the previous one.
80 ///
81 /// The current layer will be recorded for drawing.
82pub fn pop_clip(&mut self) {
83self.flush();
8485self.current = self.previous.pop().unwrap();
86 }
8788/// Pushes a new [`Transformation`] in the [`Stack`].
89 ///
90 /// Future drawing operations will be affected by this new [`Transformation`] until
91 /// it is popped using [`pop_transformation`].
92 ///
93 /// [`pop_transformation`]: Self::pop_transformation
94pub fn push_transformation(&mut self, transformation: Transformation) {
95self.transformations
96 .push(self.transformation() * transformation);
97 }
9899/// Pops the current [`Transformation`] in the [`Stack`].
100pub fn pop_transformation(&mut self) {
101let _ = self.transformations.pop();
102 }
103104/// Returns an iterator over mutable references to the layers in the [`Stack`].
105pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
106self.flush();
107108self.layers[..self.active_count].iter_mut()
109 }
110111/// Returns an iterator over immutable references to the layers in the [`Stack`].
112pub fn iter(&self) -> impl Iterator<Item = &T> {
113self.layers[..self.active_count].iter()
114 }
115116/// Returns the slice of layers in the [`Stack`].
117pub fn as_slice(&self) -> &[T] {
118&self.layers[..self.active_count]
119 }
120121/// Flushes and settles any primitives in the current layer of the [`Stack`].
122pub fn flush(&mut self) {
123self.layers[self.current].flush();
124 }
125126/// Clears the layers of the [`Stack`], allowing reuse.
127 ///
128 /// This will normally keep layer allocations for future drawing operations.
129pub fn clear(&mut self) {
130for layer in self.layers[..self.active_count].iter_mut() {
131 layer.reset();
132 }
133134self.current = 0;
135self.active_count = 1;
136self.previous.clear();
137 }
138}
139140impl<T: Layer> Default for Stack<T> {
141fn default() -> Self {
142Self::new()
143 }
144}