egui/emigui/src/layers.rs

50 lines
1.2 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
2020-04-20 21:33:16 +00:00
use crate::{math::Rect, Id, PaintCmd};
// TODO: support multiple windows
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
pub enum Layer {
Background,
Window(Id),
/// Tooltips etc
Popup,
}
2020-04-20 21:33:16 +00:00
/// Each PaintCmd is paired with a clip rectangle.
type PaintList = Vec<(Rect, PaintCmd)>;
/// TODO: improve this
#[derive(Clone, Default)]
pub struct GraphicLayers {
bg: PaintList,
windows: HashMap<Id, PaintList>,
popup: PaintList,
}
impl GraphicLayers {
pub fn layer(&mut self, layer: Layer) -> &mut PaintList {
match layer {
Layer::Background => &mut self.bg,
Layer::Window(id) => self.windows.entry(id).or_default(),
Layer::Popup => &mut self.popup,
}
}
2020-04-20 21:33:16 +00:00
pub fn drain(
&mut self,
window_oreder: &[Id],
) -> impl ExactSizeIterator<Item = (Rect, PaintCmd)> {
let mut all_commands: Vec<_> = self.bg.drain(..).collect();
for id in window_oreder {
if let Some(window) = self.windows.get_mut(id) {
all_commands.extend(window.drain(..));
}
}
all_commands.extend(self.popup.drain(..));
all_commands.into_iter()
}
}