mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-02 06:41:14 +00:00
Small consistency fixes and typos across quickstarts (#4813)
This commit is contained in:
parent
10afbbbfbb
commit
0095d65696
20 changed files with 71 additions and 66 deletions
|
@ -2,13 +2,13 @@
|
|||
|
||||
# Conclusion
|
||||
|
||||
This tutorial showed you how to combine built-in Slint elements with Rust code to build a
|
||||
This tutorial showed you how to combine built-in Slint elements with JavaScript code to build a
|
||||
game. There is much more to Slint, such as layouts, widgets, or styling.
|
||||
|
||||
We recommend the following links to continue:
|
||||
|
||||
- [Examples](https://github.com/slint-ui/slint/tree/master/examples): In the Slint repository we have collected several demos and examples. These are a great starting point to learn how to use many Slint features.
|
||||
- [Examples](https://github.com/slint-ui/slint/tree/master/examples): The Slint repository has several demos and examples. These are a great starting point to learn how to use many Slint features.
|
||||
- [Todo Example](https://github.com/slint-ui/slint/tree/master/examples/todo): This is one of the examples that implements a classic use-case.
|
||||
- [Memory Puzzle](https://github.com/slint-ui/slint/tree/master/examples/memory): This is a slightly more polished version of the code in this example and you can <a href="https://slint.dev/demos/memory/" target="_blank">play the wasm version</a> in your browser.
|
||||
- [Slint API Docs](https://slint.dev/docs/rust/slint/): The reference documentation for the main Rust crate.
|
||||
- [Slint Interpreter API Docs](https://slint.dev/docs/rust/slint_interpreter/): The reference documentation for Rust crate that allows you to dynamically load Slint files.
|
||||
- [Slint Interpreter API Docs](https://slint.dev/docs/rust/slint_interpreter/): The reference documentation for the Rust crate that allows you to dynamically load Slint files.
|
||||
|
|
|
@ -4,19 +4,19 @@
|
|||
|
||||
This step places the game tiles randomly.
|
||||
|
||||
The code takes the list of tiles, duplicates it, and shuffles it, accessing the `memory_tiles` property through the JavaScript code.
|
||||
|
||||
As `memory_tiles` is an array, it's represented as a JavaScript [`Array`](https://slint.dev/docs/node/).
|
||||
You can't change the model generated by Slint, but you can extract the tiles from it, and put them
|
||||
in a [`slint.ArrayModel`](https://slint.dev/docs/node/classes/arraymodel.html) which implements the [`Model`](https://slint.dev/docs/node/interfaces/model.html) interface.
|
||||
`ArrayModel` allows you to make changes and you can use it to replace the static generated model.
|
||||
|
||||
Change `main.js` to the following:
|
||||
|
||||
```js
|
||||
{{#include main_tiles_from_js.js:main}}
|
||||
```
|
||||
|
||||
The code takes the list of tiles, duplicates it, and shuffles it, accessing the `memory_tiles` property through the JavaScript code.
|
||||
|
||||
As `memory_tiles` is an array, it's represented as a JavaScript [`Array`](https://slint.dev/docs/node/).
|
||||
You can't change the model generated by Slint, but you can extract the tiles from it and put them
|
||||
in a [`slint.ArrayModel`](https://slint.dev/docs/node/classes/arraymodel.html) which implements the [`Model`](https://slint.dev/docs/node/interfaces/model.html) interface.
|
||||
`ArrayModel` allows you to make changes and you can use it to replace the static generated model.
|
||||
|
||||
Running this code opens a window that now shows a 4 by 4 grid of rectangles, which show or hide
|
||||
the icons when a player clicks on them.
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
After modeling a single tile, this step creates a grid of them. For the grid to be a game board, you need two features:
|
||||
|
||||
1. **A data model**: An array created as a Rust model, where each element describes the tile data structure, such as:
|
||||
1. **A data model**: An array created as a JavaScript model, where each element describes the tile data structure, such as:
|
||||
|
||||
- URL of the image
|
||||
- Whether the image is visible
|
||||
|
@ -15,7 +15,7 @@ After modeling a single tile, this step creates a grid of them. For the grid to
|
|||
With Slint you declare an array of structures based on a model using square brackets. Use a <span class="hljs-keyword">for</span> loop
|
||||
to create multiple instances of the same element.
|
||||
|
||||
With Slint the for loop is declarative and automatically updates when
|
||||
The <span class="hljs-keyword">for</span> loop is declarative and automatically updates when
|
||||
the model changes. The loop instantiates all the <span class="hljs-title">MemoryTile</span> elements and places them on a grid based on their
|
||||
index with spacing between the tiles.
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
# Game Logic In JavaScript
|
||||
|
||||
This step implements the rules of the game in Rust as well.
|
||||
This step implements the rules of the game in JavaScript.
|
||||
|
||||
Slint's general philosophy is that you implement the user interface in Slint and the business logic in your favorite programming
|
||||
language.
|
||||
|
@ -23,7 +23,7 @@ tile interaction, to prevent the player from opening more tiles than allowed. No
|
|||
|
||||
The last change to the code is to act when the <span class="hljs-title">MemoryTile</span> signals that a player clicked it.
|
||||
|
||||
Add the following handler in <span class="hljs-title">MainWindow</span>:
|
||||
Add the following handler in the <span class="hljs-title">MainWindow</span> `for` loop `clicked` handler:
|
||||
|
||||
```slint
|
||||
{{#include ../../rust/src/main_game_logic_in_rust.rs:tile_click_logic}}
|
||||
|
|
|
@ -5,8 +5,9 @@
|
|||
This tutorial uses JavaScript as the host programming language. Slint also supports other programming languages like
|
||||
[Rust](https://slint.dev/docs/rust/slint/) or [C++](https://slint.dev/docs/cpp/).
|
||||
|
||||
Slint has an application template you can use to create a project with dependencies already set up that follows recommended best practices.
|
||||
We recommend using [our editor integrations for Slint](https://github.com/slint-ui/slint/tree/master/editors) for following this tutorial.
|
||||
|
||||
Slint has an application template you can use to create a project with dependencies already set up that follows recommended best practices.
|
||||
|
||||
Clone the template with the following command:
|
||||
|
||||
|
@ -29,7 +30,7 @@ Replace the contents of `src/main.js` with the following:
|
|||
{{#include main_initial.js:main}}
|
||||
```
|
||||
|
||||
Note that `slint.loadFile` resolves files from the process's current working directory, so from the `package.json` file's location.
|
||||
The `slint.loadFile` method resolves files from the process's current working directory, so from the `package.json` file's location.
|
||||
|
||||
Replace the contents of `ui/appwindow.slint` with the following:
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
# Introduction
|
||||
|
||||
This tutorial introduces you to the Slint UI framework in a playful way by implementing a memory game. It combines the `.slint` language for the graphics with the game rules implemented in Rust.
|
||||
This tutorial introduces you to the Slint UI framework in a playful way by implementing a memory game. It combines the Slint language for the graphics with the game rules implemented in JavaScript.
|
||||
|
||||
The game consists of a grid of 16 rectangular tiles. Clicking on a tile uncovers an icon underneath.
|
||||
There are 8 different icons in total, so each tile has a sibling somewhere in the grid with the
|
||||
|
|
|
@ -2,33 +2,31 @@
|
|||
|
||||
# Memory Tile
|
||||
|
||||
With the skeleton in place, this step looks at the first element of the game, the memory tile. It's the
|
||||
visual building block that consists of an underlying filled rectangle background, the icon image. Later you'll add a
|
||||
covering rectangle that acts as a curtain.
|
||||
With the skeleton code in place, this step looks at the first element of the game, the memory tile. It's the
|
||||
visual building block that consists of an underlying filled rectangle background, the icon image. Later steps add a covering rectangle that acts as a curtain.
|
||||
|
||||
You declare the background rectangle as 64 logical pixels wide and tall
|
||||
and it's filled with a soothing tone of blue.
|
||||
filled with a soothing tone of blue.
|
||||
|
||||
Note how lengths in the `.slint` language have a unit, here
|
||||
the `px` suffix. That makes the code easier to read and the compiler can detect when you accidentally
|
||||
Lengths in Slint have a unit, here, the `px` suffix.
|
||||
This makes the code easier to read and the compiler can detect when you accidentally
|
||||
mix values with different units attached to them.
|
||||
|
||||
Copy the following into the `ui/appwindow.slint` file:
|
||||
Copy the following code into `ui/appwindow.slint` file, replacing the current content:
|
||||
|
||||
```slint
|
||||
{{#include memory_tile.slint:main_window}}
|
||||
```
|
||||
|
||||
The code exports the <span class="hljs-title">MainWindow</span> component so that the business logic can access it later.
|
||||
The code exports the <span class="hljs-title">MainWindow</span> component so that the JavaScript code can access it later.
|
||||
|
||||
Inside the <span class="hljs-built_in">Rectangle</span> place an <span class="hljs-built_in">Image</span> element that
|
||||
loads an icon with the <span class="hljs-built_in">@image-url()</span> macro.
|
||||
loads an icon with the <span class="hljs-built_in">@image-url()</span> macro. The path is relative to the location of `ui/appwindow.slint`.
|
||||
|
||||
The path is relative to the folder that contains `ui/appwindow.slint`. You need to install this icon and others you use later first. You can download a pre-prepared
|
||||
[Zip archive](https://slint.dev/blog/memory-game-tutorial/icons.zip) and extract it with the
|
||||
following two commands:
|
||||
You need to install this icon and others you use later first. You can download a pre-prepared
|
||||
[Zip archive](https://slint.dev/blog/memory-game-tutorial/icons.zip) to the `ui` folder,
|
||||
|
||||
If you are on Linux or macOS, download and extract it with the following two commands:
|
||||
If you are on Linux or macOS, download and extract it with the following commands:
|
||||
|
||||
```sh
|
||||
cd ui
|
||||
|
@ -48,7 +46,7 @@ cd ..
|
|||
|
||||
This unpacks an `icons` directory containing several icons.
|
||||
|
||||
Running the program with `npm start` now opens a window that shows the icon of a bus on a
|
||||
Running the program with `npm start` opens a window that shows the icon of a bus on a
|
||||
blue background.
|
||||
|
||||

|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue