Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI layout tree debug print #8521

Merged
merged 7 commits into from May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 79 additions & 0 deletions crates/bevy_ui/src/layout/debug.rs
@@ -0,0 +1,79 @@
use crate::UiSurface;
use bevy_ecs::prelude::Entity;
use bevy_utils::HashMap;
use taffy::prelude::Node;
use taffy::tree::LayoutTree;

pub fn print_ui_layout_tree(ui_surface: &UiSurface) {
ickshonpe marked this conversation as resolved.
Show resolved Hide resolved
let taffy_to_entity: HashMap<Node, Entity> = ui_surface
.entity_to_taffy
.iter()
.map(|(entity, node)| (*node, *entity))
.collect();
for (&entity, &node) in ui_surface.window_nodes.iter() {
println!("Layout tree for window entity: {entity:?}");
print_node(
ui_surface,
&taffy_to_entity,
entity,
node,
false,
String::new(),
);
}
}

fn print_node(
ui_surface: &UiSurface,
taffy_to_entity: &HashMap<Node, Entity>,
entity: Entity,
node: Node,
has_sibling: bool,
lines_string: String,
) {
let tree = &ui_surface.taffy;
let layout = tree.layout(node).unwrap();
let style = tree.style(node).unwrap();

let num_children = tree.child_count(node).unwrap();

let display = match (num_children, style.display) {
(_, taffy::style::Display::None) => "NONE",
(0, _) => "LEAF",
(_, taffy::style::Display::Flex) => "FLEX",
(_, taffy::style::Display::Grid) => "GRID",
};

let fork_string = if has_sibling {
"├── "
} else {
"└── "
};
println!(
"{lines}{fork} {display} [x: {x:<4} y: {y:<4} width: {width:<4} height: {height:<4}] ({entity:?}) {measured}",
lines = lines_string,
fork = fork_string,
display = display,
x = layout.location.x,
y = layout.location.y,
width = layout.size.width,
height = layout.size.height,
measured = if tree.needs_measure(node) { "measured" } else { "" }
);
let bar = if has_sibling { "│ " } else { " " };
let new_string = lines_string + bar;

// Recurse into children
for (index, child_node) in tree.children(node).unwrap().iter().enumerate() {
let has_sibling = index < num_children - 1;
let child_entity = taffy_to_entity.get(child_node).unwrap();
print_node(
ui_surface,
taffy_to_entity,
*child_entity,
*child_node,
has_sibling,
new_string.clone(),
);
}
}
1 change: 1 addition & 0 deletions crates/bevy_ui/src/layout/mod.rs
@@ -1,4 +1,5 @@
mod convert;
pub mod debug;

use crate::{CalculatedSize, Node, Style, UiScale};
use bevy_ecs::{
Expand Down