mirror of
https://github.com/storytold/storyteller-bevy.git
synced 2026-10-18 07:35:19 +00:00
Scene Inspector (#11)
This PR adds the bones of a user-facing scene inspector. ### Work Done To facilitate this work, the Bevy/frontend interface has been expanded to accommodate two-way communications: - The Bevy app can now dispatch JavaScript `CustomEvent`s via `wasm::dispatch_to_js(...)` - The frontend application can receive those events by listening for the given event type on the `window` object In its current state, the inspection system works by attaching the `Inspectable` marker component to any entities which should be exposed to the user. A Bevy system runs on `Update` to check for newly spawned or despawned entities with that component, and emits events to notify the frontend. The frontend listens for those events and updates its model of the scene accordingly, which is then displayed with a simple tree view. ### Future Enhancements There are many improvements that must be made to make this system actually useful, but this PR is being expedited to focus on migrating to Bevy 0.12. A shortlist of planned updates: - Identify and expose the "type" of entity (e.g. lights, cameras, meshes, skeletons, etc.) via a field on the `Inspectable` component - Highlight the currently selected entity in the frontend hierarchy - Ask the Bevy app to update the selection when the user selects an entry in the hierarchy - Add a separate UI panel to inspect and modify the selected entity's components via the frontend
This commit is contained in:
@@ -15,6 +15,14 @@ impl Plugin for SelectionPlugin {
|
||||
PostUpdate,
|
||||
draw_selection_outline.run_if(resource_changed_or_removed::<Selection>()),
|
||||
);
|
||||
|
||||
#[cfg(feature = "wasm")]
|
||||
app.add_systems(
|
||||
PostUpdate,
|
||||
wasm::notify_entity_selection_change
|
||||
.pipe(error)
|
||||
.run_if(resource_changed_or_removed::<Selection>()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +32,7 @@ pub struct Selectable;
|
||||
#[derive(Component, Clone, Copy)]
|
||||
pub struct Selected;
|
||||
|
||||
#[derive(Resource, Deref, DerefMut)]
|
||||
#[derive(Resource, Deref, DerefMut, Debug)]
|
||||
pub struct Selection(Entity);
|
||||
|
||||
fn update_selection(
|
||||
@@ -105,3 +113,41 @@ fn draw_selection_outline(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "wasm")]
|
||||
mod wasm {
|
||||
use bevy::{
|
||||
prelude::{Query, Res, With},
|
||||
window::{PrimaryWindow, Window},
|
||||
};
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
use super::Selection;
|
||||
use crate::wasm;
|
||||
|
||||
#[wasm_bindgen(typescript_custom_section)]
|
||||
const EVENT_DECLARATION: &str = r#"
|
||||
/**
|
||||
* An event indicating that an entity has been selected or that the selection
|
||||
* has been cleared.
|
||||
*/
|
||||
export interface EntitySelectEvent extends CustomEvent {
|
||||
/** The selected entity ID. If `null`, the selection has been cleared. */
|
||||
detail: string | null;
|
||||
}
|
||||
|
||||
declare global {
|
||||
export interface HTMLElementEventMap {
|
||||
"entity-select": EntitySelectEvent;
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
pub(super) fn notify_entity_selection_change(
|
||||
r_selection: Option<Res<Selection>>,
|
||||
q_window: Query<&Window, With<PrimaryWindow>>,
|
||||
) -> Result<(), String> {
|
||||
let entity = r_selection.map(|selection| **selection);
|
||||
wasm::dispatch_to_js(q_window.single(), "entity-select", &entity)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ impl Plugin for TransformationPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.insert_resource(TransformMode::default());
|
||||
app.add_plugins(GizmoPlugin);
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
#[cfg(feature = "wasm")]
|
||||
app.add_systems(PreUpdate, wasm::update_transform_mode);
|
||||
}
|
||||
}
|
||||
@@ -83,7 +83,7 @@ impl fmt::Debug for TransformType {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
#[cfg(feature = "wasm")]
|
||||
mod wasm {
|
||||
use bevy::prelude::*;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
Reference in New Issue
Block a user