Skip to content

Commit c54e573

Browse files
committed
Simplify image rotation API and its internals
1 parent 8f3bec7 commit c54e573

23 files changed

Lines changed: 218 additions & 224 deletions

File tree

core/src/angle.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ impl From<u8> for Radians {
6565
}
6666
}
6767

68+
impl From<Radians> for f32 {
69+
fn from(radians: Radians) -> Self {
70+
radians.0
71+
}
72+
}
73+
6874
impl From<Radians> for f64 {
6975
fn from(radians: Radians) -> Self {
7076
Self::from(radians.0)

core/src/content_fit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::Size;
1111
/// in CSS, see [Mozilla's docs][1], or run the `tour` example
1212
///
1313
/// [1]: https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
14-
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
14+
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq, Default)]
1515
pub enum ContentFit {
1616
/// Scale as big as it can be without needing to crop or hide parts.
1717
///
@@ -23,6 +23,7 @@ pub enum ContentFit {
2323
/// This is a great fit for when you need to display an image without losing
2424
/// any part of it, particularly when the image itself is the focus of the
2525
/// screen.
26+
#[default]
2627
Contain,
2728

2829
/// Scale the image to cover all of the bounding box, cropping if needed.

core/src/image.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Load and draw raster graphics.
22
pub use bytes::Bytes;
33

4-
use crate::{Rectangle, Size};
4+
use crate::{Radians, Rectangle, Size};
55

66
use rustc_hash::FxHasher;
77
use std::hash::{Hash, Hasher};
@@ -173,7 +173,6 @@ pub trait Renderer: crate::Renderer {
173173
handle: Self::Handle,
174174
filter_method: FilterMethod,
175175
bounds: Rectangle,
176-
rotation: f32,
177-
scale: Size,
176+
rotation: Radians,
178177
);
179178
}

core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub use pixels::Pixels;
6565
pub use point::Point;
6666
pub use rectangle::Rectangle;
6767
pub use renderer::Renderer;
68-
pub use rotation::RotationLayout;
68+
pub use rotation::Rotation;
6969
pub use shadow::Shadow;
7070
pub use shell::Shell;
7171
pub use size::Size;

core/src/rectangle.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,19 @@ where
227227
}
228228
}
229229
}
230+
231+
impl<T> std::ops::Mul<Vector<T>> for Rectangle<T>
232+
where
233+
T: std::ops::Mul<Output = T> + Copy,
234+
{
235+
type Output = Rectangle<T>;
236+
237+
fn mul(self, scale: Vector<T>) -> Self {
238+
Rectangle {
239+
x: self.x * scale.x,
240+
y: self.y * scale.y,
241+
width: self.width * scale.x,
242+
height: self.height * scale.y,
243+
}
244+
}
245+
}

core/src/renderer/null.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use crate::renderer::{self, Renderer};
44
use crate::svg;
55
use crate::text::{self, Text};
66
use crate::{
7-
Background, Color, Font, Pixels, Point, Rectangle, Size, Transformation,
7+
Background, Color, Font, Pixels, Point, Radians, Rectangle, Size,
8+
Transformation,
89
};
910

1011
impl Renderer for () {
@@ -171,8 +172,7 @@ impl image::Renderer for () {
171172
_handle: Self::Handle,
172173
_filter_method: image::FilterMethod,
173174
_bounds: Rectangle,
174-
_rotation: f32,
175-
_scale: Size,
175+
_rotation: Radians,
176176
) {
177177
}
178178
}
@@ -187,8 +187,7 @@ impl svg::Renderer for () {
187187
_handle: svg::Handle,
188188
_color: Option<Color>,
189189
_bounds: Rectangle,
190-
_rotation: f32,
191-
_scale: Size,
190+
_rotation: Radians,
192191
) {
193192
}
194193
}

core/src/rotation.rs

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,68 @@
1-
//! Control the rotation of some content (like an image) with the `RotationLayout` within a
2-
//! space.
3-
use crate::Size;
1+
//! Control the rotation of some content (like an image) within a space.
2+
use crate::{Radians, Size};
43

