Fix Image::bounds when rotation present in iced_graphics
This commit is contained in:
parent
568ac66486
commit
eac5bcb64f
5 changed files with 37 additions and 17 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{Point, Size, Vector};
|
use crate::{Point, Radians, Size, Vector};
|
||||||
|
|
||||||
/// A rectangle.
|
/// An axis-aligned rectangle.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||||
pub struct Rectangle<T = f32> {
|
pub struct Rectangle<T = f32> {
|
||||||
/// X coordinate of the top-left corner.
|
/// X coordinate of the top-left corner.
|
||||||
|
|
@ -172,6 +172,18 @@ impl Rectangle<f32> {
|
||||||
height: self.height + amount * 2.0,
|
height: self.height + amount * 2.0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Rotates the [`Rectangle`] and returns the smallest [`Rectangle`]
|
||||||
|
/// containing it.
|
||||||
|
pub fn rotate(self, rotation: Radians) -> Self {
|
||||||
|
let size = self.size().rotate(rotation);
|
||||||
|
let position = Point::new(
|
||||||
|
self.center_x() - size.width / 2.0,
|
||||||
|
self.center_y() - size.height / 2.0,
|
||||||
|
);
|
||||||
|
|
||||||
|
Self::new(position, size)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Mul<f32> for Rectangle<f32> {
|
impl std::ops::Mul<f32> for Rectangle<f32> {
|
||||||
|
|
|
||||||
|
|
@ -36,20 +36,12 @@ impl Rotation {
|
||||||
Degrees(self.radians().0.to_degrees())
|
Degrees(self.radians().0.to_degrees())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Rotates the given [`Size`].
|
/// Applies the [`Rotation`] to the given [`Size`], returning
|
||||||
|
/// the minimum [`Size`] containing the rotated one.
|
||||||
pub fn apply(self, size: Size) -> Size {
|
pub fn apply(self, size: Size) -> Size {
|
||||||
match self {
|
match self {
|
||||||
Self::Floating(_) => size,
|
Self::Floating(_) => size,
|
||||||
Self::Solid(rotation) => {
|
Self::Solid(rotation) => size.rotate(rotation),
|
||||||
let radians = f32::from(rotation);
|
|
||||||
|
|
||||||
Size {
|
|
||||||
width: (size.width * radians.cos()).abs()
|
|
||||||
+ (size.height * radians.sin()).abs(),
|
|
||||||
height: (size.width * radians.sin()).abs()
|
|
||||||
+ (size.height * radians.cos()).abs(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::Vector;
|
use crate::{Radians, Vector};
|
||||||
|
|
||||||
/// An amount of space in 2 dimensions.
|
/// An amount of space in 2 dimensions.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
|
||||||
|
|
@ -51,6 +51,19 @@ impl Size {
|
||||||
height: self.height + other.height,
|
height: self.height + other.height,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Rotates the given [`Size`] and returns the minimum [`Size`]
|
||||||
|
/// containing it.
|
||||||
|
pub fn rotate(self, rotation: Radians) -> Size {
|
||||||
|
let radians = f32::from(rotation);
|
||||||
|
|
||||||
|
Size {
|
||||||
|
width: (self.width * radians.cos()).abs()
|
||||||
|
+ (self.height * radians.sin()).abs(),
|
||||||
|
height: (self.width * radians.sin()).abs()
|
||||||
|
+ (self.height * radians.cos()).abs(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> From<[T; 2]> for Size<T> {
|
impl<T> From<[T; 2]> for Size<T> {
|
||||||
|
|
|
||||||
|
|
@ -7,4 +7,4 @@ publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
iced.workspace = true
|
iced.workspace = true
|
||||||
iced.features = ["image"]
|
iced.features = ["image", "debug"]
|
||||||
|
|
|
||||||
|
|
@ -41,9 +41,12 @@ impl Image {
|
||||||
/// Returns the bounds of the [`Image`].
|
/// Returns the bounds of the [`Image`].
|
||||||
pub fn bounds(&self) -> Rectangle {
|
pub fn bounds(&self) -> Rectangle {
|
||||||
match self {
|
match self {
|
||||||
Image::Raster { bounds, .. } | Image::Vector { bounds, .. } => {
|
Image::Raster {
|
||||||
*bounds
|
bounds, rotation, ..
|
||||||
}
|
}
|
||||||
|
| Image::Vector {
|
||||||
|
bounds, rotation, ..
|
||||||
|
} => bounds.rotate(*rotation),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue