This commit is contained in:
1 parent
d038cf3670
commit
ba59f57a5a
11 files changed
+466
-156
No files matched your search
+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) {
|
||||
|
||||
Reference in new issue
Block a user