use bevy::camera::RenderTarget;
use bevy::prelude::*;
use bevy::window::{PrimaryWindow, WindowRef, WindowResolution};
use bevy_egui::EguiMultipassSchedule;
use crate::editor_state::EditorWindow;
use crate::EditorWindowContextPass;
pub fn handle_editor_hotkeys(
input: Res<ButtonInput<KeyCode>>,
primary_window: Query<Entity, With<PrimaryWindow>>,
existing_editor_windows: Query<Entity, With<EditorWindow>>,
mut commands: Commands,
) {
if input.pressed(KeyCode::ControlLeft) && input.just_pressed(KeyCode::KeyO) {
if let Ok(_primary_entity) = primary_window.single() {
if existing_editor_windows.is_empty() {
spawn_editor_window(&mut commands);
} else {
info!("🪟 Editor window already exists, ignoring Ctrl+O");
}
}
}
}
fn spawn_editor_window(commands: &mut Commands) {
let window_entity = commands.spawn((
Window {
title: "Gearbox Editor".to_string(),
resolution: WindowResolution::new(1200, 800),
..default()
},
EditorWindow,
)).id();
commands.spawn((
Camera3d::default(),
Camera {
order: 3,
target: RenderTarget::Window(WindowRef::Entity(window_entity)),
..default()
},
EguiMultipassSchedule::new(EditorWindowContextPass),
EditorWindow, ));
info!("🪟 Spawned new editor window");
}
pub fn cleanup_editor_window(
remove: On<Remove, Window>,
cameras: Query<(Entity, &Camera), With<EditorWindow>>,
mut editor_state: ResMut<crate::editor_state::EditorState>,
mut commands: Commands,
) {
let removed_window = remove.entity;
for (cam_entity, camera) in cameras.iter() {
if let RenderTarget::Window(WindowRef::Entity(win_entity)) = camera.target {
if win_entity == removed_window {
commands.entity(cam_entity).despawn();
}
}
}
editor_state.open_machines.clear();
}