diff --git a/egui/src/containers/area.rs b/egui/src/containers/area.rs index f4c1f27b..054d30ff 100644 --- a/egui/src/containers/area.rs +++ b/egui/src/containers/area.rs @@ -139,7 +139,7 @@ impl Area { let state = ctx.memory().areas.get(id).cloned(); let mut state = state.unwrap_or_else(|| State { pos: default_pos.unwrap_or_else(|| automatic_area_position(ctx)), - size: Vec2::zero(), + size: Vec2::ZERO, interactable, }); state.pos = new_pos.unwrap_or(state.pos); @@ -198,7 +198,7 @@ impl Prepared { } pub(crate) fn content_ui(&self, ctx: &CtxRef) -> Ui { - let max_rect = Rect::from_min_size(self.state.pos, Vec2::infinity()); + let max_rect = Rect::from_min_size(self.state.pos, Vec2::INFINITY); let shadow_radius = ctx.style().visuals.window_shadow.extrusion; // hacky let mut clip_rect = max_rect .expand(ctx.style().visuals.clip_rect_margin) @@ -241,7 +241,7 @@ impl Prepared { }; let move_response = ctx.interact( - Rect::everything(), + Rect::EVERYTHING, ctx.style().spacing.item_spacing, layer_id, interact_id, diff --git a/egui/src/containers/popup.rs b/egui/src/containers/popup.rs index 4c17f367..e0beffd4 100644 --- a/egui/src/containers/popup.rs +++ b/egui/src/containers/popup.rs @@ -37,7 +37,7 @@ pub fn show_tooltip(ctx: &CtxRef, add_contents: impl FnOnce(&mut Ui)) { let id = Id::tooltip(); let response = show_tooltip_area(ctx, id, window_pos, add_contents); - let tooltip_rect = tooltip_rect.unwrap_or_else(Rect::nothing); + let tooltip_rect = tooltip_rect.unwrap_or(Rect::NOTHING); ctx.frame_state().tooltip_rect = Some(tooltip_rect.union(response.rect)); } @@ -114,7 +114,7 @@ pub fn popup_below_widget( let frame = Frame::popup(ui.style()); let frame_margin = frame.margin; frame.show(ui, |ui| { - ui.with_layout(Layout::top_down_justified(Align::left()), |ui| { + ui.with_layout(Layout::top_down_justified(Align::LEFT), |ui| { ui.set_width(widget_response.rect.width() - 2.0 * frame_margin.x); add_contents(ui) }); diff --git a/egui/src/containers/resize.rs b/egui/src/containers/resize.rs index 7ba84f7e..712d4f4d 100644 --- a/egui/src/containers/resize.rs +++ b/egui/src/containers/resize.rs @@ -124,7 +124,7 @@ impl Resize { /// Not manually resizable, just takes the size of its contents. /// Text will not wrap, but will instead make your window width expand. pub fn auto_sized(self) -> Self { - self.min_size(Vec2::zero()) + self.min_size(Vec2::ZERO) .default_size(Vec2::splat(f32::INFINITY)) .resizable(false) } diff --git a/egui/src/containers/scroll_area.rs b/egui/src/containers/scroll_area.rs index e9e44f6e..117dcd82 100644 --- a/egui/src/containers/scroll_area.rs +++ b/egui/src/containers/scroll_area.rs @@ -19,9 +19,9 @@ pub(crate) struct State { impl Default for State { fn default() -> Self { Self { - offset: Vec2::zero(), + offset: Vec2::ZERO, show_scroll: false, - vel: Vec2::zero(), + vel: Vec2::ZERO, scroll_start_offset_from_top: None, } } @@ -222,7 +222,7 @@ impl Prepared { let friction = friction_coeff * dt; if friction > state.vel.length() || state.vel.length() < stop_speed { - state.vel = Vec2::zero(); + state.vel = Vec2::ZERO; } else { state.vel -= friction * state.vel.normalized(); // Offset has an inverted coordinate system compared to @@ -244,7 +244,7 @@ impl Prepared { if scrolling_up || scrolling_down { state.offset.y -= scroll_delta.y; // Clear scroll delta so no parent scroll will use it. - frame_state.scroll_delta = Vec2::zero(); + frame_state.scroll_delta = Vec2::ZERO; } } @@ -310,7 +310,7 @@ impl Prepared { state.offset.y = state.offset.y.min(max_offset); #[allow(clippy::float_cmp)] if state.offset.y != unbounded_offset_y { - state.vel = Vec2::zero(); + state.vel = Vec2::ZERO; } // Avoid frame-delay by calculating a new handle rect: diff --git a/egui/src/containers/window.rs b/egui/src/containers/window.rs index a60cc70d..398287d9 100644 --- a/egui/src/containers/window.rs +++ b/egui/src/containers/window.rs @@ -705,7 +705,7 @@ fn show_title_bar( title_label, title_galley, min_rect, - rect: Rect::invalid(), // Will be filled in later + rect: Rect::NAN, // Will be filled in later } }); diff --git a/egui/src/context.rs b/egui/src/context.rs index 6980c9df..3884f840 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -51,11 +51,11 @@ impl Default for FrameState { fn default() -> Self { Self { used_ids: Default::default(), - available_rect: Rect::invalid(), - unused_rect: Rect::invalid(), - used_by_panels: Rect::invalid(), + available_rect: Rect::NAN, + unused_rect: Rect::NAN, + used_by_panels: Rect::NAN, tooltip_rect: None, - scroll_delta: Vec2::zero(), + scroll_delta: Vec2::ZERO, scroll_target: None, } } @@ -76,7 +76,7 @@ impl FrameState { used_ids.clear(); *available_rect = input.screen_rect(); *unused_rect = input.screen_rect(); - *used_by_panels = Rect::nothing(); + *used_by_panels = Rect::NOTHING; *tooltip_rect = None; *scroll_delta = input.scroll_delta; *scroll_target = None; @@ -118,7 +118,7 @@ impl FrameState { pub(crate) fn allocate_central_panel(&mut self, panel_rect: Rect) { // Note: we do not shrink `available_rect`, because // we allow windows to cover the CentralPanel. - self.unused_rect = Rect::nothing(); // Nothing left unused after this + self.unused_rect = Rect::NOTHING; // Nothing left unused after this self.used_by_panels = self.used_by_panels.union(panel_rect); } } diff --git a/egui/src/data/input.rs b/egui/src/data/input.rs index caa22cc2..e6f44665 100644 --- a/egui/src/data/input.rs +++ b/egui/src/data/input.rs @@ -49,7 +49,7 @@ impl Default for RawInput { fn default() -> Self { #![allow(deprecated)] // for screen_size Self { - scroll_delta: Vec2::zero(), + scroll_delta: Vec2::ZERO, screen_size: Default::default(), screen_rect: None, pixels_per_point: None, diff --git a/egui/src/experimental/easy_mark_viewer.rs b/egui/src/experimental/easy_mark_viewer.rs index 91a1748a..dbc6c356 100644 --- a/egui/src/experimental/easy_mark_viewer.rs +++ b/egui/src/experimental/easy_mark_viewer.rs @@ -8,7 +8,7 @@ pub fn easy_mark(ui: &mut Ui, easy_mark: &str) { pub fn easy_mark_it<'em>(ui: &mut Ui, items: impl Iterator>) { ui.horizontal_wrapped(|ui| { - ui.spacing_mut().item_spacing = Vec2::zero(); + ui.spacing_mut().item_spacing = Vec2::ZERO; for item in items { item_ui(ui, item); } diff --git a/egui/src/input_state.rs b/egui/src/input_state.rs index a853df49..7d5ee657 100644 --- a/egui/src/input_state.rs +++ b/egui/src/input_state.rs @@ -114,7 +114,7 @@ impl InputState { } pub fn wants_repaint(&self) -> bool { - self.pointer.wants_repaint() || self.scroll_delta != Vec2::zero() || !self.events.is_empty() + self.pointer.wants_repaint() || self.scroll_delta != Vec2::ZERO || !self.events.is_empty() } /// Was the given key pressed this frame? @@ -259,8 +259,8 @@ impl Default for PointerState { Self { latest_pos: None, interact_pos: None, - delta: Vec2::zero(), - velocity: Vec2::zero(), + delta: Vec2::ZERO, + velocity: Vec2::ZERO, pos_history: History::new(1000, 0.1), down: Default::default(), press_origin: None, @@ -357,7 +357,7 @@ impl PointerState { self.delta = if let (Some(old_pos), Some(new_pos)) = (old_pos, self.latest_pos) { new_pos - old_pos } else { - Vec2::zero() + Vec2::ZERO }; if let Some(pos) = self.latest_pos { @@ -380,7 +380,7 @@ impl PointerState { } fn wants_repaint(&self) -> bool { - !self.pointer_events.is_empty() || self.delta != Vec2::zero() + !self.pointer_events.is_empty() || self.delta != Vec2::ZERO } /// How much the pointer moved compared to last frame, in points. @@ -429,7 +429,7 @@ impl PointerState { /// Is the pointer currently moving? /// This is smoothed so a few frames of stillness is required before this returns `true`. pub fn is_moving(&self) -> bool { - self.velocity != Vec2::zero() + self.velocity != Vec2::ZERO } /// Was any pointer button pressed (`!down -> down`) this frame? diff --git a/egui/src/layout.rs b/egui/src/layout.rs index 70661cba..b03d14ca 100644 --- a/egui/src/layout.rs +++ b/egui/src/layout.rs @@ -133,7 +133,7 @@ impl Default for Layout { Self { main_dir: Direction::TopDown, main_wrap: false, - cross_align: Align::left(), + cross_align: Align::LEFT, cross_justify: false, } } @@ -253,7 +253,7 @@ impl Layout { fn horizontal_align(&self) -> Align { match self.main_dir { - // Direction::LeftToRight => Align::left(), + // Direction::LeftToRight => Align::LEFT, // Direction::RightToLeft => Align::right(), Direction::LeftToRight | Direction::RightToLeft => Align::Center, // looks better to e.g. center text within a button @@ -263,8 +263,8 @@ impl Layout { fn vertical_align(&self) -> Align { match self.main_dir { - // Direction::TopDown => Align::top(), - // Direction::BottomUp => Align::bottom(), + // Direction::TopDown => Align::TOP, + // Direction::BottomUp => Align::BOTTOM, Direction::TopDown | Direction::BottomUp => Align::Center, // looks better to e.g. center text within a button Direction::LeftToRight | Direction::RightToLeft => self.cross_align, @@ -300,7 +300,7 @@ impl Layout { pub(crate) fn region_from_max_rect(&self, max_rect: Rect) -> Region { let cursor = self.initial_cursor(max_rect); - let min_rect = Rect::from_min_size(cursor, Vec2::zero()); + let min_rect = Rect::from_min_size(cursor, Vec2::ZERO); Region { min_rect, max_rect, diff --git a/egui/src/menu.rs b/egui/src/menu.rs index d64ad056..a6f6119a 100644 --- a/egui/src/menu.rs +++ b/egui/src/menu.rs @@ -111,7 +111,7 @@ fn menu_impl<'c>( style.visuals.widgets.inactive.bg_fill = Color32::TRANSPARENT; style.visuals.widgets.inactive.bg_stroke = Stroke::none(); ui.set_style(style); - ui.with_layout(Layout::top_down_justified(Align::left()), add_contents); + ui.with_layout(Layout::top_down_justified(Align::LEFT), add_contents); }) }); diff --git a/egui/src/placer.rs b/egui/src/placer.rs index d56a41f4..120ad89c 100644 --- a/egui/src/placer.rs +++ b/egui/src/placer.rs @@ -127,7 +127,7 @@ impl Placer { self.layout.advance_cursor(&mut self.region.cursor, amount); self.region - .expand_to_include_rect(Rect::from_min_size(self.cursor(), Vec2::zero())); + .expand_to_include_rect(Rect::from_min_size(self.cursor(), Vec2::ZERO)); } /// Advance cursor after a widget was added to a specific rectangle diff --git a/egui/src/ui.rs b/egui/src/ui.rs index d7eaf8ac..616a1702 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -625,7 +625,7 @@ impl Ui { /// } /// /// if scroll_bottom { - /// ui.scroll_to_cursor(Align::bottom()); + /// ui.scroll_to_cursor(Align::BOTTOM); /// } /// }); /// ``` @@ -1223,7 +1223,7 @@ impl Ui { pos2(pos.x + column_width, self.max_rect().right_bottom().y), ); let mut column_ui = - self.child_ui(child_rect, Layout::top_down_justified(Align::left())); + self.child_ui(child_rect, Layout::top_down_justified(Align::LEFT)); column_ui.set_width(column_width); column_ui }) diff --git a/egui_demo_lib/src/apps/demo/scrolling.rs b/egui_demo_lib/src/apps/demo/scrolling.rs index ac146e02..80e65afa 100644 --- a/egui_demo_lib/src/apps/demo/scrolling.rs +++ b/egui_demo_lib/src/apps/demo/scrolling.rs @@ -90,7 +90,7 @@ impl super::View for Scrolling { let (current_scroll, max_scroll) = scroll_area.show(ui, |ui| { if scroll_top { - ui.scroll_to_cursor(Align::top()); + ui.scroll_to_cursor(Align::TOP); } ui.vertical(|ui| { for item in 1..=50 { @@ -104,7 +104,7 @@ impl super::View for Scrolling { }); if scroll_bottom { - ui.scroll_to_cursor(Align::bottom()); + ui.scroll_to_cursor(Align::BOTTOM); } let margin = ui.visuals().clip_rect_margin; diff --git a/emath/src/align.rs b/emath/src/align.rs index 7f9aef02..991e57c2 100644 --- a/emath/src/align.rs +++ b/emath/src/align.rs @@ -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`. diff --git a/emath/src/pos2.rs b/emath/src/pos2.rs index 21342dfe..ba9d47aa 100644 --- a/emath/src/pos2.rs +++ b/emath/src/pos2.rs @@ -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 { diff --git a/emath/src/rect.rs b/emath/src/rect.rs index 12606874..177b6ba5 100644 --- a/emath/src/rect.rs +++ b/emath/src/rect.rs @@ -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 } } diff --git a/emath/src/rot2.rs b/emath/src/rot2.rs index d8b9e687..2ec1783d 100644 --- a/emath/src/rot2.rs +++ b/emath/src/rot2.rs @@ -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. diff --git a/emath/src/vec2.rs b/emath/src/vec2.rs index 9e2a42f0..65309381 100644 --- a/emath/src/vec2.rs +++ b/emath/src/vec2.rs @@ -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) -> 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 } } diff --git a/epaint/src/tessellator.rs b/epaint/src/tessellator.rs index b7e25e42..e2af6915 100644 --- a/epaint/src/tessellator.rs +++ b/epaint/src/tessellator.rs @@ -81,9 +81,9 @@ impl Path { let mut n1 = (points[i + 1] - points[i]).normalized().rot90(); // Handle duplicated points (but not triplicated...): - if n0 == Vec2::zero() { + if n0 == Vec2::ZERO { n0 = n1; - } else if n1 == Vec2::zero() { + } else if n1 == Vec2::ZERO { n1 = n0; } @@ -121,9 +121,9 @@ impl Path { let mut n1 = (points[(i + 1) % n] - points[i]).normalized().rot90(); // Handle duplicated points (but not triplicated...): - if n0 == Vec2::zero() { + if n0 == Vec2::ZERO { n0 = n1; - } else if n1 == Vec2::zero() { + } else if n1 == Vec2::ZERO { n1 = n0; } @@ -461,7 +461,7 @@ impl Tessellator { pub fn from_options(options: TessellationOptions) -> Self { Self { options, - clip_rect: Rect::everything(), + clip_rect: Rect::EVERYTHING, scratchpad_points: Default::default(), scratchpad_path: Default::default(), } @@ -744,7 +744,7 @@ pub fn tessellate_shapes( if options.debug_paint_clip_rects { for ClippedMesh(clip_rect, mesh) in &mut clipped_meshes { - tessellator.clip_rect = Rect::everything(); + tessellator.clip_rect = Rect::EVERYTHING; tessellator.tessellate_shape( fonts, Shape::Rect { @@ -760,7 +760,7 @@ pub fn tessellate_shapes( if options.debug_ignore_clip_rects { for ClippedMesh(clip_rect, _) in &mut clipped_meshes { - *clip_rect = Rect::everything(); + *clip_rect = Rect::EVERYTHING; } }