mirror of
https://github.com/slint-ui/slint.git
synced 2025-08-31 15:47:26 +00:00
Replace inline .60 code in game logic in $lang chapter
This commit is contained in:
parent
56e56f7028
commit
c540cbaf2d
3 changed files with 16 additions and 72 deletions
|
@ -14,45 +14,14 @@ the callback and property declarations into <span class="hljs-title">MainWindow<
|
|||
|
||||
|
||||
```60
|
||||
...
|
||||
MainWindow := Window {
|
||||
callback check_if_pair_solved(); // Added
|
||||
property <bool> disable_tiles; // Added
|
||||
|
||||
width: 326px;
|
||||
height: 326px;
|
||||
|
||||
property <[TileData]> memory_tiles: [
|
||||
{ image: img!"icons/at.png" },
|
||||
...
|
||||
{{#include main_game_logic_in_rust.rs:mainwindow_interface}}
|
||||
```
|
||||
|
||||
The last change to the `.60` markup is to act when the <span class="hljs-title">MemoryTile</span> signals that it was clicked on. We add the following handler:
|
||||
The last change to the `.60` markup is to act when the <span class="hljs-title">MemoryTile</span> signals that it was clicked on.
|
||||
We add the following handler in <span class="hljs-title">MainWindow</span>:
|
||||
|
||||
```60
|
||||
...
|
||||
MainWindow := Window {
|
||||
...
|
||||
for tile[i] in memory_tiles : MemoryTile {
|
||||
x: mod(i, 4) * 74px;
|
||||
y: floor(i / 4) * 74px;
|
||||
width: 64px;
|
||||
height: 64px;
|
||||
icon: tile.image;
|
||||
open_curtain: tile.image_visible || tile.solved;
|
||||
// propagate the solved status from the model to the tile
|
||||
solved: tile.solved;
|
||||
|
||||
clicked => {
|
||||
// old: tile.image_visible = !tile.image_visible;
|
||||
// new:
|
||||
if (!root.disable_tiles) {
|
||||
tile.image_visible = !tile.image_visible;
|
||||
root.check_if_pair_solved();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
{{#include main_game_logic_in_rust.rs:tile_click_logic}}
|
||||
```
|
||||
|
||||
On the Rust side, we can now add an handler to the `check_if_pair_solved` callback, that will check if
|
||||
|
|
|
@ -109,15 +109,17 @@ sixtyfps::sixtyfps! {
|
|||
}
|
||||
}
|
||||
}
|
||||
// ANCHOR: mainwindow_interface
|
||||
MainWindow := Window {
|
||||
width: 326px;
|
||||
height: 326px;
|
||||
|
||||
callback check_if_pair_solved();
|
||||
property <bool> disable_tiles;
|
||||
callback check_if_pair_solved(); // Added
|
||||
property <bool> disable_tiles; // Added
|
||||
|
||||
property <[TileData]> memory_tiles: [
|
||||
{ image: @image-url("icons/at.png") },
|
||||
// ANCHOR_END: mainwindow_interface
|
||||
{ image: @image-url("icons/balance-scale.png") },
|
||||
{ image: @image-url("icons/bicycle.png") },
|
||||
{ image: @image-url("icons/bus.png") },
|
||||
|
@ -126,6 +128,7 @@ sixtyfps::sixtyfps! {
|
|||
{ image: @image-url("icons/motorcycle.png") },
|
||||
{ image: @image-url("icons/video.png") },
|
||||
];
|
||||
// ANCHOR: tile_click_logic
|
||||
for tile[i] in memory_tiles : MemoryTile {
|
||||
x: mod(i, 4) * 74px;
|
||||
y: floor(i / 4) * 74px;
|
||||
|
@ -136,11 +139,14 @@ sixtyfps::sixtyfps! {
|
|||
// propagate the solved status from the model to the tile
|
||||
solved: tile.solved;
|
||||
clicked => {
|
||||
// old: tile.image_visible = !tile.image_visible;
|
||||
// new:
|
||||
if (!root.disable_tiles) {
|
||||
tile.image_visible = !tile.image_visible;
|
||||
root.check_if_pair_solved();
|
||||
}
|
||||
}
|
||||
}
|
||||
// ANCHOR_END: tile_click_logic
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue