rect changes

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

View File

@ -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<S: AsRef<str>>(&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) {

View File

@ -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<Rect> 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<Rect> for RECT {
impl From<Rect> 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<Rect> for D2D_RECT_U {
impl From<Rect> 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,
}
}
}

View File

@ -61,6 +61,17 @@ impl Renderer {
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) {
unsafe {
self.target.as_ref().unwrap().DrawText(