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

Entity inspector #42

Merged
merged 4 commits into from
Nov 19, 2023
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
57 changes: 57 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"skills/manipulation-flatscreen",
"skills/openxr-6dof",
"skills/xr-ik-mirror",
"skills/entity-inspector",
]

# These settings will apply to all members of the workspace that opt in to them
Expand Down
19 changes: 19 additions & 0 deletions skills/entity-inspector/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "entity-inspector"
version.workspace = true
license.workspace = true
repository.workspace = true
edition.workspace = true
rust-version.workspace = true
publish = false

# Current maintainer(s)
authors = ["lucas lelievre <loucass003@gmail.com>"]

[dependencies]
bevy.workspace = true
egui.workspace = true
bevy_egui.workspace = true
bevy-inspector-egui.workspace = true
color-eyre.workspace = true
tracing.workspace = true
8 changes: 8 additions & 0 deletions skills/entity-inspector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# `entity-inspector`

A entity inspector

To run the code:
```bash
cargo run -p entity-inspector
```
135 changes: 135 additions & 0 deletions skills/entity-inspector/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
use bevy::{pbr::DirectionalLightShadowMap, prelude::*, window::PrimaryWindow};
use bevy_egui::{EguiContext, EguiPlugin};
use bevy_inspector_egui::{
bevy_inspector::hierarchy::SelectedEntities, DefaultInspectorConfigPlugin,
};
use color_eyre::eyre::Result;
use tracing::info;

const ASSET_FOLDER: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../assets/");

fn main() -> Result<()> {
// Set up nice error messages
color_eyre::install()?;

info!("Running `entity-inspector` skill");

App::new()
.add_plugins(DefaultPlugins.set(AssetPlugin {
file_path: ASSET_FOLDER.to_string(),
..Default::default()
}))
.add_plugins(EguiPlugin)
.add_plugins(DefaultInspectorConfigPlugin)
.add_systems(Startup, setup)
.add_systems(Update, animate_light)
.add_systems(PostUpdate, inspector_ui)
.run();

Ok(())
}

fn setup(
assets: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut commands: Commands,
) {
info!("Running setup system");

// Load assets
let tree_img: Handle<Image> = assets.load("tree.png");

// Build cube
commands.spawn(PbrBundle {
mesh: meshes.add(shape::Cube::default().into()),
material: materials.add(StandardMaterial {
base_color_texture: Some(tree_img),
..default()
}),
..default()
});

// Build the rest of the scene
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
shadows_enabled: true,
illuminance: 10000.,
..default()
},
transform: Transform::from_xyz(8.0, 16.0, 8.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.insert_resource(DirectionalLightShadowMap { size: 4096 });
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0.0, 6., 12.0)
.looking_at(Vec3::new(0., 1., 0.), Vec3::Y),
..default()
});
commands.spawn(PbrBundle {
mesh: meshes.add(
shape::Plane {
size: 10.,
subdivisions: 4,
}
.into(),
),
material: materials.add(Color::MIDNIGHT_BLUE.into()),
transform: Transform::from_xyz(0., 0., 0.),
..default()
});
}

fn animate_light(mut query: Query<&mut Transform, With<DirectionalLight>>) {
for mut t in query.iter_mut() {
let t: &mut Transform = &mut t;
t.rotate_y(0.01);
}
}

fn inspector_ui(world: &mut World, mut selected_entities: Local<SelectedEntities>) {
let mut egui_context = world
.query_filtered::<&mut EguiContext, With<PrimaryWindow>>()
.single(world)
.clone();

egui::SidePanel::left("hierarchy")
.default_width(200.0)
.show(egui_context.get_mut(), |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
ui.heading("Hierarchy");

bevy_inspector_egui::bevy_inspector::hierarchy::hierarchy_ui(
world,
ui,
&mut selected_entities,
);

ui.label("Press escape to toggle UI");
ui.allocate_space(ui.available_size());
});
});

egui::SidePanel::right("inspector")
.default_width(250.0)
.show(egui_context.get_mut(), |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
ui.heading("Inspector");

match selected_entities.as_slice() {
&[entity] => {
bevy_inspector_egui::bevy_inspector::ui_for_entity(
world, entity, ui,
);
}
entities => {
bevy_inspector_egui::bevy_inspector::ui_for_entities_shared_components(
world, entities, ui,
);
}
}

ui.allocate_space(ui.available_size());
});
});
}
Loading