diff options
author | 2023-08-15 17:05:46 +0800 | |
---|---|---|
committer | 2024-01-31 19:21:10 +0100 | |
commit | c077e107f2eea618ea644652707254a402527de3 (patch) | |
tree | dff9d70f26d834c2cc4f4b886bc81bc3d84a8e4d | |
parent | 8ed3490280a4dd8a1d4e3cd421b1785725f65865 (diff) | |
download | iced-c077e107f2eea618ea644652707254a402527de3.tar.gz iced-c077e107f2eea618ea644652707254a402527de3.tar.bz2 iced-c077e107f2eea618ea644652707254a402527de3.zip |
Use `Radians` in arc and ellipse types
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | core/src/angle.rs | 46 | ||||
-rw-r--r-- | examples/loading_spinners/src/circular.rs | 17 | ||||
-rw-r--r-- | graphics/src/geometry/path/arc.rs | 22 | ||||
-rw-r--r-- | graphics/src/geometry/path/builder.rs | 14 |
5 files changed, 74 insertions, 26 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cf01a82..2149e4fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Update `wgpu` to `0.17`. [#2065](https://github.com/iced-rs/iced/pull/2065) - Support automatic style type casting for `Button`. [#2046](https://github.com/iced-rs/iced/pull/2046) - `with_clip` and `with_save` in `Frame` can now return the data of the provided closure. [#1994](https://github.com/iced-rs/iced/pull/1994) +- `Arc` and `arc::Elliptical` now use `Radians` for angle fields. [#2027](https://github.com/iced-rs/iced/pull/2027) ### Fixed - Clipping of `TextInput` selection. [#2199](https://github.com/iced-rs/iced/pull/2199) diff --git a/core/src/angle.rs b/core/src/angle.rs index 102b69cf..e63df726 100644 --- a/core/src/angle.rs +++ b/core/src/angle.rs @@ -1,7 +1,7 @@ use crate::{Point, Rectangle, Vector}; use std::f32::consts::{FRAC_PI_2, PI}; -use std::ops::RangeInclusive; +use std::ops::{Add, AddAssign, Div, Mul, RangeInclusive, Sub, SubAssign}; /// Degrees #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)] @@ -71,3 +71,47 @@ impl Radians { (start, end) } } + +impl SubAssign<Radians> for Radians { + fn sub_assign(&mut self, rhs: Radians) { + self.0 = self.0 - rhs.0; + } +} + +impl AddAssign<Radians> for Radians { + fn add_assign(&mut self, rhs: Radians) { + self.0 = self.0 + rhs.0; + } +} + +impl Add<Radians> for Radians { + type Output = Radians; + + fn add(self, rhs: Radians) -> Self::Output { + Radians(self.0 + rhs.0) + } +} + +impl Sub<Radians> for Radians { + type Output = Radians; + + fn sub(self, rhs: Radians) -> Self::Output { + Radians(self.0 - rhs.0) + } +} + +impl Mul<f32> for Radians { + type Output = Radians; + + fn mul(self, rhs: f32) -> Self::Output { + Radians(self.0 * rhs) + } +} + +impl Div<f32> for Radians { + type Output = Radians; + + fn div(self, rhs: f32) -> Self::Output { + Radians(self.0 / rhs) + } +} diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs index 8598b20a..2f2172f3 100644 --- a/examples/loading_spinners/src/circular.rs +++ b/examples/loading_spinners/src/circular.rs @@ -8,6 +8,7 @@ use iced::mouse; use iced::time::Instant; use iced::widget::canvas; use iced::window::{self, RedrawRequest}; +use iced::Radians; use iced::{ Background, Color, Element, Event, Length, Rectangle, Renderer, Size, Vector, @@ -18,8 +19,8 @@ use super::easing::{self, Easing}; use std::f32::consts::PI; use std::time::Duration; -const MIN_RADIANS: f32 = PI / 8.0; -const WRAP_RADIANS: f32 = 2.0 * PI - PI / 4.0; +const MIN_ANGLE: Radians = Radians(PI / 8.0); +const WRAP_ANGLE: Radians = Radians(2.0 * PI - PI / 4.0); const BASE_ROTATION_SPEED: u32 = u32::MAX / 80; #[allow(missing_debug_implementations)] @@ -139,7 +140,7 @@ impl Animation { progress: 0.0, rotation: rotation.wrapping_add( BASE_ROTATION_SPEED.wrapping_add( - ((WRAP_RADIANS / (2.0 * PI)) * u32::MAX as f32) as u32, + ((WRAP_ANGLE.0 / (2.0 * PI)) * u32::MAX as f32) as u32, ), ), last: now, @@ -318,7 +319,7 @@ where let mut builder = canvas::path::Builder::new(); - let start = state.animation.rotation() * 2.0 * PI; + let start = iced::Radians(state.animation.rotation() * 2.0 * PI); match state.animation { Animation::Expanding { progress, .. } => { @@ -327,8 +328,8 @@ where radius: track_radius, start_angle: start, end_angle: start - + MIN_RADIANS - + WRAP_RADIANS * (self.easing.y_at_x(progress)), + + MIN_ANGLE + + WRAP_ANGLE * (self.easing.y_at_x(progress)), }); } Animation::Contracting { progress, .. } => { @@ -336,8 +337,8 @@ where center: frame.center(), radius: track_radius, start_angle: start - + WRAP_RADIANS * (self.easing.y_at_x(progress)), - end_angle: start + MIN_RADIANS + WRAP_RADIANS, + + WRAP_ANGLE * (self.easing.y_at_x(progress)), + end_angle: start + MIN_ANGLE + WRAP_ANGLE, }); } } diff --git a/graphics/src/geometry/path/arc.rs b/graphics/src/geometry/path/arc.rs index dd4fcf33..2600497f 100644 --- a/graphics/src/geometry/path/arc.rs +++ b/graphics/src/geometry/path/arc.rs @@ -1,5 +1,5 @@ //! Build and draw curves. -use iced_core::{Point, Vector}; +use iced_core::{Point, Radians, Vector}; /// A segment of a differentiable curve. #[derive(Debug, Clone, Copy)] @@ -8,10 +8,10 @@ pub struct Arc { pub center: Point, /// The radius of the arc. pub radius: f32, - /// The start of the segment's angle in radians, clockwise rotation from positive x-axis. - pub start_angle: f32, - /// The end of the segment's angle in radians, clockwise rotation from positive x-axis. - pub end_angle: f32, + /// The start of the segment's angle, clockwise rotation from positive x-axis. + pub start_angle: Radians, + /// The end of the segment's angle, clockwise rotation from positive x-axis. + pub end_angle: Radians, } /// An elliptical [`Arc`]. @@ -22,11 +22,11 @@ pub struct Elliptical { /// The radii of the arc's ellipse. The horizontal and vertical half-dimensions of the ellipse will match the x and y values of the radii vector. pub radii: Vector, /// The clockwise rotation of the arc's ellipse. - pub rotation: f32, - /// The start of the segment's angle in radians, clockwise rotation from positive x-axis. - pub start_angle: f32, - /// The end of the segment's angle in radians, clockwise rotation from positive x-axis. - pub end_angle: f32, + pub rotation: Radians, + /// The start of the segment's angle, clockwise rotation from positive x-axis. + pub start_angle: Radians, + /// The end of the segment's angle, clockwise rotation from positive x-axis. + pub end_angle: Radians, } impl From<Arc> for Elliptical { @@ -34,7 +34,7 @@ impl From<Arc> for Elliptical { Elliptical { center: arc.center, radii: Vector::new(arc.radius, arc.radius), - rotation: 0.0, + rotation: Radians(0.0), start_angle: arc.start_angle, end_angle: arc.end_angle, } diff --git a/graphics/src/geometry/path/builder.rs b/graphics/src/geometry/path/builder.rs index b0959fbf..1ccd83f2 100644 --- a/graphics/src/geometry/path/builder.rs +++ b/graphics/src/geometry/path/builder.rs @@ -1,6 +1,6 @@ use crate::geometry::path::{arc, Arc, Path}; -use iced_core::{Point, Size}; +use iced_core::{Point, Radians, Size}; use lyon_path::builder::{self, SvgPathBuilder}; use lyon_path::geom; @@ -106,9 +106,11 @@ impl Builder { let arc = geom::Arc { center: math::Point::new(arc.center.x, arc.center.y), radii: math::Vector::new(arc.radii.x, arc.radii.y), - x_rotation: math::Angle::radians(arc.rotation), - start_angle: math::Angle::radians(arc.start_angle), - sweep_angle: math::Angle::radians(arc.end_angle - arc.start_angle), + x_rotation: math::Angle::radians(arc.rotation.0), + start_angle: math::Angle::radians(arc.start_angle.0), + sweep_angle: math::Angle::radians( + (arc.end_angle - arc.start_angle).0, + ), }; let _ = self.raw.move_to(arc.sample(0.0)); @@ -165,8 +167,8 @@ impl Builder { self.arc(Arc { center, radius, - start_angle: 0.0, - end_angle: 2.0 * std::f32::consts::PI, + start_angle: Radians(0.0), + end_angle: Radians(2.0 * std::f32::consts::PI), }); } |