Files
storyteller-bevy/studio/src/viewport.rs
Danny McGee c788bcf6b7 Refactor Studio entry-points (#54)
This PR heavily refactors the entry-point functions in
`studio/src/lib.rs` to simplify and consolidate. Additionally, the
frontend application should now be configurable as expected with any
valid combination of the following URL parameters:

* `objectId` : A pre-bundled library object to load (e.g., "couch.gltf")
* `bvh` : Any resolvable path to a BVH animation with a MocapNET
skeleton (e.g., "mocap/shuffle.bvh")
* `mixamo` : Any resolvable path to a glTF animation with the Mixamo
skeleton (e.g., "mocap/hip-hop-dancing.gltf")
* `sceneImport` : Any resolvable path to an arbitrary glTF scene
(untested, but should theoretically work)
* `scene` : Any resolvable path to a previously saved Storyteller Studio
scene in `*.scn.ron` format
* `skybox` : The ID of a pre-bundled skybox asset. Valid options are:
  - "gum_trees_4k"
  - "kloofenfal_28d_misty_4k"
  - "meadow_4k"
  - "promenade_de_vidy_4k"
  - "scythian_tombs_4k"
  - "test_scene" (this is the default if not specified)
* `mode` : One of "studio" or "viewer" (the default if not specified)

"Validity" rules are as follows:

* `objectId`, `bvh`, `mixamo`, `sceneImport` and `scene` are mutually
exclusive
* `skybox` is not supported for `scene` (the skybox should be saved and
loaded as part of the scene description, though this is not currently
working)
* Both `mode` options are valid for all other parameters
2024-02-27 10:22:00 -07:00

94 lines
2.3 KiB
Rust

use core::fmt;
use bevy::window::WindowResolution;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[derive(Clone, Copy, Serialize, Deserialize)]
pub struct ViewportSize {
pub width: f32,
pub height: f32,
}
#[wasm_bindgen]
impl ViewportSize {
#[wasm_bindgen(constructor)]
pub fn new(width: f32, height: f32) -> Self {
Self { width, height }
}
}
impl Default for ViewportSize {
fn default() -> Self {
WindowResolution::default().into()
}
}
impl From<ViewportSize> for WindowResolution {
fn from(value: ViewportSize) -> Self {
Self::new(value.width, value.height)
}
}
impl From<WindowResolution> for ViewportSize {
fn from(value: WindowResolution) -> Self {
Self {
width: value.width(),
height: value.height(),
}
}
}
impl fmt::Debug for ViewportSize {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("({:.2}, {:.2})", self.width, self.height))
}
}
#[cfg(feature = "wasm")]
pub mod wasm {
use bevy::prelude::*;
use wasm_bindgen::prelude::*;
use crate::wasm::{StaticBuffer, WasmMessageBuffer};
use super::ViewportSize;
static RESIZE_BUF: WasmMessageBuffer<ViewportSize> = WasmMessageBuffer::new();
#[wasm_bindgen]
pub fn resize(viewport_size: ViewportSize) {
RESIZE_BUF.write_message(viewport_size);
}
pub struct ViewportPlugin;
impl Plugin for ViewportPlugin {
fn build(&self, app: &mut App) {
app.add_event::<ViewportResizeEvent>()
.add_systems(PreUpdate, dispatch_resize_events)
.add_systems(Update, sync_window_to_viewport);
}
}
#[derive(Event, Clone, Copy)]
struct ViewportResizeEvent(ViewportSize);
fn sync_window_to_viewport(
mut events: EventReader<ViewportResizeEvent>,
mut window: Query<&mut Window>,
) {
if let Some(event) = events.read().last() {
let ViewportSize { width, height } = event.0;
window.single_mut().resolution.set(width, height);
}
}
fn dispatch_resize_events(mut events: EventWriter<ViewportResizeEvent>) {
if let Some(resize) = RESIZE_BUF.take_message() {
events.send(ViewportResizeEvent(resize));
}
}
}