modernize mom downloader

This commit is contained in:
griffi-gh 2024-03-07 00:41:26 +01:00
parent 2c20da0b3b
commit 677cc7d37d
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.
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<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();
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 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<Box<dyn hui::element::UiElement>>)) -> ElementList {
let mut e = vec![];
f(&mut e);
ElementList(e)
}
#[path = "../boilerplate.rs"]
#[macro_use]
mod boilerplate;
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();
ui_main!{
init: |ui| {
let font_handle = ui.add_font(include_bytes!("../assets/roboto/Roboto-Regular.ttf"));
ui.push_font(font_handle);
Instant::now()
},
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();
run: |ui, max_size, instant| {
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| {
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. {
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()
}));
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 {
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()
}));
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 {
window_target.exit();
std::process::exit(0);
}
}),
..Default::default()
})]),
..Default::default()
}, resolution);
hui.end();
backend.update(&hui);
backend.draw(&mut frame, resolution);
frame.finish().unwrap();
})
.add_child(ui);
})
.add_root(ui, max_size)
}
_ => (),
}
}).unwrap();
}