From 6eb7a3f690d1daabc16b5bdfcad22bbfcdc4a870 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Tue, 21 Nov 2023 01:03:04 +0100 Subject: [PATCH] wip new gui --- kubi/src/gui_v2.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++ kubi/src/lib.rs | 1 + 2 files changed, 93 insertions(+) create mode 100644 kubi/src/gui_v2.rs diff --git a/kubi/src/gui_v2.rs b/kubi/src/gui_v2.rs new file mode 100644 index 0000000..f1df83e --- /dev/null +++ b/kubi/src/gui_v2.rs @@ -0,0 +1,92 @@ +use glam::{Vec2, vec2, Vec4}; +use shipyard::Unique; + +pub enum UiSize { + Auto, + Percentage(f32), + Pixels(f32), +} + +struct LayoutInfo { + position: Vec2, + max_preferred_size: Vec2, +} + +struct Response { + size: Vec2 +} + +pub trait UiElement { + fn process(&self, layout: &LayoutInfo, draw: &mut Vec) -> Response; + fn measure(&self, layout: &LayoutInfo) -> Option { None } +} + +pub enum LayoutDirection { + Horizontal, + Vertical +} + +pub struct LayoutBox { + pub direction: LayoutDirection, + pub gap: f32, + pub elements: Vec>, +} + +struct ProgressBar { + size: (UiSize, UiSize), + value: f32, + color_foreground: Vec4, + color_background: Vec4, +} + +const BAR_HEIGHT: f32 = 20.0; + +impl UiElement for ProgressBar { + fn measure(&self, layout: &LayoutInfo) -> Option { + let width = match self.size.0 { + UiSize::Auto => layout.max_preferred_size.x, + UiSize::Percentage(p) => layout.max_preferred_size.x * p, + UiSize::Pixels(p) => p, + }; + let height = match self.size.1 { + UiSize::Auto => BAR_HEIGHT, + UiSize::Percentage(p) => layout.max_preferred_size.y * p, + UiSize::Pixels(p) => p, + }; + let size = Vec2::new(width, height); + Some(Response { size }) + } + + fn process(&self, layout: &LayoutInfo, draw: &mut Vec) -> Response { + let measure = self.measure(layout).unwrap(); + + draw.push(UiDrawCall::Rectangle { + position: layout.position, + size: measure.size, + color: self.color_background + }); + draw.push(UiDrawCall::Rectangle { + position: layout.position, + size: measure.size * vec2(self.value, 1.0), + color: self.color_foreground + }); + + measure + } +} + +enum UiDrawCall { + Rectangle { + ///Position in pixels + position: Vec2, + ///Size in pixels + size: Vec2, + ///Color (RGBA) + color: Vec4, + } +} + +#[derive(Unique)] +struct UiDrawCalls { + pub calls: Vec, +} diff --git a/kubi/src/lib.rs b/kubi/src/lib.rs index 889d76d..67cf8c1 100644 --- a/kubi/src/lib.rs +++ b/kubi/src/lib.rs @@ -30,6 +30,7 @@ pub(crate) mod cursor_lock; pub(crate) mod control_flow; pub(crate) mod state; pub(crate) mod gui; +pub(crate) mod gui_v2; pub(crate) mod networking; pub(crate) mod init; pub(crate) mod color;