From 3d72f494a2cc136f216422aefbbdfcacff7b8dd1 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Sun, 27 Oct 2024 20:08:51 +0000 Subject: [PATCH] Layout rounding debug example (#16096) # Objective Simple example for debugging layout rounding errors. layout_rounding_debug Any white lines are gaps in the layout caused by coordinate rounding errors. --- Cargo.toml | 9 +++++ examples/testbed/ui_layout_rounding.rs | 56 ++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 examples/testbed/ui_layout_rounding.rs diff --git a/Cargo.toml b/Cargo.toml index a565240deebe0..76e31bc4fedc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3856,3 +3856,12 @@ doc-scrape-examples = true [package.metadata.example.testbed_3d] hidden = true + + +[[example]] +name = "testbed_ui_layout_rounding" +path = "examples/testbed/ui_layout_rounding.rs" +doc-scrape-examples = true + +[package.metadata.example.testbed_ui_layout_rounding] +hidden = true diff --git a/examples/testbed/ui_layout_rounding.rs b/examples/testbed/ui_layout_rounding.rs new file mode 100644 index 0000000000000..fc59e7c6cb138 --- /dev/null +++ b/examples/testbed/ui_layout_rounding.rs @@ -0,0 +1,56 @@ +//! Spawns a simple grid layout with nodes laid out covering a white background useful for catching layout rounding errors. +//! Any white lines seen are gaps in the layout are caused by coordinate rounding bugs. + +use bevy::{ + color::palettes::css::{DARK_BLUE, MAROON}, + prelude::*, + ui::UiScale, + winit::WinitSettings, +}; + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .insert_resource(WinitSettings::desktop_app()) + .insert_resource(UiScale(1.5)) + .add_systems(Startup, setup) + .run(); +} + +fn setup(mut commands: Commands) { + commands.spawn((Camera2d, UiAntiAlias::Off)); + + commands + .spawn(( + Node { + display: Display::Grid, + width: Val::Percent(100.), + height: Val::Percent(100.), + grid_template_rows: vec![RepeatedGridTrack::fr(10, 1.)], + ..Default::default() + }, + BackgroundColor(Color::WHITE), + )) + .with_children(|commands| { + for i in 2..12 { + commands + .spawn(Node { + display: Display::Grid, + grid_template_columns: vec![RepeatedGridTrack::fr(i, 1.)], + ..Default::default() + }) + .with_children(|commands| { + for _ in 0..i { + commands.spawn(( + Node { + border: UiRect::all(Val::Px(5.)), + ..Default::default() + }, + BackgroundColor(MAROON.into()), + BorderColor(DARK_BLUE.into()), + )); + } + }); + } + }); +}