From 2896799faef838d005158109b12a088126619bd0 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Sat, 12 Jun 2021 03:05:05 +0200 Subject: [PATCH] grid offset: use map_key() --- nested/src/grid/mod.rs | 1 - nested/src/grid/offset.rs | 75 ++++----------------------------------- 2 files changed, 7 insertions(+), 69 deletions(-) diff --git a/nested/src/grid/mod.rs b/nested/src/grid/mod.rs index 38f9309..e2692a4 100644 --- a/nested/src/grid/mod.rs +++ b/nested/src/grid/mod.rs @@ -9,7 +9,6 @@ use { }; pub mod offset; -pub use offset::GridOffset; //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> diff --git a/nested/src/grid/offset.rs b/nested/src/grid/offset.rs index 77d9ab1..170a9db 100644 --- a/nested/src/grid/offset.rs +++ b/nested/src/grid/offset.rs @@ -4,81 +4,20 @@ use { std::sync::RwLock, crate::{ core::{ - View, - Observer, - ObserverExt, - ObserverBroadcast, - InnerViewPort + OuterViewPort }, - index::{IndexView}, grid::{GridView} } }; -pub struct GridOffset { - src: Option>, - offset: Vector2, - cast: Arc>>> -} -impl GridOffset { - pub fn new(port: InnerViewPort>) -> Arc> { - let offset_view = - Arc::new(RwLock::new( - GridOffset:: { - 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) { - 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 View for GridOffset { - type Msg = Point2; -} - -impl IndexView> for GridOffset { - type Item = V::Item; - - fn get(&self, pos: &Point2) -> Option { - self.src.as_ref()?.get(&(pos - self.offset)) - } - - fn area(&self) -> Option>> { - Some( - self.src.as_ref()? - .area()?.into_iter() - .map(|pos| pos + self.offset) - .collect() +impl OuterViewPort> +where Item: 'static { + pub fn offset(&self, offset: Vector2) -> OuterViewPort> { + self.map_key( + move |pt| pt + offset, + move |pt| Some(pt - offset) ) } } -impl Observer for GridOffset { - fn reset(&mut self, view: Option>) { - 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) { - self.cast.notify(&(msg + self.offset)); - } -} -