Initial commit
This commit is contained in:
commit
db54accc82
27 files changed
+130202
No files matched your search
@@ -0,0 +1,13 @@
|
|||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
|
||||||
|
[*.rs]
|
||||||
|
ident_style = space
|
||||||
|
ident_size = 4
|
||||||
|
tab_width = 4
|
||||||
|
|
||||||
|
[*.{scss,css}]
|
||||||
|
ident_style = space
|
||||||
|
ident_size = 2
|
||||||
|
tab_width = 2
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
/target
|
||||||
|
.DS_Store
|
||||||
|
/assets/tailwind.css
|
||||||
|
|
||||||
|
# These are backup files generated by rustfmt
|
||||||
|
**/*.rs.bk
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
{
|
||||||
|
"languages": {
|
||||||
|
"CSS": {
|
||||||
|
"language_servers": [
|
||||||
|
"tailwindcss-intellisense-css",
|
||||||
|
"!vscode-css-language-server"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"Rust": {
|
||||||
|
"language_servers": [
|
||||||
|
"tailwindcss-language-server",
|
||||||
|
"rust-analyzer"
|
||||||
|
],
|
||||||
|
"format_on_save": "on",
|
||||||
|
"formatter": [
|
||||||
|
{ "language_server": { "name": "rust-analyzer" } },
|
||||||
|
{ "external": { "command": "dx", "arguments": ["fmt", "-f", "-"] } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"lsp": {
|
||||||
|
"tailwindcss-language-server": {
|
||||||
|
"settings": {
|
||||||
|
"experimental": {
|
||||||
|
"classRegex": ["class: \"(.*)\""],
|
||||||
|
},
|
||||||
|
"emmetCompletions": false,
|
||||||
|
"includedLanguages": {
|
||||||
|
"rust": "html"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,265 @@
|
|||||||
|
You are an expert [0.7 Dioxus](https://dioxuslabs.com/learn/0.7) assistant. Dioxus 0.7 changes every api in dioxus. Only use this up to date documentation. `cx`, `Scope`, and `use_state` are gone
|
||||||
|
|
||||||
|
Provide concise code examples with detailed descriptions
|
||||||
|
|
||||||
|
# Dioxus Dependency
|
||||||
|
|
||||||
|
You can add Dioxus to your `Cargo.toml` like this:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[dependencies]
|
||||||
|
dioxus = { version = "0.7.1" }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["web", "webview", "server"]
|
||||||
|
web = ["dioxus/web"]
|
||||||
|
webview = ["dioxus/desktop"]
|
||||||
|
server = ["dioxus/server"]
|
||||||
|
```
|
||||||
|
|
||||||
|
# Launching your application
|
||||||
|
|
||||||
|
You need to create a main function that sets up the Dioxus runtime and mounts your root component.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
dioxus::launch(App);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
fn App() -> Element {
|
||||||
|
rsx! { "Hello, Dioxus!" }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Then serve with `dx serve`:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -sSL http://dioxus.dev/install.sh | sh
|
||||||
|
dx serve
|
||||||
|
```
|
||||||
|
|
||||||
|
# UI with RSX
|
||||||
|
|
||||||
|
```rust
|
||||||
|
rsx! {
|
||||||
|
div {
|
||||||
|
class: "container", // Attribute
|
||||||
|
color: "red", // Inline styles
|
||||||
|
width: if condition { "100%" }, // Conditional attributes
|
||||||
|
"Hello, Dioxus!"
|
||||||
|
}
|
||||||
|
// Prefer loops over iterators
|
||||||
|
for i in 0..5 {
|
||||||
|
div { "{i}" } // use elements or components directly in loops
|
||||||
|
}
|
||||||
|
if condition {
|
||||||
|
div { "Condition is true!" } // use elements or components directly in conditionals
|
||||||
|
}
|
||||||
|
|
||||||
|
{children} // Expressions are wrapped in brace
|
||||||
|
{(0..5).map(|i| rsx! { span { "Item {i}" } })} // Iterators must be wrapped in braces
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
# Assets
|
||||||
|
|
||||||
|
The asset macro can be used to link to local files to use in your project. All links start with `/` and are relative to the root of your project.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
rsx! {
|
||||||
|
img {
|
||||||
|
src: asset!("/assets/image.png"),
|
||||||
|
alt: "An image",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Styles
|
||||||
|
|
||||||
|
The `document::Stylesheet` component will inject the stylesheet into the `<head>` of the document
|
||||||
|
|
||||||
|
```rust
|
||||||
|
rsx! {
|
||||||
|
document::Stylesheet {
|
||||||
|
href: asset!("/assets/styles.css"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
# Components
|
||||||
|
|
||||||
|
Components are the building blocks of apps
|
||||||
|
|
||||||
|
* Component are functions annotated with the `#[component]` macro.
|
||||||
|
* The function name must start with a capital letter or contain an underscore.
|
||||||
|
* A component re-renders only under two conditions:
|
||||||
|
1. Its props change (as determined by `PartialEq`).
|
||||||
|
2. An internal reactive state it depends on is updated.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[component]
|
||||||
|
fn Input(mut value: Signal<String>) -> Element {
|
||||||
|
rsx! {
|
||||||
|
input {
|
||||||
|
value,
|
||||||
|
oninput: move |e| {
|
||||||
|
*value.write() = e.value();
|
||||||
|
},
|
||||||
|
onkeydown: move |e| {
|
||||||
|
if e.key() == Key::Enter {
|
||||||
|
value.write().clear();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Each component accepts function arguments (props)
|
||||||
|
|
||||||
|
* Props must be owned values, not references. Use `String` and `Vec<T>` instead of `&str` or `&[T]`.
|
||||||
|
* Props must implement `PartialEq` and `Clone`.
|
||||||
|
* To make props reactive and copy, you can wrap the type in `ReadOnlySignal`. Any reactive state like memos and resources that read `ReadOnlySignal` props will automatically re-run when the prop changes.
|
||||||
|
|
||||||
|
# State
|
||||||
|
|
||||||
|
A signal is a wrapper around a value that automatically tracks where it's read and written. Changing a signal's value causes code that relies on the signal to rerun.
|
||||||
|
|
||||||
|
## Local State
|
||||||
|
|
||||||
|
The `use_signal` hook creates state that is local to a single component. You can call the signal like a function (e.g. `my_signal()`) to clone the value, or use `.read()` to get a reference. `.write()` gets a mutable reference to the value.
|
||||||
|
|
||||||
|
Use `use_memo` to create a memoized value that recalculates when its dependencies change. Memos are useful for expensive calculations that you don't want to repeat unnecessarily.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[component]
|
||||||
|
fn Counter() -> Element {
|
||||||
|
let mut count = use_signal(|| 0);
|
||||||
|
let mut doubled = use_memo(move || count() * 2); // doubled will re-run when count changes because it reads the signal
|
||||||
|
|
||||||
|
rsx! {
|
||||||
|
h1 { "Count: {count}" } // Counter will re-render when count changes because it reads the signal
|
||||||
|
h2 { "Doubled: {doubled}" }
|
||||||
|
button {
|
||||||
|
onclick: move |_| *count.write() += 1, // Writing to the signal rerenders Counter
|
||||||
|
"Increment"
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
onclick: move |_| count.with_mut(|count| *count += 1), // use with_mut to mutate the signal
|
||||||
|
"Increment with with_mut"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Context API
|
||||||
|
|
||||||
|
The Context API allows you to share state down the component tree. A parent provides the state using `use_context_provider`, and any child can access it with `use_context`
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[component]
|
||||||
|
fn App() -> Element {
|
||||||
|
let mut theme = use_signal(|| "light".to_string());
|
||||||
|
use_context_provider(|| theme); // Provide a type to children
|
||||||
|
rsx! { Child {} }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
fn Child() -> Element {
|
||||||
|
let theme = use_context::<Signal<String>>(); // Consume the same type
|
||||||
|
rsx! {
|
||||||
|
div {
|
||||||
|
"Current theme: {theme}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
# Async
|
||||||
|
|
||||||
|
For state that depends on an asynchronous operation (like a network request), Dioxus provides a hook called `use_resource`. This hook manages the lifecycle of the async task and provides the result to your component.
|
||||||
|
|
||||||
|
* The `use_resource` hook takes an `async` closure. It re-runs this closure whenever any signals it depends on (reads) are updated
|
||||||
|
* The `Resource` object returned can be in several states when read:
|
||||||
|
1. `None` if the resource is still loading
|
||||||
|
2. `Some(value)` if the resource has successfully loaded
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let mut dog = use_resource(move || async move {
|
||||||
|
// api request
|
||||||
|
});
|
||||||
|
|
||||||
|
match dog() {
|
||||||
|
Some(dog_info) => rsx! { Dog { dog_info } },
|
||||||
|
None => rsx! { "Loading..." },
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
# Routing
|
||||||
|
|
||||||
|
All possible routes are defined in a single Rust `enum` that derives `Routable`. Each variant represents a route and is annotated with `#[route("/path")]`. Dynamic Segments can capture parts of the URL path as parameters by using `:name` in the route string. These become fields in the enum variant.
|
||||||
|
|
||||||
|
The `Router<Route> {}` component is the entry point that manages rendering the correct component for the current URL.
|
||||||
|
|
||||||
|
You can use the `#[layout(NavBar)]` to create a layout shared between pages and place an `Outlet<Route> {}` inside your layout component. The child routes will be rendered in the outlet.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[derive(Routable, Clone, PartialEq)]
|
||||||
|
enum Route {
|
||||||
|
#[layout(NavBar)] // This will use NavBar as the layout for all routes
|
||||||
|
#[route("/")]
|
||||||
|
Home {},
|
||||||
|
#[route("/blog/:id")] // Dynamic segment
|
||||||
|
BlogPost { id: i32 },
|
||||||
|
}
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
fn NavBar() -> Element {
|
||||||
|
rsx! {
|
||||||
|
a { href: "/", "Home" }
|
||||||
|
Outlet<Route> {} // Renders Home or BlogPost
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
fn App() -> Element {
|
||||||
|
rsx! { Router::<Route> {} }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```toml
|
||||||
|
dioxus = { version = "0.7.1", features = ["router"] }
|
||||||
|
```
|
||||||
|
|
||||||
|
# Fullstack
|
||||||
|
|
||||||
|
Fullstack enables server rendering and ipc calls. It uses Cargo features (`server` and a client feature like `web`) to split the code into a server and client binaries.
|
||||||
|
|
||||||
|
```toml
|
||||||
|
dioxus = { version = "0.7.1", features = ["fullstack"] }
|
||||||
|
```
|
||||||
|
|
||||||
|
## Server Functions
|
||||||
|
|
||||||
|
Use the `#[post]` / `#[get]` macros to define an `async` function that will only run on the server. On the server, this macro generates an API endpoint. On the client, it generates a function that makes an HTTP request to that endpoint.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[post("/api/double/:path/&query")]
|
||||||
|
async fn double_server(number: i32, path: String, query: i32) -> Result<i32, ServerFnError> {
|
||||||
|
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||||
|
Ok(number * 2)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Hydration
|
||||||
|
|
||||||
|
Hydration is the process of making a server-rendered HTML page interactive on the client. The server sends the initial HTML, and then the client-side runs, attaches event listeners, and takes control of future rendering.
|
||||||
|
|
||||||
|
### Errors
|
||||||
|
The initial UI rendered by the component on the client must be identical to the UI rendered on the server.
|
||||||
|
|
||||||
|
* Use the `use_server_future` hook instead of `use_resource`. It runs the future on the server, serializes the result, and sends it to the client, ensuring the client has the data immediately for its first render.
|
||||||
|
* Any code that relies on browser-specific APIs (like accessing `localStorage`) must be run *after* hydration. Place this code inside a `use_effect` hook.
|
||||||
Generated
+4104
File diff suppressed because it is too large.
Load diff
+28
@@ -0,0 +1,28 @@
|
|||||||
|
[package]
|
||||||
|
name = "rustoid"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["pimpest <82343504+pimpest@users.noreply.github.com>"]
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
dioxus = { version = "0.7.1", features = ["router", "fullstack"] }
|
||||||
|
dioxus-free-icons = { version = "0.10.0", features = ["bootstrap"] }
|
||||||
|
gloo-render = "0.3.1"
|
||||||
|
tokio = { version = "1.54.1", features = ["sync"] }
|
||||||
|
tracing = "0.1.44"
|
||||||
|
wasm-bindgen = { version = "0.2.130" }
|
||||||
|
web-sys = { version = "0.3.107", features = [
|
||||||
|
"CanvasRenderingContext2d",
|
||||||
|
"HtmlCanvasElement",
|
||||||
|
"HtmlImageElement",
|
||||||
|
"ImageBitmap",
|
||||||
|
"ImageBitmapOptions",
|
||||||
|
"ResizeQuality",
|
||||||
|
] }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["web"]
|
||||||
|
web = ["dioxus/web"]
|
||||||
|
server = ["dioxus/server"]
|
||||||
+21
@@ -0,0 +1,21 @@
|
|||||||
|
[application]
|
||||||
|
|
||||||
|
[web.app]
|
||||||
|
|
||||||
|
# HTML title tag content
|
||||||
|
title = "rustoid"
|
||||||
|
|
||||||
|
# include `assets` in web platform
|
||||||
|
[web.resource]
|
||||||
|
|
||||||
|
# Additional CSS style files
|
||||||
|
style = []
|
||||||
|
|
||||||
|
# Additional JavaScript files
|
||||||
|
script = []
|
||||||
|
|
||||||
|
[web.resource.dev]
|
||||||
|
|
||||||
|
# Javascript code file
|
||||||
|
# serve: [dev-server] only
|
||||||
|
script = []
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
# Development
|
||||||
|
|
||||||
|
Your new jumpstart project includes basic organization with an organized `assets` folder and a `components` folder.
|
||||||
|
If you chose to develop with the router feature, you will also have a `views` folder.
|
||||||
|
|
||||||
|
```
|
||||||
|
project/
|
||||||
|
├─ assets/ # Any assets that are used by the app should be placed here
|
||||||
|
├─ src/
|
||||||
|
│ ├─ main.rs # The entrypoint for the app. It also defines the routes for the app.
|
||||||
|
│ ├─ components/
|
||||||
|
│ │ ├─ mod.rs # Defines the components module
|
||||||
|
│ │ ├─ hero.rs # The Hero component for use in the home page
|
||||||
|
│ │ ├─ echo.rs # The echo component uses server functions to communicate with the server
|
||||||
|
│ ├─ views/ # The views each route will render in the app.
|
||||||
|
│ │ ├─ mod.rs # Defines the module for the views route and re-exports the components for each route
|
||||||
|
│ │ ├─ blog.rs # The component that will render at the /blog/:id route
|
||||||
|
│ │ ├─ home.rs # The component that will render at the / route
|
||||||
|
├─ Cargo.toml # The Cargo.toml file defines the dependencies and feature flags for your project
|
||||||
|
```
|
||||||
|
|
||||||
|
### Automatic Tailwind (Dioxus 0.7+)
|
||||||
|
|
||||||
|
As of Dioxus 0.7, there no longer is a need to manually install tailwind. Simply `dx serve` and you're good to go!
|
||||||
|
|
||||||
|
Automatic tailwind is supported by checking for a file called `tailwind.css` in your app's manifest directory (next to Cargo.toml). To customize the file, use the dioxus.toml:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[application]
|
||||||
|
tailwind_input = "my.css"
|
||||||
|
tailwind_output = "assets/out.css"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Tailwind Manual Install
|
||||||
|
|
||||||
|
To use tailwind plugins or manually customize tailwind, you can can install the Tailwind CLI and use it directly.
|
||||||
|
|
||||||
|
1. Install npm: https://docs.npmjs.com/downloading-and-installing-node-js-and-npm
|
||||||
|
2. Install the Tailwind CSS CLI: https://tailwindcss.com/docs/installation/tailwind-cli
|
||||||
|
3. Run the following command in the root of the project to start the Tailwind CSS compiler:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npx @tailwindcss/cli -i ./input.css -o ./assets/tailwind.css --watch
|
||||||
|
```
|
||||||
|
|
||||||
|
### Serving Your App
|
||||||
|
|
||||||
|
Run the following command in the root of your project to start developing with the default platform:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
dx serve --platform web
|
||||||
|
```
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 130 KiB |
@@ -0,0 +1,20 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1007 197"><style>
|
||||||
|
@keyframes a0_t { 0% { transform: translate(225px,339.9px) scale(0,1) translate(-89.5px,-24px); } 35% { transform: translate(225px,339.9px) scale(0,1) translate(-89.5px,-24px); animation-timing-function: cubic-bezier(.6,0,.4,1); } 45% { transform: translate(225px,339.9px) scale(1,1) translate(-89.5px,-24px); } 50% { transform: translate(225px,339.9px) scale(1,1) translate(-89.5px,-24px); animation-timing-function: cubic-bezier(.6,0,.4,1); } 60% { transform: translate(225px,339.9px) scale(0,1) translate(-89.5px,-24px); } 100% { transform: translate(225px,339.9px) scale(0,1) translate(-89.5px,-24px); } }
|
||||||
|
@keyframes a1_t { 0% { transform: translate(225px,348.9px) scale(0,1) translate(-41.5px,-11px); } 20% { transform: translate(225px,348.9px) scale(0,1) translate(-41.5px,-11px); animation-timing-function: cubic-bezier(.6,0,.4,1); } 30% { transform: translate(225px,348.9px) scale(1,1) translate(-41.5px,-11px); } 35% { transform: translate(225px,348.9px) scale(1,1) translate(-41.5px,-11px); } 45% { transform: translate(225px,348.9px) scale(0,1) translate(-41.5px,-11px); } 100% { transform: translate(225px,348.9px) scale(0,1) translate(-41.5px,-11px); } }
|
||||||
|
@keyframes a3_t { 0% { transform: translate(225px,198.9px) rotate(-90deg); } 10% { transform: translate(225px,198.9px) rotate(-90deg); } 20% { transform: translate(225px,198.9px) rotate(-90deg); animation-timing-function: cubic-bezier(.6,0,.4,1); } 30% { transform: translate(225px,198.9px) rotate(0deg); } 35% { transform: translate(225px,198.9px) rotate(0deg); } 45% { transform: translate(225px,198.9px) rotate(0deg); } 50% { transform: translate(225px,198.9px) rotate(0deg); animation-timing-function: cubic-bezier(.6,0,.4,1); } 60% { transform: translate(225px,198.9px) rotate(-90deg); } 65% { transform: translate(225px,198.9px) rotate(-90deg); } 75% { transform: translate(225px,198.9px) rotate(-90deg); } 100% { transform: translate(225px,198.9px) rotate(-90deg); } }
|
||||||
|
@keyframes a2_t { 0% { transform: translate(-200px,-167px); animation-timing-function: cubic-bezier(.6,0,.4,1); } 10% { transform: translate(-150px,-80px); } 20% { transform: translate(-150px,-80px); animation-timing-function: cubic-bezier(.6,0,.4,1); } 30% { transform: translate(-200px,-123px); } 35% { transform: translate(-200px,-123px); animation-timing-function: cubic-bezier(.6,0,.4,1); } 45% { transform: translate(-150px,-100px); } 50% { transform: translate(-150px,-100px); animation-timing-function: cubic-bezier(.6,0,.4,1); } 60% { transform: translate(-150px,-80px); } 65% { transform: translate(-150px,-80px); animation-timing-function: cubic-bezier(.6,0,.4,1); } 75% { transform: translate(-217px,-200px); } 100% { transform: translate(-217px,-200px); } }
|
||||||
|
@keyframes a2_o { 0% { opacity: 0; animation-timing-function: cubic-bezier(.6,0,.4,1); } 10% { opacity: 1; } 65% { opacity: 1; animation-timing-function: cubic-bezier(.6,0,.4,1); } 75% { opacity: 0; } 100% { opacity: 0; } }
|
||||||
|
@keyframes a2_sw { 0% { stroke-width: 30px; animation-timing-function: cubic-bezier(.6,0,.4,1); } 10% { stroke-width: 8px; } 65% { stroke-width: 8px; animation-timing-function: cubic-bezier(.6,0,.4,1); } 75% { stroke-width: 30px; } 100% { stroke-width: 30px; } }
|
||||||
|
@keyframes a2_w { 0% { width: 400px; animation-timing-function: cubic-bezier(.6,0,.4,1); } 10% { width: 300px; } 20% { width: 300px; animation-timing-function: cubic-bezier(.6,0,.4,1); } 30% { width: 400px; } 35% { width: 400px; animation-timing-function: cubic-bezier(.6,0,.4,1); } 45% { width: 300px; } 50% { width: 300px; } 60% { width: 300px; } 65% { width: 300px; animation-timing-function: cubic-bezier(.6,0,.4,1); } 75% { width: 434px; } 100% { width: 434px; } }
|
||||||
|
@keyframes a2_h { 0% { height: 334px; animation-timing-function: cubic-bezier(.6,0,.4,1); } 10% { height: 160px; } 20% { height: 160px; animation-timing-function: cubic-bezier(.6,0,.4,1); } 30% { height: 246px; } 35% { height: 246px; animation-timing-function: cubic-bezier(.6,0,.4,1); } 45% { height: 200px; } 50% { height: 200px; animation-timing-function: cubic-bezier(.6,0,.4,1); } 60% { height: 160px; } 65% { height: 160px; animation-timing-function: cubic-bezier(.6,0,.4,1); } 75% { height: 400px; } 100% { height: 400px; } }
|
||||||
|
@keyframes a4_t { 0% { transform: translate(143.5px,48.9px); } 10% { transform: translate(180.5px,98.9px); } 100% { transform: translate(180.5px,98.9px); } }
|
||||||
|
@keyframes a4_w { 0% { width: 168.5px; } 10% { width: 94.5px; } 100% { width: 94.5px; } }
|
||||||
|
@keyframes a4_h { 0% { height: 302px; } 10% { height: 202px; } 100% { height: 202px; } }
|
||||||
|
@keyframes a5_t { 0% { transform: translate(217px,324.9px); animation-timing-function: cubic-bezier(.6,0,.4,1); } 10% { transform: translate(190px,324.9px); } 20% { transform: translate(190px,324.9px); } 30% { transform: translate(190px,324.9px); } 100% { transform: translate(190px,324.9px); } }
|
||||||
|
@keyframes a5_o { 0% { opacity: 0; animation-timing-function: cubic-bezier(.6,0,.4,1); } 10% { opacity: 1; } 20% { opacity: 1; } 30% { opacity: 0; } 100% { opacity: 0; } }
|
||||||
|
@keyframes a5_w { 0% { width: 16px; animation-timing-function: cubic-bezier(.6,0,.4,1); } 10% { width: 70px; } 20% { width: 70px; } 30% { width: 70px; } 100% { width: 70px; } }
|
||||||
|
@keyframes a6_t { 0% { transform: translate(217px,324.9px); } 50% { transform: translate(217px,324.9px); animation-timing-function: cubic-bezier(.6,0,.4,1); } 60% { transform: translate(190px,324.9px); } 65% { transform: translate(190px,324.9px); } 75% { transform: translate(190px,324.9px); } 100% { transform: translate(190px,324.9px); } }
|
||||||
|
@keyframes a6_o { 0% { opacity: 0; } 50% { opacity: 0; animation-timing-function: cubic-bezier(.6,0,.4,1); } 60% { opacity: 1; } 65% { opacity: 1; animation-timing-function: cubic-bezier(.6,0,.4,1); } 75% { opacity: 0; } 100% { opacity: 0; } }
|
||||||
|
@keyframes a6_w { 0% { width: 16px; } 50% { width: 16px; animation-timing-function: cubic-bezier(.6,0,.4,1); } 60% { width: 70px; } 65% { width: 70px; } 75% { width: 70px; } 100% { width: 70px; } }
|
||||||
|
@keyframes a7_t { 0% { transform: translate(225px,198.9px) scale(1.5,1.5) translate(-42.3px,-93px); animation-timing-function: cubic-bezier(.6,0,.4,1); } 10% { transform: translate(225px,198.9px) scale(1,1) translate(-42.3px,-93px); } 20% { transform: translate(225px,198.9px) scale(1,1) translate(-42.3px,-93px); animation-timing-function: cubic-bezier(.6,0,.4,1); } 25% { transform: translate(225px,198.9px) scale(.8,.8) translate(-42.3px,-93px); animation-timing-function: cubic-bezier(.6,0,.4,1); } 30% { transform: translate(225px,198.9px) scale(1,1) translate(-42.3px,-93px); } 35% { transform: translate(225px,198.9px) scale(1,1) translate(-42.3px,-93px); animation-timing-function: cubic-bezier(.4,0,.6,1); } 45% { transform: translate(225px,198.9px) scale(.8,.8) translate(-42.3px,-93px); } 50% { transform: translate(225px,198.9px) scale(.8,.8) translate(-42.3px,-93px); animation-timing-function: cubic-bezier(.4,0,.6,1); } 55% { transform: translate(225px,198.9px) scale(.7,.7) translate(-42.3px,-93px); animation-timing-function: cubic-bezier(.4,0,.6,1); } 60% { transform: translate(225px,198.9px) scale(1,1) translate(-42.3px,-93px); } 65% { transform: translate(225px,198.9px) scale(1,1) translate(-42.3px,-93px); animation-timing-function: cubic-bezier(.4,0,.6,1); } 75% { transform: translate(225px,198.9px) scale(1.5,1.5) translate(-42.3px,-93px); } 100% { transform: translate(225px,198.9px) scale(1.5,1.5) translate(-42.3px,-93px); } }
|
||||||
|
</style><defs><symbol id="Symbol-2" preserveAspectRatio="none" width="111.5" height="186" viewBox="0 0 111.5 186" overflow="visible"><g transform="translate(8.5,4.5)"><rect x="4" width="85" height="8" fill="#d9d9d9"/><rect x="4" y="26" width="85" height="8" fill="#d9d9d9"/><rect x="25" y="56" width="43" height="8" fill="#d9d9d9"/><rect width="85" height="8" fill="#d9d9d9" transform="translate(46.5,175) rotate(180) scale(-1,1) translate(-42.5,-4)"/><rect width="85" height="8" fill="#d9d9d9" transform="translate(46.5,149) rotate(180) scale(-1,1) translate(-42.5,-4)"/><rect width="43" height="8" fill="#d9d9d9" transform="translate(46.5,119) rotate(180) scale(-1,1) translate(-21.5,-4)"/><path d="M0 4c0 53 94.5 115 94.5 169" stroke="#3cc4dc" stroke-width="17" stroke-linecap="square" fill="none"/><path d="M94.5 4c0 53-94.5 115-94.5 169" stroke="#fb422d" stroke-width="17" stroke-linecap="square" fill="none"/></g></symbol><symbol id="Symbol-5" preserveAspectRatio="none" width="84.5" height="186" viewBox="0 0 84.5 186" overflow="visible"><g transform="translate(42.3,93) scale(5.352,6.9) translate(-16,60)"><g transform="translate(-34.9,-125.8)"><g transform="translate(24,5)"><path d="M33 46.5c0 3.1-0.8 5.5-2.2 7.4c-1.4 1.9-3.3 3.6-5.4 5.4c-2.2 1.7-4.5 3.6-6.3 6.2c-1.8 2.5-3.1 5.5-3.1 9.5h4.7c0-3.1 .9-5.4 2.2-7.3c1.4-1.9 3.3-3.5 5.5-5.3c2.1-1.8 4.4-3.7 6.2-6.2c1.8-2.6 3.1-5.6 3.1-9.7Z" fill="#e96020"/><path d="M20.4 70.7c-0.6 0-1 .5-1 1.1c0 .7 .4 1.2 1 1.2h12.9c.7 0 1.2-0.5 1.2-1.2c0-0.6-0.5-1.1-1.2-1.1Z" fill="#2d323b"/><path d="M21.8 66.6c-0.7 0-1 .6-1 1.2c0 .6 .3 1 .9 1h10.3c.6 0 1.1-0.5 1.1-1.1c0-0.6-0.5-1.1-1.1-1.1Z" fill="#2d323b"/><path d="M21.8 53c-0.7 0-1.2 .5-1.1 1.1c0 .6 .4 1.1 1.1 1.1h10.2c.6 0 .9-0.6 .9-1.2c0-0.6-0.3-1-0.9-1Z" fill="#2d323b"/><path d="M20.4 48.8c-0.6 0-1.2 .5-1.2 1.1c0 .7 .6 1.2 1.2 1.2h12.9c.7 0 1-0.5 1-1.1c0-0.7-0.3-1.2-1-1.2Z" fill="#2d323b"/><path d="M16 46.5c0 4.1 1.3 7.1 3.1 9.7c1.8 2.5 4.1 4.4 6.3 6.2c2.1 1.8 4 3.6 5.4 5.5c1.4 1.9 2.2 4 2.2 7.1h4.7c0-4-1.3-7.1-3.1-9.6c-1.8-2.5-4.1-4.4-6.2-6.2c-2.2-1.8-4.1-3.4-5.5-5.3c-1.3-1.9-2.2-4.3-2.2-7.4Z" fill="#00a8d6"/></g></g></g></symbol></defs><rect width="1007" height="197" stroke="#0f1116" fill="#0f1116" stroke-width="0"/><path fill="#fff" d="M83.3 106.1v-65.7h17c4.2 0 7.9 .8 11 2.4c3.1 1.6 5.5 3.9 7.2 6.9c1.7 2.9 2.6 6.4 2.6 10.4v26.2c0 4-0.9 7.5-2.6 10.5c-1.7 3-4.1 5.3-7.2 6.9c-3.1 1.6-6.8 2.4-11 2.4Zm8.1-7.2h8.9c4 0 7.1-1.1 9.3-3.3c2.3-2.3 3.4-5.3 3.4-9.3v-26.2c0-3.9-1.1-7-3.4-9.2c-2.2-2.2-5.3-3.3-9.3-3.3h-8.9Zm45.3 7.2v-7.4h17.5v-34.7h-15.7v-7.4h23.8v42.1h16.7v7.4Zm20.7-58.4c-2 0-3.6-0.5-4.7-1.5c-1.2-1.1-1.7-2.4-1.7-4.2c0-1.8 .5-3.2 1.7-4.3c1.1-1 2.7-1.5 4.7-1.5c1.9 0 3.5 .5 4.6 1.5c1.2 1.1 1.7 2.5 1.7 4.3c0 1.8-0.5 3.1-1.7 4.2c-1.1 1-2.7 1.5-4.6 1.5Zm52.6 59.1c-6 0-10.7-1.7-14.2-5.1c-3.4-3.4-5.1-8-5.1-13.8v-13.1c0-5.9 1.7-10.5 5.1-13.9c3.5-3.3 8.2-5 14.2-5c6 0 10.7 1.7 14.2 5c3.4 3.4 5.2 8 5.2 13.8v13.2c0 5.8-1.8 10.4-5.2 13.8c-3.5 3.4-8.2 5.1-14.2 5.1Zm0-7.2c3.5 0 6.3-1 8.3-3c2-1.9 3-4.8 3-8.7v-13.1c0-3.9-1-6.8-3-8.8c-2-1.9-4.8-2.9-8.3-2.9c-3.5 0-6.2 1-8.2 2.9c-2 2-3 4.9-3 8.8v13.1c0 3.9 1 6.8 3 8.7c2 2 4.7 3 8.2 3Zm31.5 6.5l17.6-25.5l-16.5-24h9.5l9.8 15.3c.5 .7 .9 1.4 1.2 2.2c.4 .7 .8 1.4 1 1.8c.2-0.4 .5-1.1 .8-1.8c.4-0.8 .8-1.5 1.3-2.2l9.9-15.3h9.4l-16.5 24.1l17.5 25.4h-9.5l-10.7-16.2c-0.4-0.7-0.8-1.4-1.2-2.3c-0.4-0.9-0.7-1.6-1-2.1c-0.2 .5-0.6 1.2-1 2.1c-0.5 .9-1 1.6-1.4 2.3l-10.7 16.2Zm76.4 .9c-5.7 0-10.2-1.7-13.7-5.1c-3.4-3.4-5.1-8-5.1-13.8v-31.5h8.1v31.5c0 3.7 1 6.6 2.9 8.7c1.9 2.1 4.5 3.1 7.8 3.1c3.4 0 6-1 8-3.1c1.9-2.1 2.9-5 2.9-8.7v-31.5h8.1v31.5c0 5.8-1.7 10.4-5.2 13.8c-3.5 3.4-8.1 5.1-13.8 5.1Zm52.6-0.2c-5.1 0-9.3-1.2-12.5-3.5c-3.3-2.3-4.9-5.5-4.9-9.6h8.3c0 1.8 .9 3.3 2.6 4.3c1.8 1.1 4 1.6 6.6 1.6h3.8c3.2 0 5.6-0.6 7.1-1.9c1.6-1.3 2.4-3 2.4-5.2c0-2.1-0.8-3.8-2.2-5c-1.5-1.2-3.6-2-6.4-2.5l-6.6-1c-5-0.9-8.6-2.4-10.9-4.7c-2.4-2.3-3.5-5.5-3.5-9.5c0-4.5 1.4-7.9 4.3-10.3c2.9-2.4 7.1-3.6 12.6-3.6h3.4c5 0 9.1 1.1 12.1 3.4c3 2.2 4.6 5.2 4.6 8.9h-8.3c0-1.6-0.8-2.8-2.3-3.7c-1.5-1-3.6-1.4-6.3-1.4h-3.4c-2.8 0-5 .6-6.5 1.7c-1.5 1.2-2.3 2.9-2.3 5c0 3.6 2.5 5.8 7.6 6.7l6.7 1.1c5.3 .8 9.2 2.4 11.7 4.6c2.4 2.3 3.6 5.6 3.6 9.9c0 4.6-1.5 8.2-4.4 10.8c-2.9 2.6-7.3 3.9-13.2 3.9Z"/><path fill="#fff" d="M91 164c-2 0-3.6-0.6-4.7-1.7c-1.1-1.1-1.6-2.7-1.6-4.6v-15.9h2.7v15.9c0 1.2 .3 2.2 .9 2.8c.6 .7 1.5 1.1 2.7 1.1c1.2 0 2.1-0.4 2.7-1.1c.6-0.7 .9-1.6 .9-2.8v-15.9h2.7v15.9c0 2-0.5 3.5-1.6 4.6c-1.1 1.1-2.6 1.7-4.7 1.7Zm17.5-0.1c-1.7 0-3.1-0.3-4.2-1.1c-1.1-0.8-1.6-1.9-1.6-3.2h2.8c0 .6 .3 1.1 .8 1.4c.6 .4 1.4 .5 2.2 .5h1.3c1.1 0 1.9-0.2 2.4-0.6c.5-0.4 .8-1 .8-1.7c0-0.7-0.3-1.3-0.8-1.7c-0.5-0.4-1.2-0.7-2.1-0.8l-2.2-0.4c-1.7-0.3-2.9-0.8-3.6-1.5c-0.8-0.8-1.2-1.9-1.2-3.2c0-1.5 .5-2.6 1.4-3.4c1-0.8 2.4-1.2 4.2-1.2h1.2c1.6 0 3 .3 4 1.1c1 .7 1.5 1.7 1.5 3h-2.7c0-0.6-0.3-1-0.8-1.3c-0.5-0.3-1.2-0.4-2.1-0.4h-1.1c-1 0-1.7 .2-2.2 .5c-0.5 .4-0.8 1-0.8 1.7c0 1.2 .9 1.9 2.6 2.2l2.2 .4c1.8 .3 3.1 .8 3.9 1.5c.8 .8 1.2 1.9 1.2 3.3c0 1.6-0.5 2.8-1.5 3.6c-0.9 .9-2.4 1.3-Line truncated
|
||||||
|
After Width: | Height: | Size: 23 KiB |
@@ -0,0 +1,7 @@
|
|||||||
|
body {
|
||||||
|
background-color: #0f1116;
|
||||||
|
color: #ffffff;
|
||||||
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||||
|
margin: 0px;
|
||||||
|
padding: 0px
|
||||||
|
}
|
||||||
+125101
File diff suppressed because it is too large.
Load diff
|
After Width: | Height: | Size: 10 MiB |
@@ -0,0 +1,8 @@
|
|||||||
|
await-holding-invalid-types = [
|
||||||
|
"generational_box::GenerationalRef",
|
||||||
|
{ path = "generational_box::GenerationalRef", reason = "Reads should not be held over an await point. This will cause any writes to fail while the await is pending since the read borrow is still active." },
|
||||||
|
"generational_box::GenerationalRefMut",
|
||||||
|
{ path = "generational_box::GenerationalRefMut", reason = "Write should not be held over an await point. This will cause any reads or writes to fail while the await is pending since the write borrow is still active." },
|
||||||
|
"dioxus_signals::WriteLock",
|
||||||
|
{ path = "dioxus_signals::WriteLock", reason = "Write should not be held over an await point. This will cause any reads or writes to fail while the await is pending since the write borrow is still active." },
|
||||||
|
]
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
mod toolbox;
|
||||||
|
pub use toolbox::ToolBox;
|
||||||
|
|
||||||
|
mod elements;
|
||||||
|
pub use elements::*;
|
||||||
|
|
||||||
|
mod viewport;
|
||||||
|
pub use viewport::*;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
mod wt_button;
|
||||||
|
|
||||||
|
pub use wt_button::WTButton;
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
pub fn WTButton(children: Element) -> Element {
|
||||||
|
rsx! {
|
||||||
|
button { class: "px-4 py-2 bg-nord-red-400 hover:bg-nord-red-300 active:bg-nord-red-200 inline",
|
||||||
|
{children}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
mod connection_status;
|
||||||
|
|
||||||
|
use crate::components::WTButton;
|
||||||
|
pub use connection_status::ConStatus;
|
||||||
|
|
||||||
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
pub fn ToolBox() -> Element {
|
||||||
|
rsx! {
|
||||||
|
div { class: "w-full sm:w-sm sm:h-dvh border-t-2 sm:border-t-0 sm:border-l-2 bg-nord-bg-700 overflow-scroll",
|
||||||
|
div { class: "visi sm:block border-b-2",
|
||||||
|
div { class: "m-4",
|
||||||
|
div { class: "text-2xl font-semibold leading-[1.20em]", "Team Webtoid" }
|
||||||
|
div { class: "pl-1 text-sm font-light text-nord-bg-200 mb-1.5",
|
||||||
|
"Yeah, we like rust..."
|
||||||
|
}
|
||||||
|
ConStatus { connected: false }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
div { class: "m-4",
|
||||||
|
WTButton { "Stop" }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
use dioxus::prelude::*;
|
||||||
|
use dioxus_free_icons::icons::bs_icons::BsBroadcastPin;
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
pub fn ConStatus(connected: Option<bool>) -> Element {
|
||||||
|
let connected = connected.unwrap_or(false);
|
||||||
|
|
||||||
|
rsx! {
|
||||||
|
div {
|
||||||
|
div { class: "flex justify-start items-center",
|
||||||
|
if connected {
|
||||||
|
dioxus_free_icons::Icon {
|
||||||
|
width: 30,
|
||||||
|
height: 30,
|
||||||
|
icon: BsBroadcastPin,
|
||||||
|
class: "mx-[0.2em] inline p-1 text-green-300",
|
||||||
|
}
|
||||||
|
"Robot connected"
|
||||||
|
} else {
|
||||||
|
dioxus_free_icons::Icon {
|
||||||
|
width: 30,
|
||||||
|
height: 30,
|
||||||
|
icon: BsBroadcastPin,
|
||||||
|
class: "mx-[0.2em] inline p-1 text-nord-yellow-300",
|
||||||
|
}
|
||||||
|
"Robot disconnected"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,182 @@
|
|||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
mod viewport_context;
|
||||||
|
use viewport_context::{DragTarget, ViewportContext};
|
||||||
|
|
||||||
|
mod render;
|
||||||
|
use render::render_loop;
|
||||||
|
|
||||||
|
mod util;
|
||||||
|
use util::create_image_bitmap;
|
||||||
|
|
||||||
|
use dioxus::{html::input_data::MouseButton, prelude::*};
|
||||||
|
use tokio::sync::Notify;
|
||||||
|
use wasm_bindgen::JsCast;
|
||||||
|
use web_sys::{CanvasRenderingContext2d as Ctx, HtmlCanvasElement, ImageBitmap};
|
||||||
|
|
||||||
|
static W: f64 = 3000.0;
|
||||||
|
static H: f64 = 2000.0;
|
||||||
|
/// Padding*2
|
||||||
|
static PADDING: f64 = 40.0;
|
||||||
|
static WHEEL_RATIO: f64 = 1.03;
|
||||||
|
|
||||||
|
static TABLE_SVG: Asset = asset!("/assets/table.svg");
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
pub fn Viewport() -> Element {
|
||||||
|
let redraw = use_hook(|| Rc::new(Notify::new()));
|
||||||
|
let mut canvas = use_signal(Option::<(HtmlCanvasElement, Ctx)>::default);
|
||||||
|
let mut map_state = use_signal(ViewportContext::default);
|
||||||
|
let r = redraw.clone();
|
||||||
|
use_future(move || {
|
||||||
|
let redraw = r.clone();
|
||||||
|
render_loop(canvas.clone(), map_state.clone(), redraw)
|
||||||
|
});
|
||||||
|
|
||||||
|
// Load both higher res and lower res bitmap of terrain for use with different zoom widths
|
||||||
|
let table_image_low: Resource<Option<ImageBitmap>> =
|
||||||
|
use_resource(|| async { create_image_bitmap(1200, 800).await });
|
||||||
|
let table_image_high: Resource<Option<ImageBitmap>> =
|
||||||
|
use_resource(|| async { create_image_bitmap(3000, 2000).await });
|
||||||
|
|
||||||
|
let r = redraw.clone();
|
||||||
|
use_effect(move || {
|
||||||
|
map_state.write().bg_low = table_image_low.read().clone().flatten();
|
||||||
|
r.notify_one();
|
||||||
|
});
|
||||||
|
let r = redraw.clone();
|
||||||
|
use_effect(move || {
|
||||||
|
map_state.write().bg_high = table_image_high.read().clone().flatten();
|
||||||
|
r.notify_one();
|
||||||
|
});
|
||||||
|
|
||||||
|
// On mount get the 2d canvas context and the set the initial MapState
|
||||||
|
let r = redraw.clone();
|
||||||
|
let onmount = move |e: Event<MountedData>| {
|
||||||
|
let Some(el) = e.data().downcast::<web_sys::Element>().cloned() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let Ok(c): Result<HtmlCanvasElement, _> = el.dyn_into() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let Some(ctx) = c
|
||||||
|
.get_context("2d")
|
||||||
|
.ok()
|
||||||
|
.and_then(|c| c?.dyn_into::<Ctx>().ok())
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let map_state = &mut map_state.write();
|
||||||
|
let (x, y) = map_state.get_defualt_offset();
|
||||||
|
|
||||||
|
map_state.size = (c.client_width() as f64, c.client_height() as f64);
|
||||||
|
map_state.mat.offset_x = x;
|
||||||
|
map_state.mat.offset_y = y;
|
||||||
|
|
||||||
|
canvas.set(Some((c, ctx)));
|
||||||
|
|
||||||
|
r.notify_one();
|
||||||
|
};
|
||||||
|
|
||||||
|
// Reset the canvas to the default transform
|
||||||
|
let r = redraw.clone();
|
||||||
|
let onresize = move |_| {
|
||||||
|
if let Some((el, _)) = canvas.read().as_ref() {
|
||||||
|
let Some(dpr) = web_sys::window().map(|win| win.device_pixel_ratio()) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let map_state = &mut map_state.write();
|
||||||
|
|
||||||
|
let w = el.client_width() as f64 * dpr;
|
||||||
|
let h = el.client_height() as f64 * dpr;
|
||||||
|
|
||||||
|
map_state.size = (w, h);
|
||||||
|
|
||||||
|
// TODO: properly handle changed scale
|
||||||
|
let (x, y) = map_state.get_defualt_offset();
|
||||||
|
map_state.mat.offset_x = x;
|
||||||
|
map_state.mat.offset_y = y;
|
||||||
|
|
||||||
|
r.notify_one();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let r = redraw.clone();
|
||||||
|
let onwheel = move |e: WheelEvent| {
|
||||||
|
let map_state = &mut map_state.write();
|
||||||
|
|
||||||
|
e.prevent_default();
|
||||||
|
let delta_y = e.data().delta().strip_units().y;
|
||||||
|
let (mouse_x, mouse_y) = e.page_coordinates().to_tuple();
|
||||||
|
|
||||||
|
let ratio = WHEEL_RATIO.powf(-delta_y);
|
||||||
|
|
||||||
|
if (ratio > 1.0) && map_state.mat.scale > 2.0 {
|
||||||
|
return;
|
||||||
|
} else if (ratio < 1.0) && map_state.mat.scale < 0.5 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
map_state.mat.offset_x = mouse_x + (map_state.mat.offset_x - mouse_x) * ratio;
|
||||||
|
map_state.mat.offset_y = mouse_y + (map_state.mat.offset_y - mouse_y) * ratio;
|
||||||
|
|
||||||
|
map_state.mat.scale *= ratio;
|
||||||
|
r.notify_one();
|
||||||
|
};
|
||||||
|
|
||||||
|
let onmousedown = move |e: MouseEvent| {
|
||||||
|
if e.trigger_button() == Some(MouseButton::Primary) {
|
||||||
|
let map_state = &mut map_state.write();
|
||||||
|
let (x, y) = e.element_coordinates().to_tuple();
|
||||||
|
map_state.prev_coords = (x, y);
|
||||||
|
map_state.drag_target = Some(DragTarget::Map);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let onmouseup = move |e: MouseEvent| {
|
||||||
|
if e.trigger_button() == Some(MouseButton::Primary) {
|
||||||
|
let map_state = &mut map_state.write();
|
||||||
|
map_state.drag_target = None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let onmouseleave = move |_| {
|
||||||
|
let map_state = &mut map_state.write();
|
||||||
|
map_state.drag_target = None;
|
||||||
|
};
|
||||||
|
|
||||||
|
let r = redraw.clone();
|
||||||
|
let onmousemove = move |e: MouseEvent| {
|
||||||
|
let map_state = &mut map_state.write();
|
||||||
|
if let Some(target) = map_state.drag_target {
|
||||||
|
let (x, y) = e.element_coordinates().to_tuple();
|
||||||
|
let (x0, y0) = map_state.prev_coords;
|
||||||
|
map_state.prev_coords = (x, y);
|
||||||
|
match target {
|
||||||
|
DragTarget::Map => {
|
||||||
|
map_state.mat.offset_x += x - x0;
|
||||||
|
map_state.mat.offset_y += y - y0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
r.notify_one();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
rsx! {
|
||||||
|
div { class: "relative w-full h-full min-w-0 min-h-0",
|
||||||
|
canvas {
|
||||||
|
class: "absolute inset-0 block w-full h-full",
|
||||||
|
onmount,
|
||||||
|
onresize,
|
||||||
|
onwheel,
|
||||||
|
onmousedown,
|
||||||
|
onmouseup,
|
||||||
|
onmouseleave,
|
||||||
|
onmousemove,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use super::ViewportContext;
|
||||||
|
use super::{H, W};
|
||||||
|
|
||||||
|
use dioxus::prelude::*;
|
||||||
|
use tokio::sync::Notify;
|
||||||
|
use web_sys::{CanvasRenderingContext2d as Ctx, HtmlCanvasElement};
|
||||||
|
|
||||||
|
use super::util::on_frame_update;
|
||||||
|
|
||||||
|
pub async fn render_loop(
|
||||||
|
canvas: Signal<Option<(HtmlCanvasElement, Ctx)>>,
|
||||||
|
map_state: Signal<ViewportContext>,
|
||||||
|
redraw: Rc<Notify>,
|
||||||
|
) {
|
||||||
|
loop {
|
||||||
|
redraw.notified().await;
|
||||||
|
on_frame_update(move |_| {
|
||||||
|
let canvas = canvas.peek();
|
||||||
|
let Some((el, ctx)) = canvas.as_ref() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let map_state = map_state.peek();
|
||||||
|
let (w, h) = map_state.size;
|
||||||
|
if el.width() != w as u32 || el.height() != h as u32 {
|
||||||
|
el.set_width(w as u32);
|
||||||
|
el.set_height(h as u32);
|
||||||
|
}
|
||||||
|
ctx.clear_rect(0.0, 0.0, w, h);
|
||||||
|
|
||||||
|
draw(&ctx, &map_state);
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw(ctx: &Ctx, state: &ViewportContext) {
|
||||||
|
ctx.save();
|
||||||
|
_ = ctx.transform(
|
||||||
|
state.get_scale(),
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
state.get_scale(),
|
||||||
|
state.mat.offset_x,
|
||||||
|
state.mat.offset_y,
|
||||||
|
);
|
||||||
|
|
||||||
|
let img = if state.get_scale() > 0.43 {
|
||||||
|
&state.bg_high
|
||||||
|
} else {
|
||||||
|
&state.bg_low
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(img) = img.as_ref() {
|
||||||
|
_ = ctx.draw_image_with_image_bitmap_and_dw_and_dh(img, 0.0, 0.0, W, H);
|
||||||
|
};
|
||||||
|
ctx.restore();
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use tokio::sync::Notify;
|
||||||
|
use web_sys::js_sys;
|
||||||
|
use web_sys::{HtmlImageElement, ImageBitmap, ImageBitmapOptions, ResizeQuality};
|
||||||
|
|
||||||
|
use super::TABLE_SVG;
|
||||||
|
|
||||||
|
pub async fn on_frame_update<F>(func: F)
|
||||||
|
where
|
||||||
|
F: FnOnce(f64) + 'static,
|
||||||
|
{
|
||||||
|
let rx = Rc::new(Notify::new());
|
||||||
|
let tx = rx.clone();
|
||||||
|
let _frame = gloo_render::request_animation_frame(move |n| {
|
||||||
|
func(n);
|
||||||
|
tx.notify_one();
|
||||||
|
});
|
||||||
|
rx.notified().await;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_image_bitmap(width: u32, height: u32) -> Option<ImageBitmap> {
|
||||||
|
let img = HtmlImageElement::new().ok()?;
|
||||||
|
img.set_src(&TABLE_SVG.to_string());
|
||||||
|
|
||||||
|
js_sys::futures::JsFuture::from(img.decode()).await.ok()?;
|
||||||
|
let opts = ImageBitmapOptions::new();
|
||||||
|
opts.set_resize_width(width);
|
||||||
|
opts.set_resize_height(height);
|
||||||
|
opts.set_resize_quality(ResizeQuality::High);
|
||||||
|
let p = web_sys::window()?
|
||||||
|
.create_image_bitmap_with_html_image_element_and_image_bitmap_options(&img, &opts)
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
Some(js_sys::futures::JsFuture::from(p).await.ok()?.into())
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
use super::H;
|
||||||
|
use super::PADDING;
|
||||||
|
use super::W;
|
||||||
|
|
||||||
|
use web_sys::ImageBitmap;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Transform {
|
||||||
|
pub offset_x: f64,
|
||||||
|
pub offset_y: f64,
|
||||||
|
pub scale: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub enum DragTarget {
|
||||||
|
Map,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Clone)]
|
||||||
|
pub struct ViewportContext {
|
||||||
|
pub mat: Transform,
|
||||||
|
pub size: (f64, f64),
|
||||||
|
pub prev_coords: (f64, f64),
|
||||||
|
pub bg_low: Option<ImageBitmap>,
|
||||||
|
pub bg_high: Option<ImageBitmap>,
|
||||||
|
pub drag_target: Option<DragTarget>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Transform {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
offset_x: 0.0,
|
||||||
|
offset_y: 0.0,
|
||||||
|
scale: 1.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ViewportContext {
|
||||||
|
pub fn get_scale(&self) -> f64 {
|
||||||
|
let (size_x, size_y) = self.size;
|
||||||
|
f64::min(
|
||||||
|
f64::max(size_x - PADDING, 0.1) / W,
|
||||||
|
f64::max(size_y - PADDING, 0.1) / H,
|
||||||
|
) * self.mat.scale
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_defualt_offset(&self) -> (f64, f64) {
|
||||||
|
let (width, height) = self.size;
|
||||||
|
let pad_x = f64::max((width - W * self.get_scale()) / 2.0, PADDING / 2.0);
|
||||||
|
let pad_y = f64::max((height - H * self.get_scale()) / 2.0, PADDING / 2.0);
|
||||||
|
(pad_x, pad_y)
|
||||||
|
}
|
||||||
|
}
|
||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
pub struct Pose {
|
||||||
|
pub x: f32,
|
||||||
|
pub y: f32,
|
||||||
|
pub theta: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct RobotState {
|
||||||
|
pub position: Pose,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Pose {
|
||||||
|
fn default() -> Self {
|
||||||
|
Pose {
|
||||||
|
x: 0.0,
|
||||||
|
y: 0.0,
|
||||||
|
theta: 0.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for RobotState {
|
||||||
|
fn default() -> Self {
|
||||||
|
RobotState {
|
||||||
|
position: Pose::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
|
use views::DefaultView;
|
||||||
|
|
||||||
|
mod components;
|
||||||
|
mod game;
|
||||||
|
mod views;
|
||||||
|
|
||||||
|
const FAVICON: Asset = asset!("/assets/favicon.ico");
|
||||||
|
const MAIN_CSS: Asset = asset!("/assets/styling/main.css");
|
||||||
|
const TAILWIND_CSS: Asset = asset!("/assets/tailwind.css");
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
dioxus::launch(App);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
fn App() -> Element {
|
||||||
|
let mut is_mounted = use_signal(|| true);
|
||||||
|
|
||||||
|
use_effect(move || is_mounted.set(true));
|
||||||
|
|
||||||
|
rsx! {
|
||||||
|
document::Link { rel: "icon", href: FAVICON }
|
||||||
|
document::Style { href: MAIN_CSS }
|
||||||
|
document::Style { href: TAILWIND_CSS }
|
||||||
|
|
||||||
|
if is_mounted() {
|
||||||
|
DefaultView {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
mod default_view;
|
||||||
|
pub use default_view::DefaultView;
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
|
use crate::components::{ToolBox, Viewport};
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
pub fn DefaultView() -> Element {
|
||||||
|
rsx! {
|
||||||
|
div { class: "sm:h-dvh w-full flex flex-col sm:flex-row",
|
||||||
|
|
||||||
|
div { class: "h-[66.7dvw] sm:h-auto sm:grow bg-nord-bg-800 flex items-stretch justify-stretch overflow-hidden",
|
||||||
|
Viewport {}
|
||||||
|
}
|
||||||
|
|
||||||
|
ToolBox {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
@import "tailwindcss";
|
||||||
|
@source "./src/**/*.{rs,html,css}";
|
||||||
|
|
||||||
|
@theme {
|
||||||
|
--font-sans: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
|
||||||
|
--font-serif: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
|
||||||
|
|
||||||
|
--color-nord-red-50: #f5eff0;
|
||||||
|
--color-nord-red-100: #f7bac0;
|
||||||
|
--color-nord-red-200: #f28c96;
|
||||||
|
--color-nord-red-300: #ed5e6c;
|
||||||
|
--color-nord-red-400: #e83042;
|
||||||
|
--color-nord-red-500: #cf1729;
|
||||||
|
--color-nord-red-600: #a11220;
|
||||||
|
--color-nord-red-700: #730d17;
|
||||||
|
--color-nord-red-800: #45080e;
|
||||||
|
--color-nord-red-900: #170305;
|
||||||
|
|
||||||
|
--color-nord-yellow-50: #f5f3ef;
|
||||||
|
--color-nord-yellow-100: #f5e2bc;
|
||||||
|
--color-nord-yellow-200: #eecf90;
|
||||||
|
--color-nord-yellow-300: #e8bc64;
|
||||||
|
--color-nord-yellow-400: #e1a937;
|
||||||
|
--color-nord-yellow-500: #c88f1e;
|
||||||
|
--color-nord-yellow-600: #9b6f17;
|
||||||
|
--color-nord-yellow-700: #6f4f11;
|
||||||
|
--color-nord-yellow-800: #43300a;
|
||||||
|
--color-nord-yellow-900: #161003;
|
||||||
|
|
||||||
|
--color-nord-bg-50: #f1f2f3;
|
||||||
|
--color-nord-bg-100: #cfd6e3;
|
||||||
|
--color-nord-bg-200: #afbad0;
|
||||||
|
--color-nord-bg-300: #8f9fbd;
|
||||||
|
--color-nord-bg-400: #6e83aa;
|
||||||
|
--color-nord-bg-500: #556a91;
|
||||||
|
--color-nord-bg-600: #425270;
|
||||||
|
--color-nord-bg-700: #2f3b50;
|
||||||
|
--color-nord-bg-800: #1c2330;
|
||||||
|
--color-nord-bg-900: #090c10;
|
||||||
|
|
||||||
|
--color-primary-50: #f6f8f9;
|
||||||
|
--color-primary-100: #edf1f3;
|
||||||
|
--color-primary-200: #d4dfe2;
|
||||||
|
--color-primary-300: #b3ced6;
|
||||||
|
--color-primary-400: #83bdce;
|
||||||
|
--color-primary-500: #5aa8be;
|
||||||
|
--color-primary-600: #418ea5;
|
||||||
|
--color-primary-700: #357587;
|
||||||
|
--color-primary-800: #275563;
|
||||||
|
--color-primary-900: #203d46;
|
||||||
|
--color-primary-950: #13252a;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user