add helper methods for creating a window
This commit is contained in:
parent
260ae6a1a0
commit
e91ee6032d
3 changed files with 59 additions and 6 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use crate::{
|
||||
DataRef, DisplayBitVec, Grid, GridMut, Payload, ValueGrid, Window,
|
||||
PIXEL_HEIGHT, PIXEL_WIDTH,
|
||||
WindowMut, PIXEL_HEIGHT, PIXEL_WIDTH,
|
||||
};
|
||||
use ::bitvec::{order::Msb0, prelude::BitSlice, slice::IterMut};
|
||||
use inherent::inherent;
|
||||
|
|
@ -176,6 +176,32 @@ impl Bitmap {
|
|||
row: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a window into the bitmap.
|
||||
///
|
||||
/// Returns None in case the window does not fit.
|
||||
pub fn window(
|
||||
&self,
|
||||
x: usize,
|
||||
y: usize,
|
||||
width: usize,
|
||||
height: usize,
|
||||
) -> Option<Window<bool, Self>> {
|
||||
Window::new(self, x, y, width, height)
|
||||
}
|
||||
|
||||
/// Creates a mutable window into the bitmap.
|
||||
///
|
||||
/// Returns None in case the window does not fit.
|
||||
pub fn window_mut(
|
||||
&mut self,
|
||||
x: usize,
|
||||
y: usize,
|
||||
width: usize,
|
||||
height: usize,
|
||||
) -> Option<WindowMut<bool, Self>> {
|
||||
WindowMut::new(self, x, y, width, height)
|
||||
}
|
||||
}
|
||||
|
||||
#[inherent]
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{DataRef, Grid, GridMut, Window};
|
||||
use crate::{DataRef, Grid, GridMut, Window, WindowMut};
|
||||
use inherent::inherent;
|
||||
use std::fmt::Debug;
|
||||
use std::slice::{Iter, IterMut};
|
||||
|
|
@ -310,6 +310,32 @@ impl<T: Value> ValueGrid<T> {
|
|||
row: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a window into the grid.
|
||||
///
|
||||
/// Returns None in case the window does not fit.
|
||||
pub fn window(
|
||||
&self,
|
||||
x: usize,
|
||||
y: usize,
|
||||
width: usize,
|
||||
height: usize,
|
||||
) -> Option<Window<T, Self>> {
|
||||
Window::new(self, x, y, width, height)
|
||||
}
|
||||
|
||||
/// Creates a mutable window into the grid.
|
||||
///
|
||||
/// Returns None in case the window does not fit.
|
||||
pub fn window_mut(
|
||||
&mut self,
|
||||
x: usize,
|
||||
y: usize,
|
||||
width: usize,
|
||||
height: usize,
|
||||
) -> Option<WindowMut<T, Self>> {
|
||||
WindowMut::new(self, x, y, width, height)
|
||||
}
|
||||
}
|
||||
|
||||
/// Errors that can occur when loading a grid
|
||||
|
|
|
|||
|
|
@ -105,7 +105,7 @@ mod tests {
|
|||
let mut bitmap = Bitmap::new(8, 4).unwrap();
|
||||
|
||||
// non-byte-aligned views work
|
||||
let mut view = WindowMut::new(&mut bitmap, 3, 1, 4, 2).unwrap();
|
||||
let mut view = bitmap.window_mut(3, 1, 4, 2).unwrap();
|
||||
view.fill(true);
|
||||
|
||||
assert_eq!(bitmap.data_ref(), &[0, 30, 30, 0]);
|
||||
|
|
@ -113,7 +113,7 @@ mod tests {
|
|||
assert_eq!(bitmap.set_optional(99, 99, false), false);
|
||||
|
||||
// full size view works
|
||||
_ = Window::new(&mut bitmap, 0, 0, 8, 4).unwrap();
|
||||
bitmap.window(0, 0, 8, 4).unwrap();
|
||||
|
||||
// zero size view does not work
|
||||
assert!(Window::new(&mut bitmap, 1, 2, 3, 0).is_none());
|
||||
|
|
@ -130,12 +130,13 @@ mod tests {
|
|||
let mut grid = CharGrid::new(3, 4);
|
||||
grid.fill(' ');
|
||||
|
||||
let mut view = WindowMut::new(&mut grid, 1, 1, 1, 2).unwrap();
|
||||
let mut view = WindowMut::new(&mut grid, 1, 1, 1, 3).unwrap();
|
||||
view.fill('#');
|
||||
view.set(0,0, '!');
|
||||
|
||||
assert_eq!(
|
||||
grid.data_ref(),
|
||||
&[' ', ' ', ' ', ' ', '#', ' ', ' ', '#', ' ', ' ', ' ', ' ']
|
||||
&[' ', ' ', ' ', ' ', '!', ' ', ' ', '#', ' ', ' ', '#', ' ']
|
||||
);
|
||||
|
||||
// full size view works
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue