This commit is contained in:
commit
f29712d158
259 files changed
+52069
No files matched your search
@@ -0,0 +1,390 @@
|
||||
//! Prefab instance overrides — apply/revert per field group (v2).
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_egui::egui;
|
||||
use egui_phosphor_icons::icons;
|
||||
use scene::{migrate_scene_text, strip_schema_version, validate_level_text};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use shared::{EditorVisibility, MaterialDesc, PrefabInstance};
|
||||
|
||||
use crate::history::{set_material_with_history, set_transform_with_history};
|
||||
use crate::ui::inspector::{
|
||||
apply_component_card_response, component_card, component_card_context, property_row,
|
||||
ComponentCardOptions,
|
||||
};
|
||||
|
||||
#[derive(Resource, Default)]
|
||||
pub struct PrefabOverrideClipboard {
|
||||
pub property_path: String,
|
||||
pub value_ron: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
|
||||
pub struct PrefabOverrides {
|
||||
#[serde(default)]
|
||||
pub transform: Option<Transform>,
|
||||
#[serde(default)]
|
||||
pub material: Option<MaterialDesc>,
|
||||
#[serde(default)]
|
||||
pub child_visibility: HashMap<String, bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum PrefabOverrideField {
|
||||
Transform,
|
||||
Material,
|
||||
ChildVisibility,
|
||||
}
|
||||
|
||||
pub fn read_overrides(instance: &PrefabInstance) -> PrefabOverrides {
|
||||
instance
|
||||
.overrides_ron
|
||||
.as_ref()
|
||||
.and_then(|ron| ron::from_str(ron).ok())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn write_overrides(world: &mut World, entity: Entity, overrides: &PrefabOverrides) {
|
||||
let Some(mut instance) = world.get::<PrefabInstance>(entity).cloned() else {
|
||||
return;
|
||||
};
|
||||
instance.overrides_ron = if overrides == &PrefabOverrides::default() {
|
||||
None
|
||||
} else {
|
||||
Some(ron::to_string(overrides).unwrap_or_default())
|
||||
};
|
||||
if let Ok(mut entity_mut) = world.get_entity_mut(entity) {
|
||||
entity_mut.insert(instance);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_override(overrides: &PrefabOverrides, field: PrefabOverrideField) -> bool {
|
||||
match field {
|
||||
PrefabOverrideField::Transform => overrides.transform.is_some(),
|
||||
PrefabOverrideField::Material => overrides.material.is_some(),
|
||||
PrefabOverrideField::ChildVisibility => !overrides.child_visibility.is_empty(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn capture_transform_override(world: &mut World, entity: Entity) {
|
||||
let Some(transform) = world.get::<Transform>(entity).copied() else {
|
||||
return;
|
||||
};
|
||||
let Some(instance) = world.get::<PrefabInstance>(entity) else {
|
||||
return;
|
||||
};
|
||||
let mut overrides = read_overrides(instance);
|
||||
overrides.transform = Some(transform);
|
||||
write_overrides(world, entity, &overrides);
|
||||
}
|
||||
|
||||
pub fn revert_transform_override(world: &mut World, entity: Entity) {
|
||||
let Some(instance) = world.get::<PrefabInstance>(entity).cloned() else {
|
||||
return;
|
||||
};
|
||||
let Some(old) = world.get::<Transform>(entity).copied() else {
|
||||
return;
|
||||
};
|
||||
let Some(source_transform) = prefab_root_transform(&instance.source_path) else {
|
||||
world.resource_mut::<crate::scene_io::SceneIo>().status =
|
||||
"Could not read prefab source for revert".into();
|
||||
return;
|
||||
};
|
||||
set_transform_with_history(world, entity, old, source_transform);
|
||||
let mut overrides = read_overrides(&instance);
|
||||
overrides.transform = None;
|
||||
write_overrides(world, entity, &overrides);
|
||||
}
|
||||
|
||||
pub fn apply_material_override(world: &mut World, entity: Entity) {
|
||||
let Some(instance) = world.get::<PrefabInstance>(entity).cloned() else {
|
||||
return;
|
||||
};
|
||||
let Some(material) = world.get::<MaterialDesc>(entity).cloned() else {
|
||||
world.resource_mut::<crate::scene_io::SceneIo>().status =
|
||||
"Prefab instance has no MaterialDesc to capture".into();
|
||||
return;
|
||||
};
|
||||
let mut overrides = read_overrides(&instance);
|
||||
overrides.material = Some(material);
|
||||
write_overrides(world, entity, &overrides);
|
||||
world.resource_mut::<crate::scene_io::SceneIo>().status = "Material override applied".into();
|
||||
}
|
||||
|
||||
pub fn revert_material_override(world: &mut World, entity: Entity) {
|
||||
let Some(instance) = world.get::<PrefabInstance>(entity).cloned() else {
|
||||
return;
|
||||
};
|
||||
let Some(source_material) = prefab_root_material(&instance.source_path) else {
|
||||
world.resource_mut::<crate::scene_io::SceneIo>().status =
|
||||
"Could not read prefab source material".into();
|
||||
return;
|
||||
};
|
||||
let old = world.get::<MaterialDesc>(entity).cloned();
|
||||
if old.is_some() {
|
||||
set_material_with_history(world, entity, source_material.clone());
|
||||
} else {
|
||||
world.entity_mut(entity).insert(source_material.clone());
|
||||
}
|
||||
let mut overrides = read_overrides(&instance);
|
||||
overrides.material = None;
|
||||
write_overrides(world, entity, &overrides);
|
||||
}
|
||||
|
||||
pub fn apply_child_visibility_override(world: &mut World, entity: Entity, child: Entity) {
|
||||
let Some(instance) = world.get::<PrefabInstance>(entity).cloned() else {
|
||||
return;
|
||||
};
|
||||
let Some(name) = world.get::<Name>(child).map(|n| n.to_string()) else {
|
||||
return;
|
||||
};
|
||||
let visible = world
|
||||
.get::<EditorVisibility>(child)
|
||||
.map(|v| v.visible)
|
||||
.unwrap_or(true);
|
||||
|
||||
let mut overrides = read_overrides(&instance);
|
||||
overrides.child_visibility.insert(name, visible);
|
||||
write_overrides(world, entity, &overrides);
|
||||
}
|
||||
|
||||
pub fn revert_child_visibility_overrides(world: &mut World, entity: Entity) {
|
||||
let Some(instance) = world.get::<PrefabInstance>(entity).cloned() else {
|
||||
return;
|
||||
};
|
||||
for child in prefab_instance_children(world, entity) {
|
||||
if let Ok(mut entity_mut) = world.get_entity_mut(child) {
|
||||
entity_mut.remove::<EditorVisibility>();
|
||||
}
|
||||
}
|
||||
let mut overrides = read_overrides(&instance);
|
||||
overrides.child_visibility.clear();
|
||||
write_overrides(world, entity, &overrides);
|
||||
}
|
||||
|
||||
pub fn apply_stored_overrides(world: &mut World, entity: Entity) {
|
||||
let Some(instance) = world.get::<PrefabInstance>(entity).cloned() else {
|
||||
return;
|
||||
};
|
||||
let overrides = read_overrides(&instance);
|
||||
|
||||
if let Some(transform) = overrides.transform {
|
||||
if let Ok(mut entity_mut) = world.get_entity_mut(entity) {
|
||||
entity_mut.insert(transform);
|
||||
}
|
||||
}
|
||||
if let Some(material) = overrides.material {
|
||||
if let Ok(mut entity_mut) = world.get_entity_mut(entity) {
|
||||
entity_mut.insert(material);
|
||||
}
|
||||
}
|
||||
for child in prefab_instance_children(world, entity) {
|
||||
let Some(name) = world.get::<Name>(child).map(|n| n.to_string()) else {
|
||||
continue;
|
||||
};
|
||||
if let Some(&visible) = overrides.child_visibility.get(&name) {
|
||||
if let Ok(mut entity_mut) = world.get_entity_mut(child) {
|
||||
entity_mut.insert(EditorVisibility { visible });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prefab_instance_inspector_ui(world: &mut World, ui: &mut egui::Ui, entity: Entity) {
|
||||
let Some(instance) = world.get::<PrefabInstance>(entity).cloned() else {
|
||||
return;
|
||||
};
|
||||
let overrides = read_overrides(&instance);
|
||||
|
||||
let card = component_card_context(
|
||||
world,
|
||||
entity,
|
||||
ComponentCardOptions::removable(
|
||||
shared::COMPONENT_PREFAB_INSTANCE,
|
||||
"Prefab Instance",
|
||||
icons::PACKAGE,
|
||||
),
|
||||
);
|
||||
let card_response = component_card(ui, &card, |ui| {
|
||||
property_row(ui, "Asset ID", |ui| {
|
||||
ui.add(egui::Label::new(instance.asset_id.as_str()).truncate());
|
||||
});
|
||||
property_row(ui, "Source", |ui| {
|
||||
ui.add(egui::Label::new(instance.source_path.as_str()).truncate());
|
||||
});
|
||||
|
||||
ui.collapsing("Transform override", |ui| {
|
||||
let active = has_override(&overrides, PrefabOverrideField::Transform);
|
||||
if active {
|
||||
ui.label(egui::RichText::new("Override active").weak());
|
||||
}
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
if ui.button("Apply").clicked() {
|
||||
capture_transform_override(world, entity);
|
||||
world.resource_mut::<crate::scene_io::SceneIo>().status =
|
||||
"Transform override saved".into();
|
||||
}
|
||||
if ui
|
||||
.add_enabled(active, egui::Button::new("Revert"))
|
||||
.clicked()
|
||||
{
|
||||
revert_transform_override(world, entity);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
ui.collapsing("Material override", |ui| {
|
||||
let active = has_override(&overrides, PrefabOverrideField::Material);
|
||||
if active {
|
||||
ui.label(egui::RichText::new("Override active").weak());
|
||||
}
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
if ui.button("Apply").clicked() {
|
||||
apply_material_override(world, entity);
|
||||
}
|
||||
if ui
|
||||
.add_enabled(active, egui::Button::new("Revert"))
|
||||
.clicked()
|
||||
{
|
||||
revert_material_override(world, entity);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
ui.collapsing("Child visibility overrides", |ui| {
|
||||
let active = has_override(&overrides, PrefabOverrideField::ChildVisibility);
|
||||
if active {
|
||||
ui.label(egui::RichText::new("Overrides stored by child name").weak());
|
||||
}
|
||||
for child in prefab_instance_children(world, entity) {
|
||||
let name = world
|
||||
.get::<Name>(child)
|
||||
.map(|n| n.to_string())
|
||||
.unwrap_or_else(|| format!("{child:?}"));
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
ui.add(egui::Label::new(name).truncate());
|
||||
if ui.small_button("Apply").clicked() {
|
||||
apply_child_visibility_override(world, entity, child);
|
||||
}
|
||||
});
|
||||
}
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
if ui
|
||||
.add_enabled(active, egui::Button::new("Revert all"))
|
||||
.clicked()
|
||||
{
|
||||
revert_child_visibility_overrides(world, entity);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
if ui.button("Unpack Instance").clicked() {
|
||||
crate::history::unpack_prefab_instance(world, entity);
|
||||
}
|
||||
});
|
||||
});
|
||||
apply_component_card_response(world, entity, card_response);
|
||||
}
|
||||
|
||||
fn prefab_instance_children(world: &World, root: Entity) -> Vec<Entity> {
|
||||
let mut children = Vec::new();
|
||||
let mut stack = Vec::new();
|
||||
if let Some(kids) = world.get::<Children>(root) {
|
||||
stack.extend(kids.iter());
|
||||
}
|
||||
while let Some(entity) = stack.pop() {
|
||||
children.push(entity);
|
||||
if let Some(kids) = world.get::<Children>(entity) {
|
||||
stack.extend(kids.iter());
|
||||
}
|
||||
}
|
||||
children
|
||||
}
|
||||
|
||||
fn prefab_root_transform(source_path: &str) -> Option<Transform> {
|
||||
parse_prefab_root_component::<Transform>(source_path, "Transform")
|
||||
}
|
||||
|
||||
fn prefab_root_material(source_path: &str) -> Option<MaterialDesc> {
|
||||
parse_prefab_root_component::<MaterialDesc>(source_path, "MaterialDesc")
|
||||
}
|
||||
|
||||
fn parse_prefab_root_component<T: serde::de::DeserializeOwned>(
|
||||
source_path: &str,
|
||||
type_name: &str,
|
||||
) -> Option<T> {
|
||||
let path = Path::new(source_path);
|
||||
let text = std::fs::read_to_string(path).ok()?;
|
||||
validate_level_text(&text).ok()?;
|
||||
let body = strip_schema_version(&migrate_scene_text(&text).ok()?).ok()?;
|
||||
let root_tuple = body.trim().trim_start_matches('(').trim_end_matches(')');
|
||||
for segment in split_top_level_fields(root_tuple) {
|
||||
let segment = segment.trim();
|
||||
if segment.starts_with("entities:") {
|
||||
let entities_body = segment.trim_start_matches("entities:").trim();
|
||||
return parse_first_entity_component(entities_body, type_name);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn split_top_level_fields(input: &str) -> Vec<String> {
|
||||
let mut fields = Vec::new();
|
||||
let mut depth: i32 = 0;
|
||||
let mut start = 0;
|
||||
for (index, ch) in input.char_indices() {
|
||||
match ch {
|
||||
'(' | '{' => depth += 1,
|
||||
')' | '}' => depth = depth.saturating_sub(1),
|
||||
',' if depth == 0 => {
|
||||
fields.push(input[start..index].to_string());
|
||||
start = index + 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if start < input.len() {
|
||||
fields.push(input[start..].to_string());
|
||||
}
|
||||
fields
|
||||
}
|
||||
|
||||
fn parse_first_entity_component<T: serde::de::DeserializeOwned>(
|
||||
entities_body: &str,
|
||||
type_name: &str,
|
||||
) -> Option<T> {
|
||||
let trimmed = entities_body.trim();
|
||||
let inner = trimmed.strip_prefix('{')?.strip_suffix('}')?.trim();
|
||||
let first_entity = inner.split(',').next()?.trim();
|
||||
let components = first_entity
|
||||
.split(':')
|
||||
.nth(1)?
|
||||
.trim()
|
||||
.strip_prefix('(')?
|
||||
.strip_suffix(')')?;
|
||||
for component in split_top_level_fields(components) {
|
||||
if component.contains(type_name) {
|
||||
return ron::from_str::<T>(&format!("({component})")).ok();
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn list_prefab_dependencies(path: &str) -> Vec<String> {
|
||||
vec![path.to_string()]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn empty_overrides_roundtrip() {
|
||||
let instance = PrefabInstance::new("id", "assets/prefabs/test.scn.ron");
|
||||
assert_eq!(read_overrides(&instance), PrefabOverrides::default());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user