mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 05:40:03 -04:00
Replace uses of RangeInclusive<f32> with emath::Rangef (#3221)
* Replace uses of `RangeInclusive<f32>` with `emath::Rangef` * Fix doc-test
This commit is contained in:
@@ -36,14 +36,60 @@ impl Rangef {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn span(&self) -> f32 {
|
||||
pub fn point(min_and_max: f32) -> Self {
|
||||
Self {
|
||||
min: min_and_max,
|
||||
max: min_and_max,
|
||||
}
|
||||
}
|
||||
|
||||
/// The length of the range, i.e. `max - min`.
|
||||
#[inline]
|
||||
pub fn span(self) -> f32 {
|
||||
self.max - self.min
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn contains(&self, x: f32) -> bool {
|
||||
#[must_use]
|
||||
pub fn contains(self, x: f32) -> bool {
|
||||
self.min <= x && x <= self.max
|
||||
}
|
||||
|
||||
/// Equivalent to `x.clamp(min, max)`
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn clamp(self, x: f32) -> f32 {
|
||||
x.clamp(self.min, self.max)
|
||||
}
|
||||
|
||||
/// Flip `min` and `max` if needed, so that `min <= max` after.
|
||||
#[inline]
|
||||
pub fn as_positive(self) -> Self {
|
||||
Rangef {
|
||||
min: self.min.min(self.max),
|
||||
max: self.min.max(self.max),
|
||||
}
|
||||
}
|
||||
|
||||
/// Shrink by this much on each side, keeping the center
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn shrink(self, amnt: f32) -> Self {
|
||||
Self {
|
||||
min: self.min + amnt,
|
||||
max: self.max - amnt,
|
||||
}
|
||||
}
|
||||
|
||||
/// Expand by this much on each side, keeping the center
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn expand(self, amnt: f32) -> Self {
|
||||
Self {
|
||||
min: self.min - amnt,
|
||||
max: self.max + amnt,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Rangef> for RangeInclusive<f32> {
|
||||
@@ -108,3 +154,17 @@ impl From<RangeToInclusive<f32>> for Rangef {
|
||||
Self::new(f32::NEG_INFINITY, range.end)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<RangeInclusive<f32>> for Rangef {
|
||||
#[inline]
|
||||
fn eq(&self, other: &RangeInclusive<f32>) -> bool {
|
||||
self.min == *other.start() && self.max == *other.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<Rangef> for RangeInclusive<f32> {
|
||||
#[inline]
|
||||
fn eq(&self, other: &Rangef) -> bool {
|
||||
*self.start() == other.min && *self.end() == other.max
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user