2020-05-24 09:36:24 +00:00
|
|
|
use ahash::AHashMap;
|
2020-05-10 12:27:02 +00:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
|
|
|
|
2020-05-19 20:28:57 +00:00
|
|
|
use crate::{math::Rect, paint::PaintCmd, Id};
|
2020-04-17 12:26:36 +00:00
|
|
|
|
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
|
2020-04-17 12:26:36 +00:00
|
|
|
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-04-17 12:26:36 +00:00
|
|
|
}
|
|
|
|
|
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"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)]
|
2020-05-24 09:36:24 +00:00
|
|
|
pub struct GraphicLayers(AHashMap<Layer, PaintList>);
|
2020-04-17 12:26:36 +00:00
|
|
|
|
|
|
|
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-04-17 12:26:36 +00:00
|
|
|
}
|
|
|
|
|
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-04-17 12:26:36 +00:00
|
|
|
|
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-04-17 21:22:28 +00:00
|
|
|
}
|
2020-04-17 12:26:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 12:27:02 +00:00
|
|
|
if let Some(commands) = self.0.get_mut(&Layer::debug()) {
|
|
|
|
all_commands.extend(commands.drain(..));
|
|
|
|
}
|
|
|
|
|
2020-04-17 12:26:36 +00:00
|
|
|
all_commands.into_iter()
|
|
|
|
}
|
|
|
|
}
|