Skip to content

Commit

Permalink
Fixed style
Browse files Browse the repository at this point in the history
  • Loading branch information
alexb910 authored and cart committed Oct 16, 2020
1 parent b158211 commit 73f6817
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
4 changes: 3 additions & 1 deletion crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub enum CursorLockMode {
Unlocked,
}

/// Event that should be sent when the cursor should be locked or unlocked.
#[derive(Debug, Clone)]
pub struct ChangeCursorLockState {
pub id: WindowId,
Expand All @@ -59,8 +60,9 @@ pub enum CursorShowMode {
Hide,
}

/// Event that should be sent when the cursor should be hidden or shown.
#[derive(Debug, Clone)]
pub struct ChangeCursorVisibility {
pub id: WindowId,
pub mode: CursorShowMode,
}
}
36 changes: 20 additions & 16 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use bevy_app::{prelude::*, AppExit};
use bevy_ecs::{IntoThreadLocalSystem, Resources, World};
use bevy_math::Vec2;
use bevy_window::{
CreateWindow, CursorMoved, Window, WindowCloseRequested, WindowCreated, WindowResized, Windows,
ChangeCursorLockState, CursorLockMode, ChangeCursorVisibility, CursorShowMode,
ChangeCursorLockState, ChangeCursorVisibility, CreateWindow, CursorLockMode, CursorMoved,
CursorShowMode, Window, WindowCloseRequested, WindowCreated, WindowResized, Windows,
};
use winit::{
event::{self, DeviceEvent, Event, WindowEvent},
Expand Down Expand Up @@ -146,7 +146,8 @@ pub fn winit_runner(mut app: App) {
let mut create_window_event_reader = EventReader::<CreateWindow>::default();
let mut app_exit_event_reader = EventReader::<AppExit>::default();
let mut cursor_lock_state_event_reader = EventReader::<ChangeCursorLockState>::default();
let mut cursor_visibility_change_event_reader = EventReader::<ChangeCursorVisibility>::default();
let mut cursor_visibility_change_event_reader =
EventReader::<ChangeCursorVisibility>::default();

app.resources
.insert_thread_local(EventLoopProxyPtr(
Expand Down Expand Up @@ -279,9 +280,9 @@ pub fn winit_runner(mut app: App) {
&mut create_window_event_reader,
);
handle_cursor_state_change_events(
&mut app.resources,
&mut cursor_lock_state_event_reader,
&mut cursor_visibility_change_event_reader
&mut app.resources,
&mut cursor_lock_state_event_reader,
&mut cursor_visibility_change_event_reader,
);
app.update();
}
Expand Down Expand Up @@ -316,29 +317,32 @@ fn handle_create_window_events(
fn handle_cursor_state_change_events(
resources: &mut Resources,
cursor_lock_state_event_reader: &mut EventReader<ChangeCursorLockState>,
cursor_visibility_change_event_reader: &mut EventReader<ChangeCursorVisibility>
cursor_visibility_change_event_reader: &mut EventReader<ChangeCursorVisibility>,
) {
let winit_windows = resources.get_mut::<WinitWindows>().unwrap();
let change_cursor_lock_state_events = resources.get::<Events<ChangeCursorLockState>>().unwrap();
let change_cursor_visibility_events = resources.get::<Events<ChangeCursorVisibility>>().unwrap();
let change_cursor_visibility_events =
resources.get::<Events<ChangeCursorVisibility>>().unwrap();

for cursor_lock_event in cursor_lock_state_event_reader.iter(&change_cursor_lock_state_events) {
let id = cursor_lock_event.id;
let window = winit_windows.get_window(id).unwrap();
window.set_cursor_grab(match cursor_lock_event.mode {
CursorLockMode::Locked => true,
CursorLockMode::Unlocked => false,
}).unwrap();

window
.set_cursor_grab(match cursor_lock_event.mode {
CursorLockMode::Locked => true,
CursorLockMode::Unlocked => false,
})
.unwrap();
}

for cursor_visibility_event in cursor_visibility_change_event_reader.iter(&change_cursor_visibility_events) {
for cursor_visibility_event in
cursor_visibility_change_event_reader.iter(&change_cursor_visibility_events)
{
let id = cursor_visibility_event.id;
let window = winit_windows.get_window(id).unwrap();
window.set_cursor_visible(match cursor_visibility_event.mode {
CursorShowMode::Show => true,
CursorShowMode::Hide => false,
});

}
}
}
14 changes: 9 additions & 5 deletions examples/window/cursor_lock.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use bevy::prelude::*;
use bevy::{window::{WindowId, ChangeCursorLockState, ChangeCursorVisibility, CursorShowMode, CursorLockMode}, winit::WinitWindows};
use bevy::{
prelude::*,
window::{
ChangeCursorLockState, ChangeCursorVisibility, CursorLockMode, CursorShowMode, WindowId,
},
};

fn main() {
App::build()
Expand All @@ -21,13 +25,13 @@ fn grab_cursor_system(
let id = WindowId::primary();

lock_state.send(ChangeCursorLockState {
id: id,
id,
mode: CursorLockMode::Locked,
});

show_state.send(ChangeCursorVisibility {
id: id,
id,
mode: CursorShowMode::Hide,
});
}
}
}

0 comments on commit 73f6817

Please sign in to comment.