-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Entity inspector * Update skills/entity-inspector/src/main.rs Co-authored-by: Ryan Butler <[email protected]> * Change author * Format --------- Co-authored-by: Ryan Butler <[email protected]>
- Loading branch information
1 parent
11b60b4
commit d13bced
Showing
5 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]>"] | ||
|
||
[dependencies] | ||
bevy.workspace = true | ||
egui.workspace = true | ||
bevy_egui.workspace = true | ||
bevy-inspector-egui.workspace = true | ||
color-eyre.workspace = true | ||
tracing.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
}); | ||
} |