This commit is contained in:
Snorre Ettrup Altschul 2025-02-16 14:56:47 +01:00
parent 6b2aa9fe14
commit 3485c89905
4 changed files with 252 additions and 227 deletions

View file

@ -233,12 +233,12 @@ fn main() -> Result<(), String> {
(left_button, left_button && !previous_left_button),
(right_button, right_button && !previous_right_button),
(rl.get_char_pressed(), rl.get_key_pressed_number()),
rl.get_time()
rl.get_time(),
);
let mut d = rl.begin_drawing(&thread);
d.clear_background(Color::WHITE);
d.clear_background(clay2ray((46, 52, 64).into()));
for command in render_commands {
match command.config {
@ -255,7 +255,8 @@ fn main() -> Result<(), String> {
width: command.bounding_box.width,
height: command.bounding_box.height,
},
rectangle.corner_radii.top_left,
rectangle.corner_radii.top_left
/ command.bounding_box.width.min(command.bounding_box.height),
8,
clay2ray(rectangle.color),
);

View file

@ -1,224 +0,0 @@
use std::mem::ManuallyDrop;
use clay_layout::{
Clay, Declaration, fit, fixed, grow,
layout::{LayoutDirection, Padding},
text::TextConfig,
};
use raylib::ffi::KeyboardKey;
pub type LineType = String;
pub struct Document {
lines: Vec<(String, LineType)>,
total_lines: usize,
current_line: Option<usize>,
current_char: usize,
lines_to_delete: Vec<usize>,
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<LineType>) {
self.add_line_at(self.lines.len(), content);
}
pub fn add_line_at(&mut self, idx: usize, content: impl Into<LineType>) {
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<char>, Option<u32>),
time: f64,
) {
if !self.lines_to_delete.is_empty() {
self.lines_to_delete.sort();
for line in self.lines_to_delete.iter().rev() {
self.lines.remove(*line);
}
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::<i32>::try_into(i).unwrap() - 1)
.max(0)
.min(TryInto::<i32>::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<Vec<(usize, String)>> = 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()
.background_color((46, 52, 64).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();
}
}
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::<i32>::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::<i32>::try_into(self.current_line.unwrap()).unwrap()
+ 1)
.min(TryInto::<i32>::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(),
,
|_| {},
);
}
},
);
}
},
);
}
}

View file

@ -0,0 +1,19 @@
use std::rc::Rc;
use libopenbirch::node::NodeEnum;
pub struct Line {
segments: Vec<LineSegment>,
}
pub enum LineSegment {
Text(String),
// Link(String, Link),
Math(Rc<NodeEnum>),
}
impl LineSegment {
pub fn draw<'a>(&'a self, clay: &'a clay_layout::Clay) {
}
}

229
src/app/gui/document/mod.rs Normal file
View file

@ -0,0 +1,229 @@
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<usize>,
current_char: usize,
lines_to_delete: Vec<usize>,
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<LineType>) {
self.add_line_at(self.lines.len(), content);
}
pub fn add_line_at(&mut self, idx: usize, content: impl Into<LineType>) {
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<char>, Option<u32>),
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::<i32>::try_into(i).unwrap() - 1)
// .max(0)
// .min(TryInto::<i32>::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<Vec<(usize, String)>> = 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::<i32>::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::<i32>::try_into(self.current_line.unwrap()).unwrap()
// + 1)
// .min(TryInto::<i32>::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(),
// |_| {},
// );
// }
// },
// );
// }
},
);
}
}