1
1
Fork 0
mirror of https://github.com/griffi-gh/kubi.git synced 2025-03-16 11:56:27 -05:00
kubi/kubi-ui/src/lib.rs

106 lines
2.4 KiB
Rust
Raw Normal View History

2023-11-21 07:08:22 -06:00
use std::collections::VecDeque;
use glam::Vec2;
pub mod element;
pub mod event;
pub mod draw;
pub mod backend;
pub mod measure;
pub mod state;
2023-11-23 16:13:17 -06:00
pub mod text;
2023-11-21 07:08:22 -06:00
2023-11-22 04:56:46 -06:00
use element::UiElement;
2023-11-21 07:08:22 -06:00
use state::StateRepo;
2023-11-22 04:56:46 -06:00
use event::UiEvent;
use draw::{UiDrawCommands, UiDrawPlan};
2023-11-23 16:13:17 -06:00
use text::TextRenderer;
pub struct ElementContext<'a> {
pub state: &'a mut StateRepo,
pub draw: &'a mut UiDrawCommands,
pub text: &'a mut TextRenderer,
}
2023-11-21 07:08:22 -06:00
pub struct KubiUi {
mouse_position: Vec2,
stateful_state: StateRepo,
2023-11-22 04:56:46 -06:00
event_queue: VecDeque<UiEvent>,
prev_draw_commands: UiDrawCommands,
draw_commands: UiDrawCommands,
draw_plan: UiDrawPlan,
draw_plan_modified: bool,
2023-11-23 16:13:17 -06:00
font_renderer: TextRenderer,
2023-11-21 07:08:22 -06:00
}
impl KubiUi {
pub fn new() -> Self {
KubiUi {
mouse_position: Vec2::ZERO,
stateful_state: StateRepo::default(),
event_queue: VecDeque::new(),
2023-11-22 04:56:46 -06:00
// root_elements: Vec::new(),
prev_draw_commands: UiDrawCommands::default(),
draw_commands: UiDrawCommands::default(),
draw_plan: UiDrawPlan::default(),
draw_plan_modified: false,
2023-11-23 16:13:17 -06:00
font_renderer: TextRenderer::new(),
2023-11-21 07:08:22 -06:00
}
}
2023-11-22 04:56:46 -06:00
2023-11-22 09:51:59 -06:00
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(&self.stateful_state, &layout);
element.process(&measure, &mut self.stateful_state, &layout, &mut self.draw_commands.commands);
}
2023-11-22 04:56:46 -06:00
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();
}
pub fn end(&mut self) {
if self.draw_commands.commands == self.prev_draw_commands.commands {
return
}
self.draw_plan = UiDrawPlan::build(&self.draw_commands);
self.draw_plan_modified = true;
}
pub fn draw_plan(&self) -> (bool, &UiDrawPlan) {
(self.draw_plan_modified, &self.draw_plan)
}
2023-11-21 07:08:22 -06:00
}
impl Default for KubiUi {
fn default() -> Self {
Self::new()
}
}
2023-11-23 03:53:44 -06:00
#[derive(Default, Debug, Clone, Copy)]
2023-11-21 07:08:22 -06:00
pub enum UiSize {
2023-11-21 09:14:26 -06:00
#[default]
2023-11-21 07:08:22 -06:00
Auto,
Percentage(f32),
Pixels(f32),
}
2023-11-21 09:14:26 -06:00
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
2023-11-21 07:08:22 -06:00
pub enum UiDirection {
#[default]
Vertical,
Horizontal,
}
pub struct LayoutInfo {
2023-11-21 09:14:26 -06:00
///Not availabe during measuring step
pub position: Vec2,
pub max_size: Vec2,
pub direction: UiDirection,
2023-11-21 07:08:22 -06:00
}