From 3c7bbf65c4cc7c09edb244284659b7caca4fc447 Mon Sep 17 00:00:00 2001 From: Victor Brekenfeld Date: Sat, 27 Nov 2021 20:51:32 +0100 Subject: [PATCH] utils: Add Rectangle::to_i32_* variants --- CHANGELOG.md | 4 ++++ src/utils/geometry.rs | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e66d0da..21982a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,10 @@ - The button code for a `PointerButtonEvent` may now be obtained using `PointerButtonEvent::button_code`. - `Renderer` now allows texture filtering methods to be set. +#### Utils + +- `Rectangle` can now also be converted from f64 to i32 variants + ### Bugfixes #### Clients & Protocols diff --git a/src/utils/geometry.rs b/src/utils/geometry.rs index 80b6823..3bb5823 100644 --- a/src/utils/geometry.rs +++ b/src/utils/geometry.rs @@ -725,6 +725,29 @@ impl Rectangle { } } +impl Rectangle { + /// Convert to i32 for integer-space manipulations by rounding float values + #[inline] + pub fn to_i32_round(self) -> Rectangle { + 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(self) -> Rectangle { + 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(self) -> Rectangle { + Rectangle::from_extemities(self.loc.to_i32_floor(), (self.loc + self.size).to_i32_ceil()) + } +} + impl Rectangle { /// Create a new [`Rectangle`] from the coordinates of its top-left corner and its dimensions #[inline]