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 all commits
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
48 changes: 48 additions & 0 deletions crates/bevy_window/src/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@
use crate::WindowId;
use bevy_math::Vec2;
use std::ops::Deref;

#[derive(Debug, Clone, Default, Deref)]
/// Resource storing the cursor position on the app window
///
/// ## Usage
///
/// You can use this resource to retrieve the cursor position:
///
/// ```rust
/// # use bevy_window::*;
/// # use bevy_ecs::prelude::*;
/// #
/// fn my_system(cursor: Res<CursorPosition>) {
/// if let Some(pos) = cursor.position() {
/// // Do something
/// }
/// }
/// ```
///
/// If you want both the position and window id you may do:
///
/// ```rust
/// # use bevy_window::*;
/// # use bevy_ecs::prelude::*;
/// #
/// fn my_system(cursor: Res<CursorPosition>) {
/// if let Some((window_id, pos)) = cursor.0 {
/// // Do something
/// }
/// }
/// ```
pub struct CursorPosition(pub Option<(WindowId, Vec2)>);

impl CursorPosition {
/// Retrieves the cursor position from the given [`WindowId`]
pub fn position(&self) -> Option<Vec2> {
self.0.map(|(_, p)| p)
}

/// Retrieves the cursor position from the *primary* window
pub fn window_id(&self) -> Option<WindowId> {
self.0.map(|(id, _)| id)
}
}

/// 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 [`CursorPosition`] 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::<CursorPosition>();
app.add_system(update_cursor_position);
}

if self.exit_on_all_closed {
app.add_system(exit_on_all_closed);
}
Expand Down
9 changes: 8 additions & 1 deletion crates/bevy_window/src/system.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use crate::{Window, WindowCloseRequested, WindowFocused, WindowId, Windows};
use crate::{CursorPosition, 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_position(mut positions: ResMut<CursorPosition>, windows: Res<Windows>) {
positions.0 = windows
.iter()
.find_map(|w| w.cursor_position().map(|p| (w.id(), p)));
}

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