slint/examples/memory/main.js
Florian Blasius 0045787e1c
node: api review adjustements part I (#3766)
* Update api/node/src/types/brush.rs

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update tests/cases/callbacks/handler_with_arg.slint

Co-authored-by: Olivier Goffart <olivier.goffart@slint.dev>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Olivier Goffart <olivier.goffart@slint.dev>
2023-10-26 10:02:49 +02:00

63 lines
1.7 KiB
JavaScript
Executable file

#!/usr/bin/env node
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: MIT
import * as slint from "slint-ui";
let ui = slint.loadFile("memory.slint");
let window = new ui.MainWindow();
let initial_tiles = window.memory_tiles;
let tiles = initial_tiles.concat(initial_tiles.map((tile) => Object.assign({}, tile)));
for (let i = tiles.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * i);
[tiles[i], tiles[j]] = [tiles[j], tiles[i]];
}
let model = new slint.ArrayModel(tiles);
window.memory_tiles = model;
window.check_if_pair_solved = function () {
let flipped_tiles = [];
tiles.forEach((tile, index) => {
if (tile.image_visible && !tile.solved) {
flipped_tiles.push({
index,
tile
});
}
});
if (flipped_tiles.length == 2) {
let {
tile: tile1,
index: tile1_index
} = flipped_tiles[0];
let {
tile: tile2,
index: tile2_index
} = flipped_tiles[1];
let is_pair_solved = tile1.image === tile2.image;
if (is_pair_solved) {
tile1.solved = true;
model.setRowData(tile1_index, tile1);
tile2.solved = true;
model.setRowData(tile2_index, tile2);
} else {
window.disable_tiles = true;
slint.Timer.singleShot(1000, () => {
window.disable_tiles = false;
tile1.image_visible = false;
model.setRowData(tile1_index, tile1);
tile2.image_visible = false;
model.setRowData(tile2_index, tile2);
});
}
}
};
window.run();