From b368b0ebb5e4d7691db660432b7294a95a29b7dd Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Thu, 7 Mar 2024 00:41:26 +0100 Subject: [PATCH] modernize mom downloader --- hui-examples/boilerplate.rs | 17 ++- hui-examples/examples/mom_downloader.rs | 184 +++++++++--------------- 2 files changed, 80 insertions(+), 121 deletions(-) diff --git a/hui-examples/boilerplate.rs b/hui-examples/boilerplate.rs index 3373419..f9e6db3 100644 --- a/hui-examples/boilerplate.rs +++ b/hui-examples/boilerplate.rs @@ -9,20 +9,29 @@ use hui_glium::GliumUiRenderer; /// Generates a `main` function that initializes glium renderer, `UiInstance`, and runs the event loop. macro_rules! ui_main { - (init: $closure0: expr, run: $closure1: expr) => { + ($name:literal, init: $closure0:expr, run: $closure1:expr) => { fn main() { - $crate::boilerplate::ui($closure0, $closure1); + $crate::boilerplate::ui($closure0, $closure1, $name); + } + }; + (init: $closure0:expr, run: $closure1:expr) => { + fn main() { + $crate::boilerplate::ui($closure0, $closure1, "hUI example"); } }; ($closure: expr) => { fn main() { - $crate::boilerplate::ui(|_|(), $closure); + $crate::boilerplate::ui(|_|(), $closure, "hUI example"); } }; } /// Initializes glium renderer, `UiInstance`, and runs the event loop. -pub fn ui(mut init: impl FnMut(&mut UiInstance) -> T, mut draw: impl FnMut(&mut UiInstance, Vec2, &T)) { +pub fn ui( + mut init: impl FnMut(&mut UiInstance) -> T, + mut draw: impl FnMut(&mut UiInstance, Vec2, &T), + name: &'static str +) { kubi_logging::init(); let event_loop = EventLoopBuilder::new().build().unwrap(); diff --git a/hui-examples/examples/mom_downloader.rs b/hui-examples/examples/mom_downloader.rs index 448daa8..c4ea751 100644 --- a/hui-examples/examples/mom_downloader.rs +++ b/hui-examples/examples/mom_downloader.rs @@ -1,126 +1,76 @@ -//WARNING: THIS EXAMPLE IS EXTREMELY OUTDATED AND USES DEPRECATED API - use std::time::Instant; -use glam::{UVec2, vec4}; -use glium::{backend::glutin::SimpleWindowBuilder, Surface}; -use winit::{ - event::{Event, WindowEvent}, - event_loop::{EventLoopBuilder, ControlFlow} -}; use hui::{ + size, + layout::{Alignment, UiDirection}, element::{ container::Container, progress_bar::ProgressBar, - text::Text, ElementList, + text::Text, + UiElementExt, }, - layout::{Alignment, UiDirection, Size}, - rectangle::{Corners, Sides}, - UiInstance, }; -use hui_glium::GliumUiRenderer; -fn elements(mut f: impl FnMut(&mut Vec>)) -> ElementList { - let mut e = vec![]; - f(&mut e); - ElementList(e) -} - -fn main() { - kubi_logging::init(); - - let event_loop = EventLoopBuilder::new().build().unwrap(); - let (window, display) = SimpleWindowBuilder::new().build(&event_loop); - window.set_title("Mom Downloader 2000"); - - let mut hui = UiInstance::new(); - let mut backend = GliumUiRenderer::new(&display); - - let font_handle = hui.add_font(include_bytes!("../assets/roboto/Roboto-Regular.ttf")); - - let instant = Instant::now(); - - event_loop.run(|event, window_target| { - window_target.set_control_flow(ControlFlow::Poll); - match event { - Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { - window_target.exit(); - }, - Event::AboutToWait => { - let mut frame = display.draw(); - frame.clear_color_srgb(0., 0., 0., 1.); - - let resolution = UVec2::from(display.get_framebuffer_dimensions()).as_vec2(); - - hui.begin(); - - let mom_ratio = (instant.elapsed().as_secs_f32() / 60.).powf(0.5); - - hui.add(Container { - align: Alignment::Center.into(), - size: (Size::Fraction(1.), Size::Fraction(1.)).into(), - background: vec4(0.1, 0.1, 0.1, 1.).into(), - children: ElementList(vec![Box::new(Container { - gap: 5., - padding: Sides::all(10.), - size: (Size::Static(450.), Size::Auto).into(), - background: vec4(0.2, 0.2, 0.5, 1.).into(), - corner_radius: Corners::all(8.), - children: elements(|el| { - if instant.elapsed().as_secs_f32() < 5. { - el.push(Box::new(Text { - text: "Downloading your mom...".into(), - font: Some(font_handle), - text_size: 24, - ..Default::default() - })); - el.push(Box::new(ProgressBar { - value: mom_ratio, - corner_radius: Corners::all(0.125 * ProgressBar::DEFAULT_HEIGHT), - ..Default::default() - })); - el.push(Box::new(Container { - direction: UiDirection::Horizontal, - align: (Alignment::End, Alignment::Center).into(), - size: (Size::Fraction(1.), Size::Auto).into(), - children: ElementList(vec![Box::new(Text { - text: format!("{:.2}% ({:.1} GB)", mom_ratio * 100., mom_ratio * 10000.).into(), - font: Some(font_handle), - text_size: 16, - ..Default::default() - })]), - ..Default::default() - })); - } else if instant.elapsed().as_secs() < 10 { - el.push(Box::new(Text { - text: "Error 413: Request Entity Too Large".into(), - font: Some(font_handle), - color: vec4(1., 0.125, 0.125, 1.), - text_size: 20, - ..Default::default() - })); - el.push(Box::new(Text { - text: format!("Exiting in {}...", 10 - instant.elapsed().as_secs()).into(), - font: Some(font_handle), - text_size: 16, - ..Default::default() - })); - } else { - window_target.exit(); - } - }), - ..Default::default() - })]), - ..Default::default() - }, resolution); - - hui.end(); - - backend.update(&hui); - backend.draw(&mut frame, resolution); - - frame.finish().unwrap(); - } - _ => (), - } - }).unwrap(); +#[path = "../boilerplate.rs"] +#[macro_use] +mod boilerplate; + +ui_main!{ + init: |ui| { + let font_handle = ui.add_font(include_bytes!("../assets/roboto/Roboto-Regular.ttf")); + ui.push_font(font_handle); + Instant::now() + }, + run: |ui, max_size, instant| { + let mom_ratio = (instant.elapsed().as_secs_f32() / 60.).powf(0.5); + + Container::default() + .with_align(Alignment::Center) + .with_size(size!(100%)) + .with_background((0.1, 0.1, 0.1)) + .with_children(|ui| { + Container::default() + .with_gap(5.) + .with_padding(10.) + .with_size(size!(450, auto)) + .with_background((0.2, 0.2, 0.5)) + .with_corner_radius(8.) + .with_children(|ui| { + if instant.elapsed().as_secs_f32() < 5. { + Text::default() + .with_text("Downloading your mom...") + .with_text_size(24) + .add_child(ui); + ProgressBar::default() + .with_value(mom_ratio) + .with_corner_radius(0.125 * ProgressBar::DEFAULT_HEIGHT) + .add_child(ui); + Container::default() + .with_direction(UiDirection::Horizontal) + .with_align((Alignment::End, Alignment::Center)) + .with_size(size!(100%, auto)) + .with_children(|ui| { + Text::default() + .with_text(format!("{:.2}% ({:.1} GB)", mom_ratio * 100., mom_ratio * 10000.)) + .with_text_size(16) + .add_child(ui); + }) + .add_child(ui); + } else if instant.elapsed().as_secs() < 10 { + Text::default() + .with_text("Error 413: Request Entity Too Large") + .with_color((1., 0.125, 0.125, 1.)) + .with_text_size(20) + .add_child(ui); + Text::default() + .with_text(format!("Exiting in {}...", 10 - instant.elapsed().as_secs())) + .with_text_size(16) + .add_child(ui); + } else { + std::process::exit(0); + } + }) + .add_child(ui); + }) + .add_root(ui, max_size) + } }