add worked event update

This commit is contained in:
rewin
2024-02-27 21:24:26 +03:00
parent 9f8553f24f
commit 748aa70bcf
3 changed files with 117 additions and 46 deletions
+12
View File
@@ -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);
});
+2
View File
@@ -1 +1,3 @@
import "./app/app.element";
import "./inspector_dummy";
+103 -46
View File
@@ -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::<EntityDescription>();
app.add_event::<UpdateEntityDescription>();
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::<Transform>();
app.register_inspect::<Name>();
app.register_inspect::<MeshPrimitivePrefab>();
}
}
@@ -37,15 +47,21 @@ trait AppInspectable {
impl AppInspectable for App {
fn register_inspect<T: Component + Reflect + GetTypeRegistration>(&mut self) {
self
.register_type::<T>()
self.register_type::<T>()
.add_event::<UpdateInspect<T>>()
.add_systems(Update, (
update_inspect_by_select_change::<T>,
update_inspect_by_remove::<T>,
update_inspect_by_change::<T>
).in_set(InspectorSystem::DetectChanges))
.add_systems(Update, auto_serialize_system::<T>.in_set(InspectorSystem::PerComponentCollect));
.add_systems(
Update,
(
update_inspect_by_select_change::<T>,
update_inspect_by_remove::<T>,
update_inspect_by_change::<T>,
)
.in_set(InspectorSystem::DetectChanges),
)
.add_systems(
Update,
auto_serialize_system::<T>.in_set(InspectorSystem::PerComponentCollect),
);
}
}
@@ -66,7 +82,9 @@ pub struct UpdateEntityDescription;
impl<T> Default for UpdateInspect<T> {
fn default() -> Self {
Self { _phantom: PhantomData }
Self {
_phantom: PhantomData,
}
}
}
@@ -76,7 +94,7 @@ pub struct EntityDescription {
}
fn update_inspect_by_select_change<T: Component + Reflect + GetTypeRegistration>(
mut ev_inspect : EventWriter<UpdateInspect<T>>,
mut ev_inspect: EventWriter<UpdateInspect<T>>,
q_added: Query<(), Added<Selected>>,
q_removed: RemovedComponents<Selected>,
) {
@@ -90,8 +108,8 @@ fn update_inspect_by_select_change<T: Component + Reflect + GetTypeRegistration>
}
fn update_inspect_by_remove<T: Component + Reflect + GetTypeRegistration>(
mut ev_inspect : EventWriter<UpdateInspect<T>>,
mut q_t_removed : RemovedComponents<T>,
mut ev_inspect: EventWriter<UpdateInspect<T>>,
mut q_t_removed: RemovedComponents<T>,
) {
if let Some(t) = q_t_removed.iter().next() {
ev_inspect.send(UpdateInspect::<T>::default());
@@ -99,8 +117,8 @@ fn update_inspect_by_remove<T: Component + Reflect + GetTypeRegistration>(
}
fn update_inspect_by_change<T: Component + Reflect + GetTypeRegistration>(
mut ev_inspect : EventWriter<UpdateInspect<T>>,
q_t_changed : Query<&T, (With<Selected>, Changed<T>)>,
mut ev_inspect: EventWriter<UpdateInspect<T>>,
q_t_changed: Query<&T, (With<Selected>, Changed<T>)>,
) {
if let Some(_) = q_t_changed.iter().next() {
ev_inspect.send(UpdateInspect::<T>::default());
@@ -108,11 +126,11 @@ fn update_inspect_by_change<T: Component + Reflect + GetTypeRegistration>(
}
fn auto_serialize_system<T: Component + Reflect + GetTypeRegistration>(
q_t : Query<&T, With<Selected>>,
mut description : ResMut<EntityDescription>,
registry : Res<AppTypeRegistry>,
mut ev_inspect : EventReader<UpdateInspect<T>>,
mut ev_update_description : EventWriter<UpdateEntityDescription>,
q_t: Query<&T, With<Selected>>,
mut description: ResMut<EntityDescription>,
registry: Res<AppTypeRegistry>,
mut ev_inspect: EventReader<UpdateInspect<T>>,
mut ev_update_description: EventWriter<UpdateEntityDescription>,
) {
if ev_inspect.is_empty() {
return;
@@ -124,23 +142,28 @@ fn auto_serialize_system<T: Component + Reflect + GetTypeRegistration>(
let registry = registry.read();
let json = serde_json::to_value(ReflectSerializer::new(t, &registry)).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<T: Asset + Reflect>(
mut assets : Res<Assets<T>>,
) {
}
fn auto_handle_inspect<T: Asset + Reflect>(mut assets: Res<Assets<T>>) {}
fn update_description(
mut description : ResMut<EntityDescription>,
mut ev_update_description : EventReader<UpdateEntityDescription>,
mut description: ResMut<EntityDescription>,
mut ev_update_description: EventReader<UpdateEntityDescription>,
mut q_windows: Query<&Window, With<PrimaryWindow>>,
) {
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::<Vec<_>>();
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::<Vec<_>>();
//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);
}
}