From c50333805bcc17762ccba32f0cca28c07c1bd2b8 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Mon, 19 Feb 2024 04:37:28 +0100 Subject: [PATCH] wip --- hui-examples/examples/rounded.rs | 55 ++++++++++++++++++++++++++++++++ hui/src/draw.rs | 34 ++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 hui-examples/examples/rounded.rs diff --git a/hui-examples/examples/rounded.rs b/hui-examples/examples/rounded.rs new file mode 100644 index 0000000..449c2d8 --- /dev/null +++ b/hui-examples/examples/rounded.rs @@ -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(); +} diff --git a/hui/src/draw.rs b/hui/src/draw.rs index 67a12f4..10f472c 100644 --- a/hui/src/draw.rs +++ b/hui/src/draw.rs @@ -136,9 +136,43 @@ impl UiDrawPlan { let corner_radius = corner_radius.unwrap_or(0.0); let vidx = swapper.current().vertices.len() as u32; if corner_radius > 0.0 { + let mut vidx_ctr = vidx; todo!("rounded corners are not implemented"); //TODO vtx-based rounded corners //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 { swapper.current_mut().indices.extend([vidx, vidx + 1, vidx + 2, vidx, vidx + 2, vidx + 3]); swapper.current_mut().vertices.extend([