Skip to content

Commit

Permalink
Store mouse cursor position in Window (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
smokku authored Dec 3, 2020
1 parent b8f8d46 commit 1f2e417
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
10 changes: 10 additions & 0 deletions crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ pub struct CursorMoved {
pub position: Vec2,
}

#[derive(Debug, Clone)]
pub struct CursorEntered {
pub id: WindowId,
}

#[derive(Debug, Clone)]
pub struct CursorLeft {
pub id: WindowId,
}

/// An event that is sent whenever a window receives a character from the OS or underlying system.
#[derive(Debug, Clone)]
pub struct ReceivedCharacter {
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ pub use window::*;
pub use windows::*;

pub mod prelude {
pub use crate::{CursorMoved, ReceivedCharacter, Window, WindowDescriptor, Windows};
pub use crate::{
CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, Window, WindowDescriptor,
Windows,
};
}

use bevy_app::prelude::*;
Expand All @@ -36,6 +39,8 @@ impl Plugin for WindowPlugin {
.add_event::<WindowCloseRequested>()
.add_event::<CloseWindow>()
.add_event::<CursorMoved>()
.add_event::<CursorEntered>()
.add_event::<CursorLeft>()
.add_event::<ReceivedCharacter>()
.init_resource::<Windows>();

Expand Down
20 changes: 18 additions & 2 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy_math::Vec2;
use bevy_utils::Uuid;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -42,6 +43,7 @@ pub struct Window {
decorations: bool,
cursor_visible: bool,
cursor_locked: bool,
cursor_position: Option<Vec2>,
mode: WindowMode,
#[cfg(target_arch = "wasm32")]
pub canvas: Option<String>,
Expand Down Expand Up @@ -107,6 +109,7 @@ impl Window {
decorations: window_descriptor.decorations,
cursor_visible: window_descriptor.cursor_visible,
cursor_locked: window_descriptor.cursor_locked,
cursor_position: None,
mode: window_descriptor.mode,
#[cfg(target_arch = "wasm32")]
canvas: window_descriptor.canvas.clone(),
Expand Down Expand Up @@ -145,13 +148,15 @@ impl Window {
.push(WindowCommand::SetResolution { width, height });
}

#[doc(hidden)]
#[allow(missing_docs)]
#[inline]
pub fn update_resolution_from_backend(&mut self, width: u32, height: u32) {
self.width = width;
self.height = height;
}

#[doc(hidden)]
#[allow(missing_docs)]
#[inline]
pub fn update_scale_factor_from_backend(&mut self, scale_factor: f64) {
self.scale_factor = scale_factor;
}
Expand Down Expand Up @@ -227,11 +232,22 @@ impl Window {
});
}

#[inline]
pub fn cursor_position(&self) -> Option<Vec2> {
self.cursor_position
}

pub fn set_cursor_position(&mut self, x: i32, y: i32) {
self.command_queue
.push(WindowCommand::SetCursorPosition { x, y });
}

#[allow(missing_docs)]
#[inline]
pub fn update_cursor_position_from_backend(&mut self, cursor_position: Option<Vec2>) {
self.cursor_position = cursor_position;
}

#[inline]
pub fn mode(&self) -> WindowMode {
self.mode
Expand Down
38 changes: 32 additions & 6 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use bevy_ecs::{Resources, World};
use bevy_math::Vec2;
use bevy_utils::tracing::{error, trace};
use bevy_window::{
CreateWindow, CursorMoved, ReceivedCharacter, Window, WindowCloseRequested, WindowCreated,
WindowResized, Windows,
CreateWindow, CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, Window,
WindowCloseRequested, WindowCreated, WindowResized, Windows,
};
use winit::{
event::{self, DeviceEvent, Event, WindowEvent},
Expand Down Expand Up @@ -224,17 +224,43 @@ pub fn winit_runner(mut app: App) {
let mut cursor_moved_events =
app.resources.get_mut::<Events<CursorMoved>>().unwrap();
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
let mut windows = app.resources.get_mut::<Windows>().unwrap();
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
let window = winit_windows.get_window(window_id).unwrap();
let position = position.to_logical(window.scale_factor());
let inner_size = window.inner_size().to_logical::<f32>(window.scale_factor());
let winit_window = winit_windows.get_window(window_id).unwrap();
let window = windows.get_mut(window_id).unwrap();
let position = position.to_logical(winit_window.scale_factor());
let inner_size = winit_window
.inner_size()
.to_logical::<f32>(winit_window.scale_factor());

// move origin to bottom left
let y_position = inner_size.height - position.y;

let position = Vec2::new(position.x, y_position);
window.update_cursor_position_from_backend(Some(position));

cursor_moved_events.send(CursorMoved {
id: window_id,
position: Vec2::new(position.x, y_position),
position,
});
}
WindowEvent::CursorEntered { .. } => {
let mut cursor_entered_events =
app.resources.get_mut::<Events<CursorEntered>>().unwrap();
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
cursor_entered_events.send(CursorEntered { id: window_id });
}
WindowEvent::CursorLeft { .. } => {
let mut cursor_left_events =
app.resources.get_mut::<Events<CursorLeft>>().unwrap();
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
let mut windows = app.resources.get_mut::<Windows>().unwrap();
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
let window = windows.get_mut(window_id).unwrap();
window.update_cursor_position_from_backend(None);
cursor_left_events.send(CursorLeft { id: window_id });
}
WindowEvent::MouseInput { state, button, .. } => {
let mut mouse_button_input_events =
app.resources.get_mut::<Events<MouseButtonInput>>().unwrap();
Expand Down

0 comments on commit 1f2e417

Please sign in to comment.