Skip to content

Commit

Permalink
chore: extract debug-window-labels plugin (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
francisdb authored Jan 31, 2025
1 parent 290bc58 commit 37c2330
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 35 deletions.
85 changes: 85 additions & 0 deletions vpxtool_gui/src/debug_window_labels.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use bevy::prelude::*;
use bevy::render::camera::RenderTarget;
use bevy::window::{WindowCreated, WindowRef};

#[derive(Component)]
struct LabelMarker;

#[derive(Resource, Clone, Default)]
pub(crate) struct WindowNameOptions {
pub enabled: bool,
}

/// Plugin that adds a label to each window with the window's name
pub(crate) fn plugin(app: &mut App) {
app.init_resource::<WindowNameOptions>();
app.add_systems(Update, (add_label_to_new_windows, switch_visibility));
}

fn switch_visibility(
options: Res<WindowNameOptions>,
mut visibility_query: Query<&mut Visibility, With<LabelMarker>>,
) {
for mut visibility in visibility_query.iter_mut() {
*visibility = match options.enabled {
true => Visibility::Visible,
false => Visibility::Hidden,
};
}
}

fn add_label_to_new_windows(
mut created_events: EventReader<WindowCreated>,
mut commands: Commands,
window_query: Query<(Entity, &Window)>,
camera_query: Query<(Entity, &Camera)>,
) {
for event in created_events.read() {
info!("Window created: {:?}", event);
let (_, window) = window_query.get(event.window).unwrap();
// find the camera entity for the window
//camera {
// target: RenderTarget::Window(

let window_camera_entity = camera_query
.iter()
.find(|(_, camera)| {
if let RenderTarget::Window(WindowRef::Entity(window_entity)) = camera.target {
window_entity == event.window
} else {
false
}
})
.map(|(entity, _)| entity);

// if there is no camera for the window this must be the primary window
let window_camera_entity = window_camera_entity.unwrap_or_else(|| {
camera_query
.iter()
.find(|(_, camera)| {
matches!(camera.target, RenderTarget::Window(WindowRef::Primary))
})
.map(|(entity, _)| entity)
.unwrap()
});

label_window(&mut commands, window_camera_entity, window);
}
}

fn label_window(commands: &mut Commands, window_camera_entity: Entity, window: &Window) {
let window_label_node = Node {
position_type: PositionType::Absolute,
top: Val::Px(12.0),
left: Val::Px(12.0),
..default()
};
let name = window.name.clone().unwrap_or("Unnamed Window".to_string());
commands.spawn((
Text::new(name),
TextFont::from_font_size(8.0),
window_label_node,
TargetCamera(window_camera_entity),
LabelMarker,
));
}
9 changes: 6 additions & 3 deletions vpxtool_gui/src/guifrontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ pub fn guifrontend(config: ResolvedConfig) {
#[cfg(debug_assertions)]
{
app.add_plugins(bevy_mini_fps::fps_plugin!());
app.add_plugins(bevy_dev_tools::ui_debug_overlay::DebugUiPlugin)
app.add_plugins(crate::debug_window_labels::plugin)
//app.add_plugins(bevy_dev_tools::ui_debug_overlay::DebugUiPlugin)
.add_systems(Update, toggle_overlay);
app.add_plugins(bevy_dev_tools::fps_overlay::FpsOverlayPlugin {
config: bevy_dev_tools::fps_overlay::FpsOverlayConfig {
Expand All @@ -197,12 +198,14 @@ pub fn guifrontend(config: ResolvedConfig) {
// The system that will enable/disable the debug outlines around the nodes
fn toggle_overlay(
input: Res<ButtonInput<KeyCode>>,
mut options: ResMut<bevy_dev_tools::ui_debug_overlay::UiDebugOptions>,
//mut options: ResMut<bevy_dev_tools::ui_debug_overlay::UiDebugOptions>,
mut window_name_options: ResMut<crate::debug_window_labels::WindowNameOptions>,
) {
info_once!("The debug outlines are enabled, press Space to turn them on/off");
if input.just_pressed(KeyCode::KeyD) {
// The toggle method will enable the debug_overlay if disabled and disable if enabled
options.toggle();
//options.toggle();
window_name_options.enabled = !window_name_options.enabled;
}
}

Expand Down
4 changes: 4 additions & 0 deletions vpxtool_gui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ mod pipelines;
mod process;
mod wheel;
mod windowing;

// only for development
#[cfg(debug_assertions)]
mod debug_window_labels;
42 changes: 10 additions & 32 deletions vpxtool_gui/src/windowing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,15 @@ fn setup_other_window(commands: &mut Commands, vpx_config: &VpxConfig, window_ty
};
let window_entity = commands.spawn((window, vpx_window_info)).id();
let render_layers = layer_for_window_type(window_type);
let window_camera = commands
.spawn((
DMDCamera,
Camera2d,
Camera {
target: RenderTarget::Window(WindowRef::Entity(window_entity)),
..default()
},
render_layers,
))
.id();

#[cfg(debug_assertions)]
label_window(commands, window_camera, &window_type.to_string());
commands.spawn((
DMDCamera,
Camera2d,
Camera {
target: RenderTarget::Window(WindowRef::Entity(window_entity)),
..default()
},
render_layers,
));
} else {
info!("Window [{}] disabled", window_type);
}
Expand All @@ -229,9 +224,7 @@ fn setup_playfield_window2(
vpx_config: &VpxConfig,
primary_window_query: Query<Entity, With<PrimaryWindow>>,
) {
let playfield_window_camera = commands.spawn((PlayfieldCamera, Camera2d)).id();
#[cfg(debug_assertions)]
label_window(commands, playfield_window_camera, "Playfield");
commands.spawn((PlayfieldCamera, Camera2d));
// add VpxWindowInfo to playfield window, don't think we can do this in the WindowPlugin
if let Some(playfield_info) = vpx_config.config.get_window_info(WindowType::Playfield) {
let vpx_window_info = VpxWindowInfo {
Expand All @@ -243,21 +236,6 @@ fn setup_playfield_window2(
}
}

fn label_window(commands: &mut Commands, window_camera: Entity, name: &str) {
let window_label_node = Node {
position_type: PositionType::Absolute,
top: Val::Px(12.0),
left: Val::Px(12.0),
..default()
};
commands.spawn((
Text::new(name),
TextFont::from_font_size(8.0),
window_label_node,
TargetCamera(window_camera),
));
}

pub(crate) fn setup_playfield_window(vpinball_config: &VPinballConfig) -> Window {
let mut window = Window {
name: Some("playfield".to_string()),
Expand Down

0 comments on commit 37c2330

Please sign in to comment.