From 5753fb5af8eb043d9b96390aa936bb265b551ff2 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Mon, 16 Aug 2021 00:08:33 +0200 Subject: [PATCH] singleton: to_index() & to_grid() --- nested/src/singleton/mod.rs | 2 + nested/src/singleton/to_index.rs | 85 ++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 nested/src/singleton/to_index.rs diff --git a/nested/src/singleton/mod.rs b/nested/src/singleton/mod.rs index 5b55b2f..2a5b7a7 100644 --- a/nested/src/singleton/mod.rs +++ b/nested/src/singleton/mod.rs @@ -1,6 +1,8 @@ pub mod buffer; pub mod map; +pub mod to_index; +//pub mod unwrap; use { std::{ diff --git a/nested/src/singleton/to_index.rs b/nested/src/singleton/to_index.rs new file mode 100644 index 0000000..28372af --- /dev/null +++ b/nested/src/singleton/to_index.rs @@ -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 OuterViewPort> { + pub fn to_index(&self) -> OuterViewPort> { + 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> { + 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 +where SrcView: SingletonView + ?Sized +{ + src_view: Option>, + cast: Arc>>> +} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +impl View for Singleton2Index +where SrcView: SingletonView + ?Sized +{ + type Msg = (); +} + +impl IndexView<()> for Singleton2Index +where SrcView: SingletonView + ?Sized +{ + type Item = SrcView::Item; + + fn area(&self) -> Option> { + Some(vec![()]) + } + fn get(&self, _msg: &()) -> Option { + Some(self.src_view.as_ref().unwrap().get()) + } +} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +impl Observer for Singleton2Index +where SrcView: SingletonView + ?Sized +{ + fn reset(&mut self, view: Option>) { + self.src_view = view; + self.cast.notify(&()); + } + + fn notify(&mut self, msg: &()) { + self.cast.notify(msg); + } +} +