Skip to content

Commit

Permalink
switch winit size to logical to be dpi independent (#947)
Browse files Browse the repository at this point in the history
* switch winit size to logical

* make scale factor available from bevy_window
  • Loading branch information
mockersf authored Dec 1, 2020
1 parent 4cd3dd7 commit 6b004f7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 32 deletions.
12 changes: 12 additions & 0 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub struct Window {
#[cfg(target_arch = "wasm32")]
pub canvas: Option<String>,
command_queue: Vec<WindowCommand>,
scale_factor: f64,
}

#[derive(Debug)]
Expand Down Expand Up @@ -110,6 +111,7 @@ impl Window {
#[cfg(target_arch = "wasm32")]
canvas: window_descriptor.canvas.clone(),
command_queue: Vec::new(),
scale_factor: 1.0,
}
}

Expand Down Expand Up @@ -141,6 +143,16 @@ impl Window {
self.height = height;
}

#[doc(hidden)]
pub fn update_scale_factor_from_backend(&mut self, scale_factor: f64) {
self.scale_factor = scale_factor;
}

#[inline]
pub fn scale_factor(&self) -> f64 {
self.scale_factor
}

#[inline]
pub fn title(&self) -> &str {
&self.title
Expand Down
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
74 changes: 46 additions & 28 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,18 @@ 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_scale_factor_from_backend(scale_factor);
window.update_resolution_from_backend(size.width, size.height);
}
_ => {}
},
event::Event::DeviceEvent { ref event, .. } => {
Expand Down Expand Up @@ -327,8 +345,8 @@ fn handle_create_window_events(
let create_window_events = resources.get::<Events<CreateWindow>>().unwrap();
let mut window_created_events = resources.get_mut::<Events<WindowCreated>>().unwrap();
for create_window_event in create_window_event_reader.iter(&create_window_events) {
let window = Window::new(create_window_event.id, &create_window_event.descriptor);
winit_windows.create_window(event_loop, &window);
let mut window = Window::new(create_window_event.id, &create_window_event.descriptor);
winit_windows.create_window(event_loop, &mut window);
let window_id = window.id();
windows.add(window);
window_created_events.send(WindowCreated { id: window_id });
Expand Down
6 changes: 4 additions & 2 deletions crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl WinitWindows {
pub fn create_window(
&mut self,
event_loop: &winit::event_loop::EventLoopWindowTarget<()>,
window: &Window,
window: &mut Window,
) {
#[cfg(target_os = "windows")]
let mut winit_window_builder = {
Expand All @@ -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 Expand Up @@ -100,6 +100,8 @@ impl WinitWindows {
}
}

window.update_scale_factor_from_backend(winit_window.scale_factor());

self.windows.insert(winit_window.id(), winit_window);
}

Expand Down

0 comments on commit 6b004f7

Please sign in to comment.