Add View
This commit is contained in:
parent
97a8099b7a
commit
b31c2de425
3 changed files with 54 additions and 7 deletions
|
|
@ -47,10 +47,4 @@ impl<C> Buffer<C> {
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
52
showbits-common/src/view.rs
Normal file
52
showbits-common/src/view.rs
Normal file
|
|
@ -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<C>,
|
||||
}
|
||||
|
||||
impl<'a, C> View<'a, C> {
|
||||
pub fn new(buffer: &'a mut Buffer<C>) -> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue