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

Exposed a CursorPosition resource #5079

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
32 changes: 32 additions & 0 deletions crates/bevy_window/src/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
use crate::WindowId;
use bevy_math::Vec2;
use bevy_utils::HashMap;

#[derive(Debug, Clone, Default)]
/// Resource storing the cursor position on the app windows
pub struct CursorPositions {
pub(crate) positions: HashMap<WindowId, Vec2>,
}

impl CursorPositions {
/// Retrieves the cursor position from the given [`WindowId`]
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()
}
Copy link
Member

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?

Copy link
Contributor Author

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

}

/// The icon to display for a window's cursor.
///
/// Examples of all of these cursors can be found [here](https://www.w3schools.com/cssref/playit.asp?filename=playcss_cursor).
Expand Down
8 changes: 8 additions & 0 deletions crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub struct WindowPlugin {
/// If this system (or a replacement) is not running, the close button will have no effect.
/// This may surprise your users. It is recommended to leave this setting as `true`.
pub close_when_requested: bool,
/// Whether the plugin will update a [`CursorPositions`] resource every frame.
pub update_cursor_position: bool,
}

impl Default for WindowPlugin {
Expand All @@ -55,6 +57,7 @@ impl Default for WindowPlugin {
add_primary_window: true,
exit_on_all_closed: true,
close_when_requested: true,
update_cursor_position: true,
}
}
}
Expand Down Expand Up @@ -91,6 +94,11 @@ impl Plugin for WindowPlugin {
});
}

if self.update_cursor_position {
app.init_resource::<CursorPositions>();
app.add_system(update_cursor_positions);
}

if self.exit_on_all_closed {
app.add_system(exit_on_all_closed);
}
Expand Down
10 changes: 9 additions & 1 deletion crates/bevy_window/src/system.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
use crate::{Window, WindowCloseRequested, WindowFocused, WindowId, Windows};
use crate::{CursorPositions, Window, WindowCloseRequested, WindowFocused, WindowId, Windows};

use bevy_app::AppExit;
use bevy_ecs::prelude::*;
use bevy_input::{keyboard::KeyCode, Input};

/// Updates the [`CursorPositions`] resource
pub fn update_cursor_positions(mut positions: ResMut<CursorPositions>, windows: Res<Windows>) {
positions.positions = windows
.iter()
.flat_map(|w| w.cursor_position().map(|p| (w.id(), p)))
.collect();
}

/// Exit the application when there are no open windows.
///
/// This system is added by the [`WindowPlugin`] in the default configuration.
Expand Down