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;
buildRustCrateForPkgs = customBuildInfo;
rootFeatures = [ "screenplay" ];
release = false;
release = true;
}).rootCrate.build;
};
};

View File

@ -3,7 +3,7 @@ use gtk::prelude::*;
use kifu_core::{CoreApp, Request, Response};
use kifu_gtk::{ui::PlayingField, CoreApi};
use std::{
sync::{Arc, Mutex},
sync::{Arc, Mutex, RwLock},
time::Duration,
};
use tokio::{
@ -52,12 +52,23 @@ fn main() {
gtk_rx.attach(None, {
let api = api.clone();
let playing_field = Arc::new(RwLock::new(None));
move |message| {
match message {
Response::PlayingFieldView(view) => {
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)

View File

@ -82,6 +82,7 @@ impl ObjectImpl for BoardPrivate {
self.drawing_area
.set_draw_func(move |_, context, width, height| {
let render_start = std::time::Instant::now();
let board = board.borrow();
match background {
Ok(Some(ref background)) => {
@ -125,11 +126,10 @@ impl ObjectImpl for BoardPrivate {
(0..19).for_each(|col| {
(0..19).for_each(|row| {
match board.stone(row, col) {
IntersectionElement::Unplayable => {}
IntersectionElement::Empty(request) => {}
IntersectionElement::Filled(stone) => {
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();

View File

@ -85,6 +85,13 @@ impl PlayingField {
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")]