From c9400343dd781a1e0cf127a8e0e8a772d6182c1b Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sat, 2 Dec 2023 19:46:08 +0100 Subject: [PATCH] it works --- .../examples/{text.rs => mom_downloader.rs} | 33 ++++++++++++++----- kubi-ui/src/draw.rs | 5 ++- kubi-ui/src/lib.rs | 13 ++++++++ 3 files changed, 42 insertions(+), 9 deletions(-) rename kubi-ui-examples/examples/{text.rs => mom_downloader.rs} (66%) diff --git a/kubi-ui-examples/examples/text.rs b/kubi-ui-examples/examples/mom_downloader.rs similarity index 66% rename from kubi-ui-examples/examples/text.rs rename to kubi-ui-examples/examples/mom_downloader.rs index b17a779..009a2bf 100644 --- a/kubi-ui-examples/examples/text.rs +++ b/kubi-ui-examples/examples/mom_downloader.rs @@ -15,7 +15,7 @@ use kubi_ui::{ }, interaction::IntoInteractable, UiSize, - UiDirection, IfModified, + UiDirection, IfModified, elements, }; use kubi_ui_glium::GliumUiRenderer; @@ -24,12 +24,15 @@ fn main() { let event_loop = EventLoopBuilder::new().build().unwrap(); let (window, display) = SimpleWindowBuilder::new().build(&event_loop); + window.set_title("Mom downloader 2000"); let mut kui = KubiUi::new(); let mut backend = GliumUiRenderer::new(&display); let font_handle = kui.add_font_from_bytes(include_bytes!("../../assets/fonts/roboto/Roboto-Regular.ttf")); + let instant = Instant::now(); + event_loop.run(|event, window_target| { window_target.set_control_flow(ControlFlow::Poll); match event { @@ -49,13 +52,27 @@ fn main() { padding: Sides::all(5.), align: (Alignment::Begin, Alignment::Begin), size: (UiSize::Percentage(1.), UiSize::Percentage(1.)), - elements: vec![ - Box::new(Text { - text: "Hello_world".into(), - font: font_handle, - ..Default::default() - }), - ], + background: Some(vec4(0.2, 0.2, 0.5, 1.)), + elements: elements(|el| { + if instant.elapsed().as_secs_f32() < 5. { + el.add(Text { + text: "Downloading your mom...".into(), + font: font_handle, + ..Default::default() + }); + el.add(ProgressBar { + value: (instant.elapsed().as_secs_f32() / 60.).powf(0.5), + ..Default::default() + }); + } else { + el.add(Text { + text: "Error 413 (Request Entity Too Large)".into(), + font: font_handle, + color: vec4(1., 0., 0., 1.), + ..Default::default() + }); + } + }), ..Default::default() }, resolution); diff --git a/kubi-ui/src/draw.rs b/kubi-ui/src/draw.rs index e220b3d..610916b 100644 --- a/kubi-ui/src/draw.rs +++ b/kubi-ui/src/draw.rs @@ -160,12 +160,15 @@ impl UiDrawPlan { let mut layout = Layout::new(CoordinateSystem::PositiveYDown); layout.append( &[tr.internal_font(*font)], - &TextStyle::new(&text, *size as f32, 0) + &TextStyle::new(text, *size as f32, 0) ); let glyphs = layout.glyphs(); //let mut rpos_x = 0.; for layout_glyph in glyphs { + if !layout_glyph.char_data.rasterize() { + continue + } let vidx = swapper.current().vertices.len() as u32; let glyph = tr.glyph(*font, layout_glyph.parent, layout_glyph.key.px as u8); //rpos_x += glyph.metrics.advance_width;//glyph.metrics.advance_width; diff --git a/kubi-ui/src/lib.rs b/kubi-ui/src/lib.rs index f1d6739..a2b4aac 100644 --- a/kubi-ui/src/lib.rs +++ b/kubi-ui/src/lib.rs @@ -114,3 +114,16 @@ pub struct LayoutInfo { pub max_size: Vec2, pub direction: UiDirection, } + +pub struct ElementList(Vec>); + +impl ElementList { + pub fn add(&mut self, element: impl UiElement + 'static) { + self.0.push(Box::new(element)); + } +} +pub fn elements(f: impl FnOnce(&mut ElementList)) -> Vec> { + let mut elements = ElementList(Vec::new()); + f(&mut elements); + elements.0 +}