Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple assets inspector #147

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ space_prefab = {workspace = true, version = "0.3.1" }
# Modules for external crates
space_bevy_xpbd_plugin = {version = "0.3.1", workspace = true, optional = true }


# For versions 1.74+
[lints.rust]
future-incompatible = "warn"
Expand Down
1 change: 1 addition & 0 deletions crates/editor_ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ bevy_mod_picking = { version = "0.17", default-features = false, features = [
] }
bevy_panorbit_camera = "0.9"
pretty-type-name = "1.0.1"
convert_case = "0.6"
12 changes: 8 additions & 4 deletions crates/editor_ui/src/editor_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use bevy::{prelude::*, utils::HashMap};
use bevy_egui::egui::{self, WidgetText};
use convert_case::{Case, Casing};

use super::{EditorUiRef, EditorUiReg};

Expand All @@ -12,13 +13,14 @@ pub trait EditorTab {

#[derive(Clone, Hash, PartialEq, Eq, Debug, PartialOrd, Ord)]
pub enum EditorTabName {
Hierarchy,
GameView,
CameraView,
GameView,
Hierarchy,
Inspector,
Resource,
ToolBox,
RuntimeAssets,
Settings,
ToolBox,
Other(String),
}

Expand Down Expand Up @@ -114,7 +116,9 @@ impl<'a, 'w, 's> egui_dock::TabViewer for EditorTabViewer<'a, 'w, 's> {
if let EditorTabName::Other(name) = registry.0 {
format_name = name.clone();
} else {
format_name = format!("{:?}", registry.0);
format_name = format!("{:?}", registry.0)
.from_case(Case::Pascal)
.to_case(Case::Title);
}

if ui.button(format_name).clicked() {
Expand Down
3 changes: 3 additions & 0 deletions crates/editor_ui/src/inspector/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod components_order;
pub mod refl_impl;
pub mod resources;
pub mod runtime_assets;

use std::any::TypeId;

Expand All @@ -24,6 +25,7 @@ use self::{
components_order::{ComponentsOrder, ComponentsPriority},
refl_impl::{entity_ref_ui, entity_ref_ui_readonly, many_unimplemented},
resources::ResourceTab,
runtime_assets::RuntimeAssetsTab,
};

use super::{
Expand All @@ -48,6 +50,7 @@ impl Plugin for SpaceInspectorPlugin {

app.editor_tab_by_trait(EditorTabName::Inspector, InspectorTab::default());
app.editor_tab_by_trait(EditorTabName::Resource, ResourceTab::default());
app.editor_tab_by_trait(EditorTabName::RuntimeAssets, RuntimeAssetsTab::default());

app.add_systems(Update, execute_inspect_command);

Expand Down
71 changes: 71 additions & 0 deletions crates/editor_ui/src/inspector/runtime_assets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use bevy::{asset::ReflectAsset, prelude::*, utils::HashMap};

use bevy_egui::*;
use space_shared::ext::bevy_inspector_egui;

use crate::prelude::*;

#[derive(Resource, Default)]
pub struct RuntimeAssetsTab {
open_assets: HashMap<String, bool>,
}

impl EditorTab for RuntimeAssetsTab {
fn ui(&mut self, ui: &mut egui::Ui, _: &mut Commands, world: &mut World) {
inspect(ui, world, &mut self.open_assets);
}

fn title(&self) -> egui::WidgetText {
"Runtime Assets".into()
}
}

pub fn inspect(ui: &mut egui::Ui, world: &mut World, open_assets: &mut HashMap<String, bool>) {
let type_registry = world.resource::<AppTypeRegistry>().clone();
let type_registry = type_registry.read();

let mut assets: Vec<_> = type_registry
.iter()
.filter_map(|registration| {
let reflect_asset = registration.data::<ReflectAsset>()?;
Some((
registration
.type_info()
.type_path_table()
.short_path()
.to_string(),
registration.type_id(),
reflect_asset
.ids(world)
.find(|id| id.type_id() == registration.type_id())?,
))
})
.collect();
assets.sort_by(|(name_a, _, _), (name_b, _, _)| name_a.cmp(name_b));

egui::Grid::new("Assets ID".to_string()).show(ui, |ui| {
for (asset_name, type_id, handle) in assets {
ui.push_id(format!("{:?}-{}", &type_id, &asset_name), |ui| {
let header = egui::CollapsingHeader::new(asset_name.clone())
.default_open(*open_assets.get(&asset_name).unwrap_or(&false))
.show(ui, |ui| {
ui.push_id(format!("content-{:?}-{}", &type_id, &asset_name), |ui| {
bevy_inspector_egui::bevy_inspector::by_type_id::ui_for_asset(
world,
type_id,
handle,
ui,
&type_registry,
);
});
});
if header.header_response.clicked() {
let open_name = open_assets.entry(asset_name.clone()).or_default();
//At click header not opened simultaneously so its need to check percent of opened
*open_name = header.openness < 0.5;
}
});
ui.end_row();
}
});
}