diff options
author | Emil Ernerfeldt <emil.ernerfeldt@gmail.com> | 2024-03-26 09:05:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-26 09:05:29 +0100 |
commit | 1634554032d2f32a1da2b518923a8bd43b4b2aa3 (patch) | |
tree | 52af22b4538455a1a035e8c31362fbcdd9baf32c | |
parent | cf8c37c71e249fb131a3de955d975bae53f0862b (diff) |
Add `Margin` to `epaint` (#4231)
Moved from `egui`
-rw-r--r-- | crates/egui/src/containers/frame.rs | 2 | ||||
-rw-r--r-- | crates/egui/src/lib.rs | 6 | ||||
-rw-r--r-- | crates/egui/src/style.rs | 213 | ||||
-rw-r--r-- | crates/epaint/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/epaint/src/margin.rs | 211 |
5 files changed, 220 insertions, 216 deletions
diff --git a/crates/egui/src/containers/frame.rs b/crates/egui/src/containers/frame.rs index bd7f8eb7..92113f4d 100644 --- a/crates/egui/src/containers/frame.rs +++ b/crates/egui/src/containers/frame.rs @@ -1,6 +1,6 @@ //! Frame container -use crate::{layers::ShapeIdx, style::Margin, *}; +use crate::{layers::ShapeIdx, *}; use epaint::*; /// Add a background, frame and/or margin to a rectangular background of a [`Ui`]. diff --git a/crates/egui/src/lib.rs b/crates/egui/src/lib.rs index 69328045..e64668ec 100644 --- a/crates/egui/src/lib.rs +++ b/crates/egui/src/lib.rs @@ -430,8 +430,8 @@ pub use epaint::{ mutex, text::{FontData, FontDefinitions, FontFamily, FontId, FontTweak}, textures::{TextureFilter, TextureOptions, TextureWrapMode, TexturesDelta}, - ClippedPrimitive, ColorImage, FontImage, ImageData, Mesh, PaintCallback, PaintCallbackInfo, - Rounding, Shape, Stroke, TextureHandle, TextureId, + ClippedPrimitive, ColorImage, FontImage, ImageData, Margin, Mesh, PaintCallback, + PaintCallbackInfo, Rounding, Shape, Stroke, TextureHandle, TextureId, }; pub mod text { @@ -463,7 +463,7 @@ pub use { painter::Painter, response::{InnerResponse, Response}, sense::Sense, - style::{FontSelection, Margin, Style, TextStyle, Visuals}, + style::{FontSelection, Style, TextStyle, Visuals}, text::{Galley, TextFormat}, ui::Ui, viewport::*, diff --git a/crates/egui/src/style.rs b/crates/egui/src/style.rs index 651ad269..c270add6 100644 --- a/crates/egui/src/style.rs +++ b/crates/egui/src/style.rs @@ -7,7 +7,8 @@ use std::collections::BTreeMap; use epaint::{Rounding, Shadow, Stroke}; use crate::{ - ecolor::*, emath::*, ComboBox, CursorIcon, FontFamily, FontId, Response, RichText, WidgetText, + ecolor::*, emath::*, ComboBox, CursorIcon, FontFamily, FontId, Margin, Response, RichText, + WidgetText, }; // ---------------------------------------------------------------------------- @@ -613,216 +614,6 @@ impl ScrollStyle { // ---------------------------------------------------------------------------- -#[derive(Clone, Copy, Debug, Default, PartialEq)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] -pub struct Margin { - pub left: f32, - pub right: f32, - pub top: f32, - pub bottom: f32, -} - -impl Margin { - pub const ZERO: Self = Self { - left: 0.0, - right: 0.0, - top: 0.0, - bottom: 0.0, - }; - - #[inline] - pub const fn same(margin: f32) -> Self { - Self { - left: margin, - right: margin, - top: margin, - bottom: margin, - } - } - - /// Margins with the same size on opposing sides - #[inline] - pub const fn symmetric(x: f32, y: f32) -> Self { - Self { - left: x, - right: x, - top: y, - bottom: y, - } - } - - /// Total margins on both sides - #[inline] - pub fn sum(&self) -> Vec2 { - vec2(self.left + self.right, self.top + self.bottom) - } - - #[inline] - pub const fn left_top(&self) -> Vec2 { - vec2(self.left, self.top) - } - - #[inline] - pub const fn right_bottom(&self) -> Vec2 { - vec2(self.right, self.bottom) - } - - #[inline] - pub fn is_same(&self) -> bool { - self.left == self.right && self.left == self.top && self.left == self.bottom - } - - #[inline] - pub fn expand_rect(&self, rect: Rect) -> Rect { - Rect::from_min_max(rect.min - self.left_top(), rect.max + self.right_bottom()) - } - - #[inline] - pub fn shrink_rect(&self, rect: Rect) -> Rect { - Rect::from_min_max(rect.min + self.left_top(), rect.max - self.right_bottom()) - } -} - -impl From<f32> for Margin { - #[inline] - fn from(v: f32) -> Self { - Self::same(v) - } -} - -impl From<Vec2> for Margin { - #[inline] - fn from(v: Vec2) -> Self { - Self::symmetric(v.x, v.y) - } -} - -impl std::ops::Add for Margin { - type Output = Self; - - #[inline] - fn add(self, other: Self) -> Self { - Self { - left: self.left + other.left, - right: self.right + other.right, - top: self.top + other.top, - bottom: self.bottom + other.bottom, - } - } -} - -impl std::ops::Add<f32> for Margin { - type Output = Self; - - #[inline] - fn add(self, v: f32) -> Self { - Self { - left: self.left + v, - right: self.right + v, - top: self.top + v, - bottom: self.bottom + v, - } - } -} - -impl std::ops::AddAssign<f32> for Margin { - #[inline] - fn add_assign(&mut self, v: f32) { - self.left += v; - self.right += v; - self.top += v; - self.bottom += v; - } -} - -impl std::ops::Div<f32> for Margin { - type Output = Self; - - #[inline] - fn div(self, v: f32) -> Self { - Self { - left: self.left / v, - right: self.right / v, - top: self.top / v, - bottom: self.bottom / v, - } - } -} - -impl std::ops::DivAssign<f32> for Margin { - #[inline] - fn div_assign(&mut self, v: f32) { - self.left /= v; - self.right /= v; - self.top /= v; - self.bottom /= v; - } -} - -impl std::ops::Mul<f32> for Margin { - type Output = Self; - - #[inline] - fn mul(self, v: f32) -> Self { - Self { - left: self.left * v, - right: self.right * v, - top: self.top * v, - bottom: self.bottom * v, - } - } -} - -impl std::ops::MulAssign<f32> for Margin { - #[inline] - fn mul_assign(&mut self, v: f32) { - self.left *= v; - self.right *= v; - self.top *= v; - self.bottom *= v; - } -} - -impl std::ops::Sub for Margin { - type Output = Self; - - #[inline] - fn sub(self, other: Self) -> Self { - Self { - left: self.left - other.left, - right: self.right - other.right, - top: self.top - other.top, - bottom: self.bottom - other.bottom, - } - } -} - -impl std::ops::Sub<f32> for Margin { - type Output = Self; - - #[inline] - fn sub(self, v: f32) -> Self { - Self { - left: self.left - v, - right: self.right - v, - top: self.top - v, - bottom: self.bottom - v, - } - } -} - -impl std::ops::SubAssign<f32> for Margin { - #[inline] - fn sub_assign(&mut self, v: f32) { - self.left -= v; - self.right -= v; - self.top -= v; - self.bottom -= v; - } -} - -// ---------------------------------------------------------------------------- - /// How and when interaction happens. #[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] diff --git a/crates/epaint/src/lib.rs b/crates/epaint/src/lib.rs index 8746d564..18298484 100644 --- a/crates/epaint/src/lib.rs +++ b/crates/epaint/src/lib.rs @@ -27,6 +27,7 @@ mod bezier; pub mod image; +mod margin; mod mesh; pub mod mutex; mod shadow; @@ -41,9 +42,10 @@ mod texture_handle; pub mod textures; pub mod util; -pub use { +pub use self::{ bezier::{CubicBezierShape, QuadraticBezierShape}, image::{ColorImage, FontImage, ImageData, ImageDelta}, + margin::Margin, mesh::{Mesh, Mesh16, Vertex}, shadow::Shadow, shape::{ diff --git a/crates/epaint/src/margin.rs b/crates/epaint/src/margin.rs new file mode 100644 index 00000000..815ff835 --- /dev/null +++ b/crates/epaint/src/margin.rs @@ -0,0 +1,211 @@ +use emath::{vec2, Rect, Vec2}; + +/// A value for all four sides of a rectangle, +/// often used to express padding or spacing. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +pub struct Margin { + pub left: f32, + pub right: f32, + pub top: f32, + pub bottom: f32, +} + +impl Margin { + pub const ZERO: Self = Self { + left: 0.0, + right: 0.0, + top: 0.0, + bottom: 0.0, + }; + + #[inline] + pub const fn same(margin: f32) -> Self { + Self { + left: margin, + right: margin, + top: margin, + bottom: margin, + } + } + + /// Margins with the same size on opposing sides + #[inline] + pub const fn symmetric(x: f32, y: f32) -> Self { + Self { + left: x, + right: x, + top: y, + bottom: y, + } + } + + /// Total margins on both sides + #[inline] + pub fn sum(&self) -> Vec2 { + vec2(self.left + self.right, self.top + self.bottom) + } + + #[inline] + pub const fn left_top(&self) -> Vec2 { + vec2(self.left, self.top) + } + + #[inline] + pub const fn right_bottom(&self) -> Vec2 { + vec2(self.right, self.bottom) + } + + #[inline] + pub fn is_same(&self) -> bool { + self.left == self.right && self.left == self.top && self.left == self.bottom + } + + #[inline] + pub fn expand_rect(&self, rect: Rect) -> Rect { + Rect::from_min_max(rect.min - self.left_top(), rect.max + self.right_bottom()) + } + + #[inline] + pub fn shrink_rect(&self, rect: Rect) -> Rect { + Rect::from_min_max(rect.min + self.left_top(), rect.max - self.right_bottom()) + } +} + +impl From<f32> for Margin { + #[inline] + fn from(v: f32) -> Self { + Self::same(v) + } +} + +impl From<Vec2> for Margin { + #[inline] + fn from(v: Vec2) -> Self { + Self::symmetric(v.x, v.y) + } +} + +impl std::ops::Add for Margin { + type Output = Self; + + #[inline] + fn add(self, other: Self) -> Self { + Self { + left: self.left + other.left, + right: self.right + other.right, + top: self.top + other.top, + bottom: self.bottom + other.bottom, + } + } +} + +impl std::ops::Add<f32> for Margin { + type Output = Self; + + #[inline] + fn add(self, v: f32) -> Self { + Self { + left: self.left + v, + right: self.right + v, + top: self.top + v, + bottom: self.bottom + v, + } + } +} + +impl std::ops::AddAssign<f32> for Margin { + #[inline] + fn add_assign(&mut self, v: f32) { + self.left += v; + self.right += v; + self.top += v; + self.bottom += v; + } +} + +impl std::ops::Div<f32> for Margin { + type Output = Self; + + #[inline] + fn div(self, v: f32) -> Self { + Self { + left: self.left / v, + right: self.right / v, + top: self.top / v, + bottom: self.bottom / v, + } + } +} + +impl std::ops::DivAssign<f32> for Margin { + #[inline] + fn div_assign(&mut self, v: f32) { + self.left /= v; + self.right /= v; + self.top /= v; + self.bottom /= v; + } +} + +impl std::ops::Mul<f32> for Margin { + type Output = Self; + + #[inline] + fn mul(self, v: f32) -> Self { + Self { + left: self.left * v, + right: self.right * v, + top: self.top * v, + bottom: self.bottom * v, + } + } +} + +impl std::ops::MulAssign<f32> for Margin { + #[inline] + fn mul_assign(&mut self, v: f32) { + self.left *= v; + self.right *= v; + self.top *= v; + self.bottom *= v; + } +} + +impl std::ops::Sub for Margin { + type Output = Self; + + #[inline] + fn sub(self, other: Self) -> Self { + Self { + left: self.left - other.left, + right: self.right - other.right, + top: self.top - other.top, + bottom: self.bottom - other.bottom, + } + } +} + +impl std::ops::Sub<f32> for Margin { + type Output = Self; + + #[inline] + fn sub(self, v: f32) -> Self { + Self { + left: self.left - v, + right: self.right - v, + top: self.top - v, + bottom: self.bottom - v, + } + } +} + +impl std::ops::SubAssign<f32> for Margin { + #[inline] + fn sub_assign(&mut self, v: f32) { + self.left -= v; + self.right -= v; + self.top -= v; + self.bottom -= v; + } +} |