Skip to content

Commit

Permalink
switch winit size to logical
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Nov 29, 2020
1 parent 1f3d506 commit 1fa488b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
7 changes: 5 additions & 2 deletions crates/bevy_winit/src/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ pub fn convert_mouse_button(mouse_button: winit::event::MouseButton) -> MouseBut
}
}

pub fn convert_touch_input(touch_input: winit::event::Touch) -> TouchInput {
pub fn convert_touch_input(
touch_input: winit::event::Touch,
location: winit::dpi::LogicalPosition<f32>,
) -> TouchInput {
TouchInput {
phase: match touch_input.phase {
winit::event::TouchPhase::Started => TouchPhase::Started,
winit::event::TouchPhase::Moved => TouchPhase::Moved,
winit::event::TouchPhase::Ended => TouchPhase::Ended,
winit::event::TouchPhase::Cancelled => TouchPhase::Cancelled,
},
position: Vec2::new(touch_input.location.x as f32, touch_input.location.y as f32),
position: Vec2::new(location.x as f32, location.y as f32),
force: touch_input.force.map(|f| match f {
winit::event::Force::Calibrated {
force,
Expand Down
69 changes: 43 additions & 26 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn change_window(_: &mut World, resources: &mut Resources) {
}
bevy_window::WindowCommand::SetResolution { width, height } => {
let window = winit_windows.get_window(id).unwrap();
window.set_inner_size(winit::dpi::PhysicalSize::new(width, height));
window.set_inner_size(winit::dpi::LogicalSize::new(width, height));
}
bevy_window::WindowCommand::SetVsync { .. } => (),
bevy_window::WindowCommand::SetResizable { resizable } => {
Expand All @@ -97,7 +97,7 @@ fn change_window(_: &mut World, resources: &mut Resources) {
bevy_window::WindowCommand::SetCursorPosition { x, y } => {
let window = winit_windows.get_window(id).unwrap();
window
.set_cursor_position(winit::dpi::PhysicalPosition::new(x, y))
.set_cursor_position(winit::dpi::LogicalPosition::new(x, y))
.unwrap_or_else(|e| error!("Unable to set cursor position: {}", e));
}
}
Expand Down Expand Up @@ -184,29 +184,28 @@ pub fn winit_runner(mut app: App) {
}

match event {
event::Event::WindowEvent {
event: WindowEvent::Resized(size),
window_id: winit_window_id,
..
} => {
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_resolution_from_backend(size.width, size.height);

let mut resize_events = app.resources.get_mut::<Events<WindowResized>>().unwrap();
resize_events.send(WindowResized {
id: window_id,
height: window.height() as usize,
width: window.width() as usize,
});
}
event::Event::WindowEvent {
event,
window_id: winit_window_id,
..
} => match event {
WindowEvent::Resized(size) => {
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 winit_window = winit_windows.get_window(window_id).unwrap();
let window = windows.get_mut(window_id).unwrap();
let size = size.to_logical(winit_window.scale_factor());
window.update_resolution_from_backend(size.width, size.height);

let mut resize_events =
app.resources.get_mut::<Events<WindowResized>>().unwrap();
resize_events.send(WindowResized {
id: window_id,
height: window.height() as usize,
width: window.width() as usize,
});
}
WindowEvent::CloseRequested => {
let mut window_close_requested_events = app
.resources
Expand All @@ -227,12 +226,13 @@ pub fn winit_runner(mut app: App) {
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
let window = winit_windows.get_window(window_id).unwrap();
let inner_size = window.inner_size();
let position = position.to_logical(window.scale_factor());
let inner_size = window.inner_size().to_logical::<f32>(window.scale_factor());
// move origin to bottom left
let y_position = inner_size.height as f32 - position.y as f32;
let y_position = inner_size.height - position.y;
cursor_moved_events.send(CursorMoved {
id: window_id,
position: Vec2::new(position.x as f32, y_position as f32),
position: Vec2::new(position.x, y_position),
});
}
WindowEvent::MouseInput { state, button, .. } => {
Expand Down Expand Up @@ -263,16 +263,22 @@ pub fn winit_runner(mut app: App) {
});
}
},
WindowEvent::Touch(mut touch) => {
WindowEvent::Touch(touch) => {
let mut touch_input_events =
app.resources.get_mut::<Events<TouchInput>>().unwrap();

let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
let windows = app.resources.get_mut::<Windows>().unwrap();
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
let winit_window = winit_windows.get_window(window_id).unwrap();
let mut location = touch.location.to_logical(winit_window.scale_factor());

// FIXME?: On Android window start is top while on PC/Linux/OSX on bottom
if cfg!(target_os = "android") {
let window_height = windows.get_primary().unwrap().height();
touch.location.y = window_height as f64 - touch.location.y;
location.y = window_height as f32 - location.y;
}
touch_input_events.send(converters::convert_touch_input(touch));
touch_input_events.send(converters::convert_touch_input(touch, location));
}
WindowEvent::ReceivedCharacter(c) => {
let mut char_input_events = app
Expand All @@ -288,6 +294,17 @@ pub fn winit_runner(mut app: App) {
char: c,
})
}
WindowEvent::ScaleFactorChanged {
scale_factor,
new_inner_size,
} => {
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();
let size = new_inner_size.to_logical(scale_factor);
window.update_resolution_from_backend(size.width, size.height);
}
_ => {}
},
event::Event::DeviceEvent { ref event, .. } => {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl WinitWindows {
}),
)),
_ => winit_window_builder
.with_inner_size(winit::dpi::PhysicalSize::new(
.with_inner_size(winit::dpi::LogicalSize::new(
window.width(),
window.height(),
))
Expand Down

0 comments on commit 1fa488b

Please sign in to comment.