grid offset: use map_key()
This commit is contained in:
parent
9125997c83
commit
2896799fae
2 changed files with 7 additions and 69 deletions
|
@ -9,7 +9,6 @@ use {
|
||||||
};
|
};
|
||||||
|
|
||||||
pub mod offset;
|
pub mod offset;
|
||||||
pub use offset::GridOffset;
|
|
||||||
|
|
||||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
||||||
|
|
||||||
|
|
|
@ -4,81 +4,20 @@ use {
|
||||||
std::sync::RwLock,
|
std::sync::RwLock,
|
||||||
crate::{
|
crate::{
|
||||||
core::{
|
core::{
|
||||||
View,
|
OuterViewPort
|
||||||
Observer,
|
|
||||||
ObserverExt,
|
|
||||||
ObserverBroadcast,
|
|
||||||
InnerViewPort
|
|
||||||
},
|
},
|
||||||
index::{IndexView},
|
|
||||||
grid::{GridView}
|
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> {
|
impl<Item> OuterViewPort<dyn GridView<Item = Item>>
|
||||||
pub fn new(port: InnerViewPort<dyn GridView<Item = V::Item>>) -> Arc<RwLock<Self>> {
|
where Item: 'static {
|
||||||
let offset_view =
|
pub fn offset(&self, offset: Vector2<i16>) -> OuterViewPort<dyn GridView<Item = Item>> {
|
||||||
Arc::new(RwLock::new(
|
self.map_key(
|
||||||
GridOffset::<V> {
|
move |pt| pt + offset,
|
||||||
src: None,
|
move |pt| Some(pt - offset)
|
||||||
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<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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue