bevy/crates/bevy_math/src/aspect_ratio.rs
Olle Lukowski 6b9cd57956
Introduce AspectRatio struct (#10368)
# Objective

- Fix an inconsistency in the calculation of aspect ratio's. 
- Fixes #10288 

## Solution

- Created an intermediate `AspectRatio` struct, as suggested in the
issue. This is currently just used in any places where aspect ratio
calculations happen, to prevent doing it wrong. In my and @mamekoro 's
opinion, it would be better if this was used instead of a normal `f32`
in various places, but I didn't want to make too many changes to begin
with.

## Migration Guide
- Anywhere where you are currently expecting a f32 when getting aspect
ratios, you will now receive a `AspectRatio` struct. this still holds
the same value.

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2023-12-17 02:01:26 +00:00

26 lines
665 B
Rust

//! Provides a simple aspect ratio struct to help with calculations.
/// An `AspectRatio` is the ratio of width to height.
pub struct AspectRatio(f32);
impl AspectRatio {
/// Create a new `AspectRatio` from a given `width` and `height`.
#[inline]
pub fn new(width: f32, height: f32) -> Self {
Self(width / height)
}
/// Create a new `AspectRatio` from a given amount of `x` pixels and `y` pixels.
#[inline]
pub fn from_pixels(x: u32, y: u32) -> Self {
Self::new(x as f32, y as f32)
}
}
impl From<AspectRatio> for f32 {
#[inline]
fn from(aspect_ratio: AspectRatio) -> Self {
aspect_ratio.0
}
}