SdfTerm: impl Observer to only update modified layers

This commit is contained in:
Michael Sippel 2021-09-10 12:53:08 +02:00
parent 83386d5bd9
commit 6247dcc9f5
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -75,7 +75,7 @@ struct SdfTerm {
pub src_view: Option<Arc<dyn TerminalView>>, pub src_view: Option<Arc<dyn TerminalView>>,
bg_layers: HashMap<Point2<i16>, (bool, LayerId2d)>, bg_layers: HashMap<Point2<i16>, (bool, LayerId2d)>,
fg_layers: HashMap<Point2<i16>, (bool, LayerId2d)>, fg_layers: HashMap<Point2<i16>, (bool, LayerId2d)>,
font: Font, //font: Arc<RwLock<Font>>,
renderer: Arc<Mutex<MarpBackend>> renderer: Arc<Mutex<MarpBackend>>
} }
@ -85,12 +85,12 @@ impl SdfTerm {
src_view: None, src_view: None,
bg_layers: HashMap::new(), bg_layers: HashMap::new(),
fg_layers: HashMap::new(), fg_layers: HashMap::new(),
font: Font::from_path(Path::new("/usr/share/fonts/TTF/FiraCode-Medium.ttf"),0).unwrap(), //font: Arc::new(RwLock::new(Font::from_path(Path::new("/usr/share/fonts/TTF/FiraCode-Medium.ttf"),0).unwrap())),
renderer renderer
} }
} }
pub fn get_order(&mut self) -> Vec<LayerId> { pub fn get_order(&self) -> Vec<LayerId> {
vec![ vec![
self.bg_layers.iter(), self.bg_layers.iter(),
self.fg_layers.iter() self.fg_layers.iter()
@ -177,7 +177,8 @@ impl SdfTerm {
// foreground layer // foreground layer
if let Some(c) = atom.c { if let Some(c) = atom.c {
let mut ch = Character::from_font(&self.font, c).with_size(1.0).with_tesselation_factor(0.01); let font = Font::from_path(Path::new("/usr/share/fonts/TTF/FiraCode-Medium.ttf"),0).unwrap();
let mut ch = Character::from_font(&font, c).with_size(1.0).with_tesselation_factor(0.01);
let (r,g,b) = atom.style.fg_color.unwrap_or((0, 0, 0)); let (r,g,b) = atom.style.fg_color.unwrap_or((0, 0, 0));
@ -202,14 +203,21 @@ impl SdfTerm {
} }
} }
} }
/*
impl Observer<dyn TerminalView> for SdfTerm { impl Observer<dyn TerminalView> for SdfTerm {
fn reset(&mut self, new_view: Option<Arc<dyn TerminalView>>) {
self.src_view = new_view;
for pt in self.src_view.area().unwrap_or(vec![]) {
self.notify(&pt);
}
}
fn notify(&mut self, pt: &Point2<i16>) { fn notify(&mut self, pt: &Point2<i16>) {
self.update(pt); self.update(pt);
self.update_order();
} }
} }
*/
#[async_std::main] #[async_std::main]
async fn main() { async fn main() {
let term_port = ViewPort::new(); let term_port = ViewPort::new();
@ -246,33 +254,21 @@ async fn main() {
compositor.write().unwrap().push(color_editor.get_term_view().offset(Vector2::new(0, 0))); compositor.write().unwrap().push(color_editor.get_term_view().offset(Vector2::new(0, 0)));
let color_view = color_port.outer().get_view();
let cp = color_port.clone();
let tp = term_port.clone(); let tp = term_port.clone();
let event_loop = nakorender::winit::event_loop::EventLoop::new();
let window = nakorender::winit::window::Window::new(&event_loop).unwrap();
let mut renderer = Arc::new(Mutex::new(nakorender::marp::MarpBackend::new(&window, &event_loop)));
let mut sdf_term = Arc::new(RwLock::new(SdfTerm::new(renderer.clone())));
sdf_term.write().unwrap().src_view = term_port.outer().get_view();
//term_port.outer().add_observer(sdf_term.clone());
async_std::task::spawn( async_std::task::spawn(
async move { async move {
loop { loop {
cp.update();
tp.update();
match term.next_event().await { match term.next_event().await {
TerminalEvent::Resize(new_size) => { TerminalEvent::Resize(mut new_size) => {
new_size.x = 10;
new_size.y = 5;
tp.inner().get_broadcast().notify_each( tp.inner().get_broadcast().notify_each(
nested::grid::GridWindowIterator::from( nested::grid::GridWindowIterator::from(
Point2::new(0,0) .. Point2::new(new_size.x, new_size.y) Point2::new(0,0) .. Point2::new(new_size.x, new_size.y)
) )
); );
} }
TerminalEvent::Input(Event::Key(Key::Ctrl('c'))) | TerminalEvent::Input(Event::Key(Key::Ctrl('c'))) |
TerminalEvent::Input(Event::Key(Key::Ctrl('g'))) | TerminalEvent::Input(Event::Key(Key::Ctrl('g'))) |
TerminalEvent::Input(Event::Key(Key::Ctrl('d'))) => break, TerminalEvent::Input(Event::Key(Key::Ctrl('d'))) => break,
@ -308,7 +304,16 @@ async fn main() {
term_writer.show().await.expect("output error!"); term_writer.show().await.expect("output error!");
}); });
let event_loop = nakorender::winit::event_loop::EventLoop::new();
let window = nakorender::winit::window::Window::new(&event_loop).unwrap();
let mut renderer = Arc::new(Mutex::new(nakorender::marp::MarpBackend::new(&window, &event_loop)));
// terminal view
let mut sdf_term = Arc::new(RwLock::new(SdfTerm::new(renderer.clone())));
term_port.outer().add_observer(sdf_term.clone());
// color preview
let color_view = color_port.outer().get_view();
let color_layer_id = renderer.lock().unwrap().new_layer_2d(); let color_layer_id = renderer.lock().unwrap().new_layer_2d();
renderer.lock().unwrap().update_camera_2d(color_layer_id, Camera2d{ renderer.lock().unwrap().update_camera_2d(color_layer_id, Camera2d{
extent: Vec2::new(4.0, 4.0), extent: Vec2::new(4.0, 4.0),
@ -335,11 +340,8 @@ async fn main() {
window.request_redraw(); window.request_redraw();
} }
winit::event::Event::RedrawRequested(_) => { winit::event::Event::RedrawRequested(_) => {
for pt in nested::grid::GridWindowIterator::from( color_port.update();
Point2::new(0, 0) .. Point2::new(30, 1) term_port.update();
) {
sdf_term.write().unwrap().update(&pt);
}
let c = color_view.get(); let c = color_view.get();
let color_stream = PrimaryStream2d::new() let color_stream = PrimaryStream2d::new()
@ -365,7 +367,7 @@ async fn main() {
renderer.lock().unwrap().update_sdf_2d(color_layer_id, color_stream); renderer.lock().unwrap().update_sdf_2d(color_layer_id, color_stream);
renderer.lock().unwrap().set_layer_order( renderer.lock().unwrap().set_layer_order(
vec![ vec![
sdf_term.write().unwrap().get_order().into_iter(), sdf_term.read().unwrap().get_order().into_iter(),
vec![ color_layer_id.into() ].into_iter() vec![ color_layer_id.into() ].into_iter()
] ]
.into_iter() .into_iter()