Add ZOffset

This commit is contained in:
Emil Ernerfeldt 2023-01-26 15:59:20 +01:00
parent 8fac6df249
commit ac6344c104
3 changed files with 41 additions and 20 deletions

View file

@ -117,8 +117,11 @@ impl AreaLayerId {
}
}
/// Represents the relative order an element should be displayed
/// Lower values render first, and therefore appear below higher values
// ----------------------------------------------------------------------------
/// Represents the relative order an element should be displayed.
///
/// Lower values render first, and therefore appear below higher values.
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct ZOrder(pub i32);
@ -155,6 +158,27 @@ impl Default for ZOrder {
}
}
impl std::ops::Add<ZOffset> for ZOrder {
type Output = ZOrder;
fn add(self, offset: ZOffset) -> Self::Output {
Self(self.0.saturating_add(offset))
}
}
impl std::ops::AddAssign<ZOffset> for ZOrder {
fn add_assign(&mut self, offset: ZOffset) {
self.0 = self.0.saturating_add(offset);
}
}
// ----------------------------------------------------------------------------
/// Offset within a [`ZOrder`].
pub type ZOffset = i32;
// ----------------------------------------------------------------------------
/// An identifier for a paint layer which supports Z-indexing
///
/// This says: draw on [`AreaLayerId`] with index z. This only affects the display

View file

@ -361,7 +361,7 @@ pub use {
grid::Grid,
id::{Id, IdMap},
input_state::{InputState, MultiTouchInfo, PointerState},
layers::{AreaLayerId, Order},
layers::{AreaLayerId, Order, ZOffset, ZOrder},
layout::*,
memory::{Memory, Options},
painter::Painter,

View file

@ -3,7 +3,7 @@ use std::sync::Arc;
use crate::{
emath::{Align2, Pos2, Rect, Vec2},
layers::{PaintList, ShapeIdx, ZLayer, ZOrder},
layers::{PaintList, ShapeIdx, ZLayer, ZOffset, ZOrder},
AreaLayerId, Color32, Context, FontId,
};
use epaint::{
@ -42,28 +42,25 @@ impl Painter {
}
}
/// Redirect where you are painting.
#[must_use]
pub fn with_z_layer(self, z_layer: ZLayer) -> Self {
Self {
ctx: self.ctx,
z_layer,
clip_rect: self.clip_rect,
fade_to_color: None,
}
}
/// Redirect where you are painting with default z-index
#[must_use]
pub fn with_layer_id(self, layer: AreaLayerId) -> Self {
self.with_z_layer(ZLayer::from_area_layer(layer))
pub fn with_layer_id(mut self, layer: AreaLayerId) -> Self {
self.z_layer = ZLayer::from_area_layer(layer);
self
}
/// Redirect z-index
#[must_use]
pub fn with_z(self, z: ZOrder) -> Self {
let layer = self.z_layer.area_layer.with_z(z);
self.with_z_layer(layer)
pub fn with_z(mut self, z: ZOrder) -> Self {
self.z_layer.z = z;
self
}
/// Modify z-index
#[must_use]
pub fn with_z_offset(mut self, z_offset: ZOffset) -> Self {
self.z_layer.z += z_offset;
self
}
/// Create a painter for a sub-region of this [`Painter`].