From a1a5fcfd46622d5b18d1716aa2adb4659835ccf3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 May 2020 20:28:35 +0200 Subject: Refactor `Viewport` and `Compositor` --- core/src/rectangle.rs | 33 +++++++++++++++++---------------- core/src/size.rs | 22 ++++++++++++---------- 2 files changed, 29 insertions(+), 26 deletions(-) (limited to 'core') diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index 8bc89a44..aa23372e 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -125,17 +125,29 @@ impl Rectangle { None } } + + /// Rounds the [`Rectangle`] to __unsigned__ integer coordinates. + /// + /// [`Rectangle`]: struct.Rectangle.html + pub fn round(self) -> Rectangle { + Rectangle { + x: self.x as u32, + y: self.y as u32, + width: (self.width + 0.5).round() as u32, + height: (self.height + 0.5).round() as u32, + } + } } -impl std::ops::Mul for Rectangle { +impl std::ops::Mul for Rectangle { type Output = Self; fn mul(self, scale: f32) -> Self { Self { - x: (self.x as f32 * scale).round() as u32, - y: (self.y as f32 * scale).round() as u32, - width: (self.width as f32 * scale).round() as u32, - height: (self.height as f32 * scale).round() as u32, + x: self.x as f32 * scale, + y: self.y as f32 * scale, + width: self.width * scale, + height: self.height * scale, } } } @@ -151,17 +163,6 @@ impl From> for Rectangle { } } -impl From> for Rectangle { - fn from(rectangle: Rectangle) -> Rectangle { - Rectangle { - x: rectangle.x as u32, - y: rectangle.y as u32, - width: (rectangle.width + 0.5).round() as u32, - height: (rectangle.height + 0.5).round() as u32, - } - } -} - impl std::ops::Add> for Rectangle where T: std::ops::Add, diff --git a/core/src/size.rs b/core/src/size.rs index a02299e8..aceb5311 100644 --- a/core/src/size.rs +++ b/core/src/size.rs @@ -2,11 +2,20 @@ use std::f32; /// An amount of space in 2 dimensions. #[derive(Debug, Clone, Copy, PartialEq)] -pub struct Size { +pub struct Size { /// The width. - pub width: f32, + pub width: T, /// The height. - pub height: f32, + pub height: T, +} + +impl Size { + /// Creates a new [`Size`] with the given width and height. + /// + /// [`Size`]: struct.Size.html + pub const fn new(width: T, height: T) -> Self { + Size { width, height } + } } impl Size { @@ -25,13 +34,6 @@ impl Size { /// [`Size`]: struct.Size.html pub const INFINITY: Size = Size::new(f32::INFINITY, f32::INFINITY); - /// Creates a new [`Size`] with the given width and height. - /// - /// [`Size`]: struct.Size.html - pub const fn new(width: f32, height: f32) -> Self { - Size { width, height } - } - /// Increments the [`Size`] to account for the given padding. /// /// [`Size`]: struct.Size.html -- cgit