utils: Add Rectangle::to_i32_* variants

This commit is contained in:
Victor Brekenfeld 2021-11-27 20:51:32 +01:00
parent 82f2e2def6
commit 3c7bbf65c4
2 changed files with 27 additions and 0 deletions

View File

@ -55,6 +55,10 @@
- The button code for a `PointerButtonEvent` may now be obtained using `PointerButtonEvent::button_code`. - The button code for a `PointerButtonEvent` may now be obtained using `PointerButtonEvent::button_code`.
- `Renderer` now allows texture filtering methods to be set. - `Renderer` now allows texture filtering methods to be set.
#### Utils
- `Rectangle` can now also be converted from f64 to i32 variants
### Bugfixes ### Bugfixes
#### Clients & Protocols #### Clients & Protocols

View File

@ -725,6 +725,29 @@ impl<N: Coordinate, Kind> Rectangle<N, Kind> {
} }
} }
impl<Kind> Rectangle<f64, Kind> {
/// Convert to i32 for integer-space manipulations by rounding float values
#[inline]
pub fn to_i32_round<N: Coordinate>(self) -> Rectangle<N, Kind> {
Rectangle {
loc: self.loc.to_i32_round(),
size: self.size.to_i32_round(),
}
}
/// Convert to i32 by returning the largest integer-space rectangle fitting into the float-based rectangle
#[inline]
pub fn to_i32_down<N: Coordinate>(self) -> Rectangle<N, Kind> {
Rectangle::from_extemities(self.loc.to_i32_ceil(), (self.loc + self.size).to_i32_floor())
}
/// Convert to i32 by returning the smallest integet-space rectangle encapsulating the float-based rectangle
#[inline]
pub fn to_i32_up<N: Coordinate>(self) -> Rectangle<N, Kind> {
Rectangle::from_extemities(self.loc.to_i32_floor(), (self.loc + self.size).to_i32_ceil())
}
}
impl<N: Coordinate, Kind> Rectangle<N, Kind> { impl<N: Coordinate, Kind> Rectangle<N, Kind> {
/// Create a new [`Rectangle`] from the coordinates of its top-left corner and its dimensions /// Create a new [`Rectangle`] from the coordinates of its top-left corner and its dimensions
#[inline] #[inline]