From 89839755ee53f13f90130ed154473530c86fdd59 Mon Sep 17 00:00:00 2001 From: FantasyPvP Date: Fri, 28 Apr 2023 00:34:30 +0100 Subject: [PATCH] e e --- src/user/lib/libgui/libgui_core.rs | 184 ++++++++++++------------- src/user/lib/libgui/libgui_elements.rs | 51 +++++-- 2 files changed, 122 insertions(+), 113 deletions(-) diff --git a/src/user/lib/libgui/libgui_core.rs b/src/user/lib/libgui/libgui_core.rs index 788142a..caa8857 100644 --- a/src/user/lib/libgui/libgui_core.rs +++ b/src/user/lib/libgui/libgui_core.rs @@ -1,7 +1,12 @@ +use crate::kernel::render::{BUFFER_HEIGHT, BUFFER_WIDTH, RENDERER}; use crate::std::io::Frame; -use alloc::{vec::Vec, vec, boxed::Box, string::ToString}; -use crate::kernel::render::{BUFFER_WIDTH, BUFFER_HEIGHT, RENDERER}; -use crate::{println, print}; +use crate::{print, println}; +use alloc::{ + boxed::Box, + string::{String, ToString}, + vec, + vec::Vec, +}; /* - this library will provide useful structures for creating simple @@ -9,25 +14,23 @@ use crate::{println, print}; */ - - /// all interface elements must implement this trait in order to be /// rendered on the screen -trait Element { // default behaviour for all elements - - fn render(&self) -> (Vec>, (usize, usize)) { // recursive method for rendering the - // specified frame to the screen +pub trait Element { + // default behaviour for all elements + + fn render(&self) -> (Vec>, (usize, usize)) { + // recursive method for rendering the + // specified frame to the screen // insert rendering code for specific frame here // this should also render all children of the element (Vec::>::new(), (0, 0)) } - } - - -pub struct Container { // a simple container objects for grouping - // other containers together +pub struct Container { + // a simple container objects for grouping + // other containers together frame: Vec>, elements: Vec>, position: (usize, usize), // x,y @@ -37,9 +40,8 @@ pub struct Container { // a simple container objects for grouping } impl Container { - fn new(position: (usize, usize), dimensions: (usize, usize), outlined: bool) -> Container { - Self { + Self { frame: vec![vec![' '; dimensions.0 as usize]; dimensions.1 as usize], elements: Vec::new(), position, @@ -48,14 +50,13 @@ impl Container { } } fn place(&self, element: Vec>) { - return // unimplemented + return; // unimplemented } - } impl Element for Container { - - fn render(&self) -> (Vec>, (usize, usize)) { // returns all elements as a single frame + fn render(&self) -> (Vec>, (usize, usize)) { + // returns all elements as a single frame let mut charmap = Vec::>::new(); @@ -63,55 +64,48 @@ impl Element for Container { let mut midlines: Vec; let mut lastline: Vec; - if self.outlined { frstline = vec!['┌']; midlines = vec!['│']; lastline = vec!['└']; - frstline.append(&mut vec!['─'; self.dimensions.0 -2]); - midlines.append(&mut vec![' '; self.dimensions.0 -2]); - lastline.append(&mut vec!['─'; self.dimensions.0 -2]); - + frstline.append(&mut vec!['─'; self.dimensions.0 - 2]); + midlines.append(&mut vec![' '; self.dimensions.0 - 2]); + lastline.append(&mut vec!['─'; self.dimensions.0 - 2]); + frstline.append(&mut vec!['┐']); midlines.append(&mut vec!['│']); lastline.append(&mut vec!['┘']); - - + charmap.push(frstline); - for _ in 0..self.dimensions.1 -2 { + for _ in 0..self.dimensions.1 - 2 { charmap.push(midlines.clone()); } charmap.push(lastline); - } - + // render child elements - + for element in &self.elements { let r = (*element).render(); - - // rendering code for child elements goes here + // rendering code for child elements goes here // code to render the object at the position marked by offset within the container - for (i, row) in r.0.iter().enumerate() { - for (j, chr) in row.iter().enumerate() { // r.0 is the rendered element - // r.1.0 is the x offset - charmap[i + r.1.1][j + r.1.0] = *chr; // r.1.1 is the y offset - } + for (j, chr) in row.iter().enumerate() { + // r.0 is the rendered element + // r.1.0 is the x offset + charmap[i + r.1 .1][j + r.1 .0] = *chr; // r.1.1 is the y offset + } } } - + return (charmap, self.position); - } } - - pub struct IndicatorBar { length: usize, filled: usize, @@ -121,9 +115,15 @@ pub struct IndicatorBar { impl IndicatorBar { fn new(position: (usize, usize), length: usize) -> IndicatorBar { - IndicatorBar { position, length, abs: 0, filled: 0 } + IndicatorBar { + position, + length, + abs: 0, + filled: 0, + } } - fn set_value(&mut self, value: usize) { // takes a value from 1-100% + fn set_value(&mut self, value: usize) { + // takes a value from 1-100% // and turns it into a corresponding length filled self.filled = value } @@ -133,105 +133,83 @@ impl Element for IndicatorBar { fn render(&self) -> (Vec>, (usize, usize)) { let numlen = (self.abs.to_string().as_str()).len(); let relfilled = (self.filled as f64 / 100.0 * ((self.length - numlen) as f64)) as usize; - + let mut line = Vec::::new(); line.append(&mut (self.abs.to_string().chars().collect())); line.append(&mut vec!['▓'; relfilled]); - line.append(&mut vec!['░'; self.length-numlen-relfilled]); - + line.append(&mut vec!['░'; self.length - numlen - relfilled]); let mut rendered = Vec::new(); rendered.push(line); - println!("RENDERED: {:?}", rendered); - - return (rendered, (self.position)) + + return (rendered, (self.position)); } } - - - - - - // functions that deal with the rendering and interaction between objects being // rendered. pub fn render_frame(elements: Vec) { let mut buffer: Frame = [[' '; BUFFER_WIDTH]; BUFFER_HEIGHT]; - - for frame in elements.iter() { + for frame in elements.iter() { let f = frame.render(); - + for (i, row) in f.0.iter().enumerate() { for (j, chr) in row.iter().enumerate() { - let mut current = &buffer[i + f.1.1][j + f.1.0]; + let mut current = &buffer[i + f.1 .1][j + f.1 .0]; let newchar = overlap_check(*current, *chr); - buffer[i + f.1.1][j + f.1.0] = newchar; - + buffer[i + f.1 .1][j + f.1 .0] = newchar; + //print!("{}", buffer[i+frame.position.1][j+frame.position.0]); } } - } - + //println!("{:?}", buffer); - + RENDERER.lock().render_frame(buffer) } pub fn overlap_check(oldchar: char, newchar: char) -> char { match (oldchar, newchar) { - //┌│└ ┐┘─ - ('│', '─')|('┌', '┘')|('└', '┐') => '┼', + //┌│└ ┐┘─ + ('│', '─') | ('┌', '┘') | ('└', '┐') => '┼', ('┌', '└') => '├', ('┐', '┐') => '┤', - (_, _) => newchar + (_, _) => newchar, } } // function to return a charmap of the outline of an object -fn gen_outline(dimensions: (usize, usize)) -> Vec> { +pub fn gen_outline(dimensions: (usize, usize)) -> 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.0 - 2]); + midlines.append(&mut vec![' '; dimensions.0 - 2]); + lastline.append(&mut vec!['─'; dimensions.0 - 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.1 - 2 { charmap.push(midlines.clone()); } charmap.push(lastline); - return charmap + return charmap; } - - - - - - - - - - - // testing functions pub fn test_elements() { @@ -246,20 +224,30 @@ pub fn test_elements() { containers.push(Container::new((5, 5), (15, 5), true)); containers.push(Container::new((10, 3), (50, 15), true)); - let mut bar = IndicatorBar::new((10, 10), 12); - let mut bar2 = IndicatorBar::new((10, 11), 12); - - + let mut bar = IndicatorBar::new((10, 6), 12); + let mut bar2 = IndicatorBar::new((10, 7), 12); + bar.set_value(43); bar.abs = 101; bar2.set_value(14); - bar2.abs= 15; + bar2.abs = 15; containers[1].elements.push(Box::new(bar)); containers[1].elements.push(Box::new(bar2)); - - + + use super::libgui_elements; + let tbox = libgui_elements::TextBox::new( + String::from("title"), + String::from("text"), + (10, 10), + (10, 8), + true, + ); + + containers[1].elements.push(Box::new(tbox)); + render_frame(containers); - return + + return; } // function to generate a box in a random location on the screen. @@ -271,4 +259,4 @@ fn generate_box() -> Container { let xoffset = Random::int(5, 50); let yoffset = Random::int(5, 10); Container::new((width, height), (xoffset, yoffset), true) -} \ No newline at end of file +} diff --git a/src/user/lib/libgui/libgui_elements.rs b/src/user/lib/libgui/libgui_elements.rs index 3bc453e..65acf2f 100644 --- a/src/user/lib/libgui/libgui_elements.rs +++ b/src/user/lib/libgui/libgui_elements.rs @@ -1,4 +1,10 @@ use super::libgui_core; +use alloc::{ + string::{String, ToString}, + vec::Vec, +}; + +// TEXT BOX pub struct TextBox { dimensions: (usize, usize), @@ -8,30 +14,45 @@ pub struct TextBox { outlined: bool, } -impl Element for TextBox { +impl libgui_core::Element for TextBox { fn render(&self) -> (Vec>, (usize, usize)) { - let mut charmap = Vec::>::new(); if self.outlined { charmap = libgui_core::gen_outline(self.dimensions); } - let inner_dims = (self.dimensions.0 -2, self.dimensions.1 -2); - - let mut titlechars = title.chars().collect::>(); + 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.enumerate() { - + for (i, char) in titlechars.iter().enumerate() {} + + let mut idx = 0; + + while idx < self.content.len() { + () } - - let mut idx = 0 - - while idx < content.len() - - - return (charmap, self.position) + return (charmap, self.position); } -} \ No newline at end of file +} + +impl TextBox { + pub fn new( + title: String, + content: String, + dimensions: (usize, usize), + position: (usize, usize), + outlined: bool, + ) -> TextBox { + TextBox { + dimensions, + position, + content, + title, + outlined, + } + } +}