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

+370
View File
@@ -0,0 +1,370 @@
//! Editor extensibility: command registry, inspector sections, and extension hooks.
use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts, EguiPrimaryContextPass};
use crate::history::group_selection_with_history;
use crate::state::{EditorMode, PlayPossession};
use crate::ui::helpers::{
reset_scene_lighting_to_project_defaults, toggle_play_mode, toggle_play_paused,
};
use crate::ui::selection_ops::focus_editor_camera_on_selection;
use crate::ui::theme::{apply_editor_theme, PANEL_BG, TEXT, TEXT_DIM};
use crate::ui::UiState;
/// Named editor command invokable from the palette and BRP.
pub trait EditorCommand: Send + Sync {
fn name(&self) -> &str;
fn execute(&self, world: &mut World);
}
#[derive(Resource, Default)]
pub struct EditorCommandRegistry {
commands: Vec<Box<dyn EditorCommand>>,
}
impl EditorCommandRegistry {
pub fn register(&mut self, command: Box<dyn EditorCommand>) {
self.commands.push(command);
}
pub fn names(&self) -> Vec<String> {
self.commands
.iter()
.map(|cmd| cmd.name().to_string())
.collect()
}
pub fn run(&self, world: &mut World, name: &str) -> bool {
if let Some(command) = self.commands.iter().find(|cmd| cmd.name() == name) {
command.execute(world);
true
} else {
false
}
}
}
/// Custom inspector body for level objects (game crates register via [`register_actor_inspector_section`]).
pub trait ActorInspectorSection: Send + Sync {
fn id(&self) -> &str;
fn title(&self) -> &str;
fn order(&self) -> i32;
fn applies_to(&self, world: &World, entity: Entity) -> bool;
fn ui(&self, world: &mut World, ui: &mut egui::Ui, entity: Entity);
}
#[derive(Resource, Default)]
pub struct ActorInspectorSectionRegistry {
sections: Vec<Box<dyn ActorInspectorSection>>,
}
impl ActorInspectorSectionRegistry {
pub fn register(&mut self, section: Box<dyn ActorInspectorSection>) {
self.sections.push(section);
}
pub fn sections_for(&self, world: &World, entity: Entity) -> Vec<&dyn ActorInspectorSection> {
let mut matched: Vec<_> = self
.sections
.iter()
.filter(|section| section.applies_to(world, entity))
.map(|section| section.as_ref())
.collect();
matched.sort_by_key(|section| section.order());
matched
}
pub fn draw_sections(&self, world: &mut World, ui: &mut egui::Ui, entity: Entity) {
let mut matched: Vec<_> = self
.sections
.iter()
.filter(|section| section.applies_to(world, entity))
.collect();
matched.sort_by_key(|section| section.order());
for section in matched {
section.ui(world, ui, entity);
}
}
}
pub fn register_actor_inspector_section(app: &mut App, section: Box<dyn ActorInspectorSection>) {
if !app
.world()
.contains_resource::<ActorInspectorSectionRegistry>()
{
app.init_resource::<ActorInspectorSectionRegistry>();
}
app.world_mut()
.resource_mut::<ActorInspectorSectionRegistry>()
.register(section);
}
/// Optional extension point for registering editor panels and menu hooks.
pub trait EditorExtension: Send + Sync {
fn name(&self) -> &str;
fn register(&self, _app: &mut App) {}
}
/// Formal editor plugin surface for game-specific panels (H5).
pub trait EditorPlugin: Send + Sync {
fn name(&self) -> &str;
fn register(&self, app: &mut App);
}
#[derive(Resource, Default)]
pub struct EditorExtensions {
pub extensions: Vec<Box<dyn EditorExtension>>,
}
#[derive(Resource, Default)]
pub struct RegisteredEditorPlugins {
pub plugins: Vec<Box<dyn EditorPlugin>>,
}
#[derive(Resource, Default)]
pub struct CommandPalette {
pub open: bool,
pub filter: String,
pub pending_run: Option<String>,
}
pub struct ExtensibilityPlugin;
impl Plugin for ExtensibilityPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<EditorCommandRegistry>()
.init_resource::<ActorInspectorSectionRegistry>()
.init_resource::<EditorExtensions>()
.init_resource::<RegisteredEditorPlugins>()
.init_resource::<CommandPalette>()
.init_resource::<crate::command_queue::PendingEditorCommands>()
.add_systems(Startup, register_builtin_commands)
.add_systems(Update, run_palette_commands)
.add_systems(EguiPrimaryContextPass, command_palette_ui);
}
}
pub fn register_editor_plugin(app: &mut App, plugin: Box<dyn EditorPlugin>) {
plugin.register(app);
app.world_mut()
.resource_mut::<RegisteredEditorPlugins>()
.plugins
.push(plugin);
}
fn register_builtin_commands(mut registry: ResMut<EditorCommandRegistry>) {
registry.register(Box::new(TogglePlayCommand));
registry.register(Box::new(TogglePlayPausedCommand));
registry.register(Box::new(TogglePossessionCommand));
registry.register(Box::new(ResetLightingCommand));
registry.register(Box::new(GroupSelectionCommand));
registry.register(Box::new(FocusSelectionCommand));
registry.register(Box::new(CreatePostProcessVolumeCommand));
registry.register(Box::new(FocusActiveVolumesCommand));
registry.register(Box::new(SelectVolumesAtCameraCommand));
}
fn run_palette_commands(world: &mut World) {
let pending = world.resource_mut::<CommandPalette>().pending_run.take();
if let Some(name) = pending {
dispatch_editor_command(world, &name);
}
let names = std::mem::take(
&mut world
.resource_mut::<crate::command_queue::PendingEditorCommands>()
.queue,
);
for name in names {
dispatch_editor_command(world, &name);
}
}
fn dispatch_editor_command(world: &mut World, name: &str) {
let ran = world
.resource_scope(|world, registry: Mut<EditorCommandRegistry>| registry.run(world, name));
if !ran {
warn!("unknown editor command: {name}");
}
}
struct TogglePlayCommand;
impl EditorCommand for TogglePlayCommand {
fn name(&self) -> &str {
"play.toggle"
}
fn execute(&self, world: &mut World) {
toggle_play_mode(world);
}
}
struct TogglePlayPausedCommand;
impl EditorCommand for TogglePlayPausedCommand {
fn name(&self) -> &str {
"play.toggle_pause"
}
fn execute(&self, world: &mut World) {
toggle_play_paused(world);
}
}
struct TogglePossessionCommand;
impl EditorCommand for TogglePossessionCommand {
fn name(&self) -> &str {
"play.toggle_possession"
}
fn execute(&self, world: &mut World) {
if *world.resource::<State<EditorMode>>().get() != EditorMode::Playing {
return;
}
let next = match *world.resource::<PlayPossession>() {
PlayPossession::Possessed => PlayPossession::Ejected,
PlayPossession::Ejected => PlayPossession::Possessed,
};
*world.resource_mut::<PlayPossession>() = next;
}
}
struct ResetLightingCommand;
impl EditorCommand for ResetLightingCommand {
fn name(&self) -> &str {
"scene.reset_lighting"
}
fn execute(&self, world: &mut World) {
reset_scene_lighting_to_project_defaults(world);
}
}
struct GroupSelectionCommand;
impl EditorCommand for GroupSelectionCommand {
fn name(&self) -> &str {
"selection.group"
}
fn execute(&self, world: &mut World) {
let selected: Vec<Entity> = world
.resource::<UiState>()
.selected_entities
.iter()
.collect();
group_selection_with_history(world, &selected);
}
}
struct FocusSelectionCommand;
impl EditorCommand for FocusSelectionCommand {
fn name(&self) -> &str {
"selection.focus"
}
fn execute(&self, world: &mut World) {
focus_editor_camera_on_selection(world);
}
}
struct CreatePostProcessVolumeCommand;
impl EditorCommand for CreatePostProcessVolumeCommand {
fn name(&self) -> &str {
"rendering.create_volume"
}
fn execute(&self, world: &mut World) {
crate::rendering_diagnostics::spawn_post_process_volume_at_camera(world);
}
}
struct FocusActiveVolumesCommand;
impl EditorCommand for FocusActiveVolumesCommand {
fn name(&self) -> &str {
"rendering.focus_active_volumes"
}
fn execute(&self, world: &mut World) {
crate::rendering_diagnostics::select_volumes_at_camera(world);
focus_editor_camera_on_selection(world);
}
}
struct SelectVolumesAtCameraCommand;
impl EditorCommand for SelectVolumesAtCameraCommand {
fn name(&self) -> &str {
"rendering.select_volumes_at_camera"
}
fn execute(&self, world: &mut World) {
crate::rendering_diagnostics::select_volumes_at_camera(world);
}
}
fn command_palette_ui(
mut contexts: EguiContexts,
mut palette: ResMut<CommandPalette>,
registry: Res<EditorCommandRegistry>,
) -> Result {
let Ok(ctx) = contexts.ctx_mut() else {
return Ok(());
};
apply_editor_theme(ctx);
if ctx.input(|input| input.modifiers.command && input.key_pressed(egui::Key::P)) {
palette.open = true;
}
if !palette.open {
return Ok(());
}
let mut open = palette.open;
egui::Window::new("Command Palette")
.open(&mut open)
.default_width(400.0)
.frame(egui::Frame::window(&ctx.style()).fill(PANEL_BG))
.show(ctx, |ui| {
ui.label(
egui::RichText::new("Ctrl+P — filter and run commands")
.color(TEXT_DIM)
.small(),
);
ui.add(
egui::TextEdit::singleline(&mut palette.filter)
.hint_text("Type to filter...")
.desired_width(f32::INFINITY),
);
ui.separator();
egui::ScrollArea::vertical()
.max_height(280.0)
.show(ui, |ui| {
for name in registry.names() {
if !palette.filter.is_empty()
&& !name.to_lowercase().contains(&palette.filter.to_lowercase())
{
continue;
}
let response =
ui.selectable_label(false, egui::RichText::new(&name).color(TEXT));
if response.clicked() {
palette.pending_run = Some(name);
palette.open = false;
ui.close();
}
}
});
});
palette.open = open;
Ok(())
}