Compare commits

...

6 Commits

Author SHA1 Message Date
TheOddGarlic b3aab25989 fix: Row now restores previous X position
This patch fixes a bug that's extremely similar to the bug fixed by the
commit before this.
2022-05-01 18:35:01 +03:00
TheOddGarlic 88f518f7c1 fix: Column now restores previous Y position
This patch fixes the bug where positioning is all wonky after having
rendered a Column, because Column didn't restore the previous Y
position. Similar bugs probably will occur but this is the first one I
catched.
2022-05-01 18:29:36 +03:00
TheOddGarlic 3c5c57cf22 formatting changes 2022-05-01 18:24:13 +03:00
TheOddGarlic 365fb6741c render visibility modifier changed to pub(crate) 2022-04-29 22:07:14 +03:00
TheOddGarlic 0e2066e9fe label background color 2022-04-29 22:02:12 +03:00
TheOddGarlic 4ef18b26e8 rect changes 2022-04-29 21:53:56 +03:00
8 changed files with 92 additions and 36 deletions

View File

@ -11,8 +11,8 @@ pub mod color;
pub mod rect;
use brush::Brush;
use raw_window_handle::RawWindowHandle;
use color::Color;
use raw_window_handle::RawWindowHandle;
use rect::Rect;
#[cfg(windows)]
@ -28,7 +28,7 @@ impl Renderer {
pub fn new(handle: RawWindowHandle) -> Self {
let handle = match handle {
RawWindowHandle::Win32(handle) => handle,
_ => todo!()
_ => todo!(),
};
Self {
@ -46,18 +46,27 @@ impl Renderer {
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) {
@ -85,7 +99,7 @@ impl Renderer {
pub fn resized(&mut self, width: u32, height: u32) {
self.renderer.resized(width, height)
}
pub fn set_brush(&mut self, brush: Brush) {
self.renderer.set_brush(brush.into())
}

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

@ -25,7 +25,7 @@ pub struct Renderer {
brush: Option<ID2D1Brush>,
stroke_style: ID2D1StrokeStyle,
system_fonts: IDWriteFontCollection,
text_format: IDWriteTextFormat
text_format: IDWriteTextFormat,
}
impl Renderer {
@ -50,7 +50,7 @@ impl Renderer {
text_format,
}
}
pub fn begin_draw(&mut self) {
self.setup_target();
@ -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(

View File

@ -20,5 +20,10 @@ fn launch() -> _ {
.color(rgb!(0xFF00FFFF)))
.add(Column::new()
.add(Label::new("World!"))
.add(Label::new("AbleTK!")))))
.add(Label::new("AbleTK!")))
.add(Label::new("this is a label!")
.bg_color(rgb!(0xFF0000FF))))
.on_event(WindowEvent::Closed, |_, window| {
window.set_title("CLOSING!")
}))
}

View File

@ -15,6 +15,7 @@ pub struct Column {
impl Widget for Column {
fn draw(&self, renderer: &mut Renderer) {
let (_, prev_y) = renderer.position();
self.widgets.iter().for_each(|widget| {
let pos = widget.position(renderer);
renderer.position_at(
@ -27,6 +28,8 @@ impl Widget for Column {
renderer.position().1 + pos.height()
);
});
renderer.position_at(renderer.position().0, prev_y)
}
fn position(&self, renderer: &mut Renderer) -> Position {

View File

@ -11,12 +11,16 @@ use crate::{widget::Widget, layout::position::Position};
pub struct Label {
text: String,
color: Color,
fg_color: Color,
bg_color: Color,
}
impl Widget for Label {
fn draw(&self, renderer: &mut Renderer) {
renderer.set_brush(Brush::Solid(self.color));
let pos = self.position(renderer);
renderer.set_brush(Brush::Solid(self.bg_color));
renderer.fill_rect(pos.width(), pos.height());
renderer.set_brush(Brush::Solid(self.fg_color));
renderer.draw_text(&self.text);
}
@ -30,12 +34,18 @@ impl Label {
pub fn new<S: Into<String>>(text: S) -> Self {
Self {
text: text.into(),
color: rgb!(0x000000FF),
fg_color: rgb!(0x000000FF),
bg_color: rgb!(0x00000000),
}
}
pub fn color(mut self, color: Color) -> Self {
self.color = color;
self.fg_color = color;
self
}
pub fn bg_color(mut self, bg: Color) -> Self {
self.bg_color = bg;
self
}
}

View File

@ -15,6 +15,7 @@ pub struct Row {
impl Widget for Row {
fn draw(&self, renderer: &mut Renderer) {
let (prev_x, _) = renderer.position();
self.widgets.iter().for_each(|widget| {
let pos = widget.position(renderer);
renderer.position_at(
@ -27,6 +28,8 @@ impl Widget for Row {
renderer.position().1
);
});
renderer.position_at(prev_x, renderer.position().1)
}
fn position(&self, renderer: &mut Renderer) -> Position {

View File

@ -71,7 +71,7 @@ impl Window {
WindowBuilder::new(root)
}
pub fn render(&mut self) {
pub(crate) fn render(&mut self) {
let position = self.root.position(&mut self.renderer);
eprintln!("Rendering window with id {:?}", self.id());
eprintln!("Root widget position: {position:?}");