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:
Simon Hausmann 2021-06-15 19:08:08 +02:00
parent fbf011abf2
commit 9da1bc7eb6
6 changed files with 93 additions and 72 deletions

View file

@ -20,30 +20,7 @@ in a [`VecModel`](https://sixtyfps.io/docs/rust/sixtyfps/struct.vecmodel) which
We modify the main function like so: We modify the main function like so:
```rust ```rust
fn main() { {{#include main_tiles_from_rust.rs}}
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(&amp;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();
}
``` ```
Note that we clone the `tiles_model` because we'll use it later to update the game logic. Note that we clone the `tiles_model` because we'll use it later to update the game logic.

View file

@ -63,43 +63,7 @@ one cannot click anything during this time.
Insert this code before the `main_window.run()` call: Insert this code before the `main_window.run()` call:
```rust ```rust
// ... {{#include main_game_logic_in_rust.rs:game_logic}}
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;&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();
``` ```
Notice that we take a [Weak](https://sixtyfps.io/docs/rust/sixtyfps/struct.weak) pointer of our `main_window`. This is very Notice that we take a [Weak](https://sixtyfps.io/docs/rust/sixtyfps/struct.weak) pointer of our `main_window`. This is very

View file

@ -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`: Finally we copy the hello world program from the [SixtyFPS documentation](https://sixtyfps.io/docs/rust/sixtyfps/) into our `src/main.rs`:
```rust ```rust
sixtyfps::sixtyfps!{ {{#include main_initial.rs}}
MainWindow := Window {
Text {
text: "hello world";
color: green;
}
}
}
fn main() {
MainWindow::new().run();
}
``` ```
We run this example with `cargo run` and a window will appear with the green "Hello World" greeting. We run this example with `cargo run` and a window will appear with the green "Hello World" greeting.

View 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;
&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
}

View file

@ -0,0 +1,11 @@
sixtyfps::sixtyfps! {
MainWindow := Window {
Text {
text: "hello world";
color: green;
}
}
}
fn main() {
MainWindow::new().run();
}

View 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(&amp;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();
}