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)
}
fn main() { ui_main!{
kubi_logging::init(); init: |ui| {
let font_handle = ui.add_font(include_bytes!("../assets/roboto/Roboto-Regular.ttf"));
let event_loop = EventLoopBuilder::new().build().unwrap(); ui.push_font(font_handle);
let (window, display) = SimpleWindowBuilder::new().build(&event_loop); Instant::now()
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 => { run: |ui, max_size, instant| {
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); let mom_ratio = (instant.elapsed().as_secs_f32() / 60.).powf(0.5);
hui.add(Container { Container::default()
align: Alignment::Center.into(), .with_align(Alignment::Center)
size: (Size::Fraction(1.), Size::Fraction(1.)).into(), .with_size(size!(100%))
background: vec4(0.1, 0.1, 0.1, 1.).into(), .with_background((0.1, 0.1, 0.1))
children: ElementList(vec![Box::new(Container { .with_children(|ui| {
gap: 5., Container::default()
padding: Sides::all(10.), .with_gap(5.)
size: (Size::Static(450.), Size::Auto).into(), .with_padding(10.)
background: vec4(0.2, 0.2, 0.5, 1.).into(), .with_size(size!(450, auto))
corner_radius: Corners::all(8.), .with_background((0.2, 0.2, 0.5))
children: elements(|el| { .with_corner_radius(8.)
.with_children(|ui| {
if instant.elapsed().as_secs_f32() < 5. { if instant.elapsed().as_secs_f32() < 5. {
el.push(Box::new(Text { Text::default()
text: "Downloading your mom...".into(), .with_text("Downloading your mom...")
font: Some(font_handle), .with_text_size(24)
text_size: 24, .add_child(ui);
..Default::default() ProgressBar::default()
})); .with_value(mom_ratio)
el.push(Box::new(ProgressBar { .with_corner_radius(0.125 * ProgressBar::DEFAULT_HEIGHT)
value: mom_ratio, .add_child(ui);
corner_radius: Corners::all(0.125 * ProgressBar::DEFAULT_HEIGHT), Container::default()
..Default::default() .with_direction(UiDirection::Horizontal)
})); .with_align((Alignment::End, Alignment::Center))
el.push(Box::new(Container { .with_size(size!(100%, auto))
direction: UiDirection::Horizontal, .with_children(|ui| {
align: (Alignment::End, Alignment::Center).into(), Text::default()
size: (Size::Fraction(1.), Size::Auto).into(), .with_text(format!("{:.2}% ({:.1} GB)", mom_ratio * 100., mom_ratio * 10000.))
children: ElementList(vec![Box::new(Text { .with_text_size(16)
text: format!("{:.2}% ({:.1} GB)", mom_ratio * 100., mom_ratio * 10000.).into(), .add_child(ui);
font: Some(font_handle), })
text_size: 16, .add_child(ui);
..Default::default()
})]),
..Default::default()
}));
} else if instant.elapsed().as_secs() < 10 { } else if instant.elapsed().as_secs() < 10 {
el.push(Box::new(Text { Text::default()
text: "Error 413: Request Entity Too Large".into(), .with_text("Error 413: Request Entity Too Large")
font: Some(font_handle), .with_color((1., 0.125, 0.125, 1.))
color: vec4(1., 0.125, 0.125, 1.), .with_text_size(20)
text_size: 20, .add_child(ui);
..Default::default() Text::default()
})); .with_text(format!("Exiting in {}...", 10 - instant.elapsed().as_secs()))
el.push(Box::new(Text { .with_text_size(16)
text: format!("Exiting in {}...", 10 - instant.elapsed().as_secs()).into(), .add_child(ui);
font: Some(font_handle),
text_size: 16,
..Default::default()
}));
} else { } else {
window_target.exit(); std::process::exit(0);
} }
}), })
..Default::default() .add_child(ui);
})]), })
..Default::default() .add_root(ui, max_size)
}, resolution);
hui.end();
backend.update(&hui);
backend.draw(&mut frame, resolution);
frame.finish().unwrap();
} }
_ => (),
}
}).unwrap();
} }