diff --git a/hui/src/draw/corner_radius.rs b/hui/src/draw/corner_radius.rs index bf2b02c..9ab90e2 100644 --- a/hui/src/draw/corner_radius.rs +++ b/hui/src/draw/corner_radius.rs @@ -1,5 +1,5 @@ use std::num::NonZeroU16; -use crate::Corners; +use crate::rectangle::Corners; fn point_count(corners: Corners) -> NonZeroU16 { //Increase for higher quality diff --git a/hui/src/element.rs b/hui/src/element.rs index b1acc3c..03eb2ae 100644 --- a/hui/src/element.rs +++ b/hui/src/element.rs @@ -4,7 +4,7 @@ use crate::{ measure::Response, state::StateRepo, text::TextMeasure, - LayoutInfo + layout::LayoutInfo }; mod builtin; diff --git a/hui/src/element/builtin/progress_bar.rs b/hui/src/element/builtin/progress_bar.rs index 550b4cb..c9fa675 100644 --- a/hui/src/element/builtin/progress_bar.rs +++ b/hui/src/element/builtin/progress_bar.rs @@ -3,7 +3,7 @@ use crate::{ draw::UiDrawCommand, element::{MeasureContext, ProcessContext, UiElement}, measure::Response, - UiSize + layout::UiSize }; #[derive(Debug, Clone, Copy)] diff --git a/hui/src/element/builtin/rect.rs b/hui/src/element/builtin/rect.rs index bae669c..1d65b2c 100644 --- a/hui/src/element/builtin/rect.rs +++ b/hui/src/element/builtin/rect.rs @@ -3,7 +3,7 @@ use crate::{ draw::UiDrawCommand, element::{MeasureContext, ProcessContext, UiElement}, measure::Response, - UiSize + layout::UiSize }; pub struct Rect { diff --git a/hui/src/element/builtin/spacer.rs b/hui/src/element/builtin/spacer.rs index 1c81537..98b9e05 100644 --- a/hui/src/element/builtin/spacer.rs +++ b/hui/src/element/builtin/spacer.rs @@ -2,7 +2,7 @@ use glam::vec2; use crate::{ element::{MeasureContext, ProcessContext, UiElement}, measure::Response, - UiDirection + layout::UiDirection }; pub struct Spacer(pub f32); diff --git a/hui/src/element/builtin/text.rs b/hui/src/element/builtin/text.rs index 0993567..6bef31c 100644 --- a/hui/src/element/builtin/text.rs +++ b/hui/src/element/builtin/text.rs @@ -5,7 +5,7 @@ use crate::{ element::{MeasureContext, ProcessContext, UiElement}, measure::Response, text::{FontHandle, BUILTIN_FONT}, - UiSize + layout::UiSize }; pub enum TextSize { diff --git a/hui/src/instance.rs b/hui/src/instance.rs new file mode 100644 index 0000000..b072bb5 --- /dev/null +++ b/hui/src/instance.rs @@ -0,0 +1,97 @@ +use std::collections::VecDeque; +use glam::Vec2; +use crate:: { + layout::{UiDirection, LayoutInfo}, + element::{MeasureContext, ProcessContext, UiElement}, + event::UiEvent, + state::StateRepo, + draw::{UiDrawCommandList, UiDrawPlan}, + text::{TextRenderer, FontTextureInfo, FontHandle}, +}; + +pub struct UiInstance { + //mouse_position: Vec2, + stateful_state: StateRepo, + //event_queue: VecDeque, + prev_draw_commands: UiDrawCommandList, + draw_commands: UiDrawCommandList, + draw_plan: UiDrawPlan, + draw_plan_modified: bool, + text_renderer: TextRenderer, + events: VecDeque, +} + +impl UiInstance { + pub fn new() -> Self { + UiInstance { + //mouse_position: Vec2::ZERO, + stateful_state: StateRepo::default(), + //event_queue: VecDeque::new(), + // root_elements: Vec::new(), + prev_draw_commands: UiDrawCommandList::default(), + draw_commands: UiDrawCommandList::default(), + draw_plan: UiDrawPlan::default(), + draw_plan_modified: false, + // ftm: FontTextureManager::default(), + text_renderer: TextRenderer::new(), + events: VecDeque::new(), + } + } + + pub fn add_font_from_bytes(&mut self, font: &[u8]) -> FontHandle { + self.text_renderer.add_font_from_bytes(font) + } + + pub fn add(&mut self, element: T, max_size: Vec2) { + let layout = LayoutInfo { + position: Vec2::ZERO, + max_size, + direction: UiDirection::Vertical, + }; + let measure = element.measure(MeasureContext { + state: &self.stateful_state, + layout: &layout, + text_measure: self.text_renderer.to_measure(), + }); + element.process(ProcessContext { + measure: &measure, + state: &mut self.stateful_state, + layout: &layout, + draw: &mut self.draw_commands, + text_measure: self.text_renderer.to_measure(), + }); + } + + pub fn begin(&mut self) { + std::mem::swap(&mut self.prev_draw_commands, &mut self.draw_commands); + self.draw_plan_modified = false; + self.draw_commands.commands.clear(); + self.text_renderer.reset_frame(); + } + + pub fn end(&mut self) { + if self.draw_commands.commands == self.prev_draw_commands.commands { + return + } + self.draw_plan = UiDrawPlan::build(&self.draw_commands, &mut self.text_renderer); + self.draw_plan_modified = true; + } + + pub fn draw_plan(&self) -> (bool, &UiDrawPlan) { + (self.draw_plan_modified, &self.draw_plan) + } + + pub fn font_texture(&self) -> FontTextureInfo { + self.text_renderer.font_texture() + } + + pub fn push_event(&mut self, event: UiEvent) { + self.events.push_back(event); + } +} + +impl Default for UiInstance { + fn default() -> Self { + Self::new() + } +} diff --git a/hui/src/lib.rs b/hui/src/lib.rs index 22f2961..7ec317e 100644 --- a/hui/src/lib.rs +++ b/hui/src/lib.rs @@ -8,8 +8,7 @@ #![forbid(unsafe_code)] #![forbid(unsafe_op_in_unsafe_fn)] -use std::collections::VecDeque; - +mod instance; pub mod layout; pub mod rectangle; pub mod element; @@ -20,126 +19,27 @@ pub mod measure; pub mod state; pub mod text; -use layout::{UiDirection, UiSize, LayoutInfo}; -use rectangle::{Corners, Sides}; -use element::{MeasureContext, ProcessContext, UiElement}; -use event::UiEvent; -use state::StateRepo; -use draw::{UiDrawCommandList, UiDrawPlan}; -use text::{TextRenderer, FontTextureInfo, FontHandle}; -use glam::Vec2; +pub use instance::UiInstance; -// pub struct ElementContext<'a> { -// pub state: &'a mut StateRepo, -// pub draw: &'a mut UiDrawCommands, -// pub text: &'a mut TextRenderer, -// } pub trait IfModified { fn if_modified(&self) -> Option<&T>; } -pub struct UiInstance { - //mouse_position: Vec2, - stateful_state: StateRepo, - //event_queue: VecDeque, - prev_draw_commands: UiDrawCommandList, - draw_commands: UiDrawCommandList, - draw_plan: UiDrawPlan, - draw_plan_modified: bool, - text_renderer: TextRenderer, - events: VecDeque, -} - -impl UiInstance { - pub fn new() -> Self { - UiInstance { - //mouse_position: Vec2::ZERO, - stateful_state: StateRepo::default(), - //event_queue: VecDeque::new(), - // root_elements: Vec::new(), - prev_draw_commands: UiDrawCommandList::default(), - draw_commands: UiDrawCommandList::default(), - draw_plan: UiDrawPlan::default(), - draw_plan_modified: false, - // ftm: FontTextureManager::default(), - text_renderer: TextRenderer::new(), - events: VecDeque::new(), - } - } - - pub fn add_font_from_bytes(&mut self, font: &[u8]) -> FontHandle { - self.text_renderer.add_font_from_bytes(font) - } - - pub fn add(&mut self, element: T, max_size: Vec2) { - let layout = LayoutInfo { - position: Vec2::ZERO, - max_size, - direction: UiDirection::Vertical, - }; - let measure = element.measure(MeasureContext { - state: &self.stateful_state, - layout: &layout, - text_measure: self.text_renderer.to_measure(), - }); - element.process(ProcessContext { - measure: &measure, - state: &mut self.stateful_state, - layout: &layout, - draw: &mut self.draw_commands, - text_measure: self.text_renderer.to_measure(), - }); - } - - pub fn begin(&mut self) { - std::mem::swap(&mut self.prev_draw_commands, &mut self.draw_commands); - self.draw_plan_modified = false; - self.draw_commands.commands.clear(); - self.text_renderer.reset_frame(); - } - - pub fn end(&mut self) { - if self.draw_commands.commands == self.prev_draw_commands.commands { - return - } - self.draw_plan = UiDrawPlan::build(&self.draw_commands, &mut self.text_renderer); - self.draw_plan_modified = true; - } - - pub fn draw_plan(&self) -> (bool, &UiDrawPlan) { - (self.draw_plan_modified, &self.draw_plan) - } - - pub fn font_texture(&self) -> FontTextureInfo { - self.text_renderer.font_texture() - } - - pub fn push_event(&mut self, event: UiEvent) { - self.events.push_back(event); - } -} - -impl Default for UiInstance { - fn default() -> Self { - Self::new() - } -} - #[allow(deprecated)] #[deprecated(since = "0.1.0-alpha.3", note = "will be removed in the next release")] -pub struct ElementList(Vec>); +pub struct ElementList(Vec>); #[allow(deprecated)] #[deprecated(since = "0.1.0-alpha.3", note = "will be removed in the next release")] impl ElementList { - pub fn add(&mut self, element: impl UiElement + 'static) { + pub fn add(&mut self, element: impl element::UiElement + 'static) { self.0.push(Box::new(element)); } } #[allow(deprecated)] #[deprecated(since = "0.1.0-alpha.3", note = "will be removed in the next release")] -pub fn elements(f: impl FnOnce(&mut ElementList)) -> Vec> { +pub fn elements(f: impl FnOnce(&mut ElementList)) -> Vec> { let mut elements = ElementList(Vec::new()); f(&mut elements); elements.0