-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Exposed a CursorPosition
resource
#5079
Conversation
crates/bevy_window/src/cursor.rs
Outdated
pub fn get(&self, id: WindowId) -> Option<Vec2> { | ||
self.positions.get(&id).copied() | ||
} | ||
|
||
/// Retrieves the cursor position from the *primary* window | ||
pub fn primary_position(&self) -> Option<Vec2> { | ||
self.get(WindowId::primary()) | ||
} | ||
|
||
/// Returns an iterator on cursor positions | ||
pub fn positions_iter(&self) -> impl Iterator<Item = &Vec2> { | ||
self.positions.values() | ||
} | ||
|
||
/// Returns an iterator on window ids and cursor positions | ||
pub fn iter(&self) -> impl Iterator<Item = (&WindowId, &Vec2)> { | ||
self.positions.iter() | ||
} |
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.
Why not just have these be methods on Windows
?
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 mean it could be but the related issue #5034 wants a separate resource
Only one window can have the cursor hover over it at a time, so pub struct CursorPosition {
pub(crate) position: Option<(Vec2, WindowId)>,
}
impl CursorPosition {
// Most games only have one window. Make this case the simpler one.
pub fn get(&self) -> Option<Vec2> {
self.position.map(|(pos, _)| pos)
}
pub fn get_with_id(&self) -> Option<(Vec2, WindowId)> {
self.position
}
} Then it becomes: fn my_system(cursor_position: Res<CursorPosition>) {
if let Some(pos) = cursor_position.get() {
// Do stuff
}
} |
@devil-ira thanks, I didn't know that only one window could store the cursor position. I updated the resource |
CursorPositions
resourceCursorPosition
resource
I'm not very happy with this API. I'm closing it |
Whaaat. I like this though. It's certainly an improvement over the current state. Can you say exactly what you're unhappy with? |
Sure, you can adopt it ! I don't really like the extra system approach, I think |
Objective
Addresses #5034
Solution
I added a
CursorPosition
resource, updated every frame ifWindowPlugin::update_cursor_position
is enabled.Usage:
Instead of:
We have:
Notes
I think the update system should probably be in a
PreUpdate
stage?