diff --git a/pulse-ui/src/layout.rs b/pulse-ui/src/layout.rs index 44716e7..f30f424 100644 --- a/pulse-ui/src/layout.rs +++ b/pulse-ui/src/layout.rs @@ -1,17 +1,4 @@ -#[derive(Debug, Clone, Copy)] -pub enum Size { - Fixed(u16), - Flex(u16), - Percent(u16), -} - -#[derive(Debug, Clone, Copy)] -pub struct Rect { - pub x: u16, - pub y: u16, - pub width: u16, - pub height: u16, -} +use crate::unit::{Rect, Size}; #[derive(Debug, Clone)] pub enum LayoutItem { @@ -91,37 +78,6 @@ impl LayoutItem { } } -impl Size { - pub fn get_fixed(&self, alloc: u16) -> u16 { - match self { - Self::Fixed(v) => *v, - Self::Percent(v) => alloc * (*v) / 100, - Self::Flex(_) => 0, - } - } - - pub fn get_flex(&self) -> u16 { - match self { - Self::Flex(f) => *f, - _ => 0, - } - } - - pub fn get(&self, alloc: u16, remaining: u16, total_weight: u16) -> u16 { - match self { - Self::Fixed(v) => *v, - Self::Percent(v) => alloc * (*v) / 100, - Self::Flex(v) => { - if total_weight == 0 { - 0 - } else { - remaining * v / total_weight - } - } - } - } -} - impl Rect { pub fn allocate(&self, layout: &LayoutItem) -> Allocation { let mut widget_alloc = Vec::new(); diff --git a/pulse-ui/src/lib.rs b/pulse-ui/src/lib.rs index dd64619..c06e713 100644 --- a/pulse-ui/src/lib.rs +++ b/pulse-ui/src/lib.rs @@ -1 +1,4 @@ pub mod layout; +pub mod render; +pub mod widget; +pub mod unit; diff --git a/pulse-ui/src/render.rs b/pulse-ui/src/render.rs new file mode 100644 index 0000000..168eb00 --- /dev/null +++ b/pulse-ui/src/render.rs @@ -0,0 +1,51 @@ +use std::fmt::Display; + +use crate::unit::{Point, Rect}; + +pub enum Instr { + DrawText(Point, String), + DrawOutline(Rect), +} + +pub struct RenderScope { + pub rect: Rect, + draw_instructions: Vec, +} + +impl RenderScope { + pub fn draw_text, T: Display>(&mut self, at: P, text: T) { + self.draw_instructions + .push(Instr::DrawText(at.into(), text.to_string())); + } + + pub fn draw_outline>(&mut self, rect: Rect) { + self.draw_instructions.push(Instr::DrawOutline(rect)); + } +} + +impl RenderScope { + pub fn new(rect: Rect) -> Self { + Self { + rect, + draw_instructions: Vec::new(), + } + } +} + +impl From<(u16, u16)> for Point { + fn from(value: (u16, u16)) -> Self { + Self { + x: value.0, + y: value.1, + } + } +} + +impl From for Point { + fn from(value: Rect) -> Self { + Self { + x: value.x, + y: value.y, + } + } +} diff --git a/pulse-ui/src/unit.rs b/pulse-ui/src/unit.rs new file mode 100644 index 0000000..83ff31d --- /dev/null +++ b/pulse-ui/src/unit.rs @@ -0,0 +1,51 @@ +#[derive(Debug, Clone, Copy)] +pub struct Point { + pub x: u16, + pub y: u16, +} + +#[derive(Debug, Clone, Copy)] +pub enum Size { + Fixed(u16), + Flex(u16), + Percent(u16), +} + +#[derive(Debug, Clone, Copy)] +pub struct Rect { + pub x: u16, + pub y: u16, + pub width: u16, + pub height: u16, +} + +impl Size { + pub fn get_fixed(&self, alloc: u16) -> u16 { + match self { + Self::Fixed(v) => *v, + Self::Percent(v) => alloc * (*v) / 100, + Self::Flex(_) => 0, + } + } + + pub fn get_flex(&self) -> u16 { + match self { + Self::Flex(f) => *f, + _ => 0, + } + } + + pub fn get(&self, alloc: u16, remaining: u16, total_weight: u16) -> u16 { + match self { + Self::Fixed(v) => *v, + Self::Percent(v) => alloc * (*v) / 100, + Self::Flex(v) => { + if total_weight == 0 { + 0 + } else { + remaining * v / total_weight + } + } + } + } +} diff --git a/pulse-ui/src/widget.rs b/pulse-ui/src/widget.rs new file mode 100644 index 0000000..081d857 --- /dev/null +++ b/pulse-ui/src/widget.rs @@ -0,0 +1,5 @@ +use crate::render::RenderScope; + +pub trait Widget { + fn render(&self, scope: &mut RenderScope); +}