Add editor operator lifecycle bridge
CI / Format, lint, test, build (push) Waiting to run

This commit is contained in:
Rbanh committed 2026-06-06 04:07:58 -04:00
1 parent 2054fa2558
commit d77cc6a40e
8 files changed
+447 -8

No files matched your search

+51 -7
View File
@@ -4,6 +4,7 @@ use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts, EguiPrimaryContextPass};
use crate::history::group_selection_with_history;
use crate::operators::{run_immediate_operator, OperatorAvailability};
use crate::state::{EditorMode, PlayPossession};
use crate::ui::helpers::{
reset_scene_lighting_to_project_defaults, toggle_play_mode, toggle_play_paused,
@@ -15,6 +16,12 @@ use crate::ui::UiState;
/// Named editor command invokable from the palette and BRP.
pub trait EditorCommand: Send + Sync {
fn name(&self) -> &str;
fn label(&self) -> &str {
self.name()
}
fn disabled_reason(&self, _world: &World) -> Option<String> {
None
}
fn execute(&self, world: &mut World);
}
@@ -35,6 +42,13 @@ impl EditorCommandRegistry {
.collect()
}
pub fn command_state(&self, world: &World, name: &str) -> Option<(String, Option<String>)> {
self.commands
.iter()
.find(|cmd| cmd.name() == name)
.map(|cmd| (cmd.label().to_string(), cmd.disabled_reason(world)))
}
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);
@@ -184,11 +198,39 @@ fn run_palette_commands(world: &mut World) {
}
fn dispatch_editor_command(world: &mut World, name: &str) {
let ran = world
.resource_scope(|world, registry: Mut<EditorCommandRegistry>| registry.run(world, name));
if !ran {
let Some((label, disabled_reason)) =
world.resource_scope(|world, registry: Mut<EditorCommandRegistry>| {
registry.command_state(world, name)
})
else {
warn!("unknown editor command: {name}");
}
return;
};
let name_for_commit = name.to_string();
let name_for_status = name.to_string();
let label_for_commit = label.clone();
run_immediate_operator(
world,
&name_for_status,
&label,
match disabled_reason {
Some(reason) => OperatorAvailability::Disabled(reason),
None => OperatorAvailability::Ready,
},
move |world| {
let ran = world.resource_scope(|world, registry: Mut<EditorCommandRegistry>| {
registry.run(world, &name_for_commit)
});
if ran {
Ok(())
} else {
Err(format!("Unknown editor command: {name_for_commit}"))
}
},
);
trace!("dispatched editor operator command {label_for_commit}");
}
struct TogglePlayCommand;
@@ -222,10 +264,12 @@ impl EditorCommand for TogglePossessionCommand {
"play.toggle_possession"
}
fn disabled_reason(&self, world: &World) -> Option<String> {
(*world.resource::<State<EditorMode>>().get() != EditorMode::Playing)
.then(|| "Enter Play mode before toggling possession".to_string())
}
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,