summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-05-02 13:15:17 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-05-02 17:27:45 +0200
commit09a6bcfffc24f5abdc8709403bab7ae1e01563f1 (patch)
tree8a09792cd01cb56a3a5d11184a60009f8a22a85f /core
parentaae8e4f5cfabfc3725ac938023fa29a6737a380c (diff)
downloadiced-09a6bcfffc24f5abdc8709403bab7ae1e01563f1.tar.gz
iced-09a6bcfffc24f5abdc8709403bab7ae1e01563f1.tar.bz2
iced-09a6bcfffc24f5abdc8709403bab7ae1e01563f1.zip
Add `Image` rotation support
Co-authored-by: DKolter <68352124+DKolter@users.noreply.github.com>
Diffstat (limited to 'core')
-rw-r--r--core/src/image.rs2
-rw-r--r--core/src/lib.rs2
-rw-r--r--core/src/renderer/null.rs4
-rw-r--r--core/src/rotation.rs37
-rw-r--r--core/src/svg.rs2
5 files changed, 47 insertions, 0 deletions
diff --git a/core/src/image.rs b/core/src/image.rs
index c38239bc..5d1ab441 100644
--- a/core/src/image.rs
+++ b/core/src/image.rs
@@ -173,5 +173,7 @@ pub trait Renderer: crate::Renderer {
handle: Self::Handle,
filter_method: FilterMethod,
bounds: Rectangle,
+ rotation: f32,
+ scale: Size,
);
}
diff --git a/core/src/lib.rs b/core/src/lib.rs
index feda4fb4..da3ddcac 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -39,6 +39,7 @@ mod padding;
mod pixels;
mod point;
mod rectangle;
+mod rotation;
mod shadow;
mod shell;
mod size;
@@ -64,6 +65,7 @@ pub use pixels::Pixels;
pub use point::Point;
pub use rectangle::Rectangle;
pub use renderer::Renderer;
+pub use rotation::RotationLayout;
pub use shadow::Shadow;
pub use shell::Shell;
pub use size::Size;
diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs
index fe38ec87..d2dcfe4d 100644
--- a/core/src/renderer/null.rs
+++ b/core/src/renderer/null.rs
@@ -171,6 +171,8 @@ impl image::Renderer for () {
_handle: Self::Handle,
_filter_method: image::FilterMethod,
_bounds: Rectangle,
+ _rotation: f32,
+ _scale: Size,
) {
}
}
@@ -185,6 +187,8 @@ impl svg::Renderer for () {
_handle: svg::Handle,
_color: Option<Color>,
_bounds: Rectangle,
+ _rotation: f32,
+ _scale: Size,
) {
}
}
diff --git a/core/src/rotation.rs b/core/src/rotation.rs
new file mode 100644
index 00000000..821aa494
--- /dev/null
+++ b/core/src/rotation.rs
@@ -0,0 +1,37 @@
+//! Control the rotation of some content (like an image) with the `RotationLayout` within a
+//! space.
+use crate::Size;
+
+/// The strategy used to rotate the content.
+///
+/// This is used to control the behavior of the layout when the content is rotated
+/// by a certain angle.
+#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
+pub enum RotationLayout {
+ /// The layout is kept exactly as it was before the rotation.
+ ///
+ /// This is especially useful when used for animations, as it will avoid the
+ /// layout being shifted or resized when smoothly i.e. an icon.
+ Keep,
+ /// The layout is adjusted to fit the rotated content.
+ ///
+ /// This allows you to rotate an image and have the layout adjust to fit the new
+ /// size of the image.
+ Change,
+}
+
+impl RotationLayout {
+ /// Applies the rotation to the layout while respecting the [`RotationLayout`] strategy.
+ /// The rotation is given in radians.
+ pub fn apply_to_size(&self, size: Size, rotation: f32) -> Size {
+ match self {
+ Self::Keep => size,
+ Self::Change => Size {
+ width: (size.width * rotation.cos()).abs()
+ + (size.height * rotation.sin()).abs(),
+ height: (size.width * rotation.sin()).abs()
+ + (size.height * rotation.cos()).abs(),
+ },
+ }
+ }
+}
diff --git a/core/src/svg.rs b/core/src/svg.rs
index 0106e0c2..74dd7f4a 100644
--- a/core/src/svg.rs
+++ b/core/src/svg.rs
@@ -100,5 +100,7 @@ pub trait Renderer: crate::Renderer {
handle: Handle,
color: Option<Color>,
bounds: Rectangle,
+ rotation: f32,
+ scale: Size,
);
}