diff --git a/src/elements/mod.rs b/src/elements/mod.rs index fdd2c95..42a8f03 100644 --- a/src/elements/mod.rs +++ b/src/elements/mod.rs @@ -2,12 +2,12 @@ pub mod div; use crate::widget::Element; -pub struct Rect; +pub struct Rect(pub u32); impl Element for Rect { fn render(&mut self, scope: &mut crate::render::RenderScope) { let (w, h) = scope.get_size(); - scope.draw_rect(0, 0, w, h, 0xffffff); + scope.draw_rect(0, 0, w, h, self.0); } fn as_any(&self) -> &dyn std::any::Any { @@ -32,3 +32,51 @@ impl Element for String { self } } + +impl Element for (String, f32) { + fn render(&mut self, scope: &mut crate::render::RenderScope) { + scope.draw_text(0, 0, 40.0 * self.1, &self.0, 0xffffff); + } + + fn as_any(&self) -> &dyn std::any::Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn std::any::Any { + self + } +} + +impl Element for Box { + fn as_any(&self) -> &dyn std::any::Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn std::any::Any { + self + } + + fn render(&mut self, scope: &mut crate::render::RenderScope) { + self(scope) + } +} + +impl Element for Box { + fn as_any(&self) -> &dyn std::any::Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn std::any::Any { + self + } + + fn render(&mut self, scope: &mut crate::render::RenderScope) { + self(scope) + } +} + +pub fn custom( + f: F, +) -> Box { + Box::new(f) +} diff --git a/src/main.rs b/src/main.rs index c03976c..937cb49 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,14 @@ use minifb::{Window, WindowOptions}; use osgui::{ - elements::div::{Div, DivStyle}, + elements::{ + custom, + div::{Div, DivStyle}, + }, extensions::{ mouse::{MouseExtension, OnClick}, velocity::{Velocity, VelocityExtension}, }, + render::RenderScope, style::Transform, Screen, }; @@ -25,7 +29,8 @@ fn main() { app.extension(MouseExtension::new()); app.extension(VelocityExtension); - milestones(&mut app); + // milestones(&mut app); + todos(&mut app); app.run().unwrap(); } @@ -54,6 +59,34 @@ fn milestones(app: &mut Screen) { osgui::style::Position::Const(145 + ((194 + 30) * i)), )) .component(OnClick(|_| println!("Clicked!"))) - .component(Velocity(50, 0)); + .component(Velocity(300, 0)); } } + +fn todos(app: &mut Screen) { + todo_box(app, Transform::center(), true, "Hello, World!"); +} + +fn todo_box(app: &mut Screen, transform: Transform, done: bool, text: &str) { + let text = text.to_string(); + app.draw(custom(move |scope: &mut RenderScope| { + scope.draw_rect_rounded(0, 2, 24, 24, 8, 0xffffff); + scope.draw_rect_rounded(3, 5, 18, 18, 4, 0x010101); + if done { + let (mut x, mut y) = (5, 14); + while x != 8 { + scope.draw_rect_rounded(x, y, 2, 2, 4, 0xffffff); + x += 1; + y += 1; + } + + while x != 17 && y != 7 { + scope.draw_rect_rounded(x, y, 3, 2, 4, 0xffffff); + x += 1; + y -= 1; + } + } + scope.draw_text(29, 0, 28.0, &text, 0xffffff); + })) + .component(transform.clone()); +}