You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.5 KiB
Rust
97 lines
2.5 KiB
Rust
use bevy::input::keyboard::KeyboardInput;
|
|
|
|
use crate::prelude::*;
|
|
|
|
pub struct EditorPlugin;
|
|
|
|
impl Plugin for EditorPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.add_systems(Startup, init_editor)
|
|
.insert_resource(
|
|
GizmoConfig {
|
|
depth_bias: -0.1,
|
|
..default()
|
|
},
|
|
)
|
|
.add_systems(Update, (
|
|
toggle_editor.run_if(on_event::<KeyboardInput>()),
|
|
aabb_gizmo,
|
|
))
|
|
// Systems that run in the editor mode
|
|
.add_systems(Update, (
|
|
selected_gizmo.run_if(any_with_component::<game::Selected>()),
|
|
selected_position.run_if(any_with_component::<game::Selected>()),
|
|
).run_if(resource_exists::<EditorActive>()));
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Resource)]
|
|
struct EditorActive;
|
|
|
|
fn init_editor(
|
|
mut commands: Commands,
|
|
) {
|
|
info!("Starting editor");
|
|
}
|
|
|
|
fn toggle_editor(
|
|
mut events: EventReader<KeyboardInput>,
|
|
active: Option<Res<EditorActive>>,
|
|
mut commands: Commands,
|
|
) {
|
|
events
|
|
.read()
|
|
.filter(|KeyboardInput {
|
|
state, key_code, ..
|
|
}| *state == ButtonState::Pressed && *key_code == Some(KeyCode::F3))
|
|
.for_each(|_| match active {
|
|
Some(_) => commands.remove_resource::<EditorActive>(),
|
|
None => commands.insert_resource(EditorActive),
|
|
});
|
|
}
|
|
|
|
fn aabb_gizmo(
|
|
added: Query<Entity, Added<game::Selected>>,
|
|
mut removed: RemovedComponents<game::Selected>,
|
|
selected: Query<Entity, With<game::Selected>>,
|
|
active: Option<Res<EditorActive>>,
|
|
mut commands: Commands,
|
|
) {
|
|
added.iter().for_each(|e| {
|
|
commands.entity(e).insert(AabbGizmo { color: Some(Color::RED) });
|
|
});
|
|
removed.read().for_each(|e| {
|
|
commands.entity(e).remove::<AabbGizmo>();
|
|
});
|
|
match active {
|
|
Some(_) => selected.iter().for_each(|e| {
|
|
commands.entity(e).insert(AabbGizmo { color: Some(Color::RED) });
|
|
}),
|
|
None => selected.iter().for_each(|e| {
|
|
commands.entity(e).remove::<AabbGizmo>();
|
|
}),
|
|
}
|
|
}
|
|
|
|
/// Draw a gizmo showing cardinal directions for a selected object
|
|
fn selected_gizmo(
|
|
selected: Query<(Entity, &Transform, &GlobalTransform), With<game::Selected>>,
|
|
mut gizmos: Gizmos,
|
|
) {
|
|
selected.iter().for_each(|(e, t, g)| {
|
|
let s = g.translation();
|
|
gizmos.ray(s, Vec3::X, Color::RED);
|
|
gizmos.ray(s, Vec3::Y, Color::GREEN);
|
|
gizmos.ray(s, Vec3::Z, Color::BLUE);
|
|
});
|
|
}
|
|
|
|
fn selected_position(
|
|
selected: Query<(Entity, &GlobalTransform), With<game::Selected>>,
|
|
mut debug_info: ResMut<debug::DebugInfo>,
|
|
) {
|
|
let val = selected.iter().map(|(e, gt)| {
|
|
format!("\n{:?} {:?}", e, gt.translation())
|
|
}).collect::<Vec<String>>().join("");
|
|
debug_info.set("Position".into(), val);
|
|
} |