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

[bevy_ui/layout] Make fields private in UiSurface #13359

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 11 additions & 86 deletions crates/bevy_ui/src/layout/debug.rs
Original file line number Diff line number Diff line change
@@ -1,92 +1,17 @@
use std::fmt::Write;

use taffy::{NodeId, TraversePartialTree};

use bevy_ecs::prelude::Entity;
use bevy_utils::HashMap;

use crate::layout::ui_surface::UiSurface;

/// Prints a debug representation of the computed layout of the UI layout tree for each window.
/// Prints a debug representation of the computed layout of the UI layout tree for each camera.
#[deprecated(
since = "0.13.3",
note = "use `ui_surface.ui_layout_tree_debug_string()` instead"
)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see: #13359 (comment)

If we leave ui_layout_tree_debug_string in UiSurface then this function should retain the deprecation notice so it can be removed later.

  • Either Remove the notice or update the version to match whatever version this is slated to land in.

pub fn print_ui_layout_tree(ui_surface: &UiSurface) {
let taffy_to_entity: HashMap<NodeId, Entity> = ui_surface
.entity_to_taffy
.iter()
.map(|(entity, node)| (*node, *entity))
.collect();
for (&entity, roots) in &ui_surface.camera_roots {
let mut out = String::new();
for root in roots {
print_node(
ui_surface,
&taffy_to_entity,
entity,
root.implicit_viewport_node,
false,
String::new(),
&mut out,
);
let debug_layout_tree = match ui_surface.ui_layout_tree_debug_string() {
Ok(output) => output,
Err(err) => {
bevy_utils::tracing::error!("Failed to print ui layout tree: {err}");
return;
}
bevy_utils::tracing::info!("Layout tree for camera entity: {entity:?}\n{out}");
}
}

/// Recursively navigates the layout tree printing each node's information.
fn print_node(
ui_surface: &UiSurface,
taffy_to_entity: &HashMap<NodeId, Entity>,
entity: Entity,
node: NodeId,
has_sibling: bool,
lines_string: String,
acc: &mut 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);

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

let fork_string = if has_sibling {
"├── "
} else {
"└── "
};
writeln!(
acc,
"{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_variant,
x = layout.location.x,
y = layout.location.y,
width = layout.size.width,
height = layout.size.height,
measured = if tree.get_node_context(node).is_some() { "measured" } else { "" }
).ok();
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(),
acc,
);
}
bevy_utils::tracing::info!("{debug_layout_tree}");
}
Loading