diff --git a/abletk-common/src/lib.rs b/abletk-common/src/lib.rs index a144b8c..d2ee630 100755 --- a/abletk-common/src/lib.rs +++ b/abletk-common/src/lib.rs @@ -45,19 +45,28 @@ impl Renderer { pub fn clear(&self, color: Color) { self.renderer.clear(color) } + + pub fn draw_rect(&self, width: u32, height: u32) { + self.renderer.draw_rect(Rect::new( + self.x as f32, + self.y as f32, + (self.x + width) as f32, + (self.y + height) as f32, + )) + } pub fn draw_text>(&self, text: S) { eprintln!("drawing text at ({}, {})", self.x, self.y); self.renderer.draw_text( text.as_ref(), - Rect { - left: self.x as f32, - top: self.y as f32, + Rect::new( + self.x as f32, + self.y as f32, // these two need to be as big as possible to make sure that // text is not cut off or wrapped - right: f32::INFINITY, - bottom: f32::INFINITY, - } + f32::INFINITY, + f32::INFINITY, + ) ) } @@ -65,8 +74,13 @@ impl Renderer { self.renderer.end_draw() } - pub fn fill_rect(&self, rect: Rect) { - self.renderer.fill_rect(rect) + pub fn fill_rect(&self, width: u32, height: u32) { + self.renderer.fill_rect(Rect::new( + self.x as f32, + self.y as f32, + (self.x + width) as f32, + (self.y + height) as f32, + )) } pub fn get_text_size(&self, text: &str) -> (u32, u32) { diff --git a/abletk-common/src/rect.rs b/abletk-common/src/rect.rs index 58450cb..1af8c21 100755 --- a/abletk-common/src/rect.rs +++ b/abletk-common/src/rect.rs @@ -12,23 +12,33 @@ use windows::Win32::{ Graphics::Direct2D::Common::{D2D_RECT_F, D2D_RECT_U}, }; -// fixme: consider splitting this into two types (f32 and u32) or make this generic #[derive(Copy, Clone, Debug)] pub struct Rect { - pub left: f32, - pub right: f32, - pub top: f32, - pub bottom: f32, + x: f32, + y: f32, + width: f32, + height: f32, +} + +impl Rect { + pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self { + Self { + x, + y, + width, + height, + } + } } #[cfg(windows)] impl From for RECT { fn from(rect: Rect) -> Self { Self { - left: rect.left as i32, - right: rect.right as i32, - top: rect.top as i32, - bottom: rect.bottom as i32, + left: rect.x as i32, + top: rect.y as i32, + right: rect.width as i32, + bottom: rect.height as i32, } } } @@ -37,10 +47,10 @@ impl From for RECT { impl From for D2D_RECT_U { fn from(rect: Rect) -> Self { Self { - left: rect.left as u32, - right: rect.right as u32, - top: rect.top as u32, - bottom: rect.bottom as u32, + left: rect.x as u32, + top: rect.y as u32, + right: rect.width as u32, + bottom: rect.height as u32, } } } @@ -49,10 +59,10 @@ impl From for D2D_RECT_U { impl From for D2D_RECT_F { fn from(rect: Rect) -> Self { Self { - left: rect.left, - right: rect.right, - top: rect.top, - bottom: rect.bottom, + left: rect.x, + top: rect.y, + right: rect.width, + bottom: rect.height, } } } diff --git a/abletk-direct2d/src/lib.rs b/abletk-direct2d/src/lib.rs index 8006340..6743621 100755 --- a/abletk-direct2d/src/lib.rs +++ b/abletk-direct2d/src/lib.rs @@ -61,6 +61,17 @@ impl Renderer { unsafe { self.target.as_ref().unwrap().Clear(&color.into()) } } + pub fn draw_rect>(&self, rect: R) { + unsafe { + self.target.as_ref().unwrap() + .DrawRectangle( + &rect.into(), + self.brush.as_ref().unwrap(), + 1.0, + &self.stroke_style) + } + } + pub fn draw_text>(&self, text: &str, layoutrect: R) { unsafe { self.target.as_ref().unwrap().DrawText(