Refactor slightly
This commit is contained in:
parent
7ccca84926
commit
29a625a736
27
src/main.rs
27
src/main.rs
@ -1,19 +1,18 @@
|
||||
::slint::include_modules!();
|
||||
|
||||
fn main() -> Result<(), slint::PlatformError> {
|
||||
use ::rand::seq::SliceRandom;
|
||||
use ::slint::include_modules;
|
||||
use ::slint::{Model, Timer, VecModel};
|
||||
use std::rc::Rc;
|
||||
use std::time::Duration;
|
||||
|
||||
include_modules!();
|
||||
let main_window = MainWindow::new()?;
|
||||
|
||||
fn main() {
|
||||
let main_window = MainWindow::new().unwrap();
|
||||
|
||||
// Fetch the tiles from the model
|
||||
// Fetch the tiles from the model, and then duplicate them so we have pairs
|
||||
let mut tiles: Vec<TileData> = main_window.get_memory_tiles().iter().collect();
|
||||
// Duplicate the list of tiles so we have pairs
|
||||
tiles.extend(tiles.clone());
|
||||
|
||||
// Randomize the tiles
|
||||
let mut rng = rand::thread_rng();
|
||||
tiles.shuffle(&mut rng);
|
||||
|
||||
@ -22,6 +21,7 @@ fn main() {
|
||||
main_window.set_memory_tiles(tiles_model.clone().into());
|
||||
|
||||
let main_window_weak = main_window.as_weak();
|
||||
|
||||
main_window.on_check_if_pair_solved(move || {
|
||||
let mut flipped_tiles = tiles_model
|
||||
.iter()
|
||||
@ -34,23 +34,28 @@ fn main() {
|
||||
let is_pair_solved = t1 == t2;
|
||||
if is_pair_solved {
|
||||
t1.solved = true;
|
||||
tiles_model.set_row_data(t1_idx, t1);
|
||||
t2.solved = true;
|
||||
tiles_model.set_row_data(t1_idx, t1);
|
||||
tiles_model.set_row_data(t2_idx, t2);
|
||||
} else {
|
||||
let main_window = main_window_weak.unwrap();
|
||||
main_window.set_disable_tiles(true);
|
||||
let tiles_model = tiles_model.clone();
|
||||
|
||||
main_window.set_disable_tiles(true);
|
||||
|
||||
Timer::single_shot(Duration::from_secs(1), move || {
|
||||
main_window.set_disable_tiles(false);
|
||||
|
||||
t1.image_visible = false;
|
||||
tiles_model.set_row_data(t1_idx, t1);
|
||||
t2.image_visible = false;
|
||||
tiles_model.set_row_data(t1_idx, t1);
|
||||
tiles_model.set_row_data(t2_idx, t2);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
main_window.run().unwrap();
|
||||
main_window.run()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user