Initial project import
CI / Format, lint, test, build (push) Waiting to run

This commit is contained in:
Rbanh committed 2026-06-05 21:44:45 -04:00
commit f29712d158
259 files changed
+52069

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
//! Project workspace I/O, settings UI, and recent-level sync.
pub mod project_io;
pub mod settings_ui;
pub mod workspace;
+162
View File
@@ -0,0 +1,162 @@
//! Project workspace I/O and user-local preferences (separate from `assets/project.ron`).
use bevy::prelude::*;
use serde::{Deserialize, Serialize};
use settings::{ProjectSettings, ProjectSettingsIo, DEFAULT_PROJECT_PATH};
use crate::scene_io::SceneIo;
const USER_CONFIG_DIR: &str = ".config/bevy-fps";
const USER_PREFS_FILE: &str = "editor_prefs.ron";
/// User-local editor preferences (not checked into the repo).
#[derive(Debug, Clone, Default, Serialize, Deserialize, Resource)]
pub struct UserPreferences {
pub recent_projects: Vec<String>,
pub recent_levels: Vec<String>,
pub last_project_root: Option<String>,
/// Serialized [`crate::ui::layout::DockLayoutSnapshot`] (RON).
pub dock_layout: Option<String>,
#[serde(default)]
pub save_before_play: bool,
/// Hierarchy search filter (persisted between sessions).
#[serde(default)]
pub hierarchy_filter: String,
/// Hierarchy paths expanded in the outliner (name paths like `Group/Cube`).
#[serde(default)]
pub hierarchy_expanded_paths: Vec<String>,
#[serde(default = "default_true")]
pub hierarchy_show_generated: bool,
#[serde(default)]
pub hierarchy_show_runtime: bool,
#[serde(default)]
pub hierarchy_sort_mode: crate::ui::hierarchy_state::HierarchySort,
}
fn default_true() -> bool {
true
}
/// Active project workspace metadata (mirrors manifest fields in `ProjectSettings`).
#[derive(Resource, Debug, Clone)]
pub struct ProjectWorkspace {
pub root: String,
pub settings_path: String,
pub settings_dirty: bool,
pub layout_dirty: bool,
}
impl Default for ProjectWorkspace {
fn default() -> Self {
let root = std::env::current_dir()
.ok()
.and_then(|path| path.into_os_string().into_string().ok())
.unwrap_or_else(|| ".".into());
Self {
root,
settings_path: DEFAULT_PROJECT_PATH.into(),
settings_dirty: false,
layout_dirty: false,
}
}
}
pub struct ProjectIoPlugin;
impl Plugin for ProjectIoPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<UserPreferences>()
.init_resource::<ProjectWorkspace>()
.add_systems(Startup, load_user_preferences_on_startup)
.add_systems(Update, sync_workspace_from_settings_system);
}
}
fn load_user_preferences(mut commands: Commands) {
let prefs = read_user_preferences();
commands.insert_resource(prefs);
}
/// Startup system; registered before [`crate::ui::apply_ui_state_from_prefs`].
pub(crate) fn load_user_preferences_on_startup(commands: Commands) {
load_user_preferences(commands);
}
fn sync_workspace_from_settings_system(
settings: Res<ProjectSettings>,
settings_io: Res<ProjectSettingsIo>,
mut workspace: ResMut<ProjectWorkspace>,
) {
if !settings_io.is_changed() && !settings.is_changed() {
return;
}
sync_workspace_from_settings(&mut workspace, &settings, &settings_io);
}
fn user_prefs_path() -> Option<std::path::PathBuf> {
std::env::var_os("HOME").map(|home| {
std::path::PathBuf::from(home)
.join(USER_CONFIG_DIR)
.join(USER_PREFS_FILE)
})
}
pub fn read_user_preferences() -> UserPreferences {
let Some(path) = user_prefs_path() else {
return UserPreferences::default();
};
match std::fs::read_to_string(path) {
Ok(text) => ron::from_str(&text).unwrap_or_default(),
Err(_) => UserPreferences::default(),
}
}
pub fn write_user_preferences(prefs: &UserPreferences) -> Result<(), String> {
let path = user_prefs_path().ok_or_else(|| "HOME not set".to_string())?;
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
}
let text = ron::ser::to_string_pretty(prefs, ron::ser::PrettyConfig::default())
.map_err(|e| e.to_string())?;
std::fs::write(path, text).map_err(|e| e.to_string())
}
pub fn push_recent(list: &mut Vec<String>, entry: String, max: usize) {
list.retain(|item| item != &entry);
list.insert(0, entry);
list.truncate(max);
}
pub fn sync_workspace_from_settings(
workspace: &mut ProjectWorkspace,
settings: &ProjectSettings,
settings_io: &ProjectSettingsIo,
) {
workspace.settings_path = settings_io.path.clone();
workspace.settings_dirty = settings_io.dirty;
let _ = settings;
}
pub fn window_title(
scene_io: &SceneIo,
workspace: &ProjectWorkspace,
settings: &ProjectSettings,
) -> String {
let mut parts = vec![format!("{} — Bevy FPS Editor", settings.name)];
if !workspace.root.is_empty() && workspace.root != "." {
parts.push(workspace.root.clone());
}
if let Some(path) = &scene_io.active_path {
parts.push(path.display().to_string());
}
if scene_io.dirty {
parts.push("*".into());
}
if workspace.settings_dirty {
parts.push("[project*]".into());
}
if workspace.layout_dirty {
parts.push("[layout*]".into());
}
parts.join(" — ")
}
+306
View File
@@ -0,0 +1,306 @@
//! Editor-only Project Settings panel (egui). Data lives in the `settings` crate.
use bevy::prelude::*;
use bevy_egui::egui;
use game_hot::sync_project_sun_from_settings;
use settings::{
load_project_settings_from_path, save_project_settings, sync_sim_tuning, ExposureMode,
ProjectSettings, ProjectSettingsChanged, ProjectSettingsIo,
};
/// Whether the Project Settings window is visible (editor UI state only).
#[derive(Resource, Debug, Default)]
pub struct ProjectSettingsPanel {
pub open: bool,
pub draft: Option<ProjectSettings>,
}
pub struct SettingsUiPlugin;
impl Plugin for SettingsUiPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<ProjectSettingsPanel>().add_systems(
PostUpdate,
apply_project_settings_changes
.after(crate::scene_view::sync_editor_camera_viewport_target),
);
}
}
pub fn open_project_settings_panel(panel: &mut ProjectSettingsPanel) {
panel.open = true;
panel.draft = None;
}
pub fn project_settings_window(
world: &mut World,
ctx: &egui::Context,
panel: &mut ProjectSettingsPanel,
) {
if !panel.open {
panel.draft = None;
return;
}
if panel.draft.is_none() {
panel.draft = Some(world.resource::<ProjectSettings>().clone());
}
let mut open = panel.open;
egui::Window::new("Project Settings")
.open(&mut open)
.default_width(420.0)
.show(ctx, |ui| {
let Some(draft) = panel.draft.as_mut() else {
return;
};
let io_path = world.resource::<ProjectSettingsIo>().path.clone();
let mut io_dirty = world.resource::<ProjectSettingsIo>().dirty;
draw_project_settings_form(world, ui, draft, &io_path, &mut io_dirty);
world.resource_mut::<ProjectSettingsIo>().dirty = io_dirty;
});
panel.open = open;
if !open {
panel.draft = None;
}
}
fn draw_project_settings_form(
world: &mut World,
ui: &mut egui::Ui,
draft: &mut ProjectSettings,
io_path: &str,
io_dirty: &mut bool,
) {
let caps = world
.get_resource::<settings::RenderingCapabilities>()
.cloned()
.unwrap_or_default();
ui.collapsing("Global Illumination", |ui| {
egui::ComboBox::from_label("GiMode")
.selected_text(format!("{:?}", draft.rendering.gi_mode))
.show_ui(ui, |ui| {
for mode in [
settings::GiMode::Auto,
settings::GiMode::Forward,
settings::GiMode::Solari,
] {
if ui
.selectable_value(&mut draft.rendering.gi_mode, mode, format!("{mode:?}"))
.clicked()
{}
}
});
ui.small("Auto uses Solari when RT GPU support is available; otherwise Forward PBR.");
let status = match (draft.rendering.gi_mode, caps.rt_supported) {
(settings::GiMode::Forward, _) => "Forward PBR",
(settings::GiMode::Solari, true) => "Solari active",
(settings::GiMode::Solari, false) => "Solari requested — will fall back (no RT)",
(settings::GiMode::Auto, true) => "Auto → Solari when playing",
(settings::GiMode::Auto, false) => "Auto → Forward fallback (no RT)",
};
ui.label(format!("Status: {status}"));
ui.small("Open Window → Rendering for resolved active-camera stack.");
});
ui.collapsing("Lighting", |ui| {
ui.label("Ambient");
ui.horizontal(|ui| {
let mut rgb = [
draft.rendering.ambient_color[0],
draft.rendering.ambient_color[1],
draft.rendering.ambient_color[2],
];
if ui.color_edit_button_rgb(&mut rgb).changed() {
draft.rendering.ambient_color = rgb;
}
});
ui.add(
egui::Slider::new(&mut draft.rendering.ambient_brightness, 0.0..=200.0)
.text("Ambient brightness"),
);
ui.add(
egui::Slider::new(&mut draft.rendering.sun_illuminance, 1_000.0..=150_000.0)
.text("Sun illuminance (lux)"),
);
ui.add(
egui::Slider::new(&mut draft.rendering.shadow_cascades, 1..=4).text("Shadow cascades"),
);
ui.add(
egui::Slider::new(&mut draft.rendering.shadow_first_cascade, 1.0..=20.0)
.text("First cascade (m)"),
);
ui.add(
egui::Slider::new(&mut draft.rendering.shadow_max_distance, 50.0..=500.0)
.text("Shadow max distance"),
);
});
ui.collapsing("Post Process", |ui| {
ui.checkbox(&mut draft.rendering.hdr, "HDR");
ui.small("The viewport uses HDR when Lit mode is active.");
ui.horizontal(|ui| {
ui.label("Exposure");
if ui
.selectable_label(
draft.rendering.exposure_mode == ExposureMode::Manual,
"Manual",
)
.clicked()
{
draft.rendering.exposure_mode = ExposureMode::Manual;
}
if ui
.selectable_label(draft.rendering.exposure_mode == ExposureMode::Auto, "Auto")
.clicked()
{
draft.rendering.exposure_mode = ExposureMode::Auto;
}
});
let manual = draft.rendering.exposure_mode == ExposureMode::Manual;
if manual {
ui.add(
egui::Slider::new(&mut draft.rendering.exposure_ev100, 0.0..=20.0)
.text("Exposure EV100"),
);
} else {
ui.add_enabled(
false,
egui::Slider::new(&mut draft.rendering.exposure_ev100, 0.0..=20.0)
.text("Exposure EV100"),
);
ui.small("Scene brightness adapts automatically (histogram metering).");
ui.add(
egui::Slider::new(
&mut draft.rendering.auto_exposure_speed_brighten,
0.1..=10.0,
)
.text("Auto brighten (f-stops/s)"),
);
ui.add(
egui::Slider::new(&mut draft.rendering.auto_exposure_speed_darken, 0.1..=10.0)
.text("Auto darken (f-stops/s)"),
);
if !draft.rendering.hdr {
ui.colored_label(
egui::Color32::from_rgb(255, 180, 100),
"Auto exposure requires HDR — enable HDR or use Manual.",
);
} else {
ui.small("Requires GPU compute; see Window → Rendering if unavailable.");
}
}
ui.checkbox(&mut draft.rendering.tonemapping_aces, "Tonemapping ACES");
ui.checkbox(&mut draft.rendering.bloom, "Bloom");
ui.checkbox(&mut draft.rendering.taa, "TAA");
ui.checkbox(&mut draft.rendering.ssao, "SSAO");
ui.checkbox(&mut draft.rendering.atmosphere, "Atmosphere");
ui.label("Fog");
ui.horizontal(|ui| {
let mut fog_rgb = draft.rendering.fog_color;
if ui.color_edit_button_rgb(&mut fog_rgb).changed() {
draft.rendering.fog_color = fog_rgb;
}
});
ui.add(
egui::Slider::new(&mut draft.rendering.fog_density, 0.0..=0.01)
.logarithmic(true)
.text("Fog density"),
);
});
ui.separator();
ui.heading("Physics");
ui.add(egui::Slider::new(&mut draft.physics.walk_speed, 1.0..=15.0).text("Walk speed"));
ui.add(egui::Slider::new(&mut draft.physics.run_speed, 1.0..=20.0).text("Run speed"));
ui.add(egui::Slider::new(&mut draft.physics.gravity, 5.0..=40.0).text("Gravity"));
ui.add(egui::Slider::new(&mut draft.physics.jump_speed, 3.0..=15.0).text("Jump speed"));
ui.separator();
ui.heading("Input");
ui.add(
egui::Slider::new(&mut draft.input.yaw_sensitivity, 0.0005..=0.01).text("Yaw sensitivity"),
);
ui.add(
egui::Slider::new(&mut draft.input.pitch_sensitivity, 0.0005..=0.01)
.text("Pitch sensitivity"),
);
ui.add(
egui::Slider::new(&mut draft.input.editor_fly_speed, 2.0..=40.0).text("Editor fly speed"),
);
ui.separator();
if ui.button("Apply").clicked() {
commit_project_settings(world, draft);
*io_dirty = true;
}
if ui.button("Save to disk").clicked() {
commit_project_settings(world, draft);
let settings = world.resource::<ProjectSettings>().clone();
let mut io = world.resource_mut::<ProjectSettingsIo>();
if let Err(error) = save_project_settings(&settings, &mut io) {
warn!("Save project settings failed: {error}");
} else {
*io_dirty = false;
}
}
if ui.button("Revert from disk").clicked() {
*draft = load_project_settings_from_path(io_path);
commit_project_settings(world, draft);
world.resource_mut::<ProjectSettingsIo>().dirty = false;
*io_dirty = false;
}
if *io_dirty {
ui.label(egui::RichText::new("Unsaved changes").color(egui::Color32::YELLOW));
}
}
fn commit_project_settings(world: &mut World, draft: &ProjectSettings) {
let previous = world.resource::<ProjectSettings>().rendering.gi_mode;
*world.resource_mut::<ProjectSettings>() = draft.clone();
if previous != draft.rendering.gi_mode {
info!(
"Project settings GI mode changed: {:?} -> {:?}",
previous, draft.rendering.gi_mode
);
} else {
debug!(
"Project settings applied: gi_mode={:?}",
draft.rendering.gi_mode
);
}
world.write_message(ProjectSettingsChanged);
}
fn apply_project_settings_changes(
mut reader: MessageReader<ProjectSettingsChanged>,
mut commands: Commands,
settings: Res<ProjectSettings>,
tuning: ResMut<settings::SimTuning>,
scene_suns: Query<
(&shared::LightDesc, Option<&shared::InspectorOrder>),
With<shared::LevelObject>,
>,
project_suns: Query<(&mut DirectionalLight, &mut Visibility), With<shared::ProjectSun>>,
) {
if reader.read().next().is_none() {
return;
}
sync_sim_tuning(&settings, tuning);
{
let ambient = settings.rendering.clone();
commands.insert_resource(GlobalAmbientLight {
color: Color::srgb(
ambient.ambient_color[0],
ambient.ambient_color[1],
ambient.ambient_color[2],
),
brightness: ambient.ambient_brightness,
..default()
});
}
sync_project_sun_from_settings(settings, scene_suns, project_suns);
// Full stack refresh runs next frame via `render_view::sync_project_render_view`
// when `ProjectSettings` changes.
}
+150
View File
@@ -0,0 +1,150 @@
//! 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())
}