From 050dde6b4f613af7d5b9fa8fd78f12efb64500ff Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 21 Sep 2019 07:44:28 -0700 Subject: [PATCH] clean up draw_image api --- window/src/bitmaps/mod.rs | 30 +++++++++++------------------- window/src/lib.rs | 18 ++---------------- window/src/os/macos/window.rs | 6 +++--- window/src/os/windows/window.rs | 6 +++--- window/src/os/x11/window.rs | 17 ++++------------- 5 files changed, 23 insertions(+), 54 deletions(-) diff --git a/window/src/bitmaps/mod.rs b/window/src/bitmaps/mod.rs index 75f20d835..406456fe0 100644 --- a/window/src/bitmaps/mod.rs +++ b/window/src/bitmaps/mod.rs @@ -87,7 +87,7 @@ pub trait BitmapImage { } } - /// Draw a line starting at (start_x, start_y) and ending at (dest_x, dest_y). + /// Draw a line starting at `start` and ending at `end`. /// The line will be anti-aliased and applied to the surface using the /// specified Operator. fn draw_line(&mut self, start: Point, end: Point, color: Color, operator: Operator) { @@ -157,30 +157,22 @@ pub trait BitmapImage { ); } - fn draw_image(&mut self, top_left: Point, im: &dyn BitmapImage, operator: Operator) { - let (width, height) = im.image_dimensions(); - self.draw_image_subset( - top_left, - Rect { - top_left: Point { x: 0, y: 0 }, - width, - height, - }, - im, - operator, - ) - } - - fn draw_image_subset( + fn draw_image( &mut self, dest_top_left: Point, - src_rect: Rect, + src_rect: Option, im: &dyn BitmapImage, operator: Operator, ) { - let (dest_width, dest_height) = im.image_dimensions(); + let (im_width, im_height) = im.image_dimensions(); + let src_rect = src_rect.unwrap_or_else(|| Rect { + top_left: Point { x: 0, y: 0 }, + width: im_width, + height: im_height, + }); + let (dim_width, dim_height) = self.image_dimensions(); - debug_assert!(src_rect.width <= dest_width && src_rect.height <= dest_height); + debug_assert!(src_rect.width <= im_width && src_rect.height <= im_height); for y in src_rect.top_left.y as usize..src_rect.top_left.y as usize + src_rect.height { let dest_y = y as isize + dest_top_left.y - src_rect.top_left.y as isize; if dest_y < 0 { diff --git a/window/src/lib.rs b/window/src/lib.rs index eaebef471..9eaba282f 100644 --- a/window/src/lib.rs +++ b/window/src/lib.rs @@ -85,24 +85,10 @@ pub trait PaintContext { /// Clear a rectangle to the specified color fn clear_rect(&mut self, rect: Rect, color: Color); - fn draw_image(&mut self, top_left: Point, im: &dyn BitmapImage, operator: Operator) { - let (width, height) = im.image_dimensions(); - self.draw_image_subset( - top_left, - Rect { - top_left: Point { x: 0, y: 0 }, - width, - height, - }, - im, - operator, - ) - } - - fn draw_image_subset( + fn draw_image( &mut self, dest_top_left: Point, - src_rect: Rect, + src_rect: Option, im: &dyn BitmapImage, operator: Operator, ); diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index 628a502dc..42dbd7ce1 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -216,15 +216,15 @@ impl<'a> PaintContext for MacGraphicsContext<'a> { } } - fn draw_image_subset( + fn draw_image( &mut self, dest_top_left: Point, - src_rect: Rect, + src_rect: Option, im: &dyn BitmapImage, operator: Operator, ) { self.buffer - .draw_image_subset(dest_top_left, src_rect, im, operator) + .draw_image(dest_top_left, src_rect, im, operator) } fn draw_line(&mut self, start: Point, end: Point, color: Color, operator: Operator) { diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index 248aabb4a..058772425 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -345,15 +345,15 @@ impl PaintContext for GdiGraphicsContext { } } - fn draw_image_subset( + fn draw_image( &mut self, dest_top_left: Point, - src_rect: Rect, + src_rect: Option, im: &dyn BitmapImage, operator: Operator, ) { self.buffer - .draw_image_subset(dest_top_left, src_rect, im, operator) + .draw_image(dest_top_left, src_rect, im, operator) } fn draw_line(&mut self, start: Point, end: Point, color: Color, operator: Operator) { diff --git a/window/src/os/x11/window.rs b/window/src/os/x11/window.rs index a69e3fb96..92df1f5c1 100644 --- a/window/src/os/x11/window.rs +++ b/window/src/os/x11/window.rs @@ -101,15 +101,15 @@ impl<'a> PaintContext for X11GraphicsContext<'a> { } } - fn draw_image_subset( + fn draw_image( &mut self, dest_top_left: Point, - src_rect: Rect, + src_rect: Option, im: &dyn BitmapImage, operator: Operator, ) { self.buffer - .draw_image_subset(dest_top_left, src_rect, im, operator) + .draw_image(dest_top_left, src_rect, im, operator) } fn draw_line(&mut self, start: Point, end: Point, color: Color, operator: Operator) { @@ -184,16 +184,7 @@ impl WindowInner { } else { let mut im = Image::new(rect.width as usize, rect.height as usize); - im.draw_image_subset( - 0, - 0, - rect.x as usize, - rect.y as usize, - rect.width as usize, - rect.height as usize, - buffer, - Operator::Source, - ); + im.draw_image(Point { x: 0, y: 0 }, Some(rect), buffer, Operator::Source); self.window_context .put_image(rect.x as i16, rect.y as i16, &im);