Skip to content

Commit fab214a

Browse files
committed
nuon: Overlay layers
1 parent d477668 commit fab214a

1 file changed

Lines changed: 53 additions & 11 deletions

File tree

nuon/src/lib.rs

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,27 @@ impl LayerData {
145145
}
146146
}
147147

148+
#[derive(Debug, Clone, Copy)]
149+
enum LayerId {
150+
Regular(usize),
151+
Overlay(usize),
152+
}
153+
148154
#[derive(Debug)]
149155
pub struct LayerStack {
150156
layers: Vec<LayerData>,
151-
history_stack: Vec<usize>,
152-
curr: usize,
157+
overlay_layers: Vec<LayerData>,
158+
history_stack: Vec<LayerId>,
159+
curr: LayerId,
153160
}
154161

155162
impl LayerStack {
156163
fn new() -> Self {
157164
LayerStack {
158165
layers: vec![LayerData::default()],
166+
overlay_layers: vec![],
159167
history_stack: vec![],
160-
curr: 0,
168+
curr: LayerId::Regular(0),
161169
}
162170
}
163171

@@ -166,11 +174,11 @@ impl LayerStack {
166174
}
167175

168176
pub fn len(&self) -> usize {
169-
self.layers.len()
177+
self.layers.len() + self.overlay_layers.len()
170178
}
171179

172180
pub fn iter(&self) -> impl Iterator<Item = &LayerData> {
173-
self.layers.iter()
181+
self.layers.iter().chain(self.overlay_layers.iter())
174182
}
175183

176184
pub fn current_scissor_rect(&self) -> Option<Rect> {
@@ -179,17 +187,38 @@ impl LayerStack {
179187
}
180188

181189
pub fn current(&self) -> &LayerData {
182-
&self.layers[self.curr]
190+
match self.curr {
191+
LayerId::Regular(id) => &self.layers[id],
192+
LayerId::Overlay(id) => &self.overlay_layers[id],
193+
}
183194
}
184195

185196
pub fn current_mut(&mut self) -> &mut LayerData {
186-
&mut self.layers[self.curr]
197+
match self.curr {
198+
LayerId::Regular(id) => &mut self.layers[id],
199+
LayerId::Overlay(id) => &mut self.overlay_layers[id],
200+
}
187201
}
188202

189203
pub fn push(&mut self) {
190204
self.history_stack.push(self.curr);
191-
self.curr = self.layers.len();
192-
self.layers.push(LayerData::default());
205+
206+
match self.curr {
207+
LayerId::Regular(_) => {
208+
self.curr = LayerId::Regular(self.layers.len());
209+
self.layers.push(LayerData::default());
210+
}
211+
LayerId::Overlay(_) => {
212+
self.curr = LayerId::Overlay(self.overlay_layers.len());
213+
self.overlay_layers.push(LayerData::default());
214+
}
215+
}
216+
}
217+
218+
pub fn push_overlay(&mut self) {
219+
self.history_stack.push(self.curr);
220+
self.curr = LayerId::Overlay(self.overlay_layers.len());
221+
self.overlay_layers.push(LayerData::default());
193222
}
194223

195224
pub fn pop(&mut self) {
@@ -202,7 +231,9 @@ impl LayerStack {
202231
self.layers[0].clear();
203232
// TODO: Reuse the memory from all dropped layers
204233
self.layers.drain(1..);
205-
self.curr = 0;
234+
self.overlay_layers.clear();
235+
236+
self.curr = LayerId::Regular(0);
206237
}
207238
}
208239

@@ -355,6 +386,7 @@ pub fn translate() -> Translate {
355386
#[derive(Debug, Clone, Default)]
356387
pub struct Layer {
357388
rect: Rect,
389+
overlay: bool,
358390
}
359391

360392
impl Layer {
@@ -367,6 +399,11 @@ impl Layer {
367399
self
368400
}
369401

402+
pub fn overlay(mut self, overlay: bool) -> Self {
403+
self.overlay = overlay;
404+
self
405+
}
406+
370407
pub fn build(&self, ui: &mut Ui, build: impl FnOnce(&mut Ui)) {
371408
let rect = if self.rect == Rect::zero() {
372409
ui.layers.current_mut().scissor_rect
@@ -377,7 +414,12 @@ impl Layer {
377414
)
378415
};
379416

380-
ui.layers.push();
417+
if self.overlay {
418+
ui.layers.push_overlay();
419+
} else {
420+
ui.layers.push();
421+
}
422+
381423
ui.layers.current_mut().scissor_rect = rect;
382424

383425
build(ui);

0 commit comments

Comments
 (0)