Some basic editor window boilerplate
For some reson the 'input_just_pressed' condition does not behave as expected...main
parent
2e32534a27
commit
43a3bdab3e
@ -0,0 +1,84 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
/// Menu Plugin; empty struct for Plugin impl
|
||||||
|
pub(crate) struct EditorPlugin;
|
||||||
|
|
||||||
|
impl Plugin for EditorPlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.init_state::<EditorState>();
|
||||||
|
app.add_systems(Startup, init_editor);
|
||||||
|
app.add_systems(Update, toggle_editor.run_if(input_just_pressed(KeyCode::F3)));
|
||||||
|
app.add_systems(OnEnter(EditorState::Open), open_editor);
|
||||||
|
app.add_systems(OnExit(EditorState::Open), close_editor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// State tracking if the editor is open
|
||||||
|
#[derive(States, Debug, Clone, PartialEq, Eq, Hash, Default, Component)]
|
||||||
|
enum EditorState {
|
||||||
|
#[default]
|
||||||
|
Closed,
|
||||||
|
Open,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
struct Editor;
|
||||||
|
|
||||||
|
/// Spawns all base editor entities including window, camera, and UI elements
|
||||||
|
fn init_editor(
|
||||||
|
mut commands: Commands
|
||||||
|
) {
|
||||||
|
// Spawn root editor entity hierarchy
|
||||||
|
commands.spawn(SpatialBundle { ..default() })
|
||||||
|
.with_children(|parent| {
|
||||||
|
|
||||||
|
let editor_window = parent.spawn((
|
||||||
|
Editor,
|
||||||
|
Window {
|
||||||
|
title: "Editor".into(),
|
||||||
|
name: Some("Editor".into()),
|
||||||
|
visible: false,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
)).id();
|
||||||
|
|
||||||
|
// Spawn editor camera
|
||||||
|
let _editor_camera = parent.spawn((
|
||||||
|
Editor,
|
||||||
|
Camera3dBundle {
|
||||||
|
camera: Camera {
|
||||||
|
target: RenderTarget::Window(WindowRef::Entity(editor_window)),
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
)).id();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn open_editor(
|
||||||
|
mut ws: Query<&mut Window, With<Editor>>,
|
||||||
|
) {
|
||||||
|
ws.iter_mut().for_each(|mut w| {
|
||||||
|
w.visible = true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn close_editor(
|
||||||
|
mut ws: Query<&mut Window, With<Editor>>,
|
||||||
|
) {
|
||||||
|
ws.iter_mut().for_each(|mut w| {
|
||||||
|
w.visible = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn toggle_editor(
|
||||||
|
state: Res<State<EditorState>>,
|
||||||
|
mut next_state: ResMut<NextState<EditorState>>,
|
||||||
|
) {
|
||||||
|
info!("Toggling editor on/off");
|
||||||
|
match state.get() {
|
||||||
|
EditorState::Open => next_state.set(EditorState::Closed),
|
||||||
|
EditorState::Closed => next_state.set(EditorState::Open),
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1 @@
|
|||||||
|
// TODO: window management in this file
|
||||||
Loading…
Reference in New Issue