From 748aa70bcf663eda0d34c94ab6e3fc406b97af9f Mon Sep 17 00:00:00 2001 From: rewin Date: Tue, 27 Feb 2024 21:24:26 +0300 Subject: [PATCH] add worked event update --- studio-frontend/src/inspector_dummy.ts | 12 ++ studio-frontend/src/main.ts | 2 + studio/src/ui/inspector.rs | 149 +++++++++++++++++-------- 3 files changed, 117 insertions(+), 46 deletions(-) create mode 100644 studio-frontend/src/inspector_dummy.ts diff --git a/studio-frontend/src/inspector_dummy.ts b/studio-frontend/src/inspector_dummy.ts new file mode 100644 index 0000000..0de89eb --- /dev/null +++ b/studio-frontend/src/inspector_dummy.ts @@ -0,0 +1,12 @@ +import {type InspectorChanged} from "@storyteller/studio"; + +document.addEventListener('inspector-changed', (event) => { + // Cast the event to the InspectorDomChanged type to access its details in TypeScript + const customEvent = event as InspectorChanged; + + const data = customEvent['detail']; + + //print to console + console.log(data); + +}); \ No newline at end of file diff --git a/studio-frontend/src/main.ts b/studio-frontend/src/main.ts index cde5d45..7356e23 100644 --- a/studio-frontend/src/main.ts +++ b/studio-frontend/src/main.ts @@ -1 +1,3 @@ import "./app/app.element"; + +import "./inspector_dummy"; \ No newline at end of file diff --git a/studio/src/ui/inspector.rs b/studio/src/ui/inspector.rs index 2514498..7dbc42b 100644 --- a/studio/src/ui/inspector.rs +++ b/studio/src/ui/inspector.rs @@ -1,11 +1,17 @@ use std::{any::Any, marker::PhantomData}; -use bevy::{prelude::*, reflect::{serde::ReflectSerializer, DynamicStruct, GetTypeRegistration}, utils::HashMap}; +use bevy::{ + prelude::*, + reflect::{serde::ReflectSerializer, DynamicStruct, GetTypeRegistration}, + utils::HashMap, + window::PrimaryWindow, +}; use serde::{Deserialize, Serialize}; use serde_json::*; use space_editor::space_prefab::component::MeshPrimitivePrefab; +use wasm_bindgen::prelude::*; -use crate::interaction::Selected; +use crate::{interaction::Selected, wasm}; pub struct InspectorPlugin; @@ -14,20 +20,24 @@ impl Plugin for InspectorPlugin { app.init_resource::(); app.add_event::(); - app.configure_sets(Update, ( - InspectorSystem::DetectChanges, - InspectorSystem::PerComponentCollect, - InspectorSystem::CollectChanges, - ).chain()); + app.configure_sets( + Update, + ( + InspectorSystem::DetectChanges, + InspectorSystem::PerComponentCollect, + InspectorSystem::CollectChanges, + ) + .chain(), + ); - app.add_systems(Update, update_description.in_set(InspectorSystem::CollectChanges)); + app.add_systems( + Update, + update_description.in_set(InspectorSystem::CollectChanges), + ); app.register_inspect::(); app.register_inspect::(); app.register_inspect::(); - - - } } @@ -37,15 +47,21 @@ trait AppInspectable { impl AppInspectable for App { fn register_inspect(&mut self) { - self - .register_type::() + self.register_type::() .add_event::>() - .add_systems(Update, ( - update_inspect_by_select_change::, - update_inspect_by_remove::, - update_inspect_by_change:: - ).in_set(InspectorSystem::DetectChanges)) - .add_systems(Update, auto_serialize_system::.in_set(InspectorSystem::PerComponentCollect)); + .add_systems( + Update, + ( + update_inspect_by_select_change::, + update_inspect_by_remove::, + update_inspect_by_change::, + ) + .in_set(InspectorSystem::DetectChanges), + ) + .add_systems( + Update, + auto_serialize_system::.in_set(InspectorSystem::PerComponentCollect), + ); } } @@ -66,7 +82,9 @@ pub struct UpdateEntityDescription; impl Default for UpdateInspect { fn default() -> Self { - Self { _phantom: PhantomData } + Self { + _phantom: PhantomData, + } } } @@ -76,7 +94,7 @@ pub struct EntityDescription { } fn update_inspect_by_select_change( - mut ev_inspect : EventWriter>, + mut ev_inspect: EventWriter>, q_added: Query<(), Added>, q_removed: RemovedComponents, ) { @@ -90,8 +108,8 @@ fn update_inspect_by_select_change } fn update_inspect_by_remove( - mut ev_inspect : EventWriter>, - mut q_t_removed : RemovedComponents, + mut ev_inspect: EventWriter>, + mut q_t_removed: RemovedComponents, ) { if let Some(t) = q_t_removed.iter().next() { ev_inspect.send(UpdateInspect::::default()); @@ -99,8 +117,8 @@ fn update_inspect_by_remove( } fn update_inspect_by_change( - mut ev_inspect : EventWriter>, - q_t_changed : Query<&T, (With, Changed)>, + mut ev_inspect: EventWriter>, + q_t_changed: Query<&T, (With, Changed)>, ) { if let Some(_) = q_t_changed.iter().next() { ev_inspect.send(UpdateInspect::::default()); @@ -108,11 +126,11 @@ fn update_inspect_by_change( } fn auto_serialize_system( - q_t : Query<&T, With>, - mut description : ResMut, - registry : Res, - mut ev_inspect : EventReader>, - mut ev_update_description : EventWriter, + q_t: Query<&T, With>, + mut description: ResMut, + registry: Res, + mut ev_inspect: EventReader>, + mut ev_update_description: EventWriter, ) { if ev_inspect.is_empty() { return; @@ -124,23 +142,28 @@ fn auto_serialize_system( let registry = registry.read(); let json = serde_json::to_value(ReflectSerializer::new(t, ®istry)).unwrap(); info!("json: {:?}", json.to_string()); - description.components.insert(T::get_type_registration().type_info().type_path().to_string(), json); + description.components.insert( + T::get_type_registration() + .type_info() + .type_path() + .to_string(), + json, + ); } else { - description.components.remove(T::get_type_registration().type_info().type_path()); + description + .components + .remove(T::get_type_registration().type_info().type_path()); } ev_update_description.send(UpdateEntityDescription); } -fn auto_handle_inspect( - mut assets : Res>, -) { - -} +fn auto_handle_inspect(mut assets: Res>) {} fn update_description( - mut description : ResMut, - mut ev_update_description : EventReader, + mut description: ResMut, + mut ev_update_description: EventReader, + mut q_windows: Query<&Window, With>, ) { if ev_update_description.is_empty() { return; @@ -148,14 +171,48 @@ fn update_description( ev_update_description.clear(); - let values = description.components.iter().map(|(name, v)| { - let mut value = Value::Object(Map::new()); - value.as_object_mut().unwrap().insert("name".to_string(), Value::String(name.to_string())); - value.as_object_mut().unwrap().insert("value".to_string(), v[name].clone()); - value - }).collect::>(); + let values = description + .components + .iter() + .map(|(name, v)| { + let mut value = Value::Object(Map::new()); + value + .as_object_mut() + .unwrap() + .insert("name".to_string(), Value::String(name.to_string())); + value + .as_object_mut() + .unwrap() + .insert("value".to_string(), v[name].clone()); + value + }) + .collect::>(); //Write pretty json - info!("description: {}", serde_json::to_string_pretty(&values).unwrap()); + // info!("description: {}", serde_json::to_string_pretty(&values).unwrap()); + + if let Some(window) = q_windows.iter_mut().next() { + notify_changed(window, serde_json::to_string_pretty(&values).unwrap()); + } } +#[wasm_bindgen(typescript_custom_section)] +const EVENT_DECLARATION: &str = r#" + +export interface InspectorChanged extends CustomEvent { + detail: string | null; +} + +declare global { +export interface HTMLElementEventMap { + "inspector-changed": InspectorChanged; +} +} +"#; + +pub(super) fn notify_changed(q_window: &Window, msg: String) { + let res = wasm::dispatch_to_js(q_window, "inspector-changed", &msg); + if let Err(e) = res { + error!("Failed to dispatch inspector-changed: {}", e); + } +}