summaryrefslogtreecommitdiff
path: root/engine/src/windowing/window.rs
diff options
context:
space:
mode:
Diffstat (limited to 'engine/src/windowing/window.rs')
-rw-r--r--engine/src/windowing/window.rs171
1 files changed, 171 insertions, 0 deletions
diff --git a/engine/src/windowing/window.rs b/engine/src/windowing/window.rs
new file mode 100644
index 0000000..79b2102
--- /dev/null
+++ b/engine/src/windowing/window.rs
@@ -0,0 +1,171 @@
+use std::borrow::Cow;
+
+use ecs::Component;
+
+use crate::data_types::dimens::Dimens;
+
+pub mod platform;
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
+pub struct Id
+{
+ inner: winit::window::WindowId,
+}
+
+impl Id
+{
+ pub(crate) fn from_inner(inner: winit::window::WindowId) -> Self
+ {
+ Self { inner }
+ }
+}
+
+macro_rules! impl_creation_attributes_field_fns {
+ ($field: ident, ($($getter_ret_pre: tt)?), $getter_ret_type: ty, $field_type: ty) => {
+ impl CreationAttributes
+ {
+ pub fn $field(&self) -> $getter_ret_type
+ {
+ $($getter_ret_pre)? self.attrs.$field
+ }
+
+ paste::paste! {
+ pub fn [<with_ $field>](mut self, $field: impl Into<$field_type>) -> Self {
+ self.attrs.$field = $field.into();
+ self
+ }
+ }
+ }
+ };
+}
+
+#[derive(Debug, Component, Clone)]
+#[non_exhaustive]
+pub struct CreationAttributes
+{
+ attrs: winit::window::WindowAttributes,
+}
+
+impl_creation_attributes_field_fns!(title, (&), &str, String);
+impl_creation_attributes_field_fns!(transparent, (), bool, bool);
+
+impl CreationAttributes
+{
+ #[cfg(target_os = "linux")]
+ pub fn with_x11_visual(mut self, visual_id: XVisualID) -> Self
+ {
+ use winit::platform::x11::WindowAttributesExtX11;
+
+ self.attrs = self.attrs.with_x11_visual(visual_id);
+
+ self
+ }
+}
+
+impl CreationAttributes
+{
+ pub(crate) fn into_inner(self) -> winit::window::WindowAttributes
+ {
+ self.attrs
+ }
+}
+
+impl Default for CreationAttributes
+{
+ fn default() -> Self
+ {
+ CreationAttributes {
+ attrs: winit::window::WindowAttributes::default().with_title("Application"),
+ }
+ }
+}
+
+#[derive(Debug, Component, Clone, Copy)]
+pub struct CreationReady;
+
+#[derive(Debug, Component)]
+#[non_exhaustive]
+pub struct Window
+{
+ wid: Id,
+ pub title: Cow<'static, str>,
+ pub cursor_visible: bool,
+ pub cursor_grab_mode: CursorGrabMode,
+ inner_size: Dimens<u32>,
+}
+
+impl Window
+{
+ pub fn wid(&self) -> Id
+ {
+ self.wid
+ }
+
+ pub fn inner_size(&self) -> &Dimens<u32>
+ {
+ &self.inner_size
+ }
+
+ pub(crate) fn new(
+ winit_window: &winit::window::Window,
+ creation_attrs: &CreationAttributes,
+ ) -> Self
+ {
+ Self {
+ wid: Id::from_inner(winit_window.id()),
+ title: creation_attrs.title().to_string().into(),
+ cursor_visible: true,
+ cursor_grab_mode: CursorGrabMode::None,
+ inner_size: winit_window.inner_size().into(),
+ }
+ }
+
+ pub(crate) fn apply(&self, winit_window: &winit::window::Window)
+ {
+ winit_window.set_title(&self.title);
+ winit_window.set_cursor_visible(self.cursor_visible);
+ }
+
+ pub(crate) fn set_inner_size(&mut self, inner_size: Dimens<u32>)
+ {
+ self.inner_size = inner_size;
+ }
+}
+
+#[derive(Debug, Component)]
+pub struct Closed;
+
+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
+pub enum CursorGrabMode
+{
+ #[default]
+ None,
+
+ /// The cursor is locked to a specific position in the window.
+ Locked,
+}
+
+/// A unique identifier for an X11 visual.
+pub type XVisualID = u32;
+
+impl<P> From<winit::dpi::PhysicalSize<P>> for Dimens<P>
+{
+ fn from(size: winit::dpi::PhysicalSize<P>) -> Self
+ {
+ Self {
+ width: size.width,
+ height: size.height,
+ }
+ }
+}
+
+impl<P> From<Dimens<P>> for winit::dpi::PhysicalSize<P>
+{
+ fn from(dimens: Dimens<P>) -> Self
+ {
+ Self {
+ width: dimens.width,
+ height: dimens.height,
+ }
+ }
+}