lines, barebones drawing window

addition of lines means ipc slightly changed, though can be ignored. also, minor malvim fix
This commit is contained in:
stjet
2025-08-12 06:53:57 +00:00
parent ec5cba13c8
commit 2c4455f623
10 changed files with 346 additions and 13 deletions

View File

@@ -108,6 +108,7 @@ impl FramebufferWriter {
.copy_from_slice(&color[..]);
}
//straight horizontal line
fn _draw_line(&mut self, start_pos: usize, bytes: &[u8]) {
self.buffer[start_pos..(start_pos + bytes.len())]
.copy_from_slice(bytes);
@@ -244,6 +245,34 @@ impl FramebufferWriter {
}
}
//line
pub fn draw_line(&mut self, start: Point, end: Point, width: usize, color: RGBColor) {
//leftmost point
let lm;
let rm;
if start[0] < end[0] || (start[0] == end[0] && end[1] > start[1]) {
lm = start;
rm = end;
} else {
lm = end;
rm = start;
};
let dx = (rm[0] - lm[0]) as f64; //will not be negative
let dy = rm[1] as f64 - lm[1] as f64; //surely no point will be big enough to lose data here?
let use_x = dy.abs() < dx;
//slope
let m = if use_x { dy / dx } else { dx / dy };
for ci in 0..if use_x { dx as usize } else { dy.abs() as usize } {
let i = if !use_x && dy < 0.0 { -(ci as f64) } else { ci as f64 };
let ix = if use_x { i } else { (m * i).round() };
let iy = if use_x { (m * i).round() } else { i };
for j in 0..width {
self.draw_pixel([(lm[0] as f64 + ix) as usize + j, (lm[1] as f64 + iy) as usize], color);
}
}
}
//bmps
//reverse is workaround for when my bmp lib returns rgba instead of bgra

View File

@@ -648,6 +648,13 @@ impl WindowManager {
[top_left[0], top_left[1] + if is_window { WINDOW_TOP_HEIGHT } else { 0 }]
}
fn prevent_overflow_dimensions(dimensions: Dimensions, window_dimensions: Dimensions, top_left: Point) -> Dimensions {
[
min(dimensions[0], window_dimensions[0] - top_left[0]),
min(dimensions[1], window_dimensions[1] - top_left[1]),
]
}
//another issue with a huge vector of draw instructions; it takes up heap memory
pub fn draw(&mut self, maybe_redraw_ids: Option<Vec<usize>>, use_saved_buffer: bool) {
let theme_info = get_theme_info(&self.theme).unwrap();
@@ -694,6 +701,7 @@ impl WindowManager {
DrawInstructions::Text(top_left, fonts, text, color, bg_color, horiz_spacing, mono_width) => DrawInstructions::Text(WindowManager::get_true_top_left(top_left, is_window), fonts.clone(), text.clone(), *color, *bg_color, *horiz_spacing, *mono_width),
DrawInstructions::Bmp(top_left, path, reverse) => DrawInstructions::Bmp(WindowManager::get_true_top_left(top_left, is_window), path.to_string(), *reverse),
DrawInstructions::Gradient(top_left, dimensions, start_color, end_color, steps) => DrawInstructions::Gradient(WindowManager::get_true_top_left(top_left, is_window), *dimensions, *start_color, *end_color, *steps),
DrawInstructions::Line(start, end, width, color) => DrawInstructions::Line(WindowManager::get_true_top_left(start, is_window), WindowManager::get_true_top_left(end, is_window), *width, *color),
}
}).collect();
//draw window background
@@ -729,10 +737,7 @@ impl WindowManager {
match instruction {
DrawInstructions::Rect(top_left, dimensions, color) => {
//try and prevent overflows out of the window
let true_dimensions = [
min(dimensions[0], window_dimensions[0] - top_left[0]),
min(dimensions[1], window_dimensions[1] - top_left[1]),
];
let true_dimensions = WindowManager::prevent_overflow_dimensions(dimensions, window_dimensions, top_left);
window_writer.draw_rect(top_left, true_dimensions, color);
},
DrawInstructions::Circle(centre, radius, color) => {
@@ -746,7 +751,11 @@ impl WindowManager {
window_writer.draw_bmp(top_left, path, reverse);
},
DrawInstructions::Gradient(top_left, dimensions, start_color, end_color, steps) => {
window_writer.draw_gradient(top_left, dimensions, start_color, end_color, steps);
let true_dimensions = WindowManager::prevent_overflow_dimensions(dimensions, window_dimensions, top_left);
window_writer.draw_gradient(top_left, true_dimensions, start_color, end_color, steps);
},
DrawInstructions::Line(start, end, width, color) => {
window_writer.draw_line(start, end, width, color);
},
}
}