hUI/hui-examples/boilerplate.rs

71 lines
2 KiB
Rust
Raw Normal View History

2024-02-29 09:02:05 -06:00
use glam::{UVec2, Vec2};
use glium::{backend::glutin::SimpleWindowBuilder, Surface};
use winit::{
event::{Event, WindowEvent},
event_loop::{EventLoopBuilder, ControlFlow}
};
use hui::UiInstance;
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) => {
fn main() {
$crate::boilerplate::ui($closure0, $closure1);
}
};
2024-02-29 09:02:05 -06:00
($closure: expr) => {
fn main() {
$crate::boilerplate::ui(|_|(), $closure);
2024-02-29 09:02:05 -06:00
}
};
}
/// 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)) {
2024-02-29 09:02:05 -06:00
kubi_logging::init();
let event_loop = EventLoopBuilder::new().build().unwrap();
let (window, display) = SimpleWindowBuilder::new().build(&event_loop);
2024-02-29 09:02:05 -06:00
let mut hui = UiInstance::new();
let mut backend = GliumUiRenderer::new(&display);
let result = init(&mut hui);
2024-02-29 09:02:05 -06:00
event_loop.run(|event, window_target| {
window.request_redraw();
2024-02-29 09:02:05 -06:00
window_target.set_control_flow(ControlFlow::Poll);
2024-02-29 09:11:29 -06:00
hui_winit::handle_winit_event(&mut hui, &event);
2024-02-29 09:02:05 -06:00
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => {
window_target.exit();
},
WindowEvent::RedrawRequested => {
let mut frame = display.draw();
2024-03-01 13:49:41 -06:00
frame.clear_color_srgb(0.5, 0.5, 0.5, 1.);
2024-02-29 09:02:05 -06:00
hui.begin();
2024-02-29 09:02:05 -06:00
let size = UVec2::from(display.get_framebuffer_dimensions()).as_vec2();
draw(&mut hui, size, &result);
2024-02-29 09:02:05 -06:00
hui.end();
2024-02-29 09:02:05 -06:00
backend.update(&hui);
backend.draw(&mut frame, size);
2024-02-29 09:02:05 -06:00
frame.finish().unwrap();
},
_ => (),
},
Event::Suspended => {
#[cfg(target_os = "android")]
window_target.exit();
},
2024-02-29 09:02:05 -06:00
_ => (),
}
}).unwrap();
}