use std::{alloc::alloc, mem::ManuallyDrop}; use clay_layout::{ Clay, Declaration, fit, fixed, grow, layout::{LayoutDirection, Padding}, text::TextConfig, }; use raylib::ffi::KeyboardKey; pub type LineType = String; mod line; pub struct Document { lines: Vec<(String, LineType)>, total_lines: usize, current_line: Option, current_char: usize, lines_to_delete: Vec, lines_to_add: Vec<(usize, LineType)>, last_type_time: f64, } impl Document { pub fn new() -> Self { Self { lines: vec![("0".to_owned(), "".to_string())], total_lines: 1, current_line: Some(0), current_char: 0, lines_to_delete: vec![], lines_to_add: vec![], last_type_time: -100., } } pub fn add_line(&mut self, content: impl Into) { self.add_line_at(self.lines.len(), content); } pub fn add_line_at(&mut self, idx: usize, content: impl Into) { self.lines .insert(idx, (self.total_lines.to_string(), content.into())); self.total_lines += 1; } pub fn render<'a>( &'a mut self, clay: &'a Clay, (left_held, left_pressed): (bool, bool), (righ_held, right_pressed): (bool, bool), (latest_char, latest_key): (Option, Option), time: f64, ) { // if !self.lines_to_delete.is_empty() { // self.lines_to_delete.sort(); // // for line in self.lines_to_delete.iter().rev() { // let contents = &self.lines.remove(*line).1; // self.lines[*line - 1].1 += contents; // } // // self.lines_to_delete.clear(); // // self.current_line = if let Some(i) = self.current_line { // if self.lines.len() == 0 { // self.add_line("".to_string()); // } // Some( // (TryInto::::try_into(i).unwrap() - 1) // .max(0) // .min(TryInto::::try_into(self.lines.len()).unwrap() - 1) // .try_into() // .unwrap(), // ) // } else { // None // }; // } // // if !self.lines_to_add.is_empty() { // // i am NOT cloning this shit // let lines_to_add: ManuallyDrop> = unsafe { // let len = self.lines_to_add.len(); // let cap = self.lines_to_add.capacity(); // let fuck = self.lines_to_add.as_mut_ptr(); // std::mem::ManuallyDrop::new(Vec::from_raw_parts(fuck, len, cap)) // }; // // for (idx, line) in lines_to_add.iter() { // self.add_line_at(*idx, line); // } // // self.lines_to_add.clear(); // self.current_line = if let Some(i) = self.current_line { // Some(i + 1) // } else { // None // }; // } // // if left_pressed && clay.pointer_over(clay.id("document")) { // self.current_line = None; // } // // let len = self.lines.len(); // let mut can_go_down = true; clay.with( &Declaration::new() .id(clay.id("document")) .layout() .width(grow!(0.)) .height(grow!(0.)) .direction(LayoutDirection::TopToBottom) .padding(Padding::all(8)) .child_gap(0) .end() .corner_radius() .all(8.) .end() .background_color((67, 76, 94).into()) .scroll(false, true), |clay| { // for (i, line) in self.lines.iter_mut().enumerate() { // let current_selected = // if left_pressed && clay.pointer_over(clay.id(line.0.as_str())) { // self.current_line = Some(i); // true // } else { // Some(i) == self.current_line // }; // // if current_selected { // if let Some(char) = latest_char { // line.1 += char.to_string().as_str(); // self.last_type_time = time; // } // // match latest_key { // Some(val) if val == KeyboardKey::KEY_BACKSPACE as u32 => { // if line.1.len() == 0 { // self.lines_to_delete.push(i); // } else { // // let _ = line.1.pop(); // line.1.remove(self.current_char); // } // } // Some(val) if val == KeyboardKey::KEY_ENTER as u32 => { // self.lines_to_add.push((i + 1, "".to_string())); // } // Some(val) if val == KeyboardKey::KEY_UP as u32 => { // self.current_line = Some( // (TryInto::::try_into(self.current_line.unwrap()).unwrap() // - 1) // .max(0) // .try_into() // .unwrap(), // ); // } // Some(val) if val == KeyboardKey::KEY_DOWN as u32 && can_go_down => { // self.current_line = Some( // (TryInto::::try_into(self.current_line.unwrap()).unwrap() // + 1) // .min(TryInto::::try_into(len).unwrap() - 1) // .try_into() // .unwrap(), // ); // can_go_down = false; // } // _ => {} // } // } // // clay.with( // &Declaration::new() // .id(clay.id(line.0.as_str())) // .layout() // .width(grow!(0.)) // .height(fit!(21.)) // .direction(LayoutDirection::LeftToRight) // .padding(Padding::new(4, 4, 0, 1)) // .child_alignment(clay_layout::layout::Alignment { // x: clay_layout::layout::LayoutAlignmentX::Left, // y: clay_layout::layout::LayoutAlignmentY::Center, // }) // // .child_gap(2) // // .end() // // .border() // // .color((67, 76, 94).into()) // // .bottom(2) // // .end() // // .corner_radius() // // .all(4.) // .end(), // |clay| { // clay.text( // line.1.as_str(), // TextConfig::new() // .color((229, 233, 240).into()) // .font_size(20) // .font_id(0) // .letter_spacing(2) // .end(), // ); // if current_selected // && can_go_down // && ((time - self.last_type_time) < 0.5 || time % 1.2 < 0.6) // { // clay.with( // &Declaration::new() // .id(clay.id("text_cursor")) // .layout() // .height(grow!(0.)) // .width(fixed!(8.)) // .end() // .background_color((229, 233, 240, 100).into()), // .corner_radius() // // .all(2.) // // .end(), // |_| {}, // ); // } // }, // ); // } }, ); } }