Upgrade to Bevy 0.19 and harden editor workflows
CI / Format, lint, test, build (push) Has been cancelled
CI / Format, lint, test, build (push) Has been cancelled
This commit is contained in:
1 parent
3f85d25cc7
commit
b5e561904c
204 files changed
+6972
-29907
No files matched your search
@@ -2,4 +2,3 @@
|
||||
|
||||
pub mod project_io;
|
||||
pub mod settings_ui;
|
||||
pub mod workspace;
|
||||
@@ -1,150 +0,0 @@
|
||||
//! Project workspace New/Open and recent scene switcher (H2).
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use bevy::prelude::*;
|
||||
use settings::{
|
||||
load_project_settings_from_path, ProjectSettings, ProjectSettingsIo, DEFAULT_PROJECT_PATH,
|
||||
};
|
||||
|
||||
use crate::history::EditorHistory;
|
||||
use crate::project_io::{push_recent, ProjectWorkspace, UserPreferences};
|
||||
use crate::scene_io::{SceneIo, SceneIoRequest};
|
||||
|
||||
pub struct WorkspacePlugin;
|
||||
|
||||
impl Plugin for WorkspacePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.init_resource::<WorkspaceState>()
|
||||
.init_resource::<WorkspaceIoRequest>()
|
||||
.add_systems(Update, process_workspace_requests);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource, Default)]
|
||||
pub struct WorkspaceState {
|
||||
pub project_name: String,
|
||||
}
|
||||
|
||||
#[derive(Resource, Default, Debug)]
|
||||
pub struct WorkspaceIoRequest {
|
||||
pub new_project: bool,
|
||||
pub open_project: bool,
|
||||
}
|
||||
|
||||
pub fn request_new_project(world: &mut World) {
|
||||
world.resource_mut::<WorkspaceIoRequest>().new_project = true;
|
||||
}
|
||||
|
||||
pub fn request_open_project(world: &mut World) {
|
||||
world.resource_mut::<WorkspaceIoRequest>().open_project = true;
|
||||
}
|
||||
|
||||
pub fn request_open_recent_scene(world: &mut World, index: usize) {
|
||||
world.resource_mut::<SceneIo>().request = Some(SceneIoRequest::OpenRecent(index));
|
||||
}
|
||||
|
||||
pub fn sync_recent_levels_to_scene_io(world: &mut World) {
|
||||
let recent: Vec<PathBuf> = world
|
||||
.resource::<UserPreferences>()
|
||||
.recent_levels
|
||||
.iter()
|
||||
.map(PathBuf::from)
|
||||
.collect();
|
||||
world.resource_mut::<SceneIo>().recent_paths = recent;
|
||||
}
|
||||
|
||||
pub fn remember_opened_level(world: &mut World, path: String) {
|
||||
if let Some(mut prefs) = world.get_resource_mut::<UserPreferences>() {
|
||||
push_recent(&mut prefs.recent_levels, path, 8);
|
||||
let _ = crate::project_io::write_user_preferences(&prefs);
|
||||
}
|
||||
}
|
||||
|
||||
fn process_workspace_requests(world: &mut World) {
|
||||
let (new_project, open_project) = {
|
||||
let mut req = world.resource_mut::<WorkspaceIoRequest>();
|
||||
let new_project = req.new_project;
|
||||
let open_project = req.open_project;
|
||||
req.new_project = false;
|
||||
req.open_project = false;
|
||||
(new_project, open_project)
|
||||
};
|
||||
|
||||
if new_project {
|
||||
world.resource_mut::<SceneIo>().status = apply_new_project(world);
|
||||
} else if open_project {
|
||||
world.resource_mut::<SceneIo>().status = apply_open_project(world);
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_new_project(world: &mut World) -> String {
|
||||
let default_settings = ProjectSettings::default();
|
||||
let settings_path = DEFAULT_PROJECT_PATH.to_string();
|
||||
|
||||
*world.resource_mut::<ProjectSettings>() = default_settings.clone();
|
||||
world.resource_mut::<ProjectSettingsIo>().path = settings_path.clone();
|
||||
world.resource_mut::<ProjectSettingsIo>().dirty = false;
|
||||
|
||||
if let Some(mut workspace) = world.get_resource_mut::<ProjectWorkspace>() {
|
||||
workspace.root = std::env::current_dir()
|
||||
.ok()
|
||||
.and_then(|p| p.into_os_string().into_string().ok())
|
||||
.unwrap_or_else(|| ".".into());
|
||||
workspace.settings_path = settings_path;
|
||||
workspace.settings_dirty = false;
|
||||
}
|
||||
|
||||
world.resource_mut::<WorkspaceState>().project_name = default_settings.name.clone();
|
||||
world.resource_mut::<SceneIo>().request = Some(SceneIoRequest::New);
|
||||
world.resource_mut::<EditorHistory>().clear();
|
||||
sync_recent_levels_to_scene_io(world);
|
||||
format!("New project: {}", default_settings.name)
|
||||
}
|
||||
|
||||
fn apply_open_project(world: &mut World) -> String {
|
||||
let Some(root) = rfd::FileDialog::new()
|
||||
.set_title("Open Project Folder")
|
||||
.pick_folder()
|
||||
else {
|
||||
return "Open project cancelled".to_string();
|
||||
};
|
||||
|
||||
let settings_path = root.join("assets/project.ron");
|
||||
if !settings_path.exists() {
|
||||
return format!(
|
||||
"No assets/project.ron in {} — pick a project root folder",
|
||||
root.display()
|
||||
);
|
||||
}
|
||||
|
||||
let settings_path_label = settings_path.display().to_string();
|
||||
let settings = load_project_settings_from_path(&settings_path_label);
|
||||
|
||||
*world.resource_mut::<ProjectSettings>() = settings.clone();
|
||||
world.resource_mut::<ProjectSettingsIo>().path = settings_path_label.clone();
|
||||
world.resource_mut::<ProjectSettingsIo>().dirty = false;
|
||||
|
||||
if let Some(mut workspace) = world.get_resource_mut::<ProjectWorkspace>() {
|
||||
workspace.root = root.display().to_string();
|
||||
workspace.settings_path = settings_path_label;
|
||||
workspace.settings_dirty = false;
|
||||
}
|
||||
|
||||
if let Some(mut prefs) = world.get_resource_mut::<UserPreferences>() {
|
||||
push_recent(&mut prefs.recent_projects, workspace_root_label(&root), 8);
|
||||
prefs.last_project_root = Some(root.display().to_string());
|
||||
let _ = crate::project_io::write_user_preferences(&prefs);
|
||||
}
|
||||
|
||||
world.resource_mut::<WorkspaceState>().project_name = settings.name.clone();
|
||||
sync_recent_levels_to_scene_io(world);
|
||||
format!("Opened project: {}", settings.name)
|
||||
}
|
||||
|
||||
fn workspace_root_label(path: &Path) -> String {
|
||||
path.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.map(str::to_string)
|
||||
.unwrap_or_else(|| path.display().to_string())
|
||||
}
|
||||
Reference in new issue
Block a user