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