mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-02 14:51:15 +00:00
tutorial: separate out the rust code into .rs files
That way even the "incomplete" snippets can be compile tested as only the anchors will be pulled into the docs but the complete file can be compile-tested in the future.
This commit is contained in:
parent
fbf011abf2
commit
9da1bc7eb6
6 changed files with 93 additions and 72 deletions
|
@ -20,30 +20,7 @@ in a [`VecModel`](https://sixtyfps.io/docs/rust/sixtyfps/struct.vecmodel) which
|
|||
We modify the main function like so:
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
use sixtyfps::Model;
|
||||
|
||||
let main_window = MainWindow::new();
|
||||
|
||||
// Fetch the tiles from the model
|
||||
let mut tiles: Vec<TileData> =
|
||||
main_window.get_memory_tiles().iter().collect();
|
||||
// Duplicate them to ensure that we have pairs
|
||||
tiles.extend(tiles.clone());
|
||||
|
||||
// Randomly mix the tiles
|
||||
use rand::seq::SliceRandom;
|
||||
let mut rng = rand::thread_rng();
|
||||
tiles.shuffle(&mut rng);
|
||||
|
||||
// Assign the shuffled Vec to the model property
|
||||
let tiles_model =
|
||||
std::rc::Rc::new(sixtyfps::VecModel::from(tiles));
|
||||
main_window.set_memory_tiles(
|
||||
sixtyfps::ModelHandle::new(tiles_model.clone()));
|
||||
|
||||
main_window.run();
|
||||
}
|
||||
{{#include main_tiles_from_rust.rs}}
|
||||
```
|
||||
|
||||
Note that we clone the `tiles_model` because we'll use it later to update the game logic.
|
||||
|
|
|
@ -63,43 +63,7 @@ one cannot click anything during this time.
|
|||
Insert this code before the `main_window.run()` call:
|
||||
|
||||
```rust
|
||||
// ...
|
||||
let main_window_weak = main_window.as_weak();
|
||||
main_window.on_check_if_pair_solved(move || {
|
||||
let mut flipped_tiles =
|
||||
tiles_model.iter().enumerate().filter(|(_, tile)| {
|
||||
tile.image_visible && !tile.solved
|
||||
});
|
||||
|
||||
if let (Some((t1_idx, mut t1)), Some((t2_idx, mut t2))) =
|
||||
(flipped_tiles.next(), flipped_tiles.next())
|
||||
{
|
||||
let is_pair_solved = t1 == t2;
|
||||
if is_pair_solved {
|
||||
t1.solved = true;
|
||||
tiles_model.set_row_data(t1_idx, t1.clone());
|
||||
t2.solved = true;
|
||||
tiles_model.set_row_data(t2_idx, t2.clone());
|
||||
} else {
|
||||
let main_window = main_window_weak.unwrap();
|
||||
main_window.set_disable_tiles(true);
|
||||
let tiles_model = tiles_model.clone();
|
||||
sixtyfps::Timer::single_shot(
|
||||
std::time::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(t2_idx, t2);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
main_window.run();
|
||||
{{#include main_game_logic_in_rust.rs:game_logic}}
|
||||
```
|
||||
|
||||
Notice that we take a [Weak](https://sixtyfps.io/docs/rust/sixtyfps/struct.weak) pointer of our `main_window`. This is very
|
||||
|
|
|
@ -20,17 +20,7 @@ sixtyfps = "0.0.6"
|
|||
Finally we copy the hello world program from the [SixtyFPS documentation](https://sixtyfps.io/docs/rust/sixtyfps/) into our `src/main.rs`:
|
||||
|
||||
```rust
|
||||
sixtyfps::sixtyfps!{
|
||||
MainWindow := Window {
|
||||
Text {
|
||||
text: "hello world";
|
||||
color: green;
|
||||
}
|
||||
}
|
||||
}
|
||||
fn main() {
|
||||
MainWindow::new().run();
|
||||
}
|
||||
{{#include main_initial.rs}}
|
||||
```
|
||||
|
||||
We run this example with `cargo run` and a window will appear with the green "Hello World" greeting.
|
||||
|
|
55
docs/tutorial/src/main_game_logic_in_rust.rs
Normal file
55
docs/tutorial/src/main_game_logic_in_rust.rs
Normal file
|
@ -0,0 +1,55 @@
|
|||
fn main() {
|
||||
use sixtyfps::Model;
|
||||
|
||||
let main_window = MainWindow::new();
|
||||
|
||||
// Fetch the tiles from the model
|
||||
let mut tiles: Vec<TileData> = main_window.get_memory_tiles().iter().collect();
|
||||
// Duplicate them to ensure that we have pairs
|
||||
tiles.extend(tiles.clone());
|
||||
|
||||
// Randomly mix the tiles
|
||||
use rand::seq::SliceRandom;
|
||||
let mut rng = rand::thread_rng();
|
||||
tiles.shuffle(&mut rng);
|
||||
|
||||
// ANCHOR: game_logic
|
||||
// Assign the shuffled Vec to the model property
|
||||
let tiles_model = std::rc::Rc::new(sixtyfps::VecModel::from(tiles));
|
||||
main_window.set_memory_tiles(sixtyfps::ModelHandle::new(tiles_model.clone()));
|
||||
|
||||
let main_window_weak = main_window.as_weak();
|
||||
main_window.on_check_if_pair_solved(move || {
|
||||
let mut flipped_tiles = tiles_model.iter().enumerate().filter(|(_, tile)| {
|
||||
tile.image_visible & amp;
|
||||
&
|
||||
!tile.solved
|
||||
});
|
||||
|
||||
if let (Some((t1_idx, mut t1)), Some((t2_idx, mut t2))) =
|
||||
(flipped_tiles.next(), flipped_tiles.next())
|
||||
{
|
||||
let is_pair_solved = t1 == t2;
|
||||
if is_pair_solved {
|
||||
t1.solved = true;
|
||||
tiles_model.set_row_data(t1_idx, t1.clone());
|
||||
t2.solved = true;
|
||||
tiles_model.set_row_data(t2_idx, t2.clone());
|
||||
} else {
|
||||
let main_window = main_window_weak.unwrap();
|
||||
main_window.set_disable_tiles(true);
|
||||
let tiles_model = tiles_model.clone();
|
||||
sixtyfps::Timer::single_shot(std::time::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(t2_idx, t2);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
main_window.run();
|
||||
// ANCHOR_END: game_logic
|
||||
}
|
11
docs/tutorial/src/main_initial.rs
Normal file
11
docs/tutorial/src/main_initial.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
sixtyfps::sixtyfps! {
|
||||
MainWindow := Window {
|
||||
Text {
|
||||
text: "hello world";
|
||||
color: green;
|
||||
}
|
||||
}
|
||||
}
|
||||
fn main() {
|
||||
MainWindow::new().run();
|
||||
}
|
24
docs/tutorial/src/main_tiles_from_rust.rs
Normal file
24
docs/tutorial/src/main_tiles_from_rust.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
fn main() {
|
||||
use sixtyfps::Model;
|
||||
|
||||
let main_window = MainWindow::new();
|
||||
|
||||
// Fetch the tiles from the model
|
||||
let mut tiles: Vec<TileData> =
|
||||
main_window.get_memory_tiles().iter().collect();
|
||||
// Duplicate them to ensure that we have pairs
|
||||
tiles.extend(tiles.clone());
|
||||
|
||||
// Randomly mix the tiles
|
||||
use rand::seq::SliceRandom;
|
||||
let mut rng = rand::thread_rng();
|
||||
tiles.shuffle(&mut rng);
|
||||
|
||||
// Assign the shuffled Vec to the model property
|
||||
let tiles_model =
|
||||
std::rc::Rc::new(sixtyfps::VecModel::from(tiles));
|
||||
main_window.set_memory_tiles(
|
||||
sixtyfps::ModelHandle::new(tiles_model.clone()));
|
||||
|
||||
main_window.run();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue