grid offset: use map_key()

This commit is contained in:
Michael Sippel 2021-06-12 03:05:05 +02:00
parent 9125997c83
commit 2896799fae
Signed by: senvas
GPG key ID: F96CF119C34B64A6
2 changed files with 7 additions and 69 deletions

View file

@ -9,7 +9,6 @@ use {
};
pub mod offset;
pub use offset::GridOffset;
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>

View file

@ -4,81 +4,20 @@ use {
std::sync::RwLock,
crate::{
core::{
View,
Observer,
ObserverExt,
ObserverBroadcast,
InnerViewPort
OuterViewPort
},
index::{IndexView},
grid::{GridView}
}
};
pub struct GridOffset<V: GridView + ?Sized> {
src: Option<Arc<V>>,
offset: Vector2<i16>,
cast: Arc<RwLock<ObserverBroadcast<dyn GridView<Item = V::Item>>>>
}
impl<V: 'static + GridView + ?Sized> GridOffset<V> {
pub fn new(port: InnerViewPort<dyn GridView<Item = V::Item>>) -> Arc<RwLock<Self>> {
let offset_view =
Arc::new(RwLock::new(
GridOffset::<V> {
src: None,
offset: Vector2::new(0, 0),
cast: port.get_broadcast()
}
));
port.set_view(Some(offset_view.clone()));
offset_view
}
pub fn set_offset(&mut self, new_offset: Vector2<i16>) {
let old_area = self.area();
self.offset = new_offset;
let new_area = self.area();
if let Some(area) = old_area { self.cast.notify_each(area); }
if let Some(area) = new_area { self.cast.notify_each(area); }
}
}
impl<V: GridView + ?Sized> View for GridOffset<V> {
type Msg = Point2<i16>;
}
impl<V: GridView + ?Sized> IndexView<Point2<i16>> for GridOffset<V> {
type Item = V::Item;
fn get(&self, pos: &Point2<i16>) -> Option<Self::Item> {
self.src.as_ref()?.get(&(pos - self.offset))
}
fn area(&self) -> Option<Vec<Point2<i16>>> {
Some(
self.src.as_ref()?
.area()?.into_iter()
.map(|pos| pos + self.offset)
.collect()
impl<Item> OuterViewPort<dyn GridView<Item = Item>>
where Item: 'static {
pub fn offset(&self, offset: Vector2<i16>) -> OuterViewPort<dyn GridView<Item = Item>> {
self.map_key(
move |pt| pt + offset,
move |pt| Some(pt - offset)
)
}
}
impl<V: GridView + ?Sized> Observer<V> for GridOffset<V> {
fn reset(&mut self, view: Option<Arc<V>>) {
let old_area = self.area();
self.src = view;
let new_area = self.area();
if let Some(area) = old_area { self.cast.notify_each(area); }
if let Some(area) = new_area { self.cast.notify_each(area); }
}
fn notify(&mut self, msg: &Point2<i16>) {
self.cast.notify(&(msg + self.offset));
}
}