1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 21:00:03 -04:00

[emath] Use const values for Vec2::ZERO, Rect::EVERYTHING etc

This commit is contained in:
Emil Ernerfeldt
2021-02-05 09:49:21 +01:00
parent 10e86b055d
commit 2d9d06dbff
20 changed files with 118 additions and 71 deletions

View File

@@ -19,20 +19,29 @@ pub enum Align {
impl Align {
/// Convenience for [`Self::Min`]
pub fn left() -> Self {
Self::Min
}
pub const LEFT: Self = Self::Min;
/// Convenience for [`Self::Max`]
pub fn right() -> Self {
Self::Max
}
pub const RIGHT: Self = Self::Max;
/// Convenience for [`Self::Min`]
pub fn top() -> Self {
Self::Min
}
pub const TOP: Self = Self::Min;
/// Convenience for [`Self::Max`]
pub const BOTTOM: Self = Self::Max;
#[deprecated = "Use Self::LEFT"]
pub fn left() -> Self {
Self::LEFT
}
#[deprecated = "Use Self::RIGHT"]
pub fn right() -> Self {
Self::RIGHT
}
#[deprecated = "Use Self::TOP"]
pub fn top() -> Self {
Self::TOP
}
#[deprecated = "Use Self::BOTTOM"]
pub fn bottom() -> Self {
Self::Max
Self::BOTTOM
}
/// Convert `Min => 0.0`, `Center => 0.5` or `Max => 1.0`.

View File

@@ -82,8 +82,11 @@ impl Pos2 {
/// The zero position, the origin.
/// The top left corner in a GUI.
/// Same as `Pos2::default()`.
pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
#[deprecated = "Use Pos2::ZERO instead"]
pub const fn zero() -> Self {
Self { x: 0.0, y: 0.0 }
Self::ZERO
}
pub const fn new(x: f32, y: f32) -> Self {

View File

@@ -1,3 +1,4 @@
use std::f32::INFINITY;
use std::ops::RangeInclusive;
use crate::*;
@@ -13,7 +14,37 @@ pub struct Rect {
}
impl Rect {
/// Infinite rectangle that contains everything
/// Infinite rectangle that contains everything.
pub const EVERYTHING: Self = Self {
min: pos2(-INFINITY, -INFINITY),
max: pos2(INFINITY, INFINITY),
};
/// The inverse of [`Self::EVERYTHING`]: streches from positive infinity to negative infinity.
/// Contains no points.
///
/// This is useful as the seed for boulding bounding boxes.
///
/// # Example:
/// ```
/// # use emath::*;
/// let mut rect = Rect::NOTHING;
/// rect.extend_with(pos2(2.0, 1.0));
/// rect.extend_with(pos2(0.0, 3.0));
/// assert_eq!(rect, Rect::from_min_max(pos2(0.0, 1.0), pos2(2.0, 3.0)))
/// ```
pub const NOTHING: Self = Self {
min: pos2(INFINITY, INFINITY),
max: pos2(-INFINITY, -INFINITY),
};
/// An invalid `Rect` filled with [`f32::NAN`];
pub const NAN: Self = Self {
min: pos2(f32::NAN, f32::NAN),
max: pos2(-f32::NAN, -f32::NAN),
};
#[deprecated = "Use Rect::EVERYTHING"]
pub fn everything() -> Self {
let inf = f32::INFINITY;
Self {
@@ -22,6 +53,7 @@ impl Rect {
}
}
#[deprecated = "Use Rect::NOTHING"]
pub fn nothing() -> Self {
let inf = f32::INFINITY;
Self {
@@ -30,15 +62,12 @@ impl Rect {
}
}
/// invalid, NAN filled Rect.
#[deprecated = "Use Rect::NAN"]
pub fn invalid() -> Self {
Self {
min: pos2(f32::NAN, f32::NAN),
max: pos2(f32::NAN, f32::NAN),
}
Self::NAN
}
pub fn from_min_max(min: Pos2, max: Pos2) -> Self {
pub const fn from_min_max(min: Pos2, max: Pos2) -> Self {
Rect { min, max }
}

View File

@@ -28,8 +28,12 @@ impl Default for Rot2 {
}
impl Rot2 {
/// The identity rotation: nothing rotates
pub const IDENTITY: Self = Self { s: 0.0, c: 1.0 };
#[deprecated = "Use Rot2::IDENTITY"]
pub fn identity() -> Self {
Self { s: 0.0, c: 1.0 }
Self::IDENTITY
}
/// A 𝞃/4 = 90° rotation means rotating the X axis to the Y axis.

View File

@@ -81,23 +81,25 @@ impl Vec2 {
pub const X: Vec2 = Vec2 { x: 1.0, y: 0.0 };
pub const Y: Vec2 = Vec2 { x: 0.0, y: 1.0 };
pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
pub const INFINITY: Self = Self::splat(f32::INFINITY);
#[deprecated = "Use Vec2::ZERO instead"]
pub fn zero() -> Self {
Self { x: 0.0, y: 0.0 }
Self::ZERO
}
#[deprecated = "Use Vec2::INFINITY instead"]
pub fn infinity() -> Self {
Self {
x: f32::INFINITY,
y: f32::INFINITY,
}
Self::INFINITY
}
pub fn new(x: f32, y: f32) -> Self {
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
pub fn splat(v: impl Into<f32>) -> Self {
let v: f32 = v.into();
/// Set both `x` and `y` to the same value.
pub const fn splat(v: f32) -> Self {
Self { x: v, y: v }
}