use crate::{utils::pretty_type_name, InspectorPlugins, Inspectors};
use bevy_app::{PluginGroup, PluginGroupBuilder};
use bevy_ecs::{
prelude::{On, Res, ResMut, Resource},
schedule::SystemCondition,
};
use bevy_inspector_egui::quick::ResourceInspectorPlugin;
use bevy_minibuffer::{prelude::*, prompt::PromptState};
use bevy_reflect::Reflect;
use bevy_state::prelude::in_state;
pub struct ResourceActs {
plugins: InspectorPlugins<Self>,
acts: Acts,
}
impl ActsPluginGroup for ResourceActs {
fn acts(&self) -> &Acts {
&self.acts
}
fn acts_mut(&mut self) -> &mut Acts {
&mut self.acts
}
}
impl ResourceActs {
pub fn add<R: Resource + Reflect>(mut self) -> Self {
self.plugins.add_inspector(
pretty_type_name::<R>(),
Self::resource_inspector_plugin::<R>,
);
self
}
fn resource_inspector_plugin<R: Resource + Reflect>(
index: usize,
inspector_plugins: &mut InspectorPlugins<Self>,
) {
inspector_plugins.add_plugin(
ResourceInspectorPlugin::<R>::default().run_if(
in_state(PromptState::Visible).and(InspectorPlugins::<Self>::visible(index)),
),
);
}
}
impl Default for ResourceActs {
fn default() -> Self {
Self {
plugins: InspectorPlugins::default(),
acts: Acts::new([Act::new(inspect_resource)]),
}
}
}
fn inspect_resource(resources: Res<Inspectors<ResourceActs>>, mut minibuffer: Minibuffer) {
if !resources.visible.is_empty() {
minibuffer
.prompt_map("resource: ", resources.names.clone())
.observe(
|mut trigger: On<Completed<usize>>,
mut minibuffer: Minibuffer,
mut resources: ResMut<Inspectors<ResourceActs>>| {
match trigger.event_mut().state.take_result().unwrap() {
Ok(index) => {
resources.visible[index] = !resources.visible[index];
minibuffer.clear();
}
Err(e) => {
minibuffer.message(format!("{e}"));
}
}
},
);
} else {
minibuffer.message("No resource inspectors available.");
}
}
impl PluginGroup for ResourceActs {
fn build(self) -> PluginGroupBuilder {
self.warn_on_unused_acts();
self.plugins
.warn_on_empty("No resources registered with `ResourceActs`; consider adding some.");
self.plugins.build()
}
}