Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

visualizer: Fix camera controls in web contexts #215

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 7 additions & 43 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions visualizer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ edition = "2021"
[dependencies]
clap = { version = "4.4.11", features = ["derive"] }
ewebsock = "0.4.0"
gloo-net = "0.5.0"
itertools = "0.12.0"
reqwest = { version = "0.11.22", features = ["json", "blocking"] }
serde = { version = "1.0.164", features = ["derive"] }
serde_json = "1.0.108"
url = "2.5.0"
wasm-bindgen-futures = "0.4.39"

[dependencies.bevy]
version = "0.12.0"
Expand Down
15 changes: 2 additions & 13 deletions visualizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,8 @@ You can run the visualizer as a native binary with:
cargo run [--release] --bin rustmas-visualizer
```

Alternatively, you can run it in the web browser. First you will have to
install `wasm-server-runner`:

```
cargo install wasm-server-runner
```

and then you can start the visualizer with:

```
cd visualizer
cargo run [--release] --target wasm32-unknown-unknown --bin rustmas-visualizer
```
If you would like to run the visualizer in a web browser, the suggested way
is to [run is as a part of WebUI](../webapi/README.md#running-webui).

Visualizer needs to be started after WebAPI.

Expand Down
42 changes: 14 additions & 28 deletions visualizer/src/bin/visualizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,24 @@ struct GetPointsResponse {
points: Vec<(f32, f32, f32)>,
}

fn get_points_endpoint(endpoint: &Url) -> Url {
let mut endpoint = endpoint.clone();
endpoint.set_scheme("http").unwrap();
endpoint.join("points").unwrap()
fn get_points(endpoint: &Url) -> Vec<(f32, f32, f32)> {
let endpoint = {
let mut endpoint = endpoint.clone();
endpoint.set_scheme("http").unwrap();
endpoint.join("points").unwrap()
};

reqwest::blocking::get(endpoint)
.unwrap()
.json::<GetPointsResponse>()
.unwrap()
.points
}

fn main() {
let endpoint = Args::parse().endpoint;
let frames_endpoint = get_frames_url(&endpoint);
let points_endpoint = get_points_endpoint(&endpoint);

#[cfg(not(target_arch = "wasm32"))]
{
let points = reqwest::blocking::get(points_endpoint)
.unwrap()
.json::<GetPointsResponse>()
.unwrap()
.points;

rustmas_visualizer::run(frames_endpoint, points);
}
let points = get_points(&endpoint);

#[cfg(target_arch = "wasm32")]
wasm_bindgen_futures::spawn_local(async move {
let points = gloo_net::http::Request::get(points_endpoint.as_str())
.send()
.await
.unwrap()
.json::<GetPointsResponse>()
.await
.unwrap()
.points;
rustmas_visualizer::run(frames_endpoint, points);
});
rustmas_visualizer::run(frames_endpoint, points);
}
10 changes: 9 additions & 1 deletion webui/src/visualizer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use log::error;
use web_sys::MouseEvent;
use yew::{html, prelude::Html, Callback, Component, Context};

use crate::api;
Expand Down Expand Up @@ -53,7 +54,14 @@ impl Component for Visualizer {
fn view(&self, _ctx: &Context<Self>) -> Html {
html! {
<section class="visualizer-container">
<canvas id="visualizer"></canvas>
<canvas id="visualizer"
oncontextmenu={Callback::from(|e: MouseEvent| e.prevent_default())}
onmousedown={Callback::from(|e: MouseEvent| {
if e.button() == 1 {
e.prevent_default();
}
})}
></canvas>
</section>
}
}
Expand Down
Loading