From 3f02c7d698ca7fe9a069d0579fc72e622f00b38f Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 4 May 2023 12:27:13 +0200 Subject: [PATCH] Add Rangef to emath --- crates/egui_extras/src/dock/branch/grid.rs | 47 +----------- crates/emath/src/lib.rs | 2 + crates/emath/src/range.rs | 84 ++++++++++++++++++++++ 3 files changed, 88 insertions(+), 45 deletions(-) create mode 100644 crates/emath/src/range.rs diff --git a/crates/egui_extras/src/dock/branch/grid.rs b/crates/egui_extras/src/dock/branch/grid.rs index b805211cd..d20bf773c 100644 --- a/crates/egui_extras/src/dock/branch/grid.rs +++ b/crates/egui_extras/src/dock/branch/grid.rs @@ -1,9 +1,6 @@ -use std::{ - collections::{BTreeMap, HashMap, HashSet}, - ops::RangeInclusive, -}; +use std::collections::{BTreeMap, HashMap, HashSet}; -use egui::{pos2, vec2, Rect}; +use egui::{emath::Rangef, pos2, vec2, Rect}; use itertools::Itertools as _; use crate::dock::{ @@ -11,46 +8,6 @@ use crate::dock::{ ResizeState, }; -/// Includive range of floats, i.e. `min..=max`, but more ergonomic than [`RangeInclusive`]. -#[derive(Clone, Copy, Debug, PartialEq)] -struct Rangef { - pub min: f32, - pub max: f32, -} - -impl Rangef { - #[inline] - pub fn new(min: f32, max: f32) -> Self { - Self { min, max } - } - - #[inline] - pub fn span(&self) -> f32 { - self.max - self.min - } -} - -impl From> for Rangef { - #[inline] - fn from(range: RangeInclusive) -> Self { - Self::new(*range.start(), *range.end()) - } -} - -impl From<&RangeInclusive> for Rangef { - #[inline] - fn from(range: &RangeInclusive) -> Self { - Self::new(*range.start(), *range.end()) - } -} - -impl From for RangeInclusive { - #[inline] - fn from(Rangef { min, max }: Rangef) -> Self { - min..=max - } -} - /// Where in a grid? #[derive( Clone, diff --git a/crates/emath/src/lib.rs b/crates/emath/src/lib.rs index 95552afd1..89b2db209 100644 --- a/crates/emath/src/lib.rs +++ b/crates/emath/src/lib.rs @@ -30,6 +30,7 @@ pub mod align; mod history; mod numeric; mod pos2; +mod range; mod rect; mod rect_transform; mod rot2; @@ -41,6 +42,7 @@ pub use { history::History, numeric::*, pos2::*, + range::Rangef, rect::*, rect_transform::*, rot2::*, diff --git a/crates/emath/src/range.rs b/crates/emath/src/range.rs new file mode 100644 index 000000000..2a455caa4 --- /dev/null +++ b/crates/emath/src/range.rs @@ -0,0 +1,84 @@ +use std::ops::{RangeFrom, RangeFull, RangeInclusive, RangeToInclusive}; + +/// Includive range of floats, i.e. `min..=max`, but more ergonomic than [`RangeInclusive`]. +#[repr(C)] +#[derive(Clone, Copy, Debug, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))] +pub struct Rangef { + pub min: f32, + pub max: f32, +} + +impl Rangef { + #[inline] + pub fn new(min: f32, max: f32) -> Self { + Self { min, max } + } + + #[inline] + pub fn span(&self) -> f32 { + self.max - self.min + } + + #[inline] + pub fn contains(&self, x: f32) -> bool { + self.min <= x && x <= self.max + } +} + +impl From for RangeInclusive { + #[inline] + fn from(Rangef { min, max }: Rangef) -> Self { + min..=max + } +} + +impl From> for Rangef { + #[inline] + fn from(range: RangeInclusive) -> Self { + Self::new(*range.start(), *range.end()) + } +} + +impl From<&RangeInclusive> for Rangef { + #[inline] + fn from(range: &RangeInclusive) -> Self { + Self::new(*range.start(), *range.end()) + } +} + +impl From> for Rangef { + #[inline] + fn from(range: RangeFrom) -> Self { + Self::new(range.start, f32::INFINITY) + } +} + +impl From<&RangeFrom> for Rangef { + #[inline] + fn from(range: &RangeFrom) -> Self { + Self::new(range.start, f32::INFINITY) + } +} + +impl From for Rangef { + #[inline] + fn from(_: RangeFull) -> Self { + Self::new(f32::NEG_INFINITY, f32::INFINITY) + } +} + +impl From<&RangeFull> for Rangef { + #[inline] + fn from(_: &RangeFull) -> Self { + Self::new(f32::NEG_INFINITY, f32::INFINITY) + } +} + +impl From> for Rangef { + #[inline] + fn from(range: RangeToInclusive) -> Self { + Self::new(f32::NEG_INFINITY, range.end) + } +}