|
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}; |
4 | 3 |
|
5 | 4 | /// The strategy used to rotate the content. |
6 | 5 | /// |
7 | 6 | /// This is used to control the behavior of the layout when the content is rotated |
8 | 7 | /// 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. |
12 | 12 | /// |
13 | 13 | /// This is especially useful when used for animations, as it will avoid the |
14 | 14 | /// 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. |
17 | 20 | /// |
18 | 21 | /// This allows you to rotate an image and have the layout adjust to fit the new |
19 | 22 | /// size of the image. |
20 | | - Change, |
| 23 | + Solid(Radians), |
21 | 24 | } |
22 | 25 |
|
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 { |
27 | 36 | 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 | + } |
35 | 48 | } |
36 | 49 | } |
37 | 50 | } |
| 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 | +} |
0 commit comments