Some basic editor window boilerplate

For some reson the 'input_just_pressed' condition does not behave as
expected...
main
Elijah C. Voigt 1 year ago
parent 2e32534a27
commit 43a3bdab3e

@ -1,13 +1,12 @@
use std::f32::consts::PI; use std::f32::consts::PI;
use bevy::{ use bevy::{
prelude::*, input::common_conditions::input_just_pressed, prelude::*, render::{
render::{
camera::RenderTarget, camera::RenderTarget,
render_resource::{ render_resource::{
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
}, },
}, }
}; };
fn main() { fn main() {
@ -17,7 +16,7 @@ fn main() {
.add_systems( .add_systems(
Update, Update,
rotate_mesh rotate_mesh
.run_if(|keys: Res<ButtonInput<KeyCode>>| -> bool { keys.pressed(KeyCode::Space) }), .run_if(input_just_pressed(KeyCode::Space)),
) )
.run(); .run();
} }

@ -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),
}
}

@ -158,7 +158,6 @@ fn move_die(
} }
_ => (), _ => (),
} }
info!("Moved die {:?}", t.translation);
}); });
} }
_ => (), _ => (),

@ -16,17 +16,32 @@ pub(crate) mod ecs;
/// Imports used across the project /// Imports used across the project
pub(crate) mod prelude; pub(crate) mod prelude;
/// Different game modes
pub(crate) mod game; pub(crate) mod game;
/// Debugger/Editor for inspecting the game
pub(crate) mod editor;
use crate::prelude::*; use crate::prelude::*;
fn main() { fn main() {
let mut app = App::new(); let mut app = App::new();
app.add_plugins(bevy::DefaultPlugins.set(low_latency_window_plugin())); app.add_plugins(bevy::DefaultPlugins
.set(low_latency_window_plugin())
.set(WindowPlugin {
exit_condition: ExitCondition::OnPrimaryClosed,
close_when_requested: false,
..default()
})
);
app.add_plugins(bevy_mod_picking::DefaultPickingPlugins); app.add_plugins(bevy_mod_picking::DefaultPickingPlugins);
app.add_plugins(menu::MenuPlugin); app.add_plugins(menu::MenuPlugin);
app.add_plugins(ui::UiPlugin); app.add_plugins(ui::UiPlugin);
app.add_plugins(game::GamePlugin); app.add_plugins(game::GamePlugin);
app.add_plugins(editor::EditorPlugin);
app.add_systems(Update,
handle_window_close.run_if(on_event::<WindowCloseRequested>())
);
app.run(); app.run();
} }
@ -43,3 +58,25 @@ pub(crate) fn manage_visibility<SC: States + Component>(
} }
}); });
} }
/// When the primary window is requested to close, do so (exiting the game).
/// When a non-primary window is closed, just hide it instead of actually closing
fn handle_window_close(
mut events: EventReader<WindowCloseRequested>,
primary: Query<Entity, With<PrimaryWindow>>,
mut secondary: Query<&mut Window, Without<PrimaryWindow>>,
mut commands: Commands,
) {
events.read().for_each(|WindowCloseRequested { window }| {
if primary.contains(*window) {
commands.entity(*window).remove::<Window>();
} else {
secondary
.get_mut(*window)
.iter_mut()
.for_each(|w| {
w.visible = false;
});
}
});
}

@ -9,6 +9,10 @@ pub(crate) use bevy::render::camera::RenderTarget;
pub(crate) use bevy::render::render_resource::{ pub(crate) use bevy::render::render_resource::{
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
}; };
pub(crate) use bevy::window::WindowRef;
pub(crate) use bevy::input::common_conditions::input_just_pressed;
pub(crate) use bevy::window::{PrimaryWindow, WindowCloseRequested};
pub(crate) use bevy::window::ExitCondition;
/// Bevy Plugins /// Bevy Plugins
pub(crate) use bevy_mod_picking::prelude::*; pub(crate) use bevy_mod_picking::prelude::*;

@ -0,0 +1 @@
// TODO: window management in this file
Loading…
Cancel
Save