From 7894cd4dfdd689b98f4929fa65a950882ee41e15 Mon Sep 17 00:00:00 2001 From: charburgx Date: Sat, 17 Dec 2022 20:18:03 -0600 Subject: [PATCH] zorder/zlayer utilities --- crates/egui/src/layers.rs | 70 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/crates/egui/src/layers.rs b/crates/egui/src/layers.rs index 584361a6..02aa4a0f 100644 --- a/crates/egui/src/layers.rs +++ b/crates/egui/src/layers.rs @@ -118,12 +118,50 @@ impl AreaLayerId { pub struct ZOrder(pub i32); impl ZOrder { - const DEFAULT: ZOrder = ZOrder(0); + const BASE: ZOrder = ZOrder(0); + const ABOVE_ALL: ZOrder = ZOrder(i32::MAX); + const BELOW_ALL: ZOrder = ZOrder(i32::MIN); + + /// Base z-order, corresponding to a level of 0 + #[inline(always)] + pub fn base() -> Self { + Self::BASE + } + + /// Maximal z-order - nothing on the same area layer can render above this + #[inline(always)] + pub fn above_all() -> Self { + Self::ABOVE_ALL + } + + /// Minimal z-order - nothing on the same area layer can render below this + #[inline(always)] + pub fn below_all() -> Self { + Self::BELOW_ALL + } + + /// Above by one level + pub fn above(self) -> Self { + self.above_by(1) + } + + /// Below by one level + pub fn below(self) -> Self { + self.below_by(1) + } + + pub fn above_by(self, levels: i32) -> Self { + Self(self.0.saturating_add(levels)) + } + + pub fn below_by(self, levels: i32) -> Self { + Self(self.0.saturating_sub(levels)) + } } impl Default for ZOrder { fn default() -> Self { - Self::DEFAULT + Self::BASE } } @@ -147,6 +185,7 @@ impl ZLayer { Self { area_layer, z } } + /// Use base Z-level pub fn from_area_layer(area_layer: AreaLayerId) -> Self { Self::from_area_layer_z(area_layer, ZOrder::default()) } @@ -158,6 +197,33 @@ impl ZLayer { pub fn background() -> Self { Self::from_area_layer(AreaLayerId::background()) } + + #[must_use] + pub fn with_z(self, z: ZOrder) -> Self { + Self::from_area_layer_z(self.area_layer, z) + } + + /// Above by one level + #[must_use] + pub fn above(self) -> Self { + self.with_z(self.z.above()) + } + + #[must_use] + pub fn above_by(self, levels: i32) -> Self { + self.with_z(self.z.above_by(levels)) + } + + /// Below one level + #[must_use] + pub fn below(self) -> Self { + self.with_z(self.z.below()) + } + + #[must_use] + pub fn below_by(self, levels: i32) -> Self { + self.with_z(self.z.below_by(levels)) + } } /// A unique identifier of a specific [`Shape`] in a [`PaintList`].