2020-12-03 19:30:27 +00:00
|
|
|
use bevy_math::Vec2;
|
2020-11-28 00:39:59 +00:00
|
|
|
use bevy_utils::Uuid;
|
2020-04-05 21:12:14 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub struct WindowId(Uuid);
|
|
|
|
|
|
|
|
impl WindowId {
|
2020-06-25 23:02:21 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
WindowId(Uuid::new_v4())
|
|
|
|
}
|
|
|
|
|
2020-07-25 06:04:45 +00:00
|
|
|
pub fn primary() -> Self {
|
|
|
|
WindowId(Uuid::from_u128(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_primary(&self) -> bool {
|
2020-07-26 19:10:18 +00:00
|
|
|
*self == WindowId::primary()
|
2020-07-25 06:04:45 +00:00
|
|
|
}
|
2020-08-16 07:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
use std::fmt;
|
2020-07-25 06:04:45 +00:00
|
|
|
|
2020-08-16 07:30:04 +00:00
|
|
|
impl fmt::Display for WindowId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
self.0.to_simple().fmt(f)
|
2020-04-05 21:12:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 06:04:45 +00:00
|
|
|
impl Default for WindowId {
|
|
|
|
fn default() -> Self {
|
|
|
|
WindowId::primary()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
/// An operating system window that can present content and receive user input.
|
|
|
|
///
|
|
|
|
/// ## Window Sizes
|
|
|
|
///
|
|
|
|
/// There are three sizes associated with a window. The physical size which is
|
|
|
|
/// the height and width in physical pixels on the monitor. The logical size
|
|
|
|
/// which is the physical size scaled by an operating system provided factor to
|
|
|
|
/// account for monitors with differing pixel densities or user preference. And
|
|
|
|
/// the requested size, measured in logical pixels, which is the value submitted
|
|
|
|
/// to the API when creating the window, or requesting that it be resized.
|
|
|
|
///
|
|
|
|
/// The actual size, in logical pixels, of the window may not match the
|
|
|
|
/// requested size due to operating system limits on the window size, or the
|
|
|
|
/// quantization of the logical size when converting the physical size to the
|
|
|
|
/// logical size through the scaling factor.
|
2020-06-22 20:21:39 +00:00
|
|
|
#[derive(Debug)]
|
2020-04-05 21:12:14 +00:00
|
|
|
pub struct Window {
|
2020-10-15 18:42:19 +00:00
|
|
|
id: WindowId,
|
2020-12-13 23:05:56 +00:00
|
|
|
requested_width: f32,
|
|
|
|
requested_height: f32,
|
2020-12-07 21:32:57 +00:00
|
|
|
physical_width: u32,
|
|
|
|
physical_height: u32,
|
2020-12-28 20:26:50 +00:00
|
|
|
scale_factor_override: Option<f64>,
|
|
|
|
backend_scale_factor: f64,
|
2020-10-15 18:42:19 +00:00
|
|
|
title: String,
|
|
|
|
vsync: bool,
|
|
|
|
resizable: bool,
|
|
|
|
decorations: bool,
|
2020-10-16 21:07:01 +00:00
|
|
|
cursor_visible: bool,
|
|
|
|
cursor_locked: bool,
|
2020-12-03 19:30:27 +00:00
|
|
|
cursor_position: Option<Vec2>,
|
2020-10-15 18:42:19 +00:00
|
|
|
mode: WindowMode,
|
2020-09-21 23:12:34 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
pub canvas: Option<String>,
|
2020-10-15 18:42:19 +00:00
|
|
|
command_queue: Vec<WindowCommand>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum WindowCommand {
|
|
|
|
SetWindowMode {
|
|
|
|
mode: WindowMode,
|
|
|
|
resolution: (u32, u32),
|
|
|
|
},
|
|
|
|
SetTitle {
|
|
|
|
title: String,
|
|
|
|
},
|
2020-12-28 20:26:50 +00:00
|
|
|
SetScaleFactor {
|
|
|
|
scale_factor: f64,
|
|
|
|
},
|
2020-10-15 18:42:19 +00:00
|
|
|
SetResolution {
|
2020-12-28 20:26:50 +00:00
|
|
|
logical_resolution: (f32, f32),
|
|
|
|
scale_factor: f64,
|
2020-10-15 18:42:19 +00:00
|
|
|
},
|
|
|
|
SetVsync {
|
|
|
|
vsync: bool,
|
|
|
|
},
|
|
|
|
SetResizable {
|
|
|
|
resizable: bool,
|
|
|
|
},
|
|
|
|
SetDecorations {
|
|
|
|
decorations: bool,
|
|
|
|
},
|
2020-10-16 21:07:01 +00:00
|
|
|
SetCursorLockMode {
|
|
|
|
locked: bool,
|
|
|
|
},
|
|
|
|
SetCursorVisibility {
|
|
|
|
visible: bool,
|
|
|
|
},
|
2020-11-26 01:31:10 +00:00
|
|
|
SetCursorPosition {
|
2020-12-03 20:39:03 +00:00
|
|
|
position: Vec2,
|
2020-11-26 01:31:10 +00:00
|
|
|
},
|
2020-12-04 22:31:17 +00:00
|
|
|
SetMaximized {
|
|
|
|
maximized: bool,
|
|
|
|
},
|
2020-08-13 08:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Defines the way a window is displayed
|
|
|
|
/// The use_size option that is used in the Fullscreen variant
|
|
|
|
/// defines whether a videomode is chosen that best fits the width and height
|
|
|
|
/// in the Window structure, or if these are ignored.
|
|
|
|
/// E.g. when use_size is set to false the best video mode possible is chosen.
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum WindowMode {
|
|
|
|
Windowed,
|
|
|
|
BorderlessFullscreen,
|
|
|
|
Fullscreen { use_size: bool },
|
2020-04-05 21:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Window {
|
2020-12-13 23:05:56 +00:00
|
|
|
pub fn new(
|
|
|
|
id: WindowId,
|
|
|
|
window_descriptor: &WindowDescriptor,
|
|
|
|
physical_width: u32,
|
|
|
|
physical_height: u32,
|
|
|
|
scale_factor: f64,
|
|
|
|
) -> Self {
|
2020-04-05 21:12:14 +00:00
|
|
|
Window {
|
2020-06-25 23:02:21 +00:00
|
|
|
id,
|
2020-12-13 23:05:56 +00:00
|
|
|
requested_width: window_descriptor.width,
|
|
|
|
requested_height: window_descriptor.height,
|
|
|
|
physical_width,
|
|
|
|
physical_height,
|
2020-12-28 20:26:50 +00:00
|
|
|
scale_factor_override: window_descriptor.scale_factor_override,
|
|
|
|
backend_scale_factor: scale_factor,
|
2020-04-05 21:12:14 +00:00
|
|
|
title: window_descriptor.title.clone(),
|
|
|
|
vsync: window_descriptor.vsync,
|
2020-08-13 08:47:40 +00:00
|
|
|
resizable: window_descriptor.resizable,
|
2020-10-05 17:51:36 +00:00
|
|
|
decorations: window_descriptor.decorations,
|
2020-10-16 21:07:01 +00:00
|
|
|
cursor_visible: window_descriptor.cursor_visible,
|
|
|
|
cursor_locked: window_descriptor.cursor_locked,
|
2020-12-03 19:30:27 +00:00
|
|
|
cursor_position: None,
|
2020-08-13 08:47:40 +00:00
|
|
|
mode: window_descriptor.mode,
|
2020-09-21 23:12:34 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
canvas: window_descriptor.canvas.clone(),
|
2020-10-15 18:42:19 +00:00
|
|
|
command_queue: Vec::new(),
|
2020-04-05 21:12:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-15 18:42:19 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn id(&self) -> WindowId {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
/// The current logical width of the window's client area.
|
2020-10-15 18:42:19 +00:00
|
|
|
#[inline]
|
2020-12-13 23:05:56 +00:00
|
|
|
pub fn width(&self) -> f32 {
|
2020-12-28 20:26:50 +00:00
|
|
|
(self.physical_width as f64 / self.scale_factor()) as f32
|
2020-10-15 18:42:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
/// The current logical height of the window's client area.
|
2020-12-07 21:32:57 +00:00
|
|
|
#[inline]
|
2020-12-13 23:05:56 +00:00
|
|
|
pub fn height(&self) -> f32 {
|
2020-12-28 20:26:50 +00:00
|
|
|
(self.physical_height as f64 / self.scale_factor()) as f32
|
2020-12-02 04:25:31 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
/// The requested window client area width in logical pixels from window
|
|
|
|
/// creation or the last call to [set_resolution](Window::set_resolution).
|
|
|
|
///
|
|
|
|
/// This may differ from the actual width depending on OS size limits and
|
|
|
|
/// the scaling factor for high DPI monitors.
|
2020-12-07 21:32:57 +00:00
|
|
|
#[inline]
|
2020-12-13 23:05:56 +00:00
|
|
|
pub fn requested_width(&self) -> f32 {
|
|
|
|
self.requested_width
|
2020-12-02 04:25:31 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
/// The requested window client area height in logical pixels from window
|
|
|
|
/// creation or the last call to [set_resolution](Window::set_resolution).
|
|
|
|
///
|
|
|
|
/// This may differ from the actual width depending on OS size limits and
|
|
|
|
/// the scaling factor for high DPI monitors.
|
2020-10-15 18:42:19 +00:00
|
|
|
#[inline]
|
2020-12-13 23:05:56 +00:00
|
|
|
pub fn requested_height(&self) -> f32 {
|
|
|
|
self.requested_height
|
2020-12-07 21:32:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
/// The window's client area width in physical pixels.
|
2020-12-07 21:32:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn physical_width(&self) -> u32 {
|
|
|
|
self.physical_width
|
|
|
|
}
|
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
/// The window's client area height in physical pixels.
|
2020-12-07 21:32:57 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn physical_height(&self) -> u32 {
|
|
|
|
self.physical_height
|
2020-10-15 18:42:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 22:31:17 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn set_maximized(&mut self, maximized: bool) {
|
|
|
|
self.command_queue
|
|
|
|
.push(WindowCommand::SetMaximized { maximized });
|
|
|
|
}
|
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
/// Request the OS to resize the window such the the client area matches the
|
|
|
|
/// specified width and height.
|
2020-12-28 20:26:50 +00:00
|
|
|
#[allow(clippy::float_cmp)]
|
2020-12-13 23:05:56 +00:00
|
|
|
pub fn set_resolution(&mut self, width: f32, height: f32) {
|
2020-12-28 20:26:50 +00:00
|
|
|
if self.requested_width == width && self.requested_height == height {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-13 23:05:56 +00:00
|
|
|
self.requested_width = width;
|
|
|
|
self.requested_height = height;
|
2020-12-07 21:32:57 +00:00
|
|
|
self.command_queue.push(WindowCommand::SetResolution {
|
2020-12-28 20:26:50 +00:00
|
|
|
logical_resolution: (self.requested_width, self.requested_height),
|
|
|
|
scale_factor: self.scale_factor(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Override the os-reported scaling factor
|
|
|
|
#[allow(clippy::float_cmp)]
|
|
|
|
pub fn set_scale_factor_override(&mut self, scale_factor: Option<f64>) {
|
|
|
|
if self.scale_factor_override == scale_factor {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.scale_factor_override = scale_factor;
|
|
|
|
self.command_queue.push(WindowCommand::SetScaleFactor {
|
|
|
|
scale_factor: self.scale_factor(),
|
|
|
|
});
|
|
|
|
self.command_queue.push(WindowCommand::SetResolution {
|
|
|
|
logical_resolution: (self.requested_width, self.requested_height),
|
|
|
|
scale_factor: self.scale_factor(),
|
2020-12-07 21:32:57 +00:00
|
|
|
});
|
2020-10-15 18:42:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 19:30:27 +00:00
|
|
|
#[allow(missing_docs)]
|
|
|
|
#[inline]
|
2020-12-13 23:05:56 +00:00
|
|
|
pub fn update_scale_factor_from_backend(&mut self, scale_factor: f64) {
|
2020-12-28 20:26:50 +00:00
|
|
|
self.backend_scale_factor = scale_factor;
|
2020-10-15 18:42:19 +00:00
|
|
|
}
|
2020-12-01 02:24:49 +00:00
|
|
|
|
2020-12-03 19:30:27 +00:00
|
|
|
#[allow(missing_docs)]
|
|
|
|
#[inline]
|
2020-12-13 23:05:56 +00:00
|
|
|
pub fn update_actual_size_from_backend(&mut self, physical_width: u32, physical_height: u32) {
|
|
|
|
self.physical_width = physical_width;
|
|
|
|
self.physical_height = physical_height;
|
2020-12-01 02:24:49 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
/// The ratio of physical pixels to logical pixels
|
|
|
|
///
|
|
|
|
/// `physical_pixels = logical_pixels * scale_factor`
|
2020-12-01 02:24:49 +00:00
|
|
|
pub fn scale_factor(&self) -> f64 {
|
2020-12-28 20:26:50 +00:00
|
|
|
self.scale_factor_override
|
|
|
|
.unwrap_or(self.backend_scale_factor)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The window scale factor as reported by the window backend.
|
|
|
|
/// This value is unaffected by scale_factor_override.
|
|
|
|
#[inline]
|
|
|
|
pub fn backend_scale_factor(&self) -> f64 {
|
|
|
|
self.backend_scale_factor
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn scale_factor_override(&self) -> Option<f64> {
|
|
|
|
self.scale_factor_override
|
2020-12-01 02:24:49 +00:00
|
|
|
}
|
2020-10-15 18:42:19 +00:00
|
|
|
|
2020-11-11 01:20:31 +00:00
|
|
|
#[inline]
|
2020-10-15 18:42:19 +00:00
|
|
|
pub fn title(&self) -> &str {
|
|
|
|
&self.title
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_title(&mut self, title: String) {
|
|
|
|
self.title = title.to_string();
|
|
|
|
self.command_queue.push(WindowCommand::SetTitle { title });
|
|
|
|
}
|
|
|
|
|
2020-11-11 01:20:31 +00:00
|
|
|
#[inline]
|
2020-10-15 18:42:19 +00:00
|
|
|
pub fn vsync(&self) -> bool {
|
|
|
|
self.vsync
|
|
|
|
}
|
|
|
|
|
2020-11-11 01:20:31 +00:00
|
|
|
#[inline]
|
2020-10-15 18:42:19 +00:00
|
|
|
pub fn set_vsync(&mut self, vsync: bool) {
|
|
|
|
self.vsync = vsync;
|
|
|
|
self.command_queue.push(WindowCommand::SetVsync { vsync });
|
|
|
|
}
|
|
|
|
|
2020-11-11 01:20:31 +00:00
|
|
|
#[inline]
|
2020-10-15 18:42:19 +00:00
|
|
|
pub fn resizable(&self) -> bool {
|
|
|
|
self.resizable
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_resizable(&mut self, resizable: bool) {
|
|
|
|
self.resizable = resizable;
|
|
|
|
self.command_queue
|
|
|
|
.push(WindowCommand::SetResizable { resizable });
|
|
|
|
}
|
|
|
|
|
2020-11-11 01:20:31 +00:00
|
|
|
#[inline]
|
2020-10-15 18:42:19 +00:00
|
|
|
pub fn decorations(&self) -> bool {
|
|
|
|
self.decorations
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_decorations(&mut self, decorations: bool) {
|
|
|
|
self.decorations = decorations;
|
|
|
|
self.command_queue
|
|
|
|
.push(WindowCommand::SetDecorations { decorations });
|
|
|
|
}
|
|
|
|
|
2020-11-11 01:20:31 +00:00
|
|
|
#[inline]
|
2020-10-16 21:07:01 +00:00
|
|
|
pub fn cursor_locked(&self) -> bool {
|
|
|
|
self.cursor_locked
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_cursor_lock_mode(&mut self, lock_mode: bool) {
|
|
|
|
self.cursor_locked = lock_mode;
|
|
|
|
self.command_queue
|
|
|
|
.push(WindowCommand::SetCursorLockMode { locked: lock_mode });
|
|
|
|
}
|
|
|
|
|
2020-11-11 01:20:31 +00:00
|
|
|
#[inline]
|
2020-10-16 21:07:01 +00:00
|
|
|
pub fn cursor_visible(&self) -> bool {
|
|
|
|
self.cursor_visible
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_cursor_visibility(&mut self, visibile_mode: bool) {
|
|
|
|
self.cursor_visible = visibile_mode;
|
|
|
|
self.command_queue.push(WindowCommand::SetCursorVisibility {
|
|
|
|
visible: visibile_mode,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:30:27 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn cursor_position(&self) -> Option<Vec2> {
|
|
|
|
self.cursor_position
|
|
|
|
}
|
|
|
|
|
2020-12-03 20:39:03 +00:00
|
|
|
pub fn set_cursor_position(&mut self, position: Vec2) {
|
2020-11-26 01:31:10 +00:00
|
|
|
self.command_queue
|
2020-12-03 20:39:03 +00:00
|
|
|
.push(WindowCommand::SetCursorPosition { position });
|
2020-11-26 01:31:10 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 19:30:27 +00:00
|
|
|
#[allow(missing_docs)]
|
|
|
|
#[inline]
|
|
|
|
pub fn update_cursor_position_from_backend(&mut self, cursor_position: Option<Vec2>) {
|
|
|
|
self.cursor_position = cursor_position;
|
|
|
|
}
|
|
|
|
|
2020-11-11 01:20:31 +00:00
|
|
|
#[inline]
|
2020-10-15 18:42:19 +00:00
|
|
|
pub fn mode(&self) -> WindowMode {
|
|
|
|
self.mode
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_mode(&mut self, mode: WindowMode) {
|
|
|
|
self.mode = mode;
|
|
|
|
self.command_queue.push(WindowCommand::SetWindowMode {
|
|
|
|
mode,
|
2020-12-07 21:32:57 +00:00
|
|
|
resolution: (self.physical_width, self.physical_height),
|
2020-10-15 18:42:19 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-11 01:20:31 +00:00
|
|
|
#[inline]
|
2020-10-15 19:49:56 +00:00
|
|
|
pub fn drain_commands(&mut self) -> impl Iterator<Item = WindowCommand> + '_ {
|
2020-10-15 18:42:19 +00:00
|
|
|
self.command_queue.drain(..)
|
|
|
|
}
|
2020-04-05 21:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct WindowDescriptor {
|
2020-12-13 23:05:56 +00:00
|
|
|
pub width: f32,
|
|
|
|
pub height: f32,
|
2020-12-28 20:26:50 +00:00
|
|
|
pub scale_factor_override: Option<f64>,
|
2020-04-05 21:12:14 +00:00
|
|
|
pub title: String,
|
|
|
|
pub vsync: bool,
|
2020-08-13 08:47:40 +00:00
|
|
|
pub resizable: bool,
|
2020-10-05 17:51:36 +00:00
|
|
|
pub decorations: bool,
|
2020-10-16 21:07:01 +00:00
|
|
|
pub cursor_visible: bool,
|
|
|
|
pub cursor_locked: bool,
|
2020-08-13 08:47:40 +00:00
|
|
|
pub mode: WindowMode,
|
2020-09-21 23:12:34 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
pub canvas: Option<String>,
|
2020-04-05 21:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for WindowDescriptor {
|
|
|
|
fn default() -> Self {
|
|
|
|
WindowDescriptor {
|
|
|
|
title: "bevy".to_string(),
|
2020-12-13 23:05:56 +00:00
|
|
|
width: 1280.,
|
|
|
|
height: 720.,
|
2020-12-28 20:26:50 +00:00
|
|
|
scale_factor_override: None,
|
2020-04-05 21:12:14 +00:00
|
|
|
vsync: true,
|
2020-08-13 08:47:40 +00:00
|
|
|
resizable: true,
|
2020-10-05 17:51:36 +00:00
|
|
|
decorations: true,
|
2020-10-16 21:07:01 +00:00
|
|
|
cursor_locked: false,
|
|
|
|
cursor_visible: true,
|
2020-08-13 08:47:40 +00:00
|
|
|
mode: WindowMode::Windowed,
|
2020-09-21 23:12:34 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
canvas: None,
|
2020-04-05 21:12:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-06 23:15:59 +00:00
|
|
|
}
|