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

Remove custom rounding #16097

Merged
merged 19 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
e2b1cb7
Enable taffy's builtin layout coords rounding
ickshonpe Oct 23, 2024
275a06b
Removed the coordinate rounding from `ui_layout_system`.
ickshonpe Oct 23, 2024
4567842
Merge branch 'main' into remove-custom-rounding
ickshonpe Oct 25, 2024
5c7ca68
Changed the initial scaling in the test before the while loop from `1…
ickshonpe Oct 25, 2024
536b784
Update crates/bevy_ui/src/layout/mod.rs
ickshonpe Oct 25, 2024
22fb21f
Merge branch 'main' into remove-custom-rounding
ickshonpe Oct 25, 2024
e48b4d8
Modified `ui_rounding_text` so it passes, accuracy not so important w…
ickshonpe Oct 27, 2024
2d1c9e5
Merge branch 'main' into remove-custom-rounding
ickshonpe Oct 27, 2024
e0c651a
Merge branch 'main' into remove-custom-rounding
ickshonpe Oct 27, 2024
b9985fb
Merge branch 'main' into remove-custom-rounding
ickshonpe Oct 27, 2024
5766f43
Clean up, removed `unrounded_size` field from `Node`
ickshonpe Oct 27, 2024
7b96e5c
Removed too many brackets
ickshonpe Oct 27, 2024
9d80d38
Brought back unrounded sizes
ickshonpe Oct 27, 2024
321f3a6
Merge branch 'main' into remove-custom-rounding
ickshonpe Oct 27, 2024
faf5965
Removed the no longer necessary `absolute_location` parameter from `u…
ickshonpe Oct 27, 2024
b635211
Merge branch 'remove-custom-rounding' of https://github.com/ickshonpe…
ickshonpe Oct 27, 2024
d58f037
Comments and a few renamings of local variables for clarity.
ickshonpe Oct 28, 2024
f2d11a8
Merge branch 'main' into remove-custom-rounding
ickshonpe Oct 28, 2024
cab2a2a
Merge branch 'main' into remove-custom-rounding
ickshonpe Oct 28, 2024
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
51 changes: 8 additions & 43 deletions crates/bevy_ui/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,10 @@ with UI components as a child of an entity without UI components, your UI layout

absolute_location += layout_location;

let rounded_size = approx_round_layout_coords(absolute_location + layout_size)
- approx_round_layout_coords(absolute_location);
let rounded_size = (absolute_location + layout_size) - (absolute_location);
Copy link
Contributor

Choose a reason for hiding this comment

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

Parentheses are redundant


let rounded_location =
approx_round_layout_coords(layout_location - parent_scroll_position)
+ 0.5 * (rounded_size - parent_size);
(layout_location - parent_scroll_position) + 0.5 * (rounded_size - parent_size);

// only trigger change detection when the new values are different
if node.calculated_size != rounded_size || node.unrounded_size != layout_size {
Expand Down Expand Up @@ -423,10 +421,9 @@ with UI components as a child of an entity without UI components, your UI layout
})
.unwrap_or_default();

let round_content_size = approx_round_layout_coords(
let round_content_size =
Vec2::new(layout.content_size.width, layout.content_size.height)
* inverse_target_scale_factor,
);
* inverse_target_scale_factor;
let max_possible_offset = (round_content_size - rounded_size).max(Vec2::ZERO);
let clamped_scroll_position = scroll_position.clamp(Vec2::ZERO, max_possible_offset);

Expand Down Expand Up @@ -454,27 +451,6 @@ with UI components as a child of an entity without UI components, your UI layout
}
}

#[inline]
/// Round `value` to the nearest whole integer, with ties (values with a fractional part equal to 0.5) rounded towards positive infinity.
fn approx_round_ties_up(value: f32) -> f32 {
(value + 0.5).floor()
}

#[inline]
/// Rounds layout coordinates by rounding ties upwards.
///
/// Rounding ties up avoids gaining a pixel when rounding bounds that span from negative to positive.
///
/// Example: The width between bounds of -50.5 and 49.5 before rounding is 100, using:
/// - `f32::round`: width becomes 101 (rounds to -51 and 50).
/// - `round_ties_up`: width is 100 (rounds to -50 and 50).
fn approx_round_layout_coords(value: Vec2) -> Vec2 {
Vec2 {
x: approx_round_ties_up(value.x),
y: approx_round_ties_up(value.y),
}
}

#[cfg(test)]
mod tests {
use taffy::TraversePartialTree;
Expand All @@ -493,7 +469,7 @@ mod tests {
use bevy_hierarchy::{
despawn_with_children_recursive, BuildChildren, ChildBuild, Children, Parent,
};
use bevy_math::{vec2, Rect, UVec2, Vec2};
use bevy_math::{Rect, UVec2, Vec2};
use bevy_render::{
camera::{ManualTextureViews, OrthographicProjection},
prelude::Camera,
Expand All @@ -510,21 +486,10 @@ mod tests {
};

use crate::{
layout::{approx_round_layout_coords, ui_surface::UiSurface},
prelude::*,
ui_layout_system,
update::update_target_camera_system,
ContentSize, LayoutContext,
layout::ui_surface::UiSurface, prelude::*, ui_layout_system,
update::update_target_camera_system, ContentSize, LayoutContext,
};

#[test]
fn round_layout_coords_must_round_ties_up() {
assert_eq!(
approx_round_layout_coords(vec2(-50.5, 49.5)),
vec2(-50., 50.)
);
}

// these window dimensions are easy to convert to and from percentage values
const WINDOW_WIDTH: f32 = 1000.;
const WINDOW_HEIGHT: f32 = 100.;
Expand Down Expand Up @@ -1123,7 +1088,7 @@ mod tests {
.collect::<Vec<Entity>>();

for r in [2, 3, 5, 7, 11, 13, 17, 19, 21, 23, 29, 31].map(|n| (n as f32).recip()) {
let mut s = r;
let mut s = 1. - r;
while s <= 5. {
world.resource_mut::<UiScale>().0 = s;
ui_schedule.run(&mut world);
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_ui/src/layout/ui_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ impl fmt::Debug for UiSurface {

impl Default for UiSurface {
fn default() -> Self {
let mut taffy: TaffyTree<NodeMeasure> = TaffyTree::new();
taffy.disable_rounding();
let taffy: TaffyTree<NodeMeasure> = TaffyTree::new();
Self {
entity_to_taffy: Default::default(),
camera_entity_to_taffy: Default::default(),
Expand Down