54
/// The strategy used to rotate the content.
65
///
76
/// This is used to control the behavior of the layout when the content is rotated
87
/// by a certain angle.
9-
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
10-
pub enum RotationLayout {
11-
/// The layout is kept exactly as it was before the rotation.
8+
#[derive(Debug, Clone, Copy, PartialEq)]
9+
pub enum Rotation {
10+
/// The element will float while rotating. The layout will be kept exactly as it was
11+
/// before the rotation.
1212
///
1313
/// This is especially useful when used for animations, as it will avoid the
1414
/// layout being shifted or resized when smoothly i.e. an icon.
15-
Keep,
16-
/// The layout is adjusted to fit the rotated content.
15+
///
16+
/// This is the default.
17+
Floating(Radians),
18+
/// The element will be solid while rotating. The layout will be adjusted to fit
19+
/// the rotated content.
1720
///
1821
/// This allows you to rotate an image and have the layout adjust to fit the new
1922
/// size of the image.
20-
Change,
23+
Solid(Radians),
2124
}
2225

23-
impl RotationLayout {
24-
/// Applies the rotation to the layout while respecting the [`RotationLayout`] strategy.
25-
/// The rotation is given in radians.
26-
pub fn apply_to_size(&self, size: Size, rotation: f32) -> Size {
26+
impl Rotation {
27+
/// Returns the angle of the [`Rotation`] in [`Radians`].
28+
pub fn radians(self) -> Radians {
29+
match self {
30+
Rotation::Floating(radians) | Rotation::Solid(radians) => radians,
31+
}
32+
}
33+
34+
/// Rotates the given [`Size`].
35+
pub fn apply(self, size: Size) -> Size {
2736
match self {
28-
Self::Keep => size,
29-
Self::Change => Size {
30-
width: (size.width * rotation.cos()).abs()
31-
+ (size.height * rotation.sin()).abs(),
32-
height: (size.width * rotation.sin()).abs()
33-
+ (size.height * rotation.cos()).abs(),
34-
},
37+
Self::Floating(_) => size,
38+
Self::Solid(rotation) => {
39+
let radians = f32::from(rotation);
40+
41+
Size {
42+
width: (size.width * radians.cos()).abs()
43+
+ (size.height * radians.sin()).abs(),
44+
height: (size.width * radians.sin()).abs()
45+
+ (size.height * radians.cos()).abs(),
46+
}
47+
}
3548
}
3649
}
3750
}
51+
52+
impl Default for Rotation {
53+
fn default() -> Self {
54+
Self::Floating(Radians(0.0))
55+
}
56+
}
57+
58+
impl From<Radians> for Rotation {
59+
fn from(radians: Radians) -> Self {
60+
Self::Floating(radians)
61+
}
62+
}
63+
64+
impl From<f32> for Rotation {
65+
fn from(radians: f32) -> Self {
66+
Self::Floating(Radians(radians))
67+
}
68+
}

core/src/size.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,17 @@ where
113113
}
114114
}
115115
}
116+
117+
impl<T> std::ops::Mul<Vector<T>> for Size<T>
118+
where
119+
T: std::ops::Mul<Output = T> + Copy,
120+
{
121+
type Output = Size<T>;
122+
123+
fn mul(self, scale: Vector<T>) -> Self::Output {
124+
Size {
125+
width: self.width * scale.x,
126+
height: self.height * scale.y,
127+
}
128+
}
129+
}

core/src/svg.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Load and draw vector graphics.
2-
use crate::{Color, Rectangle, Size};
2+
use crate::{Color, Radians, Rectangle, Size};
33

44
use rustc_hash::FxHasher;
55
use std::borrow::Cow;
@@ -100,7 +100,6 @@ pub trait Renderer: crate::Renderer {
100100
handle: Handle,
101101
color: Option<Color>,
102102
bounds: Rectangle,
103-
rotation: f32,
104-
scale: Size,
103+
rotation: Radians,
105104
);
106105
}

core/src/vector.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ impl<T> Vector<T> {
1818
impl Vector {
1919
/// The zero [`Vector`].
2020
pub const ZERO: Self = Self::new(0.0, 0.0);
21+
22+
/// The unit [`Vector`].
23+
pub const UNIT: Self = Self::new(0.0, 0.0);
2124
}
2225

2326
impl<T> std::ops::Add for Vector<T>

0 commit comments

Comments
 (0)