singleton: to_index() & to_grid()

This commit is contained in:
Michael Sippel 2021-08-16 00:08:33 +02:00
parent 603034a065
commit 5753fb5af8
Signed by: senvas
GPG key ID: F96CF119C34B64A6
2 changed files with 87 additions and 0 deletions

View file

@ -1,6 +1,8 @@
pub mod buffer; pub mod buffer;
pub mod map; pub mod map;
pub mod to_index;
//pub mod unwrap;
use { use {
std::{ std::{

View file

@ -0,0 +1,85 @@
use {
std::sync::Arc,
std::sync::RwLock,
crate::{
singleton::{SingletonView},
index::{IndexView},
grid::{GridView},
core::{
Observer, ObserverExt, ObserverBroadcast,
View, ViewPort, OuterViewPort
}
}
};
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
impl<Item: 'static> OuterViewPort<dyn SingletonView<Item = Item>> {
pub fn to_index(&self) -> OuterViewPort<dyn IndexView<(), Item = Item>> {
let port = ViewPort::new();
port.add_update_hook(Arc::new(self.0.clone()));
let map = Arc::new(RwLock::new(Singleton2Index {
src_view: None,
cast: port.inner().get_broadcast()
}));
self.add_observer(map.clone());
port.inner().set_view(Some(map));
port.into_outer()
}
pub fn to_grid(&self) -> OuterViewPort<dyn GridView<Item = Item>> {
self.to_index()
.map_key(
|_msg: &()| cgmath::Point2::new(0, 0),
|pt| if pt.x == 0 && pt.y == 0 { Some(()) } else { None }
)
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub struct Singleton2Index<SrcView>
where SrcView: SingletonView + ?Sized
{
src_view: Option<Arc<SrcView>>,
cast: Arc<RwLock<ObserverBroadcast<dyn IndexView<(), Item = SrcView::Item>>>>
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
impl<SrcView> View for Singleton2Index<SrcView>
where SrcView: SingletonView + ?Sized
{
type Msg = ();
}
impl<SrcView> IndexView<()> for Singleton2Index<SrcView>
where SrcView: SingletonView + ?Sized
{
type Item = SrcView::Item;
fn area(&self) -> Option<Vec<()>> {
Some(vec![()])
}
fn get(&self, _msg: &()) -> Option<Self::Item> {
Some(self.src_view.as_ref().unwrap().get())
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
impl<SrcView> Observer<SrcView> for Singleton2Index<SrcView>
where SrcView: SingletonView + ?Sized
{
fn reset(&mut self, view: Option<Arc<SrcView>>) {
self.src_view = view;
self.cast.notify(&());
}
fn notify(&mut self, msg: &()) {
self.cast.notify(msg);
}
}