2021-01-25 04:06:06 +00:00
|
|
|
use bevy_math::IVec2;
|
2020-08-29 00:08:51 +00:00
|
|
|
use bevy_utils::HashMap;
|
2020-12-13 23:05:56 +00:00
|
|
|
use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode};
|
2021-06-02 02:59:17 +00:00
|
|
|
use raw_window_handle::HasRawWindowHandle;
|
2021-03-03 02:56:50 +00:00
|
|
|
use winit::dpi::LogicalSize;
|
2020-03-30 21:53:32 +00:00
|
|
|
|
2020-10-08 18:43:01 +00:00
|
|
|
#[derive(Debug, Default)]
|
2020-03-30 21:53:32 +00:00
|
|
|
pub struct WinitWindows {
|
|
|
|
pub windows: HashMap<winit::window::WindowId, winit::window::Window>,
|
|
|
|
pub window_id_to_winit: HashMap<WindowId, winit::window::WindowId>,
|
|
|
|
pub winit_to_window_id: HashMap<winit::window::WindowId, WindowId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WinitWindows {
|
|
|
|
pub fn create_window(
|
|
|
|
&mut self,
|
|
|
|
event_loop: &winit::event_loop::EventLoopWindowTarget<()>,
|
2020-12-13 23:05:56 +00:00
|
|
|
window_id: WindowId,
|
|
|
|
window_descriptor: &WindowDescriptor,
|
|
|
|
) -> Window {
|
2020-07-26 19:36:01 +00:00
|
|
|
#[cfg(target_os = "windows")]
|
2020-08-13 09:27:51 +00:00
|
|
|
let mut winit_window_builder = {
|
2020-07-26 19:36:01 +00:00
|
|
|
use winit::platform::windows::WindowBuilderExtWindows;
|
2020-08-13 08:47:40 +00:00
|
|
|
winit::window::WindowBuilder::new().with_drag_and_drop(false)
|
2020-07-26 19:36:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "windows"))]
|
2020-08-13 08:47:40 +00:00
|
|
|
let mut winit_window_builder = winit::window::WindowBuilder::new();
|
2020-08-13 06:18:10 +00:00
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
winit_window_builder = match window_descriptor.mode {
|
2020-08-13 09:27:51 +00:00
|
|
|
WindowMode::BorderlessFullscreen => winit_window_builder.with_fullscreen(Some(
|
|
|
|
winit::window::Fullscreen::Borderless(event_loop.primary_monitor()),
|
|
|
|
)),
|
|
|
|
WindowMode::Fullscreen { use_size } => winit_window_builder.with_fullscreen(Some(
|
|
|
|
winit::window::Fullscreen::Exclusive(match use_size {
|
2020-10-15 18:42:19 +00:00
|
|
|
true => get_fitting_videomode(
|
|
|
|
&event_loop.primary_monitor().unwrap(),
|
2020-12-13 23:05:56 +00:00
|
|
|
window_descriptor.width as u32,
|
|
|
|
window_descriptor.height as u32,
|
2020-10-15 18:42:19 +00:00
|
|
|
),
|
2020-10-02 19:24:30 +00:00
|
|
|
false => get_best_videomode(&event_loop.primary_monitor().unwrap()),
|
2020-08-13 09:27:51 +00:00
|
|
|
}),
|
|
|
|
)),
|
2020-12-28 20:26:50 +00:00
|
|
|
_ => {
|
|
|
|
let WindowDescriptor {
|
|
|
|
width,
|
|
|
|
height,
|
2021-11-06 20:34:31 +00:00
|
|
|
position,
|
2020-12-28 20:26:50 +00:00
|
|
|
scale_factor_override,
|
|
|
|
..
|
|
|
|
} = window_descriptor;
|
2021-11-06 20:34:31 +00:00
|
|
|
|
|
|
|
if let Some(position) = position {
|
|
|
|
if let Some(sf) = scale_factor_override {
|
|
|
|
winit_window_builder = winit_window_builder.with_position(
|
|
|
|
winit::dpi::LogicalPosition::new(
|
|
|
|
position[0] as f64,
|
|
|
|
position[1] as f64,
|
|
|
|
)
|
|
|
|
.to_physical::<f64>(*sf),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
winit_window_builder =
|
|
|
|
winit_window_builder.with_position(winit::dpi::LogicalPosition::new(
|
|
|
|
position[0] as f64,
|
|
|
|
position[1] as f64,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2020-12-28 20:26:50 +00:00
|
|
|
if let Some(sf) = scale_factor_override {
|
|
|
|
winit_window_builder.with_inner_size(
|
|
|
|
winit::dpi::LogicalSize::new(*width, *height).to_physical::<f64>(*sf),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
winit_window_builder
|
|
|
|
.with_inner_size(winit::dpi::LogicalSize::new(*width, *height))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.with_resizable(window_descriptor.resizable)
|
|
|
|
.with_decorations(window_descriptor.decorations),
|
2020-08-13 08:47:40 +00:00
|
|
|
};
|
2020-07-26 19:36:01 +00:00
|
|
|
|
2021-03-03 02:56:50 +00:00
|
|
|
let constraints = window_descriptor.resize_constraints.check_constraints();
|
|
|
|
let min_inner_size = LogicalSize {
|
|
|
|
width: constraints.min_width,
|
|
|
|
height: constraints.min_height,
|
|
|
|
};
|
|
|
|
let max_inner_size = LogicalSize {
|
|
|
|
width: constraints.max_width,
|
|
|
|
height: constraints.max_height,
|
|
|
|
};
|
|
|
|
|
|
|
|
let winit_window_builder =
|
|
|
|
if constraints.max_width.is_finite() && constraints.max_height.is_finite() {
|
|
|
|
winit_window_builder
|
|
|
|
.with_min_inner_size(min_inner_size)
|
|
|
|
.with_max_inner_size(max_inner_size)
|
|
|
|
} else {
|
|
|
|
winit_window_builder.with_min_inner_size(min_inner_size)
|
|
|
|
};
|
|
|
|
|
2020-09-21 23:12:34 +00:00
|
|
|
#[allow(unused_mut)]
|
2020-12-13 23:05:56 +00:00
|
|
|
let mut winit_window_builder = winit_window_builder.with_title(&window_descriptor.title);
|
2020-09-21 23:12:34 +00:00
|
|
|
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
{
|
|
|
|
use wasm_bindgen::JsCast;
|
|
|
|
use winit::platform::web::WindowBuilderExtWebSys;
|
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
if let Some(selector) = &window_descriptor.canvas {
|
2020-09-21 23:12:34 +00:00
|
|
|
let window = web_sys::window().unwrap();
|
|
|
|
let document = window.document().unwrap();
|
|
|
|
let canvas = document
|
|
|
|
.query_selector(&selector)
|
2020-12-02 19:31:16 +00:00
|
|
|
.expect("Cannot query for canvas element.");
|
2020-09-21 23:12:34 +00:00
|
|
|
if let Some(canvas) = canvas {
|
|
|
|
let canvas = canvas.dyn_into::<web_sys::HtmlCanvasElement>().ok();
|
|
|
|
winit_window_builder = winit_window_builder.with_canvas(canvas);
|
|
|
|
} else {
|
2020-12-02 19:31:16 +00:00
|
|
|
panic!("Cannot find element: {}.", selector);
|
2020-09-21 23:12:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 20:52:15 +00:00
|
|
|
let winit_window = winit_window_builder.build(event_loop).unwrap();
|
2020-08-13 09:27:51 +00:00
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
match winit_window.set_cursor_grab(window_descriptor.cursor_locked) {
|
2020-10-20 00:29:31 +00:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(winit::error::ExternalError::NotSupported(_)) => {}
|
|
|
|
Err(err) => Err(err).unwrap(),
|
|
|
|
}
|
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
winit_window.set_cursor_visible(window_descriptor.cursor_visible);
|
2020-10-16 21:07:01 +00:00
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
self.window_id_to_winit.insert(window_id, winit_window.id());
|
|
|
|
self.winit_to_window_id.insert(winit_window.id(), window_id);
|
2020-03-30 21:53:32 +00:00
|
|
|
|
2020-09-19 03:11:26 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
{
|
|
|
|
use winit::platform::web::WindowExtWebSys;
|
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
if window_descriptor.canvas.is_none() {
|
2020-09-21 23:12:34 +00:00
|
|
|
let canvas = winit_window.canvas();
|
2020-09-19 03:11:26 +00:00
|
|
|
|
2020-09-21 23:12:34 +00:00
|
|
|
let window = web_sys::window().unwrap();
|
|
|
|
let document = window.document().unwrap();
|
|
|
|
let body = document.body().unwrap();
|
2020-09-19 03:11:26 +00:00
|
|
|
|
2020-09-21 23:12:34 +00:00
|
|
|
body.append_child(&canvas)
|
2020-12-02 19:31:16 +00:00
|
|
|
.expect("Append canvas to HTML body.");
|
2020-09-21 23:12:34 +00:00
|
|
|
}
|
2020-09-19 03:11:26 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 04:06:06 +00:00
|
|
|
let position = winit_window
|
|
|
|
.outer_position()
|
|
|
|
.ok()
|
|
|
|
.map(|position| IVec2::new(position.x, position.y));
|
2020-12-07 21:32:57 +00:00
|
|
|
let inner_size = winit_window.inner_size();
|
2020-12-13 23:05:56 +00:00
|
|
|
let scale_factor = winit_window.scale_factor();
|
2021-06-02 02:59:17 +00:00
|
|
|
let raw_window_handle = winit_window.raw_window_handle();
|
2020-03-30 21:53:32 +00:00
|
|
|
self.windows.insert(winit_window.id(), winit_window);
|
2020-12-13 23:05:56 +00:00
|
|
|
Window::new(
|
|
|
|
window_id,
|
2021-07-29 20:52:15 +00:00
|
|
|
window_descriptor,
|
2020-12-13 23:05:56 +00:00
|
|
|
inner_size.width,
|
|
|
|
inner_size.height,
|
|
|
|
scale_factor,
|
2021-01-25 04:06:06 +00:00
|
|
|
position,
|
2021-06-02 02:59:17 +00:00
|
|
|
raw_window_handle,
|
2020-12-13 23:05:56 +00:00
|
|
|
)
|
2020-03-30 21:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_window(&self, id: WindowId) -> Option<&winit::window::Window> {
|
|
|
|
self.window_id_to_winit
|
|
|
|
.get(&id)
|
|
|
|
.and_then(|id| self.windows.get(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_window_id(&self, id: winit::window::WindowId) -> Option<WindowId> {
|
|
|
|
self.winit_to_window_id.get(&id).cloned()
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 18:42:19 +00:00
|
|
|
pub fn get_fitting_videomode(
|
2020-08-13 08:47:40 +00:00
|
|
|
monitor: &winit::monitor::MonitorHandle,
|
2020-10-15 18:42:19 +00:00
|
|
|
width: u32,
|
|
|
|
height: u32,
|
2020-08-13 08:47:40 +00:00
|
|
|
) -> winit::monitor::VideoMode {
|
|
|
|
let mut modes = monitor.video_modes().collect::<Vec<_>>();
|
|
|
|
|
|
|
|
fn abs_diff(a: u32, b: u32) -> u32 {
|
|
|
|
if a > b {
|
|
|
|
return a - b;
|
|
|
|
}
|
|
|
|
b - a
|
|
|
|
}
|
|
|
|
|
|
|
|
modes.sort_by(|a, b| {
|
|
|
|
use std::cmp::Ordering::*;
|
2020-10-15 18:42:19 +00:00
|
|
|
match abs_diff(a.size().width, width).cmp(&abs_diff(b.size().width, width)) {
|
2020-08-13 08:47:40 +00:00
|
|
|
Equal => {
|
2020-10-15 18:42:19 +00:00
|
|
|
match abs_diff(a.size().height, height).cmp(&abs_diff(b.size().height, height)) {
|
2020-08-13 08:47:40 +00:00
|
|
|
Equal => b.refresh_rate().cmp(&a.refresh_rate()),
|
|
|
|
default => default,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default => default,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
modes.first().unwrap().clone()
|
|
|
|
}
|
|
|
|
|
2020-10-15 18:42:19 +00:00
|
|
|
pub fn get_best_videomode(monitor: &winit::monitor::MonitorHandle) -> winit::monitor::VideoMode {
|
2020-08-13 08:47:40 +00:00
|
|
|
let mut modes = monitor.video_modes().collect::<Vec<_>>();
|
|
|
|
modes.sort_by(|a, b| {
|
|
|
|
use std::cmp::Ordering::*;
|
|
|
|
match b.size().width.cmp(&a.size().width) {
|
|
|
|
Equal => match b.size().height.cmp(&a.size().height) {
|
|
|
|
Equal => b.refresh_rate().cmp(&a.refresh_rate()),
|
|
|
|
default => default,
|
|
|
|
},
|
|
|
|
default => default,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
modes.first().unwrap().clone()
|
|
|
|
}
|
2020-09-16 20:40:32 +00:00
|
|
|
|
|
|
|
// WARNING: this only works under the assumption that wasm runtime is single threaded
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
unsafe impl Send for WinitWindows {}
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
unsafe impl Sync for WinitWindows {}
|