2020-04-17 12:26:36 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2020-04-20 21:33:16 +00:00
|
|
|
use crate::{math::Rect, Id, PaintCmd};
|
2020-04-17 12:26:36 +00:00
|
|
|
|
|
|
|
// TODO: support multiple windows
|
|
|
|
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
|
|
|
|
pub enum Layer {
|
|
|
|
Background,
|
|
|
|
Window(Id),
|
2020-04-17 21:22:28 +00:00
|
|
|
/// Tooltips etc
|
2020-04-17 12:26:36 +00:00
|
|
|
Popup,
|
2020-04-27 14:53:14 +00:00
|
|
|
/// Debug text
|
|
|
|
Debug,
|
2020-04-17 12:26:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:47:03 +00:00
|
|
|
/// Each `PaintCmd` is paired with a clip rectangle.
|
2020-04-20 21:33:16 +00:00
|
|
|
type PaintList = Vec<(Rect, PaintCmd)>;
|
2020-04-17 12:26:36 +00:00
|
|
|
|
|
|
|
/// TODO: improve this
|
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub struct GraphicLayers {
|
|
|
|
bg: PaintList,
|
|
|
|
windows: HashMap<Id, PaintList>,
|
|
|
|
popup: PaintList,
|
2020-04-27 14:53:14 +00:00
|
|
|
debug: PaintList,
|
2020-04-17 12:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-27 14:53:14 +00:00
|
|
|
Layer::Debug => &mut self.debug,
|
2020-04-17 12:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:33:16 +00:00
|
|
|
pub fn drain(
|
|
|
|
&mut self,
|
2020-05-10 08:32:28 +00:00
|
|
|
window_order: &[Id],
|
2020-04-20 21:33:16 +00:00
|
|
|
) -> impl ExactSizeIterator<Item = (Rect, PaintCmd)> {
|
2020-04-17 12:26:36 +00:00
|
|
|
let mut all_commands: Vec<_> = self.bg.drain(..).collect();
|
|
|
|
|
2020-05-10 08:32:28 +00:00
|
|
|
for id in window_order {
|
2020-04-17 21:22:28 +00:00
|
|
|
if let Some(window) = self.windows.get_mut(id) {
|
|
|
|
all_commands.extend(window.drain(..));
|
|
|
|
}
|
2020-04-17 12:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
all_commands.extend(self.popup.drain(..));
|
2020-04-27 14:53:14 +00:00
|
|
|
all_commands.extend(self.debug.drain(..));
|
2020-04-17 12:26:36 +00:00
|
|
|
all_commands.into_iter()
|
|
|
|
}
|
|
|
|
}
|