This commit is contained in:
1 parent
d038cf3670
commit
ba59f57a5a
11 files changed
+466
-156
No files matched your search
@@ -22,6 +22,7 @@ use crate::asset_db::{ensure_asset_record, AssetRegistry};
|
||||
use crate::history::{
|
||||
capture_prefab_edit_state, record_prefab_edit_state, record_prefab_source_apply,
|
||||
};
|
||||
use crate::native_dialog::NativeDialogBroker;
|
||||
use crate::ui::inspector::{
|
||||
apply_component_card_response, component_card, component_card_context, property_row,
|
||||
ComponentCardOptions,
|
||||
@@ -751,13 +752,44 @@ fn relink_prefab_source(world: &mut World, entity: Entity) {
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let Some(path) = rfd::FileDialog::new()
|
||||
.set_directory(project_root.join("assets/prefabs"))
|
||||
.add_filter("Bevy prefab", &["scn.ron", "ron"])
|
||||
.pick_file()
|
||||
else {
|
||||
let directory = project_root.join("assets/prefabs");
|
||||
let result = world.resource::<NativeDialogBroker>().request(
|
||||
move || {
|
||||
rfd::FileDialog::new()
|
||||
.set_directory(directory)
|
||||
.add_filter("Bevy prefab", &["scn.ron", "ron"])
|
||||
.pick_file()
|
||||
},
|
||||
move |world, path| {
|
||||
let Some(path) = path else {
|
||||
world
|
||||
.resource_mut::<crate::scene_io::SceneIo>()
|
||||
.set_status("Prefab relink cancelled");
|
||||
return;
|
||||
};
|
||||
finish_relink_prefab_source(world, entity, project_root, path);
|
||||
},
|
||||
);
|
||||
world
|
||||
.resource_mut::<crate::scene_io::SceneIo>()
|
||||
.set_status(match result {
|
||||
Ok(()) => "Choose a prefab source".into(),
|
||||
Err(error) => format!("Prefab relink unavailable: {error}"),
|
||||
});
|
||||
}
|
||||
|
||||
fn finish_relink_prefab_source(
|
||||
world: &mut World,
|
||||
entity: Entity,
|
||||
project_root: PathBuf,
|
||||
path: PathBuf,
|
||||
) {
|
||||
if world.get_entity(entity).is_err() {
|
||||
world
|
||||
.resource_mut::<crate::scene_io::SceneIo>()
|
||||
.set_status("Prefab relink failed: the initiating actor no longer exists");
|
||||
return;
|
||||
};
|
||||
}
|
||||
let Ok(path) = path.canonicalize() else {
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -95,6 +95,9 @@ mod tests {
|
||||
#[derive(Resource, Default)]
|
||||
struct ResultValue(Option<u32>);
|
||||
|
||||
#[derive(Resource, Default)]
|
||||
struct FrameCount(u32);
|
||||
|
||||
#[test]
|
||||
fn request_is_exclusive_and_completion_runs_once() {
|
||||
let mut app = App::new();
|
||||
@@ -125,4 +128,44 @@ mod tests {
|
||||
assert_eq!(app.world().resource::<ResultValue>().0, Some(42));
|
||||
assert!(!app.world().resource::<NativeDialogBroker>().is_pending());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blocked_dialog_worker_does_not_block_app_frames() {
|
||||
let mut app = App::new();
|
||||
app.add_plugins(NativeDialogPlugin)
|
||||
.init_resource::<ResultValue>()
|
||||
.init_resource::<FrameCount>()
|
||||
.add_systems(Update, |mut frames: ResMut<FrameCount>| frames.0 += 1);
|
||||
let (release_sender, release_receiver) = mpsc::channel();
|
||||
app.world()
|
||||
.resource::<NativeDialogBroker>()
|
||||
.request(
|
||||
move || {
|
||||
release_receiver
|
||||
.recv()
|
||||
.expect("test release sender dropped")
|
||||
},
|
||||
|world, value| world.resource_mut::<ResultValue>().0 = Some(value),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
for _ in 0..8 {
|
||||
app.update();
|
||||
}
|
||||
assert_eq!(app.world().resource::<FrameCount>().0, 8);
|
||||
assert_eq!(app.world().resource::<ResultValue>().0, None);
|
||||
assert!(app.world().resource::<NativeDialogBroker>().is_pending());
|
||||
|
||||
release_sender.send(73).unwrap();
|
||||
for _ in 0..100 {
|
||||
app.update();
|
||||
if app.world().resource::<ResultValue>().0.is_some() {
|
||||
break;
|
||||
}
|
||||
std::thread::yield_now();
|
||||
}
|
||||
assert_eq!(app.world().resource::<ResultValue>().0, Some(73));
|
||||
assert!(app.world().resource::<FrameCount>().0 > 8);
|
||||
assert!(!app.world().resource::<NativeDialogBroker>().is_pending());
|
||||
}
|
||||
}
|
||||
+217
-125
@@ -23,6 +23,7 @@ use shared::{
|
||||
|
||||
use crate::assets::{import_external_assets, EditorAssets, IMPORTABLE_ASSET_EXTENSIONS};
|
||||
use crate::history::{clear_level_objects, snapshot_entity, EditorHistory};
|
||||
use crate::native_dialog::NativeDialogBroker;
|
||||
use crate::project::collaboration::{publish_authored_file, FileSnapshot, FileWriteIntent};
|
||||
use crate::scene::recovery::{
|
||||
default_state_root, discard_recovery_snapshots, latest_recovery_snapshot,
|
||||
@@ -82,8 +83,6 @@ pub struct SceneIo {
|
||||
pub request: Option<SceneIoRequest>,
|
||||
pub status: String,
|
||||
pub dirty: bool,
|
||||
/// When set, the next I/O request needs unsaved-changes confirmation.
|
||||
pub pending_request: Option<SceneIoRequest>,
|
||||
/// Newest recovery snapshot that is newer than the active authored scene.
|
||||
pub recovery_snapshot: Option<PathBuf>,
|
||||
/// Bounded in-session audit trail for scene persistence and recovery operations.
|
||||
@@ -119,7 +118,6 @@ impl Default for SceneIo {
|
||||
request: None,
|
||||
status: "Ready".to_string(),
|
||||
dirty: false,
|
||||
pending_request: None,
|
||||
recovery_snapshot: None,
|
||||
events: VecDeque::new(),
|
||||
tabs: vec![SceneTab {
|
||||
@@ -291,39 +289,15 @@ fn load_startup_scene(world: &mut World) {
|
||||
}
|
||||
|
||||
fn process_scene_io_requests(world: &mut World) {
|
||||
let request = {
|
||||
let mut io = world.resource_mut::<SceneIo>();
|
||||
if let Some(pending) = io.pending_request.take() {
|
||||
pending
|
||||
} else {
|
||||
match io.request.take() {
|
||||
Some(req) if io.has_unsaved_tabs() && needs_dirty_confirm(&req) => {
|
||||
io.pending_request = Some(req);
|
||||
return;
|
||||
}
|
||||
Some(req) => req,
|
||||
None => return,
|
||||
}
|
||||
}
|
||||
let Some(request) = world.resource_mut::<SceneIo>().request.take() else {
|
||||
return;
|
||||
};
|
||||
|
||||
if world.resource::<SceneIo>().has_unsaved_tabs() && needs_dirty_confirm(&request) {
|
||||
match confirm_discard_or_save() {
|
||||
DirtyConfirm::Save => {
|
||||
let save_status = save_all_tabs(world);
|
||||
if save_status.starts_with("Save failed") || save_status.ends_with("cancelled") {
|
||||
world.resource_mut::<SceneIo>().set_status(save_status);
|
||||
return;
|
||||
}
|
||||
}
|
||||
DirtyConfirm::Discard => {}
|
||||
DirtyConfirm::Cancel => {
|
||||
world
|
||||
.resource_mut::<SceneIo>()
|
||||
.set_status("Operation cancelled");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if matches!(request, SceneIoRequest::SwitchProject)
|
||||
&& world.resource::<SceneIo>().has_unsaved_tabs()
|
||||
{
|
||||
request_switch_project_confirmation(world);
|
||||
return;
|
||||
}
|
||||
|
||||
let status = match request {
|
||||
@@ -348,16 +322,6 @@ fn process_scene_io_requests(world: &mut World) {
|
||||
world.resource_mut::<SceneIo>().set_status(status);
|
||||
}
|
||||
|
||||
enum DirtyConfirm {
|
||||
Save,
|
||||
Discard,
|
||||
Cancel,
|
||||
}
|
||||
|
||||
fn needs_dirty_confirm(request: &SceneIoRequest) -> bool {
|
||||
matches!(request, SceneIoRequest::SwitchProject)
|
||||
}
|
||||
|
||||
fn switch_project(world: &mut World) -> String {
|
||||
match crate::launcher::spawn_project_launcher_process() {
|
||||
Ok(()) => {
|
||||
@@ -368,19 +332,41 @@ fn switch_project(world: &mut World) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
fn confirm_discard_or_save() -> DirtyConfirm {
|
||||
use rfd::{MessageButtons, MessageDialog, MessageLevel};
|
||||
match MessageDialog::new()
|
||||
.set_title("Unsaved Changes")
|
||||
.set_description("The scene has unsaved changes. Save before continuing?")
|
||||
.set_level(MessageLevel::Warning)
|
||||
.set_buttons(MessageButtons::YesNoCancel)
|
||||
.show()
|
||||
{
|
||||
rfd::MessageDialogResult::Yes => DirtyConfirm::Save,
|
||||
rfd::MessageDialogResult::No => DirtyConfirm::Discard,
|
||||
_ => DirtyConfirm::Cancel,
|
||||
}
|
||||
fn request_switch_project_confirmation(world: &mut World) {
|
||||
let result = world.resource::<NativeDialogBroker>().request(
|
||||
|| {
|
||||
rfd::MessageDialog::new()
|
||||
.set_title("Unsaved Changes")
|
||||
.set_description("The scene has unsaved changes. Save before continuing?")
|
||||
.set_level(rfd::MessageLevel::Warning)
|
||||
.set_buttons(rfd::MessageButtons::YesNoCancel)
|
||||
.show()
|
||||
},
|
||||
|world, decision| match decision {
|
||||
rfd::MessageDialogResult::Yes => {
|
||||
let status = save_all_tabs(world);
|
||||
if status == "Saved all modified scene tabs" {
|
||||
let status = switch_project(world);
|
||||
world.resource_mut::<SceneIo>().set_status(status);
|
||||
} else {
|
||||
world.resource_mut::<SceneIo>().set_status(format!(
|
||||
"Project switch paused until every scene is saved: {status}"
|
||||
));
|
||||
}
|
||||
}
|
||||
rfd::MessageDialogResult::No => {
|
||||
let status = switch_project(world);
|
||||
world.resource_mut::<SceneIo>().set_status(status);
|
||||
}
|
||||
_ => world
|
||||
.resource_mut::<SceneIo>()
|
||||
.set_status("Project switch cancelled"),
|
||||
},
|
||||
);
|
||||
world.resource_mut::<SceneIo>().set_status(match result {
|
||||
Ok(()) => "Waiting for unsaved-scene confirmation".into(),
|
||||
Err(error) => format!("Project switch unavailable: {error}"),
|
||||
});
|
||||
}
|
||||
|
||||
fn open_recent(world: &mut World, index: usize) -> String {
|
||||
@@ -533,18 +519,14 @@ fn close_scene_tab(world: &mut World, index: usize) -> String {
|
||||
}
|
||||
|
||||
if world.resource::<SceneIo>().dirty {
|
||||
match confirm_close_scene() {
|
||||
DirtyConfirm::Save => {
|
||||
let status = save_active_or_prompt(world);
|
||||
if status.starts_with("Save failed") || status.ends_with("cancelled") {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
DirtyConfirm::Discard => {}
|
||||
DirtyConfirm::Cancel => return "Close scene cancelled".to_string(),
|
||||
}
|
||||
request_close_scene_confirmation(world);
|
||||
return "Waiting for close-scene confirmation".to_string();
|
||||
}
|
||||
|
||||
finish_close_scene_tab(world)
|
||||
}
|
||||
|
||||
fn finish_close_scene_tab(world: &mut World) -> String {
|
||||
if world.resource::<SceneIo>().tabs.len() == 1 {
|
||||
clear_scene_world(world);
|
||||
world.insert_resource(SceneComposition {
|
||||
@@ -618,18 +600,36 @@ fn reload_active_composition(world: &mut World) -> String {
|
||||
"Reloaded active scene composition".to_string()
|
||||
}
|
||||
|
||||
fn confirm_close_scene() -> DirtyConfirm {
|
||||
use rfd::{MessageButtons, MessageDialog, MessageLevel};
|
||||
match MessageDialog::new()
|
||||
.set_title("Close Scene")
|
||||
.set_description("This scene tab has unsaved changes. Save before closing it?")
|
||||
.set_level(MessageLevel::Warning)
|
||||
.set_buttons(MessageButtons::YesNoCancel)
|
||||
.show()
|
||||
{
|
||||
rfd::MessageDialogResult::Yes => DirtyConfirm::Save,
|
||||
rfd::MessageDialogResult::No => DirtyConfirm::Discard,
|
||||
_ => DirtyConfirm::Cancel,
|
||||
fn request_close_scene_confirmation(world: &mut World) {
|
||||
let result = world.resource::<NativeDialogBroker>().request(
|
||||
|| {
|
||||
rfd::MessageDialog::new()
|
||||
.set_title("Close Scene")
|
||||
.set_description("This scene tab has unsaved changes. Save before closing it?")
|
||||
.set_level(rfd::MessageLevel::Warning)
|
||||
.set_buttons(rfd::MessageButtons::YesNoCancel)
|
||||
.show()
|
||||
},
|
||||
|world, decision| {
|
||||
let status = match decision {
|
||||
rfd::MessageDialogResult::Yes => {
|
||||
let save_status = save_active_or_prompt(world);
|
||||
if save_status.starts_with("Saved ") {
|
||||
finish_close_scene_tab(world)
|
||||
} else {
|
||||
format!("Close scene paused until it is saved: {save_status}")
|
||||
}
|
||||
}
|
||||
rfd::MessageDialogResult::No => finish_close_scene_tab(world),
|
||||
_ => "Close scene cancelled".to_string(),
|
||||
};
|
||||
world.resource_mut::<SceneIo>().set_status(status);
|
||||
},
|
||||
);
|
||||
if let Err(error) = result {
|
||||
world
|
||||
.resource_mut::<SceneIo>()
|
||||
.set_status(format!("Close scene unavailable: {error}"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -694,12 +694,31 @@ fn save_selection_as_prefab(world: &mut World) -> String {
|
||||
return "Select level objects to save as prefab".to_string();
|
||||
}
|
||||
|
||||
let Some(path) = rfd::FileDialog::new()
|
||||
.set_directory("assets/prefabs")
|
||||
.add_filter("Bevy prefab", &["scn.ron", "ron"])
|
||||
.set_file_name("prefab.scn.ron")
|
||||
.save_file()
|
||||
else {
|
||||
let request = world.resource::<NativeDialogBroker>().request(
|
||||
|| {
|
||||
rfd::FileDialog::new()
|
||||
.set_directory("assets/prefabs")
|
||||
.add_filter("Bevy prefab", &["scn.ron", "ron"])
|
||||
.set_file_name("prefab.scn.ron")
|
||||
.save_file()
|
||||
},
|
||||
move |world, path| {
|
||||
let status = finish_save_selection_as_prefab(world, selection, path);
|
||||
world.resource_mut::<SceneIo>().set_status(status);
|
||||
},
|
||||
);
|
||||
match request {
|
||||
Ok(()) => "Choose a destination for the prefab".to_string(),
|
||||
Err(error) => format!("Save prefab unavailable: {error}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn finish_save_selection_as_prefab(
|
||||
world: &mut World,
|
||||
selection: Vec<Entity>,
|
||||
path: Option<PathBuf>,
|
||||
) -> String {
|
||||
let Some(path) = path else {
|
||||
return "Save prefab cancelled".to_string();
|
||||
};
|
||||
let expected = match FileSnapshot::capture(&path) {
|
||||
@@ -859,12 +878,27 @@ fn save_active_or_prompt(world: &mut World) -> String {
|
||||
}
|
||||
|
||||
fn save_with_dialog(world: &mut World) -> String {
|
||||
let Some(path) = rfd::FileDialog::new()
|
||||
.set_directory("assets/levels")
|
||||
.add_filter("Bevy scene", &["scn.ron", "ron"])
|
||||
.set_file_name("editor_scene.scn.ron")
|
||||
.save_file()
|
||||
else {
|
||||
let request = world.resource::<NativeDialogBroker>().request(
|
||||
|| {
|
||||
rfd::FileDialog::new()
|
||||
.set_directory("assets/levels")
|
||||
.add_filter("Bevy scene", &["scn.ron", "ron"])
|
||||
.set_file_name("editor_scene.scn.ron")
|
||||
.save_file()
|
||||
},
|
||||
|world, path| {
|
||||
let status = finish_save_with_dialog(world, path);
|
||||
world.resource_mut::<SceneIo>().set_status(status);
|
||||
},
|
||||
);
|
||||
match request {
|
||||
Ok(()) => "Choose a scene destination".to_string(),
|
||||
Err(error) => format!("Save unavailable: {error}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn finish_save_with_dialog(world: &mut World, path: Option<PathBuf>) -> String {
|
||||
let Some(path) = path else {
|
||||
return "Save cancelled".to_string();
|
||||
};
|
||||
let expected = match FileSnapshot::capture(&path) {
|
||||
@@ -885,31 +919,51 @@ fn save_with_dialog(world: &mut World) -> String {
|
||||
}
|
||||
|
||||
fn open_with_dialog(world: &mut World) -> String {
|
||||
let Some(path) = rfd::FileDialog::new()
|
||||
.set_directory("assets/levels")
|
||||
.add_filter("Bevy scene", &["scn.ron", "ron"])
|
||||
.pick_file()
|
||||
else {
|
||||
return "Open cancelled".to_string();
|
||||
};
|
||||
|
||||
open_path(world, path)
|
||||
let request = world.resource::<NativeDialogBroker>().request(
|
||||
|| {
|
||||
rfd::FileDialog::new()
|
||||
.set_directory("assets/levels")
|
||||
.add_filter("Bevy scene", &["scn.ron", "ron"])
|
||||
.pick_file()
|
||||
},
|
||||
|world, path| {
|
||||
let status = path.map_or_else(
|
||||
|| "Open cancelled".to_string(),
|
||||
|path| open_path(world, path),
|
||||
);
|
||||
world.resource_mut::<SceneIo>().set_status(status);
|
||||
},
|
||||
);
|
||||
match request {
|
||||
Ok(()) => "Choose a scene to open".to_string(),
|
||||
Err(error) => format!("Open unavailable: {error}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn import_with_dialog(world: &mut World) -> String {
|
||||
let Some(paths) = rfd::FileDialog::new()
|
||||
.add_filter("Editor assets", IMPORTABLE_ASSET_EXTENSIONS)
|
||||
.pick_files()
|
||||
else {
|
||||
return "Import cancelled".to_string();
|
||||
};
|
||||
|
||||
match import_external_assets(&paths) {
|
||||
Ok(count) => {
|
||||
world.resource_mut::<EditorAssets>().refresh();
|
||||
format!("Imported {count} asset(s)")
|
||||
}
|
||||
Err(err) => format!("Import failed: {err}"),
|
||||
let request = world.resource::<NativeDialogBroker>().request(
|
||||
|| {
|
||||
rfd::FileDialog::new()
|
||||
.add_filter("Editor assets", IMPORTABLE_ASSET_EXTENSIONS)
|
||||
.pick_files()
|
||||
},
|
||||
|world, paths| {
|
||||
let status = match paths {
|
||||
None => "Import cancelled".to_string(),
|
||||
Some(paths) => match import_external_assets(&paths) {
|
||||
Ok(count) => {
|
||||
world.resource_mut::<EditorAssets>().refresh();
|
||||
format!("Imported {count} asset(s)")
|
||||
}
|
||||
Err(err) => format!("Import failed: {err}"),
|
||||
},
|
||||
};
|
||||
world.resource_mut::<SceneIo>().set_status(status);
|
||||
},
|
||||
);
|
||||
match request {
|
||||
Ok(()) => "Choose assets to import".to_string(),
|
||||
Err(error) => format!("Import unavailable: {error}"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -921,14 +975,32 @@ fn export_selection_with_dialog(world: &mut World) -> String {
|
||||
return "Selected entity is not an authored level object".to_string();
|
||||
}
|
||||
|
||||
let Some(path) = rfd::FileDialog::new()
|
||||
.set_directory("assets/levels")
|
||||
.add_filter("Bevy prefab", &["scn.ron", "ron"])
|
||||
.set_file_name("selection.scn.ron")
|
||||
.save_file()
|
||||
else {
|
||||
let request = world.resource::<NativeDialogBroker>().request(
|
||||
|| {
|
||||
rfd::FileDialog::new()
|
||||
.set_directory("assets/levels")
|
||||
.add_filter("Bevy prefab", &["scn.ron", "ron"])
|
||||
.set_file_name("selection.scn.ron")
|
||||
.save_file()
|
||||
},
|
||||
move |world, path| {
|
||||
let status = finish_export_selection(world, entity, path);
|
||||
world.resource_mut::<SceneIo>().set_status(status);
|
||||
},
|
||||
);
|
||||
match request {
|
||||
Ok(()) => "Choose an export destination".to_string(),
|
||||
Err(error) => format!("Export unavailable: {error}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn finish_export_selection(world: &mut World, entity: Entity, path: Option<PathBuf>) -> String {
|
||||
let Some(path) = path else {
|
||||
return "Export cancelled".to_string();
|
||||
};
|
||||
if snapshot_entity(world, entity).is_none() {
|
||||
return "Export failed: the initiating selection no longer exists".to_string();
|
||||
}
|
||||
let expected = match FileSnapshot::capture(&path) {
|
||||
Ok(expected) => expected,
|
||||
Err(error) => return format!("Export failed: {error}"),
|
||||
@@ -1607,12 +1679,32 @@ fn save_recovery_copy_with_dialog(world: &mut World) -> String {
|
||||
|| "recovered_scene.scn.ron".to_string(),
|
||||
|stem| format!("{stem}.recovered.scn.ron"),
|
||||
);
|
||||
let Some(destination) = rfd::FileDialog::new()
|
||||
.set_directory(directory)
|
||||
.add_filter("Bevy scene", &["scn.ron", "ron"])
|
||||
.set_file_name(file_name)
|
||||
.save_file()
|
||||
else {
|
||||
let directory = directory.to_path_buf();
|
||||
let request = world.resource::<NativeDialogBroker>().request(
|
||||
move || {
|
||||
rfd::FileDialog::new()
|
||||
.set_directory(directory)
|
||||
.add_filter("Bevy scene", &["scn.ron", "ron"])
|
||||
.set_file_name(file_name)
|
||||
.save_file()
|
||||
},
|
||||
move |world, destination| {
|
||||
let status = finish_save_recovery_copy(world, snapshot, destination);
|
||||
world.resource_mut::<SceneIo>().set_status(status);
|
||||
},
|
||||
);
|
||||
match request {
|
||||
Ok(()) => "Choose a destination for the recovery copy".to_string(),
|
||||
Err(error) => format!("Save recovery copy unavailable: {error}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn finish_save_recovery_copy(
|
||||
world: &mut World,
|
||||
snapshot: PathBuf,
|
||||
destination: Option<PathBuf>,
|
||||
) -> String {
|
||||
let Some(destination) = destination else {
|
||||
return "Save recovery copy cancelled".to_string();
|
||||
};
|
||||
let expected = match FileSnapshot::capture(&destination) {
|
||||
|
||||
@@ -8,6 +8,7 @@ use bevy_inspector_egui::bevy_inspector::hierarchy::SelectedEntities;
|
||||
use egui_phosphor_icons::icons;
|
||||
use shared::{SceneComposition, SubsceneReference};
|
||||
|
||||
use crate::native_dialog::NativeDialogBroker;
|
||||
use crate::scene_io::{ComposedSceneMember, SceneIo, SceneIoRequest};
|
||||
use crate::selection::SelectedEntity;
|
||||
|
||||
@@ -195,19 +196,47 @@ fn composition_menu(
|
||||
|
||||
ui.separator();
|
||||
if ui
|
||||
.add_enabled(editing, egui::Button::new("Add Subscene..."))
|
||||
.add_enabled(
|
||||
editing && !world.resource::<NativeDialogBroker>().is_pending(),
|
||||
egui::Button::new("Add Subscene..."),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
match choose_subscene(world, &composition) {
|
||||
Ok(Some(reference)) => {
|
||||
composition.subscenes.push(reference);
|
||||
changed = true;
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(error) => world
|
||||
.resource_mut::<SceneIo>()
|
||||
.set_status(format!("Add subscene failed: {error}")),
|
||||
}
|
||||
let project_root =
|
||||
PathBuf::from(&world.resource::<crate::project_io::ProjectWorkspace>().root);
|
||||
let directory = project_root.join("assets/levels");
|
||||
let result = world.resource::<NativeDialogBroker>().request(
|
||||
move || {
|
||||
rfd::FileDialog::new()
|
||||
.set_directory(directory)
|
||||
.add_filter("Bevy scene", &["scn.ron", "ron"])
|
||||
.pick_file()
|
||||
},
|
||||
move |world, path| {
|
||||
let Some(path) = path else {
|
||||
world
|
||||
.resource_mut::<SceneIo>()
|
||||
.set_status("Add subscene cancelled");
|
||||
return;
|
||||
};
|
||||
let current = world.resource::<SceneComposition>().clone();
|
||||
match validate_subscene_choice(&project_root, ¤t, path) {
|
||||
Ok(reference) => {
|
||||
let mut updated = current;
|
||||
updated.subscenes.push(reference);
|
||||
crate::history::set_scene_composition_with_history(world, updated);
|
||||
world.resource_mut::<SceneIo>().set_status("Subscene added");
|
||||
}
|
||||
Err(error) => world
|
||||
.resource_mut::<SceneIo>()
|
||||
.set_status(format!("Add subscene failed: {error}")),
|
||||
}
|
||||
},
|
||||
);
|
||||
world.resource_mut::<SceneIo>().set_status(match result {
|
||||
Ok(()) => "Choose a scene to compose".into(),
|
||||
Err(error) => format!("Add subscene unavailable: {error}"),
|
||||
});
|
||||
}
|
||||
|
||||
if changed {
|
||||
@@ -218,20 +247,14 @@ fn composition_menu(
|
||||
}
|
||||
}
|
||||
|
||||
fn choose_subscene(
|
||||
world: &World,
|
||||
fn validate_subscene_choice(
|
||||
project_root: &Path,
|
||||
composition: &SceneComposition,
|
||||
) -> Result<Option<SubsceneReference>, String> {
|
||||
let project_root = PathBuf::from(&world.resource::<crate::project_io::ProjectWorkspace>().root)
|
||||
path: PathBuf,
|
||||
) -> Result<SubsceneReference, String> {
|
||||
let project_root = project_root
|
||||
.canonicalize()
|
||||
.map_err(|error| format!("could not resolve project root: {error}"))?;
|
||||
let Some(path) = rfd::FileDialog::new()
|
||||
.set_directory(project_root.join("assets/levels"))
|
||||
.add_filter("Bevy scene", &["scn.ron", "ron"])
|
||||
.pick_file()
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
let path = path
|
||||
.canonicalize()
|
||||
.map_err(|error| format!("could not resolve {}: {error}", path.display()))?;
|
||||
@@ -251,12 +274,12 @@ fn choose_subscene(
|
||||
{
|
||||
return Err(format!("{relative} is already composed"));
|
||||
}
|
||||
Ok(Some(SubsceneReference {
|
||||
Ok(SubsceneReference {
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
path: relative,
|
||||
visible: true,
|
||||
locked: true,
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
fn focus_subscene(world: &mut World, selected: &mut SelectedEntities, reference_id: &str) {
|
||||
|
||||
Reference in new issue
Block a user