91 lines
2.9 KiB
Rust
91 lines
2.9 KiB
Rust
use servicepoint::{Bitmap, CharGrid, CharGridMutExt, Grid, GridMut, TILE_SIZE, WindowMut};
|
|
|
|
pub type BorderPattern = [bool; TILE_SIZE];
|
|
|
|
pub const OUTER_BORDER: BorderPattern = [false, false, false, true, true, false, true, false];
|
|
pub const INNER_BORDER: BorderPattern = [false, false, false, true, true, false, false, false];
|
|
|
|
pub fn draw_border_panel<'c, 'b>(
|
|
chars: &'c mut WindowMut<char, CharGrid>,
|
|
bitmap: &'b mut WindowMut<bool, Bitmap>,
|
|
label: &str,
|
|
border_pattern: BorderPattern,
|
|
) -> (WindowMut<'c, char, CharGrid>, WindowMut<'b, bool, Bitmap>) {
|
|
let tile_width = chars.width();
|
|
let tile_height = chars.height();
|
|
let pixel_width = bitmap.width();
|
|
let pixel_height = bitmap.height();
|
|
|
|
chars
|
|
.window_mut(2..tile_width - 2, 0..1)
|
|
.unwrap()
|
|
.set_row_str(0, label)
|
|
.unwrap();
|
|
|
|
// border top+bottom
|
|
for tile_x in 1..tile_width - 1 {
|
|
for inner_x in 0..TILE_SIZE {
|
|
let x = tile_x * TILE_SIZE + inner_x;
|
|
let draw_top = tile_x <= 1 || tile_x >= label.chars().count() + 2;
|
|
|
|
for (inner_y, val) in border_pattern.iter().enumerate() {
|
|
if draw_top {
|
|
bitmap.set(x, inner_y, *val);
|
|
}
|
|
bitmap.set(x, pixel_height - inner_y - 1, *val);
|
|
}
|
|
}
|
|
}
|
|
|
|
// border left + right
|
|
for tile_y in 1..tile_height - 1 {
|
|
for inner_y in 0..TILE_SIZE {
|
|
let y = tile_y * TILE_SIZE + inner_y;
|
|
for (inner_x, val) in border_pattern.iter().enumerate() {
|
|
bitmap.set(inner_x, y, *val);
|
|
bitmap.set(pixel_width - inner_x - 1, y, *val);
|
|
}
|
|
}
|
|
}
|
|
|
|
// edges
|
|
for (pat_index, pat_val) in border_pattern.iter().enumerate() {
|
|
if !pat_val {
|
|
continue;
|
|
}
|
|
|
|
for extend in 0..=TILE_SIZE - pat_index {
|
|
bitmap.set(TILE_SIZE - extend, pat_index, true);
|
|
bitmap.set(pixel_width - 1 - TILE_SIZE + extend, pat_index, true);
|
|
|
|
bitmap.set(pat_index, TILE_SIZE - extend, true);
|
|
bitmap.set(pat_index, pixel_height - 1 - TILE_SIZE + extend, true);
|
|
|
|
bitmap.set(TILE_SIZE - extend, pixel_height - pat_index - 1, true);
|
|
bitmap.set(
|
|
pixel_width - 1 - TILE_SIZE + extend,
|
|
pixel_height - pat_index - 1,
|
|
true,
|
|
);
|
|
|
|
bitmap.set(pixel_width - pat_index - 1, TILE_SIZE - extend, true);
|
|
bitmap.set(
|
|
pixel_width - pat_index - 1,
|
|
pixel_height - 1 - TILE_SIZE + extend,
|
|
true,
|
|
);
|
|
}
|
|
}
|
|
|
|
let chars = chars
|
|
.window_mut(1..tile_width - 1, 1..tile_height - 1)
|
|
.unwrap();
|
|
let bitmap = bitmap
|
|
.window_mut(
|
|
TILE_SIZE..pixel_width - TILE_SIZE,
|
|
TILE_SIZE..pixel_height - TILE_SIZE,
|
|
)
|
|
.unwrap();
|
|
|
|
(chars, bitmap)
|
|
}
|