rect changes

This commit is contained in:
TheOddGarlic 2022-04-29 21:53:56 +03:00
parent 70a3a97d48
commit 4ef18b26e8
3 changed files with 60 additions and 25 deletions

View file

@ -46,18 +46,27 @@ impl Renderer {
self.renderer.clear(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<S: AsRef<str>>(&self, text: S) { pub fn draw_text<S: AsRef<str>>(&self, text: S) {
eprintln!("drawing text at ({}, {})", self.x, self.y); eprintln!("drawing text at ({}, {})", self.x, self.y);
self.renderer.draw_text( self.renderer.draw_text(
text.as_ref(), text.as_ref(),
Rect { Rect::new(
left: self.x as f32, self.x as f32,
top: self.y as f32, self.y as f32,
// these two need to be as big as possible to make sure that // these two need to be as big as possible to make sure that
// text is not cut off or wrapped // text is not cut off or wrapped
right: f32::INFINITY, f32::INFINITY,
bottom: f32::INFINITY, f32::INFINITY,
} )
) )
} }
@ -65,8 +74,13 @@ impl Renderer {
self.renderer.end_draw() self.renderer.end_draw()
} }
pub fn fill_rect(&self, rect: Rect) { pub fn fill_rect(&self, width: u32, height: u32) {
self.renderer.fill_rect(rect) 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) { pub fn get_text_size(&self, text: &str) -> (u32, u32) {

View file

@ -12,23 +12,33 @@ use windows::Win32::{
Graphics::Direct2D::Common::{D2D_RECT_F, D2D_RECT_U}, 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)] #[derive(Copy, Clone, Debug)]
pub struct Rect { pub struct Rect {
pub left: f32, x: f32,
pub right: f32, y: f32,
pub top: f32, width: f32,
pub bottom: f32, height: f32,
}
impl Rect {
pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
Self {
x,
y,
width,
height,
}
}
} }
#[cfg(windows)] #[cfg(windows)]
impl From<Rect> for RECT { impl From<Rect> for RECT {
fn from(rect: Rect) -> Self { fn from(rect: Rect) -> Self {
Self { Self {
left: rect.left as i32, left: rect.x as i32,
right: rect.right as i32, top: rect.y as i32,
top: rect.top as i32, right: rect.width as i32,
bottom: rect.bottom as i32, bottom: rect.height as i32,
} }
} }
} }
@ -37,10 +47,10 @@ impl From<Rect> for RECT {
impl From<Rect> for D2D_RECT_U { impl From<Rect> for D2D_RECT_U {
fn from(rect: Rect) -> Self { fn from(rect: Rect) -> Self {
Self { Self {
left: rect.left as u32, left: rect.x as u32,
right: rect.right as u32, top: rect.y as u32,
top: rect.top as u32, right: rect.width as u32,
bottom: rect.bottom as u32, bottom: rect.height as u32,
} }
} }
} }
@ -49,10 +59,10 @@ impl From<Rect> for D2D_RECT_U {
impl From<Rect> for D2D_RECT_F { impl From<Rect> for D2D_RECT_F {
fn from(rect: Rect) -> Self { fn from(rect: Rect) -> Self {
Self { Self {
left: rect.left, left: rect.x,
right: rect.right, top: rect.y,
top: rect.top, right: rect.width,
bottom: rect.bottom, bottom: rect.height,
} }
} }
} }

View file

@ -61,6 +61,17 @@ impl Renderer {
unsafe { self.target.as_ref().unwrap().Clear(&color.into()) } unsafe { self.target.as_ref().unwrap().Clear(&color.into()) }
} }
pub fn draw_rect<R: Into<D2D_RECT_F>>(&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<R: Into<D2D_RECT_F>>(&self, text: &str, layoutrect: R) { pub fn draw_text<R: Into<D2D_RECT_F>>(&self, text: &str, layoutrect: R) {
unsafe { unsafe {
self.target.as_ref().unwrap().DrawText( self.target.as_ref().unwrap().DrawText(