@@ -254,11 +254,11 @@ pub fn ui_layout_system(
254
254
255
255
absolute_location += layout_location;
256
256
257
- let rounded_size = round_layout_coords ( absolute_location + layout_size)
258
- - round_layout_coords ( absolute_location) ;
257
+ let rounded_size = approx_round_layout_coords ( absolute_location + layout_size)
258
+ - approx_round_layout_coords ( absolute_location) ;
259
259
260
260
let rounded_location =
261
- round_layout_coords ( layout_location) + 0.5 * ( rounded_size - parent_size) ;
261
+ approx_round_layout_coords ( layout_location) + 0.5 * ( rounded_size - parent_size) ;
262
262
263
263
// only trigger change detection when the new values are different
264
264
if node. calculated_size != rounded_size || node. unrounded_size != layout_size {
@@ -315,15 +315,8 @@ pub fn resolve_outlines_system(
315
315
316
316
#[ inline]
317
317
/// Round `value` to the nearest whole integer, with ties (values with a fractional part equal to 0.5) rounded towards positive infinity.
318
- fn round_ties_up ( value : f32 ) -> f32 {
319
- if value. fract ( ) != -0.5 {
320
- // The `round` function rounds ties away from zero. For positive numbers "away from zero" is towards positive infinity.
321
- // So for all positive values, and negative values with a fractional part not equal to 0.5, `round` returns the correct result.
322
- value. round ( )
323
- } else {
324
- // In the remaining cases, where `value` is negative and its fractional part is equal to 0.5, we use `ceil` to round it up towards positive infinity.
325
- value. ceil ( )
326
- }
318
+ fn approx_round_ties_up ( value : f32 ) -> f32 {
319
+ ( value + 0.5 ) . floor ( )
327
320
}
328
321
329
322
#[ inline]
@@ -334,10 +327,10 @@ fn round_ties_up(value: f32) -> f32 {
334
327
/// Example: The width between bounds of -50.5 and 49.5 before rounding is 100, using:
335
328
/// - `f32::round`: width becomes 101 (rounds to -51 and 50).
336
329
/// - `round_ties_up`: width is 100 (rounds to -50 and 50).
337
- fn round_layout_coords ( value : Vec2 ) -> Vec2 {
330
+ fn approx_round_layout_coords ( value : Vec2 ) -> Vec2 {
338
331
Vec2 {
339
- x : round_ties_up ( value. x ) ,
340
- y : round_ties_up ( value. y ) ,
332
+ x : approx_round_ties_up ( value. x ) ,
333
+ y : approx_round_ties_up ( value. y ) ,
341
334
}
342
335
}
343
336
@@ -376,7 +369,7 @@ mod tests {
376
369
use bevy_window:: WindowResolution ;
377
370
use bevy_window:: WindowScaleFactorChanged ;
378
371
379
- use crate :: layout:: round_layout_coords ;
372
+ use crate :: layout:: approx_round_layout_coords ;
380
373
use crate :: layout:: ui_surface:: UiSurface ;
381
374
use crate :: prelude:: * ;
382
375
use crate :: ui_layout_system;
@@ -385,7 +378,10 @@ mod tests {
385
378
386
379
#[ test]
387
380
fn round_layout_coords_must_round_ties_up ( ) {
388
- assert_eq ! ( round_layout_coords( vec2( -50.5 , 49.5 ) ) , vec2( -50. , 50. ) ) ;
381
+ assert_eq ! (
382
+ approx_round_layout_coords( vec2( -50.5 , 49.5 ) ) ,
383
+ vec2( -50. , 50. )
384
+ ) ;
389
385
}
390
386
391
387
// these window dimensions are easy to convert to and from percentage values
0 commit comments