egui/emigui/src/layers.rs

67 lines
1.7 KiB
Rust
Raw Normal View History

use ahash::AHashMap;
2020-05-10 12:27:02 +00:00
use serde_derive::{Deserialize, Serialize};
use crate::{math::Rect, paint::PaintCmd, Id};
2020-05-10 12:27:02 +00:00
/// Different layer categories
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Deserialize, Serialize)]
pub enum Order {
/// Painted behind all floating windows
Background,
2020-05-10 12:27:02 +00:00
/// Normal moveable windows that you reorder by click
Middle,
/// Popups, menus etc that should always be painted on top of windows
Foreground,
/// Debug layer, always painted last / on top
2020-04-27 14:53:14 +00:00
Debug,
}
2020-05-10 12:27:02 +00:00
/// An ideintifer for a paint layer.
/// Also acts as an identifier for `Area`:s.
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Deserialize, Serialize)]
pub struct Layer {
pub order: Order,
pub id: Id,
}
impl Layer {
pub fn debug() -> Self {
Self {
order: Order::Debug,
id: Id::new("debug"),
}
}
}
/// Each `PaintCmd` is paired with a clip rectangle.
2020-04-20 21:33:16 +00:00
type PaintList = Vec<(Rect, PaintCmd)>;
/// TODO: improve this
#[derive(Clone, Default)]
pub struct GraphicLayers(AHashMap<Layer, PaintList>);
impl GraphicLayers {
pub fn layer(&mut self, layer: Layer) -> &mut PaintList {
2020-05-10 12:27:02 +00:00
self.0.entry(layer).or_default()
}
2020-05-10 12:27:02 +00:00
pub fn drain(
&mut self,
area_order: &[Layer],
) -> impl ExactSizeIterator<Item = (Rect, PaintCmd)> {
let mut all_commands: Vec<_> = Default::default();
2020-05-10 12:27:02 +00:00
for layer in area_order {
if let Some(commands) = self.0.get_mut(layer) {
all_commands.extend(commands.drain(..));
}
}
2020-05-10 12:27:02 +00:00
if let Some(commands) = self.0.get_mut(&Layer::debug()) {
all_commands.extend(commands.drain(..));
}
all_commands.into_iter()
}
}