-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Add integer equivalents for Rect
#7984
Add integer equivalents for Rect
#7984
Conversation
I have put this as a draft as I am unsure of how to implement the doc tests for the associated methods. The tests require used of the arguments passed into the macro, however I cannot find an easy way to do this. I can use the Example of bevy/crates/bevy_math/src/rect.rs Lines 139 to 140 in 9439952
|
|
crates/bevy_math/src/rects/irect.rs
Outdated
/// ``` | ||
#[inline] | ||
pub fn from_center_size(origin: IVec2, size: IVec2) -> Self { | ||
assert!(size.cmpge(IVec2::ZERO).all()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we only want this to be a debug_assert
or not. I prefer keeping asserts out of potentially hot math code. Especially since we assert in from_center_half_size
as well.
Removed the assert for checking if the size is positive in bevy/crates/bevy_math/src/rects/urect.rs Line 77 in a19968e
|
#8540 suggests creating a generic |
Note that glam "solves" the issue of code duplication (think of For bevy, I'd be in favor of what nicoburns describes. Having a The disadvantage is that I always found type aliases a bit confusing in rustdoc (which is what makes glam better than nalgebra imo) but I think a |
I think the problem with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few minor things, but I think it's almost ready for merge. Really good contribution, thanks!
For For |
Did a quick poll on Discord and from this I think the best solution is to have |
Someone else also pointed out than inset could return an option instead of an empty rect when a negative insets absolute value is larger than the half size. Personally I think this should be done in a separate PR as it would be a breaking change for Infact all panicking methods should really return an option or result, I can do this in a follow up PR |
…egative insets`
The API is inspired by Druid/Kurbo. It takes the current rectangle and adds (positive) an inset, creating a new larger rectangle.
I'm not. I think the current API is correct. And as you pointed it will break everyone in possibly subtle ways.
This is redundant. If the user wants to know if the resulting rectangle is empty, they can call |
crates/bevy_math/src/rects/urect.rs
Outdated
let mut r = Self { | ||
min: self.min - inset, | ||
max: self.max + inset, | ||
min: (self.min.as_ivec2() - inset).as_uvec2(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather avoid the conversion to signed, although it's a bit unlikely it still loses some precision and will return wrong results for many u32
values which can't fit into an i32
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think expand
and shrink
methods for URect
is the correct solution?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, although that makes the API different from IRect
and Rect
which is probably confusing. But you can just calculate the new size here without conversion, just need to fall back to per-component operation because there's no good way to do it directly with the UVec2
interface I think.
Also about naming I'd expect expand
to expand the current object in-place, vs. expanded
to return a new expanded object.
|
||
/// Returns self as [`Rect`] (f32) | ||
#[inline] | ||
pub fn as_rect(&self) -> Rect { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good idea 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for following through, looks good for me!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well-made! I'm happy enough to have this in the engine even without an internal use for now: it's a natural extension and a useful primitive for game dev.
# Objective Some of the conversion methods on the new rect types introduced in #7984 have misleading names. ## Solution Rename all methods returning an `IRect` to `as_irect` and all methods returning a `URect` to `as_urect`. ## Migration Guide Replace uses of the old method names with the new method names.
…ect (#9085) # Objective Continue #7867 now that we have URect #7984 - Return `URect` instead of `(UVec2, UVec2)` in `Camera::physical_viewport_rect` - Add `URect` and `IRect` to prelude ## Changelog - Changed `Camera::physical_viewport_rect` return type from `(UVec2, UVec2)` to `URect` - `URect` and `IRect` were added to prelude ## Migration Guide Before: ```rust fn view_physical_camera_rect(camera_query: Query<&Camera>) { let camera = camera_query.single(); let Some((min, max)) = camera.physical_viewport_rect() else { return }; dbg!(min, max); } ``` After: ```rust fn view_physical_camera_rect(camera_query: Query<&Camera>) { let camera = camera_query.single(); let Some(URect { min, max }) = camera.physical_viewport_rect() else { return }; dbg!(min, max); } ```
Objective
Add integer equivalents for the
Rect
type.Closes #7967
Solution
IRect
andURect
Changelog
Added
IRect
andURect
types.