diff --git a/crates/bevy_winit/src/custom_cursor.rs b/crates/bevy_winit/src/custom_cursor.rs index fe6538b915..6d3d804dd3 100644 --- a/crates/bevy_winit/src/custom_cursor.rs +++ b/crates/bevy_winit/src/custom_cursor.rs @@ -204,11 +204,16 @@ pub(crate) fn transform_hotspot( ) -> (u16, u16) { let hotspot_x = hotspot.0 as f32; let hotspot_y = hotspot.1 as f32; + let (width, height) = (rect.width(), rect.height()); - let hotspot_x = if flip_x { width - hotspot_x } else { hotspot_x }; + let hotspot_x = if flip_x { + (width - 1.0).max(0.0) - hotspot_x + } else { + hotspot_x + }; let hotspot_y = if flip_y { - height - hotspot_y + (height - 1.0).max(0.0) - hotspot_y } else { hotspot_y }; @@ -576,46 +581,26 @@ mod tests { ); #[test] - fn test_transform_hotspot_no_flip() { - let hotspot = (10, 20); - let rect = Rect { - min: Vec2::ZERO, - max: Vec2::new(100.0, 200.0), - }; - let transformed = transform_hotspot(hotspot, false, false, rect); - assert_eq!(transformed, (10, 20)); - } + fn test_transform_hotspot() { + fn test(hotspot: (u16, u16), flip_x: bool, flip_y: bool, rect: Rect, expected: (u16, u16)) { + let transformed = transform_hotspot(hotspot, flip_x, flip_y, rect); + assert_eq!(transformed, expected); - #[test] - fn test_transform_hotspot_flip_x() { - let hotspot = (10, 20); - let rect = Rect { - min: Vec2::ZERO, - max: Vec2::new(100.0, 200.0), - }; - let transformed = transform_hotspot(hotspot, true, false, rect); - assert_eq!(transformed, (90, 20)); - } + // Round-trip test: Applying the same transformation again should + // reverse it. + let transformed = transform_hotspot(transformed, flip_x, flip_y, rect); + assert_eq!(transformed, hotspot); + } - #[test] - fn test_transform_hotspot_flip_y() { - let hotspot = (10, 20); let rect = Rect { min: Vec2::ZERO, max: Vec2::new(100.0, 200.0), }; - let transformed = transform_hotspot(hotspot, false, true, rect); - assert_eq!(transformed, (10, 180)); - } - #[test] - fn test_transform_hotspot_flip_both() { - let hotspot = (10, 20); - let rect = Rect { - min: Vec2::ZERO, - max: Vec2::new(100.0, 200.0), - }; - let transformed = transform_hotspot(hotspot, true, true, rect); - assert_eq!(transformed, (90, 180)); + test((10, 20), false, false, rect, (10, 20)); // no flip + test((10, 20), true, false, rect, (89, 20)); // flip X + test((10, 20), false, true, rect, (10, 179)); // flip Y + test((10, 20), true, true, rect, (89, 179)); // flip both + test((0, 0), true, true, rect, (99, 199)); // flip both (bounds check) } }