1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::collections::HashMap;
use crate::graphics::vertex::Vertex2d;
use super::{Layer, LayerHandle, LayerStack};
impl LayerStack {
pub fn new() -> Self {
Self {
layers: HashMap::new(),
render_order: vec![],
}
}
pub fn add_layer_to_top(&mut self) -> LayerHandle {
let handle = LayerHandle::generate();
self.layers.insert(handle, Layer::empty());
self.render_order.push(handle);
handle
}
pub fn add_layer_to_bottom(&mut self) -> LayerHandle {
let handle = LayerHandle::generate();
self.layers.insert(handle, Layer::empty());
if self.render_order.is_empty() {
self.render_order.push(handle);
} else {
self.render_order.insert(0, handle);
}
handle
}
pub fn layers(&self) -> Vec<&Layer> {
self.render_order
.iter()
.map(|handle| self.layers.get(handle).unwrap())
.collect::<Vec<&Layer>>()
}
pub fn get_layer_mut(
&mut self,
handle: &LayerHandle,
) -> Option<&mut Layer> {
self.layers.get_mut(handle)
}
pub fn vertices(&self) -> Vec<&[Vertex2d]> {
let layers: Vec<&Layer> = self
.render_order
.iter()
.map(|handle| self.layers.get(handle).unwrap())
.collect();
let mut verts: Vec<&[Vertex2d]> = vec![];
for layer in layers {
verts.reserve(layer.batches.len());
for batch in &layer.batches {
verts.push(&batch.vertices);
}
}
verts
}
}