This commit is contained in:
griffi-gh 2024-02-19 04:37:28 +01:00
parent 4828500c45
commit c50333805b
2 changed files with 89 additions and 0 deletions

View file

@ -0,0 +1,55 @@
use glam::{vec4, UVec2};
use glium::{backend::glutin::SimpleWindowBuilder, Surface};
use winit::{
event::{Event, WindowEvent},
event_loop::{EventLoopBuilder, ControlFlow}
};
use hui::{
UiInstance, UiSize,
element::container::{Alignment, Container, Sides}
};
use hui_glium::GliumUiRenderer;
fn main() {
kubi_logging::init();
let event_loop = EventLoopBuilder::new().build().unwrap();
let (_window, display) = SimpleWindowBuilder::new().build(&event_loop);
let mut hui = UiInstance::new();
let mut backend = GliumUiRenderer::new(&display);
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 => {
let mut frame = display.draw();
frame.clear_color_srgb(0.5, 0.5, 0.5, 0.);
let resolution = UVec2::from(display.get_framebuffer_dimensions()).as_vec2();
hui.begin();
hui.add(Container {
gap: 5.,
padding: Sides::all(5.),
align: (Alignment::Center, Alignment::Center),
size: (UiSize::Fraction(1.), UiSize::Fraction(1.)),
background: Some(vec4(0.1, 0.1, 0.1, 1.)),
corner_radius: Some(10.),
..Default::default()
}, resolution);
hui.end();
backend.update(&hui);
backend.draw(&mut frame, resolution);
frame.finish().unwrap();
}
_ => (),
}
}).unwrap();
}

View file

@ -136,9 +136,43 @@ impl UiDrawPlan {
let corner_radius = corner_radius.unwrap_or(0.0); let corner_radius = corner_radius.unwrap_or(0.0);
let vidx = swapper.current().vertices.len() as u32; let vidx = swapper.current().vertices.len() as u32;
if corner_radius > 0.0 { if corner_radius > 0.0 {
let mut vidx_ctr = vidx;
todo!("rounded corners are not implemented"); todo!("rounded corners are not implemented");
//TODO vtx-based rounded corners //TODO vtx-based rounded corners
//swapper.current_mut().indices.extend(); //swapper.current_mut().indices.extend();
//TODO: make this configurable or compute dynamically
let rounded_corner_verts = 4;
for i in 0..rounded_corner_verts {
let cratio = i as f32 / rounded_corner_verts as f32;
let angle = cratio * std::f32::consts::PI * 0.5;
let x = angle.sin();
let y = angle.cos();
//Top-right corner
swapper.current_mut().vertices.push(UiVertex {
position: *position + vec2(x, 1. - y) * corner_radius + *size - vec2(corner_radius, 0.),
color: *color,
uv: vec2(0.0, 0.0),
});
//Top-left corner
swapper.current_mut().vertices.push(UiVertex {
position: *position + vec2(x, y),
color: *color,
uv: vec2(0.0, 0.0),
});
//Bottom-left corner
swapper.current_mut().vertices.push(UiVertex {
position: *position + vec2(x, y),
color: *color,
uv: vec2(0.0, 0.0),
});
//Bottom-right corner
swapper.current_mut().vertices.push(UiVertex {
position: *position + vec2(x, y),
color: *color,
uv: vec2(0.0, 0.0),
});
}
} else { } else {
swapper.current_mut().indices.extend([vidx, vidx + 1, vidx + 2, vidx, vidx + 2, vidx + 3]); swapper.current_mut().indices.extend([vidx, vidx + 1, vidx + 2, vidx, vidx + 2, vidx + 3]);
swapper.current_mut().vertices.extend([ swapper.current_mut().vertices.extend([