diff --git a/showbits-common/src/buffer.rs b/showbits-common/src/buffer.rs index c4ae863..72944f2 100644 --- a/showbits-common/src/buffer.rs +++ b/showbits-common/src/buffer.rs @@ -47,10 +47,4 @@ impl Buffer { let pixel = self.data.get_mut(index)?; Some(pixel) } - - pub fn set(&mut self, pos: Vec2, color: C) { - if let Some(pixel) = self.at_mut(pos) { - *pixel = color; - } - } } diff --git a/showbits-common/src/lib.rs b/showbits-common/src/lib.rs index 91a1efd..605350f 100644 --- a/showbits-common/src/lib.rs +++ b/showbits-common/src/lib.rs @@ -1,5 +1,6 @@ -pub use crate::{buffer::Buffer, rect::Rect, vec2::Vec2}; +pub use crate::{buffer::Buffer, rect::Rect, vec2::Vec2, view::View}; mod buffer; mod rect; mod vec2; +mod view; diff --git a/showbits-common/src/view.rs b/showbits-common/src/view.rs new file mode 100644 index 0000000..7836918 --- /dev/null +++ b/showbits-common/src/view.rs @@ -0,0 +1,52 @@ +use crate::{Buffer, Rect, Vec2}; + +// TODO Add Orientation (from inkfo) + +pub struct View<'a, C> { + area: Rect, + buffer: &'a mut Buffer, +} + +impl<'a, C> View<'a, C> { + pub fn new(buffer: &'a mut Buffer) -> Self { + Self { + area: Rect::from_nw(Vec2::ZERO, buffer.size()), + buffer, + } + } + + pub fn dup(&mut self) -> View<'_, C> { + View { + area: self.area, + buffer: self.buffer, + } + } + + pub fn with_area(mut self, area: Rect) -> Self { + self.area = area; + self + } + + pub fn translated(self, delta: Vec2) -> Self { + let area = self.area + delta; + self.with_area(area) + } + + pub fn size(&self) -> Vec2 { + self.area.size() + } + + fn pos_to_buffer_pos(&self, pos: Vec2) -> Vec2 { + pos + self.area.corner_nw() + } + + pub fn at(&self, pos: Vec2) -> Option<&C> { + self.buffer.at(self.pos_to_buffer_pos(pos)) + } + + pub fn set(&mut self, pos: Vec2, color: C) { + if let Some(pixel) = self.buffer.at_mut(self.pos_to_buffer_pos(pos)) { + *pixel = color; + } + } +}