1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 06:10:06 -04:00

Add Rangef to emath

This commit is contained in:
Emil Ernerfeldt
2023-05-04 12:27:13 +02:00
parent 065801410a
commit 3f02c7d698
3 changed files with 88 additions and 45 deletions

View File

@@ -1,9 +1,6 @@
use std::{ use std::collections::{BTreeMap, HashMap, HashSet};
collections::{BTreeMap, HashMap, HashSet},
ops::RangeInclusive,
};
use egui::{pos2, vec2, Rect}; use egui::{emath::Rangef, pos2, vec2, Rect};
use itertools::Itertools as _; use itertools::Itertools as _;
use crate::dock::{ use crate::dock::{
@@ -11,46 +8,6 @@ use crate::dock::{
ResizeState, 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<RangeInclusive<f32>> for Rangef {
#[inline]
fn from(range: RangeInclusive<f32>) -> Self {
Self::new(*range.start(), *range.end())
}
}
impl From<&RangeInclusive<f32>> for Rangef {
#[inline]
fn from(range: &RangeInclusive<f32>) -> Self {
Self::new(*range.start(), *range.end())
}
}
impl From<Rangef> for RangeInclusive<f32> {
#[inline]
fn from(Rangef { min, max }: Rangef) -> Self {
min..=max
}
}
/// Where in a grid? /// Where in a grid?
#[derive( #[derive(
Clone, Clone,

View File

@@ -30,6 +30,7 @@ pub mod align;
mod history; mod history;
mod numeric; mod numeric;
mod pos2; mod pos2;
mod range;
mod rect; mod rect;
mod rect_transform; mod rect_transform;
mod rot2; mod rot2;
@@ -41,6 +42,7 @@ pub use {
history::History, history::History,
numeric::*, numeric::*,
pos2::*, pos2::*,
range::Rangef,
rect::*, rect::*,
rect_transform::*, rect_transform::*,
rot2::*, rot2::*,

84
crates/emath/src/range.rs Normal file
View File

@@ -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<Rangef> for RangeInclusive<f32> {
#[inline]
fn from(Rangef { min, max }: Rangef) -> Self {
min..=max
}
}
impl From<RangeInclusive<f32>> for Rangef {
#[inline]
fn from(range: RangeInclusive<f32>) -> Self {
Self::new(*range.start(), *range.end())
}
}
impl From<&RangeInclusive<f32>> for Rangef {
#[inline]
fn from(range: &RangeInclusive<f32>) -> Self {
Self::new(*range.start(), *range.end())
}
}
impl From<RangeFrom<f32>> for Rangef {
#[inline]
fn from(range: RangeFrom<f32>) -> Self {
Self::new(range.start, f32::INFINITY)
}
}
impl From<&RangeFrom<f32>> for Rangef {
#[inline]
fn from(range: &RangeFrom<f32>) -> Self {
Self::new(range.start, f32::INFINITY)
}
}
impl From<RangeFull> 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<RangeToInclusive<f32>> for Rangef {
#[inline]
fn from(range: RangeToInclusive<f32>) -> Self {
Self::new(f32::NEG_INFINITY, range.end)
}
}