From 491bb435ccb9c5e3ff2a867349ee29932f4b1ca6 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sat, 2 Dec 2023 19:07:31 +0100 Subject: [PATCH] `Vec` -> `UiDrawCommands` --- kubi-ui/src/draw.rs | 6 ++++++ kubi-ui/src/element.rs | 4 ++-- kubi-ui/src/element/builtin/container.rs | 8 ++++---- kubi-ui/src/element/builtin/progress_bar.rs | 8 ++++---- kubi-ui/src/element/builtin/rect.rs | 6 +++--- kubi-ui/src/element/builtin/spacer.rs | 4 ++-- kubi-ui/src/element/builtin/text.rs | 6 +++--- kubi-ui/src/interaction.rs | 4 ++-- kubi-ui/src/lib.rs | 2 +- 9 files changed, 27 insertions(+), 21 deletions(-) diff --git a/kubi-ui/src/draw.rs b/kubi-ui/src/draw.rs index 9d6c778..e801a82 100644 --- a/kubi-ui/src/draw.rs +++ b/kubi-ui/src/draw.rs @@ -33,6 +33,12 @@ pub struct UiDrawCommands { pub commands: Vec, } +impl UiDrawCommands { + pub fn add(&mut self, command: UiDrawCommand) { + self.commands.push(command); + } +} + // impl UiDrawCommands { // pub fn compare(&self, other: &Self) -> bool { // // if self.commands.len() != other.commands.len() { return false } diff --git a/kubi-ui/src/element.rs b/kubi-ui/src/element.rs index 5ba7ea0..8e5edfa 100644 --- a/kubi-ui/src/element.rs +++ b/kubi-ui/src/element.rs @@ -1,7 +1,7 @@ use std::any::Any; use crate::{ LayoutInfo, - draw::UiDrawCommand, + draw::UiDrawCommands, measure::Response, state::StateRepo }; @@ -25,5 +25,5 @@ pub trait UiElement { fn is_stateless(&self) -> bool { self.state_id().is_none() } fn init_state(&self) -> Option> { None } fn measure(&self, state: &StateRepo, layout: &LayoutInfo) -> Response; - fn process(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec); + fn process(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut UiDrawCommands); } diff --git a/kubi-ui/src/element/builtin/container.rs b/kubi-ui/src/element/builtin/container.rs index f9b084c..df04fe8 100644 --- a/kubi-ui/src/element/builtin/container.rs +++ b/kubi-ui/src/element/builtin/container.rs @@ -3,7 +3,7 @@ use crate::{ UiDirection, UiSize, LayoutInfo, - draw::UiDrawCommand, + draw::{UiDrawCommand, UiDrawCommands}, measure::{Response, Hints}, state::StateRepo, element::UiElement @@ -59,6 +59,7 @@ pub struct Container { //pub reverse: bool, pub gap: f32, pub padding: Sides, + ///Primary/secondary axis pub align: (Alignment, Alignment), pub background: Option, pub borders: Sides>, @@ -76,7 +77,6 @@ impl Default for Container { //reverse: false, gap: 0., padding: Sides::all(0.), - ///Primary/secondary axis align: (Alignment::Begin, Alignment::Begin), background: Default::default(), borders: Default::default(), @@ -150,12 +150,12 @@ impl UiElement for Container { } } - fn process(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec) { + fn process(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut UiDrawCommands) { let mut position = layout.position; //background if let Some(color) = self.background { - draw.push(UiDrawCommand::Rectangle { + draw.add(UiDrawCommand::Rectangle { position, size: measure.size, color diff --git a/kubi-ui/src/element/builtin/progress_bar.rs b/kubi-ui/src/element/builtin/progress_bar.rs index 977df55..35283c1 100644 --- a/kubi-ui/src/element/builtin/progress_bar.rs +++ b/kubi-ui/src/element/builtin/progress_bar.rs @@ -1,7 +1,7 @@ use glam::{vec2, Vec4, vec4}; use crate::{ UiSize, LayoutInfo, - draw::UiDrawCommand, + draw::{UiDrawCommand, UiDrawCommands}, measure::Response, state::StateRepo, element::UiElement @@ -50,17 +50,17 @@ impl UiElement for ProgressBar { } } - fn process(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec) { + fn process(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut UiDrawCommands) { let value = self.value.clamp(0., 1.); if value < 1. { - draw.push(UiDrawCommand::Rectangle { + draw.add(UiDrawCommand::Rectangle { position: layout.position, size: measure.size, color: self.color_background }); } if value > 0. { - draw.push(UiDrawCommand::Rectangle { + draw.add(UiDrawCommand::Rectangle { position: layout.position, size: measure.size * vec2(value, 1.0), color: self.color_foreground diff --git a/kubi-ui/src/element/builtin/rect.rs b/kubi-ui/src/element/builtin/rect.rs index 692dd06..de082f8 100644 --- a/kubi-ui/src/element/builtin/rect.rs +++ b/kubi-ui/src/element/builtin/rect.rs @@ -5,7 +5,7 @@ use crate::{ element::UiElement, state::StateRepo, measure::Response, - draw::UiDrawCommand + draw::{UiDrawCommand, UiDrawCommands} }; pub struct Rect { @@ -42,9 +42,9 @@ impl UiElement for Rect { } } - fn process(&self, measure: &Response, _state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec) { + fn process(&self, measure: &Response, _state: &mut StateRepo, layout: &LayoutInfo, draw: &mut UiDrawCommands) { if let Some(color) = self.color { - draw.push(UiDrawCommand::Rectangle { + draw.add(UiDrawCommand::Rectangle { position: layout.position, size: measure.size, color, diff --git a/kubi-ui/src/element/builtin/spacer.rs b/kubi-ui/src/element/builtin/spacer.rs index fe1fb86..15e1a00 100644 --- a/kubi-ui/src/element/builtin/spacer.rs +++ b/kubi-ui/src/element/builtin/spacer.rs @@ -5,7 +5,7 @@ use crate::{ element::UiElement, state::StateRepo, measure::Response, - draw::UiDrawCommand + draw::{UiDrawCommand, UiDrawCommands} }; pub struct Spacer(f32); @@ -28,5 +28,5 @@ impl UiElement for Spacer { } } - fn process(&self, _measure: &Response, _state: &mut StateRepo, _layout: &LayoutInfo, _draw: &mut Vec) {} + fn process(&self, _measure: &Response, _state: &mut StateRepo, _layout: &LayoutInfo, _draw: &mut UiDrawCommands) {} } diff --git a/kubi-ui/src/element/builtin/text.rs b/kubi-ui/src/element/builtin/text.rs index ce29d11..2a88f99 100644 --- a/kubi-ui/src/element/builtin/text.rs +++ b/kubi-ui/src/element/builtin/text.rs @@ -6,7 +6,7 @@ use crate::{ element::UiElement, state::StateRepo, measure::Response, - draw::UiDrawCommand, text::FontHandle + draw::{UiDrawCommand, UiDrawCommands}, text::FontHandle }; pub struct Text { @@ -47,8 +47,8 @@ impl UiElement for Text { } } - fn process(&self, _measure: &Response, _state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec) { - draw.push(UiDrawCommand::Text { + fn process(&self, _measure: &Response, _state: &mut StateRepo, layout: &LayoutInfo, draw: &mut UiDrawCommands) { + draw.add(UiDrawCommand::Text { text: self.text.clone(), position: layout.position, size: 32, diff --git a/kubi-ui/src/interaction.rs b/kubi-ui/src/interaction.rs index 92ff63f..f172ade 100644 --- a/kubi-ui/src/interaction.rs +++ b/kubi-ui/src/interaction.rs @@ -1,4 +1,4 @@ -use crate::element::UiElement; +use crate::{element::UiElement, draw::UiDrawCommands}; pub struct Interactable { pub element: T, @@ -35,7 +35,7 @@ impl UiElement for Interactable { self.element.measure(state, layout) } - fn process(&self, measure: &crate::measure::Response, state: &mut crate::state::StateRepo, layout: &crate::LayoutInfo, draw: &mut Vec) { + fn process(&self, measure: &crate::measure::Response, state: &mut crate::state::StateRepo, layout: &crate::LayoutInfo, draw: &mut UiDrawCommands) { self.element.process(measure, state, layout, draw) } } diff --git a/kubi-ui/src/lib.rs b/kubi-ui/src/lib.rs index 8b58a23..f1d6739 100644 --- a/kubi-ui/src/lib.rs +++ b/kubi-ui/src/lib.rs @@ -60,7 +60,7 @@ impl KubiUi { direction: UiDirection::Vertical, }; let measure = element.measure(&self.stateful_state, &layout); - element.process(&measure, &mut self.stateful_state, &layout, &mut self.draw_commands.commands); + element.process(&measure, &mut self.stateful_state, &layout, &mut self.draw_commands); } pub fn begin(&mut self) {