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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use ahash::AHashMap;
use crate::{math::Rect, paint::PaintCmd, Id, *};
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Order {
Background,
Middle,
Foreground,
Tooltip,
Debug,
}
impl Order {
const COUNT: usize = 5;
const ALL: [Order; Self::COUNT] = [
Self::Background,
Self::Middle,
Self::Foreground,
Self::Tooltip,
Self::Debug,
];
pub fn allow_interaction(&self) -> bool {
match self {
Self::Background | Self::Middle | Self::Foreground | Self::Debug => true,
Self::Tooltip => false,
}
}
}
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct LayerId {
pub order: Order,
pub id: Id,
}
impl LayerId {
pub fn new(order: Order, id: Id) -> Self {
Self { order, id }
}
pub fn debug() -> Self {
Self {
order: Order::Debug,
id: Id::new("debug"),
}
}
pub fn background() -> Self {
Self {
order: Order::Background,
id: Id::background(),
}
}
pub fn allow_interaction(&self) -> bool {
self.order.allow_interaction()
}
}
#[derive(Clone, Copy, PartialEq)]
pub struct PaintCmdIdx(usize);
#[derive(Clone, Default)]
pub struct PaintList(Vec<(Rect, PaintCmd)>);
impl PaintList {
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn add(&mut self, clip_rect: Rect, cmd: PaintCmd) -> PaintCmdIdx {
let idx = PaintCmdIdx(self.0.len());
self.0.push((clip_rect, cmd));
idx
}
pub fn extend(&mut self, clip_rect: Rect, mut cmds: Vec<PaintCmd>) {
self.0.extend(cmds.drain(..).map(|cmd| (clip_rect, cmd)))
}
pub fn set(&mut self, idx: PaintCmdIdx, clip_rect: Rect, cmd: PaintCmd) {
assert!(idx.0 < self.0.len());
self.0[idx.0] = (clip_rect, cmd);
}
pub fn translate(&mut self, delta: Vec2) {
for (clip_rect, cmd) in &mut self.0 {
*clip_rect = clip_rect.translate(delta);
cmd.translate(delta);
}
}
}
#[derive(Clone, Default)]
pub struct GraphicLayers([AHashMap<Id, PaintList>; Order::COUNT]);
impl GraphicLayers {
pub fn list(&mut self, layer_id: LayerId) -> &mut PaintList {
self.0[layer_id.order as usize]
.entry(layer_id.id)
.or_default()
}
pub fn drain(
&mut self,
area_order: &[LayerId],
) -> impl ExactSizeIterator<Item = (Rect, PaintCmd)> {
let mut all_commands: Vec<_> = Default::default();
for &order in &Order::ALL {
let order_map = &mut self.0[order as usize];
order_map.retain(|_, list| !list.is_empty());
for layer_id in area_order {
if layer_id.order == order {
if let Some(commands) = order_map.get_mut(&layer_id.id) {
all_commands.extend(commands.0.drain(..));
}
}
}
for commands in order_map.values_mut() {
all_commands.extend(commands.0.drain(..));
}
}
all_commands.into_iter()
}
}