|
| 1 | +use bevy_ecs::component::Component; |
| 2 | +use bevy_ecs::prelude::ReflectComponent; |
| 3 | +use bevy_math::{IVec2, UVec2}; |
| 4 | +use bevy_reflect::Reflect; |
| 5 | + |
| 6 | +#[cfg(feature = "serialize")] |
| 7 | +use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; |
| 8 | + |
| 9 | +/// Represents an available monitor as reported by the user's operating system, which can be used |
| 10 | +/// to query information about the display, such as its size, position, and video modes. |
| 11 | +/// |
| 12 | +/// Each monitor corresponds to an entity and can be used to position a monitor using |
| 13 | +/// [`crate::window::MonitorSelection::Entity`]. |
| 14 | +/// |
| 15 | +/// # Warning |
| 16 | +/// |
| 17 | +/// This component is synchronized with `winit` through `bevy_winit`, but is effectively |
| 18 | +/// read-only as `winit` does not support changing monitor properties. |
| 19 | +#[derive(Component, Debug, Clone, Reflect)] |
| 20 | +#[cfg_attr( |
| 21 | + feature = "serialize", |
| 22 | + derive(serde::Serialize, serde::Deserialize), |
| 23 | + reflect(Serialize, Deserialize) |
| 24 | +)] |
| 25 | +#[reflect(Component)] |
| 26 | +pub struct Monitor { |
| 27 | + /// The name of the monitor |
| 28 | + pub name: Option<String>, |
| 29 | + /// The height of the monitor in physical pixels |
| 30 | + pub physical_height: u32, |
| 31 | + /// The width of the monitor in physical pixels |
| 32 | + pub physical_width: u32, |
| 33 | + /// The position of the monitor in physical pixels |
| 34 | + pub physical_position: IVec2, |
| 35 | + /// The refresh rate of the monitor in millihertz |
| 36 | + pub refresh_rate_millihertz: Option<u32>, |
| 37 | + /// The scale factor of the monitor |
| 38 | + pub scale_factor: f64, |
| 39 | + /// The video modes that the monitor supports |
| 40 | + pub video_modes: Vec<VideoMode>, |
| 41 | +} |
| 42 | + |
| 43 | +/// A marker component for the primary monitor |
| 44 | +#[derive(Component, Debug, Clone, Reflect)] |
| 45 | +#[reflect(Component)] |
| 46 | +pub struct PrimaryMonitor; |
| 47 | + |
| 48 | +impl Monitor { |
| 49 | + /// Returns the physical size of the monitor in pixels |
| 50 | + pub fn physical_size(&self) -> UVec2 { |
| 51 | + UVec2::new(self.physical_width, self.physical_height) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// Represents a video mode that a monitor supports |
| 56 | +#[derive(Debug, Clone, Reflect)] |
| 57 | +#[cfg_attr( |
| 58 | + feature = "serialize", |
| 59 | + derive(serde::Serialize, serde::Deserialize), |
| 60 | + reflect(Serialize, Deserialize) |
| 61 | +)] |
| 62 | +pub struct VideoMode { |
| 63 | + /// The resolution of the video mode |
| 64 | + pub physical_size: UVec2, |
| 65 | + /// The bit depth of the video mode |
| 66 | + pub bit_depth: u16, |
| 67 | + /// The refresh rate in millihertz |
| 68 | + pub refresh_rate_millihertz: u32, |
| 69 | +} |
0 commit comments