diff --git a/src/user/lib/libgui/libgui_core.rs b/src/user/lib/libgui/libgui_core.rs index caa8857..89ecf17 100644 --- a/src/user/lib/libgui/libgui_core.rs +++ b/src/user/lib/libgui/libgui_core.rs @@ -14,6 +14,19 @@ use alloc::{ */ +#[derive(Copy, Clone)] +pub struct Pos { + pub x: usize, + pub y: usize, +} +impl Pos { + pub fn new(x: usize, y: usize) -> Pos { + Pos { x, y } + } +} + + + /// all interface elements must implement this trait in order to be /// rendered on the screen pub trait Element { @@ -186,23 +199,23 @@ pub fn overlap_check(oldchar: char, newchar: char) -> char { // function to return a charmap of the outline of an object -pub fn gen_outline(dimensions: (usize, usize)) -> Vec> { +pub fn gen_outline(dimensions: Pos) -> Vec> { let mut charmap = Vec::>::new(); let mut frstline = vec!['┌']; let mut midlines = vec!['│']; let mut lastline = vec!['└']; - frstline.append(&mut vec!['─'; dimensions.0 - 2]); - midlines.append(&mut vec![' '; dimensions.0 - 2]); - lastline.append(&mut vec!['─'; dimensions.0 - 2]); + frstline.append(&mut vec!['─'; dimensions.x - 2]); + midlines.append(&mut vec![' '; dimensions.x - 2]); + lastline.append(&mut vec!['─'; dimensions.x - 2]); frstline.append(&mut vec!['┐']); midlines.append(&mut vec!['│']); lastline.append(&mut vec!['┘']); charmap.push(frstline); - for _ in 0..dimensions.1 - 2 { + for _ in 0..dimensions.y - 2 { charmap.push(midlines.clone()); } charmap.push(lastline); @@ -222,7 +235,7 @@ pub fn test_elements() { //} containers.push(Container::new((5, 5), (15, 5), true)); - containers.push(Container::new((10, 3), (50, 15), true)); + containers.push(Container::new((10, 3), (50, 20), true)); let mut bar = IndicatorBar::new((10, 6), 12); let mut bar2 = IndicatorBar::new((10, 7), 12); @@ -237,9 +250,9 @@ pub fn test_elements() { use super::libgui_elements; let tbox = libgui_elements::TextBox::new( String::from("title"), - String::from("text"), - (10, 10), - (10, 8), + String::from("text boxes are working gg but how well will they work if they go over the end of the textbox, will it cause a crash, well ima have to keep testing to figure that out properly, this could take a while lmao, i hope it works"), + Pos::new(25, 10), + Pos::new(10, 9), true, ); diff --git a/src/user/lib/libgui/libgui_elements.rs b/src/user/lib/libgui/libgui_elements.rs index 65acf2f..8f9f9c6 100644 --- a/src/user/lib/libgui/libgui_elements.rs +++ b/src/user/lib/libgui/libgui_elements.rs @@ -1,14 +1,15 @@ -use super::libgui_core; +use super::libgui_core::{self, Pos}; use alloc::{ string::{String, ToString}, vec::Vec, }; +use crate::std::io::println; // TEXT BOX pub struct TextBox { - dimensions: (usize, usize), - position: (usize, usize), + dimensions: Pos, + position: Pos, content: String, title: String, outlined: bool, @@ -18,24 +19,54 @@ impl libgui_core::Element for TextBox { fn render(&self) -> (Vec>, (usize, usize)) { let mut charmap = Vec::>::new(); + let mut inner_dims = Pos::new(self.dimensions.x -2, self.dimensions.y -2); + if self.outlined { charmap = libgui_core::gen_outline(self.dimensions); } - let inner_dims = (self.dimensions.0 - 2, self.dimensions.1 - 2); let mut titlechars = self.title.chars().collect::>(); // render title - for (i, char) in titlechars.iter().enumerate() {} + for (i, char) in titlechars.iter().enumerate() { + if i < inner_dims.x { + charmap[0][i + 1] = *char; + } else { + break; + } + } let mut idx = 0; - while idx < self.content.len() { - () + // render text + + let mut pos = Pos::new(0,0); + + for chr in self.content.chars().collect::>() { + + if pos.x < inner_dims.x { + charmap[pos.y + 1][pos.x + 1] = chr; + pos.x += 1; + } else { + pos.y += 1; + pos.x = 1; + + if pos.y < inner_dims.y { + charmap[pos.y + 1][1] = chr; + } else { + charmap[inner_dims.y][inner_dims.x] = '.'; + charmap[inner_dims.y][inner_dims.x -1] = '.'; + charmap[inner_dims.y][inner_dims.x -2] = '.'; + break; + } + } } - return (charmap, self.position); + + + + return (charmap, (self.position.x, self.position.y)); } } @@ -43,8 +74,8 @@ impl TextBox { pub fn new( title: String, content: String, - dimensions: (usize, usize), - position: (usize, usize), + dimensions: Pos, + position: Pos, outlined: bool, ) -> TextBox { TextBox {