Skip to content

Commit

Permalink
* Renamed debug module to debug_overlay.
Browse files Browse the repository at this point in the history
* Added `UiDebugOptions` resource that can be used to enable or disable the debug overlay and set the line width.
* New `extract_debug_overlay` system that extracts the node geometry for rendering.
  • Loading branch information
ickshonpe committed Dec 6, 2024
1 parent 3497271 commit f403613
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 2 deletions.
3 changes: 3 additions & 0 deletions crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub use focus::*;
pub use geometry::*;
pub use layout::*;
pub use measurement::*;
use render::debug_overlay::UiDebugOptions;
pub use render::*;
pub use ui_material::*;
pub use ui_node::*;
Expand Down Expand Up @@ -230,6 +231,8 @@ impl Plugin for UiPlugin {
return;
}

app.init_resource::<UiDebugOptions>();

build_ui_render(app);
}

Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ui/src/render/debug.rs

This file was deleted.

98 changes: 98 additions & 0 deletions crates/bevy_ui/src/render/debug_overlay.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use bevy_asset::AssetId;
use bevy_color::Hsla;
use bevy_ecs::entity::Entity;
use bevy_ecs::system::Commands;
use bevy_ecs::system::Query;
use bevy_ecs::system::Res;
use bevy_ecs::system::ResMut;
use bevy_ecs::system::Resource;
use bevy_math::Rect;
use bevy_math::Vec2;
use bevy_render::sync_world::RenderEntity;
use bevy_render::sync_world::TemporaryRenderEntity;
use bevy_render::Extract;
use bevy_sprite::BorderRect;
use bevy_transform::components::GlobalTransform;

use crate::ComputedNode;
use crate::DefaultUiCamera;
use crate::TargetCamera;

use super::ExtractedUiItem;
use super::ExtractedUiNode;
use super::ExtractedUiNodes;
use super::NodeType;

#[derive(Resource)]
pub struct UiDebugOptions {
pub enabled: bool,
pub line_width: f32,
}

impl Default for UiDebugOptions {
fn default() -> Self {
Self {
enabled: true,
line_width: 3.,
}
}
}

#[allow(clippy::too_many_arguments)]
pub fn extract_debug_overlay(
mut commands: Commands,
debug_options: Extract<Res<UiDebugOptions>>,
mut extracted_uinodes: ResMut<ExtractedUiNodes>,
default_ui_camera: Extract<DefaultUiCamera>,
uinode_query: Extract<
Query<(
Entity,
&ComputedNode,
&GlobalTransform,
Option<&TargetCamera>,
)>,
>,
mapping: Extract<Query<RenderEntity>>,
) {
if !debug_options.enabled {
return;
}

for (entity, uinode, transform, camera) in &uinode_query {
let Some(camera_entity) = camera.map(TargetCamera::entity).or(default_ui_camera.get())
else {
continue;
};

let Ok(render_camera_entity) = mapping.get(camera_entity) else {
continue;
};

extracted_uinodes.uinodes.insert(
commands.spawn(TemporaryRenderEntity).id(),
ExtractedUiNode {
stack_index: uinode.stack_index + 2_147_483_647,
color: Hsla::sequential_dispersed(entity.index()).into(),
rect: Rect {
min: Vec2::ZERO,
max: uinode.size,
},
clip: None,
image: AssetId::default(),
camera_entity: render_camera_entity,
item: ExtractedUiItem::Node {
atlas_scaling: None,
transform: transform.compute_matrix(),
flip_x: false,
flip_y: false,
border: BorderRect::square(
debug_options.line_width / uinode.inverse_scale_factor(),
),
border_radius: uinode.border_radius(),
node_type: NodeType::Border,
},
main_entity: entity.into(),
},
);
}
}
6 changes: 5 additions & 1 deletion crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod box_shadow;
pub mod debug;
pub mod debug_overlay;
mod pipeline;
mod render_pass;
mod ui_material_pipeline;
Expand Down Expand Up @@ -42,6 +42,7 @@ use bevy_render::{
};
use bevy_sprite::TextureAtlasLayout;
use bevy_sprite::{BorderRect, SpriteAssetEvents};
use debug_overlay::extract_debug_overlay;

use crate::{Display, Node};
use bevy_text::{ComputedTextBlock, PositionedGlyph, TextColor, TextLayoutInfo};
Expand Down Expand Up @@ -99,6 +100,7 @@ pub enum RenderUiSystem {
ExtractTextureSlice,
ExtractBorders,
ExtractText,
ExtractDebugOverlay,
}

pub fn build_ui_render(app: &mut App) {
Expand Down Expand Up @@ -126,6 +128,7 @@ pub fn build_ui_render(app: &mut App) {
RenderUiSystem::ExtractTextureSlice,
RenderUiSystem::ExtractBorders,
RenderUiSystem::ExtractText,
RenderUiSystem::ExtractDebugOverlay,
)
.chain(),
)
Expand All @@ -137,6 +140,7 @@ pub fn build_ui_render(app: &mut App) {
extract_uinode_images.in_set(RenderUiSystem::ExtractImages),
extract_uinode_borders.in_set(RenderUiSystem::ExtractBorders),
extract_text_sections.in_set(RenderUiSystem::ExtractText),
extract_debug_overlay.in_set(RenderUiSystem::ExtractDebugOverlay),
),
)
.add_systems(
Expand Down

0 comments on commit f403613

Please sign in to comment.