mirror of
https://github.com/griffi-gh/hUI.git
synced 2024-11-22 07:08:42 -06:00
another refactor :3
This commit is contained in:
parent
27080e80e7
commit
af892698b5
|
@ -1,5 +1,5 @@
|
|||
use std::num::NonZeroU16;
|
||||
use crate::Corners;
|
||||
use crate::rectangle::Corners;
|
||||
|
||||
fn point_count(corners: Corners<f32>) -> NonZeroU16 {
|
||||
//Increase for higher quality
|
||||
|
|
|
@ -4,7 +4,7 @@ use crate::{
|
|||
measure::Response,
|
||||
state::StateRepo,
|
||||
text::TextMeasure,
|
||||
LayoutInfo
|
||||
layout::LayoutInfo
|
||||
};
|
||||
|
||||
mod builtin;
|
||||
|
|
|
@ -3,7 +3,7 @@ use crate::{
|
|||
draw::UiDrawCommand,
|
||||
element::{MeasureContext, ProcessContext, UiElement},
|
||||
measure::Response,
|
||||
UiSize
|
||||
layout::UiSize
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
|
|
|
@ -3,7 +3,7 @@ use crate::{
|
|||
draw::UiDrawCommand,
|
||||
element::{MeasureContext, ProcessContext, UiElement},
|
||||
measure::Response,
|
||||
UiSize
|
||||
layout::UiSize
|
||||
};
|
||||
|
||||
pub struct Rect {
|
||||
|
|
|
@ -2,7 +2,7 @@ use glam::vec2;
|
|||
use crate::{
|
||||
element::{MeasureContext, ProcessContext, UiElement},
|
||||
measure::Response,
|
||||
UiDirection
|
||||
layout::UiDirection
|
||||
};
|
||||
|
||||
pub struct Spacer(pub f32);
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::{
|
|||
element::{MeasureContext, ProcessContext, UiElement},
|
||||
measure::Response,
|
||||
text::{FontHandle, BUILTIN_FONT},
|
||||
UiSize
|
||||
layout::UiSize
|
||||
};
|
||||
|
||||
pub enum TextSize {
|
||||
|
|
97
hui/src/instance.rs
Normal file
97
hui/src/instance.rs
Normal file
|
@ -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<UiEvent>,
|
||||
prev_draw_commands: UiDrawCommandList,
|
||||
draw_commands: UiDrawCommandList,
|
||||
draw_plan: UiDrawPlan,
|
||||
draw_plan_modified: bool,
|
||||
text_renderer: TextRenderer,
|
||||
events: VecDeque<UiEvent>,
|
||||
}
|
||||
|
||||
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<T: UiElement>(&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()
|
||||
}
|
||||
}
|
110
hui/src/lib.rs
110
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<T> {
|
||||
fn if_modified(&self) -> Option<&T>;
|
||||
}
|
||||
|
||||
pub struct UiInstance {
|
||||
//mouse_position: Vec2,
|
||||
stateful_state: StateRepo,
|
||||
//event_queue: VecDeque<UiEvent>,
|
||||
prev_draw_commands: UiDrawCommandList,
|
||||
draw_commands: UiDrawCommandList,
|
||||
draw_plan: UiDrawPlan,
|
||||
draw_plan_modified: bool,
|
||||
text_renderer: TextRenderer,
|
||||
events: VecDeque<UiEvent>,
|
||||
}
|
||||
|
||||
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<T: UiElement>(&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<Box<dyn UiElement>>);
|
||||
pub struct ElementList(Vec<Box<dyn element::UiElement>>);
|
||||
|
||||
#[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<Box<dyn UiElement>> {
|
||||
pub fn elements(f: impl FnOnce(&mut ElementList)) -> Vec<Box<dyn element::UiElement>> {
|
||||
let mut elements = ElementList(Vec::new());
|
||||
f(&mut elements);
|
||||
elements.0
|
||||
|
|
Loading…
Reference in a new issue