modernize mom downloader

This commit is contained in:
griffi-gh 2024-03-07 00:41:26 +01:00
parent 36de134032
commit b368b0ebb5
2 changed files with 80 additions and 121 deletions

View file

@ -9,20 +9,29 @@ use hui_glium::GliumUiRenderer;
/// Generates a `main` function that initializes glium renderer, `UiInstance`, and runs the event loop. /// Generates a `main` function that initializes glium renderer, `UiInstance`, and runs the event loop.
macro_rules! ui_main { macro_rules! ui_main {
(init: $closure0: expr, run: $closure1: expr) => { ($name:literal, init: $closure0:expr, run: $closure1:expr) => {
fn main() { 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) => { ($closure: expr) => {
fn main() { fn main() {
$crate::boilerplate::ui(|_|(), $closure); $crate::boilerplate::ui(|_|(), $closure, "hUI example");
} }
}; };
} }
/// Initializes glium renderer, `UiInstance`, and runs the event loop. /// Initializes glium renderer, `UiInstance`, and runs the event loop.
pub fn ui<T>(mut init: impl FnMut(&mut UiInstance) -> T, mut draw: impl FnMut(&mut UiInstance, Vec2, &T)) { pub fn ui<T>(
mut init: impl FnMut(&mut UiInstance) -> T,
mut draw: impl FnMut(&mut UiInstance, Vec2, &T),
name: &'static str
) {
kubi_logging::init(); kubi_logging::init();
let event_loop = EventLoopBuilder::new().build().unwrap(); let event_loop = EventLoopBuilder::new().build().unwrap();

View file

@ -1,126 +1,76 @@
//WARNING: THIS EXAMPLE IS EXTREMELY OUTDATED AND USES DEPRECATED API
use std::time::Instant; 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::{ use hui::{
size,
layout::{Alignment, UiDirection},
element::{ element::{
container::Container, container::Container,
progress_bar::ProgressBar, 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<Box<dyn hui::element::UiElement>>)) -> ElementList { #[path = "../boilerplate.rs"]
let mut e = vec![]; #[macro_use]
f(&mut e); mod boilerplate;
ElementList(e)
} ui_main!{
init: |ui| {
fn main() { let font_handle = ui.add_font(include_bytes!("../assets/roboto/Roboto-Regular.ttf"));
kubi_logging::init(); ui.push_font(font_handle);
Instant::now()
let event_loop = EventLoopBuilder::new().build().unwrap(); },
let (window, display) = SimpleWindowBuilder::new().build(&event_loop); run: |ui, max_size, instant| {
window.set_title("Mom Downloader 2000"); let mom_ratio = (instant.elapsed().as_secs_f32() / 60.).powf(0.5);
let mut hui = UiInstance::new(); Container::default()
let mut backend = GliumUiRenderer::new(&display); .with_align(Alignment::Center)
.with_size(size!(100%))
let font_handle = hui.add_font(include_bytes!("../assets/roboto/Roboto-Regular.ttf")); .with_background((0.1, 0.1, 0.1))
.with_children(|ui| {
let instant = Instant::now(); Container::default()
.with_gap(5.)
event_loop.run(|event, window_target| { .with_padding(10.)
window_target.set_control_flow(ControlFlow::Poll); .with_size(size!(450, auto))
match event { .with_background((0.2, 0.2, 0.5))
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { .with_corner_radius(8.)
window_target.exit(); .with_children(|ui| {
}, if instant.elapsed().as_secs_f32() < 5. {
Event::AboutToWait => { Text::default()
let mut frame = display.draw(); .with_text("Downloading your mom...")
frame.clear_color_srgb(0., 0., 0., 1.); .with_text_size(24)
.add_child(ui);
let resolution = UVec2::from(display.get_framebuffer_dimensions()).as_vec2(); ProgressBar::default()
.with_value(mom_ratio)
hui.begin(); .with_corner_radius(0.125 * ProgressBar::DEFAULT_HEIGHT)
.add_child(ui);
let mom_ratio = (instant.elapsed().as_secs_f32() / 60.).powf(0.5); Container::default()
.with_direction(UiDirection::Horizontal)
hui.add(Container { .with_align((Alignment::End, Alignment::Center))
align: Alignment::Center.into(), .with_size(size!(100%, auto))
size: (Size::Fraction(1.), Size::Fraction(1.)).into(), .with_children(|ui| {
background: vec4(0.1, 0.1, 0.1, 1.).into(), Text::default()
children: ElementList(vec![Box::new(Container { .with_text(format!("{:.2}% ({:.1} GB)", mom_ratio * 100., mom_ratio * 10000.))
gap: 5., .with_text_size(16)
padding: Sides::all(10.), .add_child(ui);
size: (Size::Static(450.), Size::Auto).into(), })
background: vec4(0.2, 0.2, 0.5, 1.).into(), .add_child(ui);
corner_radius: Corners::all(8.), } else if instant.elapsed().as_secs() < 10 {
children: elements(|el| { Text::default()
if instant.elapsed().as_secs_f32() < 5. { .with_text("Error 413: Request Entity Too Large")
el.push(Box::new(Text { .with_color((1., 0.125, 0.125, 1.))
text: "Downloading your mom...".into(), .with_text_size(20)
font: Some(font_handle), .add_child(ui);
text_size: 24, Text::default()
..Default::default() .with_text(format!("Exiting in {}...", 10 - instant.elapsed().as_secs()))
})); .with_text_size(16)
el.push(Box::new(ProgressBar { .add_child(ui);
value: mom_ratio, } else {
corner_radius: Corners::all(0.125 * ProgressBar::DEFAULT_HEIGHT), std::process::exit(0);
..Default::default() }
})); })
el.push(Box::new(Container { .add_child(ui);
direction: UiDirection::Horizontal, })
align: (Alignment::End, Alignment::Center).into(), .add_root(ui, max_size)
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();
} }