Repository navigation
Expand file tree
/
Copy pathlocal_inspector.rs
More file actions
256 lines (245 loc) · 7.9 KB
/
Copy pathlocal_inspector.rs
File metadata and controls
256 lines (245 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//! Shows the `bevy_inspector` entity tree, resource list and details panels inspecting the app's
//! own world. Editing the showcase tint and shape updates the object in the scene. Some showcase
//! fields use `bevy_annotations` to set their label, range, step, unit, or make them read-only.
//! The Resources tab lists the app's resources, such as `ClearColor`, which can be edited the same
//! way. Clicking an object in the scene selects it.
//!
//! Run with the `bevy_inspector` feature enabled, and `debug` for readable names of unregistered types:
//! ```bash
//! cargo run --example local_inspector --features="bevy_inspector,debug"
//! ```
use bevy::{
annotations::{DisplayName, ReadOnly, Step, Unit, ValueRange},
feathers::{
controls::{FeathersPane, FeathersSplitPane, FeathersSplitPaneHandle},
dark_theme::create_dark_theme,
theme::UiTheme,
FeathersPlugins,
},
inspector::{
details_panel::details_panel, entity_tree::InspectorUi, world_tabs::world_tabs,
InspectableEntity, InspectorPlugin, InspectorSelection,
},
prelude::*,
ui_widgets::split_pane_self_update,
};
fn main() {
App::new()
.add_plugins((DefaultPlugins, FeathersPlugins, InspectorPlugin))
.insert_resource(UiTheme(create_dark_theme()))
.add_systems(Startup, (demo_scene.spawn(), inspector_ui.spawn()))
.add_systems(Update, (log_selection, apply_showcase))
.run();
}
/// A component exercising every widget kind the details panel renders.
#[derive(Component, Reflect, Default, Clone)]
#[reflect(Component, Default)]
struct Showcase {
enabled: bool,
#[reflect(@ValueRange(0.0..=100.0), @Step(0.5), @Unit("hp"))]
health: f32,
#[reflect(@DisplayName("Blend Ratio"), @ValueRange(0.0..=1.0), @Step(0.05))]
ratio: f64,
#[reflect(@ValueRange(1.0..=99.0))]
level: u8,
count: u32,
offset: i16,
score: i64,
#[reflect(@ReadOnly)]
seed: u64,
initial: char,
label: String,
tint: Color,
glow: LinearRgba,
mode: Mode,
shape: Shape,
speed: Option<f32>,
limit: Option<u32>,
bounds: Bounds,
size: Size,
tags: Vec<u32>,
weights: [f32; 3],
}
#[derive(Reflect, Default, Clone, Copy, PartialEq)]
enum Mode {
#[default]
Idle,
Walking,
#[reflect(@DisplayName("Sprinting"))]
Running,
}
#[derive(Reflect, Default, Clone, Copy, PartialEq)]
enum Shape {
#[default]
Hidden,
Sphere {
radius: f32,
},
Cuboid(f32, f32, f32),
}
#[derive(Reflect, Default, Clone, Copy)]
struct Size(f32, f32);
#[derive(Reflect, Default, Clone, Copy)]
struct Bounds {
min: Vec2,
max: Vec2,
}
fn demo_scene() -> impl SceneList {
bsn_list! {
Name("Camera")
Camera3d
Transform::from_xyz(0.0, 3.5, 8.0).looking_at(Vec3::ZERO, Vec3::Y)
--
Name("Sun")
DirectionalLight {
shadow_maps_enabled: true,
}
Transform::from_xyz(4.0, 8.0, 4.0).looking_at(Vec3::ZERO, Vec3::Y)
--
Name("Ground")
Mesh3d(asset_value(Circle::new(6.0)))
MeshMaterial3d::<StandardMaterial>(asset_value(Color::srgb(0.25, 0.28, 0.3)))
Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2))
--
Name("Props")
Transform::default()
Visibility::default()
Children [
Name("Left Cube")
Mesh3d(asset_value(Cuboid::new(1.0, 1.0, 1.0)))
MeshMaterial3d::<StandardMaterial>(asset_value(Color::srgb(0.8, 0.3, 0.3)))
Transform::from_xyz(-1.5, 0.5, 0.0)
--
Name("Right Cube")
Mesh3d(asset_value(Cuboid::new(1.0, 1.0, 1.0)))
MeshMaterial3d::<StandardMaterial>(asset_value(Color::srgb(0.3, 0.5, 0.8)))
Transform::from_xyz(1.5, 0.5, 0.0)
Children [
Name("Marker")
Transform::from_xyz(0.0, 1.2, 0.0)
Visibility::default()
]
--
Name("Showcase")
Mesh3d(asset_value(Sphere::new(0.4)))
MeshMaterial3d::<StandardMaterial>(asset_value(Color::srgb(0.95, 0.55, 0.2)))
Transform::from_xyz(3.0, 0.4, 1.0)
Showcase {
enabled: true,
health: 72.5,
ratio: 0.25,
level: 7,
count: 3,
offset: -4,
score: -1200,
seed: 9001,
initial: 'j',
label: "hello",
tint: Color::srgb(0.95, 0.55, 0.2),
glow: { LinearRgba::rgb(0.2, 0.6, 1.0) },
mode: Mode::Walking,
shape: { Shape::Sphere { radius: 0.4 } },
speed: { Some(3.5) },
limit: { None },
bounds: Bounds {
min: Vec2::new(-1.0, -1.0),
max: Vec2::new(2.0, 3.0),
},
size: { Size(1.5, 0.75) },
tags: { vec![1, 2, 3] },
weights: { [0.2, 0.3, 0.5] },
}
]
--
Mesh3d(asset_value(Sphere::new(0.5)))
MeshMaterial3d::<StandardMaterial>(asset_value(Color::srgb(0.9, 0.8, 0.3)))
Transform::from_xyz(0.0, 1.8, -2.0)
}
}
fn apply_showcase(
mut showcases: Query<
(
&Showcase,
&mut Mesh3d,
&mut Visibility,
&MeshMaterial3d<StandardMaterial>,
),
Changed<Showcase>,
>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
mut applied: Local<Option<Shape>>,
) {
for (showcase, mut mesh, mut visibility, material) in &mut showcases {
if let Some(mut material) = materials.get_mut(&material.0) {
material.base_color = showcase.tint;
}
if *applied != Some(showcase.shape) {
*applied = Some(showcase.shape);
let size = |value: f32| if value > 0.0 { value } else { 0.5 };
*visibility = Visibility::Inherited;
match showcase.shape {
Shape::Hidden => *visibility = Visibility::Hidden,
Shape::Sphere { radius } => mesh.0 = meshes.add(Sphere::new(size(radius))),
Shape::Cuboid(width, height, depth) => {
mesh.0 = meshes.add(Cuboid::new(size(width), size(height), size(depth)));
}
}
}
}
}
fn inspector_ui() -> impl Scene {
bsn! {
InspectorUi
Name::new("Inspector")
Node {
position_type: PositionType::Absolute,
width: percent(100),
height: percent(100),
padding: px(12),
flex_direction: FlexDirection::Column,
align_items: AlignItems::FlexStart,
}
Pickable::IGNORE
Children [
@FeathersSplitPane
Node {
width: px(720),
max_height: percent(100),
}
on(split_pane_self_update)
Children [
@FeathersPane { @size: 1.0, @min_size: 220.0 }
Children [
@world_tabs()
]
--
@FeathersSplitPaneHandle
--
@FeathersPane { @size: 2.0, @min_size: 240.0 }
Children [
@details_panel()
Node {
width: Val::Auto,
}
]
]
]
}
}
fn log_selection(selection: Res<InspectorSelection>, names: Query<&Name>) {
if !selection.is_changed() {
return;
}
match selection.0 {
Some(InspectableEntity { entity, source }) => info!(
"selected {entity} {:?}: {}",
source,
names
.get(entity)
.map(Name::as_str)
.unwrap_or("<unnamed entity>")
),
None => info!("selection cleared"),
}
}