Optimize board updates

This commit is contained in:
Savanni D'Gerinel 2023-04-06 22:13:09 -04:00
parent 8df649d6cd
commit 59a8f252da
4 changed files with 26 additions and 6 deletions

View File

@ -76,7 +76,7 @@
inherit pkgs; inherit pkgs;
buildRustCrateForPkgs = customBuildInfo; buildRustCrateForPkgs = customBuildInfo;
rootFeatures = [ "screenplay" ]; rootFeatures = [ "screenplay" ];
release = false; release = true;
}).rootCrate.build; }).rootCrate.build;
}; };
}; };

View File

@ -3,7 +3,7 @@ use gtk::prelude::*;
use kifu_core::{CoreApp, Request, Response}; use kifu_core::{CoreApp, Request, Response};
use kifu_gtk::{ui::PlayingField, CoreApi}; use kifu_gtk::{ui::PlayingField, CoreApi};
use std::{ use std::{
sync::{Arc, Mutex}, sync::{Arc, Mutex, RwLock},
time::Duration, time::Duration,
}; };
use tokio::{ use tokio::{
@ -52,12 +52,23 @@ fn main() {
gtk_rx.attach(None, { gtk_rx.attach(None, {
let api = api.clone(); let api = api.clone();
let playing_field = Arc::new(RwLock::new(None));
move |message| { move |message| {
match message { match message {
Response::PlayingFieldView(view) => { Response::PlayingFieldView(view) => {
let api = api.clone(); let api = api.clone();
let playing_field = PlayingField::new(api, view);
window.set_child(Some(&playing_field)); let start = std::time::Instant::now();
let mut playing_field = playing_field.write().unwrap();
if playing_field.is_none() {
let field = PlayingField::new(api, view);
window.set_child(Some(&field));
*playing_field = Some(field);
} else {
playing_field.as_ref().map(|field| field.update_view(view));
}
let end = std::time::Instant::now();
println!("Time to render the playing field: {:?}", end - start);
} }
} }
Continue(true) Continue(true)

View File

@ -82,6 +82,7 @@ impl ObjectImpl for BoardPrivate {
self.drawing_area self.drawing_area
.set_draw_func(move |_, context, width, height| { .set_draw_func(move |_, context, width, height| {
let render_start = std::time::Instant::now();
let board = board.borrow(); let board = board.borrow();
match background { match background {
Ok(Some(ref background)) => { Ok(Some(ref background)) => {
@ -125,11 +126,10 @@ impl ObjectImpl for BoardPrivate {
(0..19).for_each(|col| { (0..19).for_each(|col| {
(0..19).for_each(|row| { (0..19).for_each(|row| {
match board.stone(row, col) { match board.stone(row, col) {
IntersectionElement::Unplayable => {}
IntersectionElement::Empty(request) => {}
IntersectionElement::Filled(stone) => { IntersectionElement::Filled(stone) => {
pen.stone(&context, row, col, stone.color); pen.stone(&context, row, col, stone.color);
} }
_ => {}
}; };
}) })
}); });
@ -144,6 +144,8 @@ impl ObjectImpl for BoardPrivate {
), ),
_ => {} _ => {}
} }
let render_end = std::time::Instant::now();
println!("board rendering time: {:?}", render_end - render_start);
}); });
let motion_controller = gtk::EventControllerMotion::new(); let motion_controller = gtk::EventControllerMotion::new();

View File

@ -85,6 +85,13 @@ impl PlayingField {
s s
} }
pub fn update_view(&self, view: PlayingFieldView) {
self.imp().board.borrow().as_ref().map(|board| {
board.set_board(view.board);
board.set_current_player(view.current_player);
});
}
} }
#[cfg(feature = "screenplay")] #[cfg(feature = "screenplay")]