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 {
|
2024-03-01 11:21:02 -06:00
|
|
|
(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() {
|
2024-03-01 11:21:02 -06:00
|
|
|
$crate::boilerplate::ui(|_|(), $closure);
|
2024-02-29 09:02:05 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initializes glium renderer, `UiInstance`, and runs the event loop.
|
2024-03-01 11:21:02 -06:00
|
|
|
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();
|
2024-03-01 11:21:02 -06:00
|
|
|
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);
|
|
|
|
|
2024-03-01 11:21:02 -06:00
|
|
|
let result = init(&mut hui);
|
|
|
|
|
2024-02-29 09:02:05 -06:00
|
|
|
event_loop.run(|event, window_target| {
|
2024-03-01 11:21:02 -06:00
|
|
|
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 {
|
2024-03-01 11:21:02 -06:00
|
|
|
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
|
|
|
|
2024-03-01 11:21:02 -06:00
|
|
|
hui.begin();
|
2024-02-29 09:02:05 -06:00
|
|
|
|
2024-03-01 11:21:02 -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
|
|
|
|
2024-03-01 11:21:02 -06:00
|
|
|
hui.end();
|
2024-02-29 09:02:05 -06:00
|
|
|
|
2024-03-01 11:21:02 -06:00
|
|
|
backend.update(&hui);
|
|
|
|
backend.draw(&mut frame, size);
|
2024-02-29 09:02:05 -06:00
|
|
|
|
2024-03-01 11:21:02 -06:00
|
|
|
frame.finish().unwrap();
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
Event::Suspended => {
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
window_target.exit();
|
|
|
|
},
|
2024-02-29 09:02:05 -06:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}).unwrap();
|
|
|
|
}
|