summaryrefslogtreecommitdiff
path: root/engine/src/windowing
diff options
context:
space:
mode:
Diffstat (limited to 'engine/src/windowing')
-rw-r--r--engine/src/windowing/dpi.rs24
-rw-r--r--engine/src/windowing/keyboard.rs48
-rw-r--r--engine/src/windowing/monitor.rs4
-rw-r--r--engine/src/windowing/mouse.rs7
-rw-r--r--engine/src/windowing/window.rs20
5 files changed, 68 insertions, 35 deletions
diff --git a/engine/src/windowing/dpi.rs b/engine/src/windowing/dpi.rs
index e3b1be1..9bc42e1 100644
--- a/engine/src/windowing/dpi.rs
+++ b/engine/src/windowing/dpi.rs
@@ -75,6 +75,12 @@ impl<Pixel> PhysicalPosition<Pixel>
}
}
+ /// Attempts to convert the `x` and `y` values in `source` from the type `SourcePixel`
+ /// to the type `Pixel`.
+ ///
+ /// # Errors
+ /// Returns `Err` if conversion of either `x` or `y` by their respective `TryFrom`
+ /// implementations fails.
pub fn try_convert_from<SourcePixel>(
source: PhysicalPosition<SourcePixel>,
) -> Result<Self, Pixel::Error>
@@ -100,6 +106,12 @@ pub struct LogicalPosition<Pixel>
impl<Pixel> LogicalPosition<Pixel>
{
+ /// Attempts to convert the `x` and `y` values in `source` from the type `SourcePixel`
+ /// to the type `Pixel`.
+ ///
+ /// # Errors
+ /// Returns `Err` if conversion of either `x` or `y` by their respective `TryFrom`
+ /// implementations fails.
pub fn try_convert_from<SourcePixel>(
source: LogicalPosition<SourcePixel>,
) -> Result<Self, Pixel::Error>
@@ -159,6 +171,12 @@ impl<Pixel> PhysicalSize<Pixel>
impl<Pixel> PhysicalSize<Pixel>
{
+ /// Attempts to convert the `width` and `height` values in `source` from the type
+ /// `SourcePixel` to the type `Pixel`.
+ ///
+ /// # Errors
+ /// Returns `Err` if conversion of either `width` or `height` by their respective
+ /// `TryFrom` implementations fails.
pub fn try_convert_from<SourcePixel>(
source: PhysicalSize<SourcePixel>,
) -> Result<Self, Pixel::Error>
@@ -184,6 +202,12 @@ pub struct LogicalSize<Pixel>
impl<Pixel> LogicalSize<Pixel>
{
+ /// Attempts to convert the `width` and `height` values in `source` from the type
+ /// `SourcePixel` to the type `Pixel`.
+ ///
+ /// # Errors
+ /// Returns `Err` if conversion of either `width` or `height` by their respective
+ /// `TryFrom` implementations fails.
pub fn try_convert_from<SourcePixel>(
source: LogicalSize<SourcePixel>,
) -> Result<Self, Pixel::Error>
diff --git a/engine/src/windowing/keyboard.rs b/engine/src/windowing/keyboard.rs
index dffa95e..a8ec6df 100644
--- a/engine/src/windowing/keyboard.rs
+++ b/engine/src/windowing/keyboard.rs
@@ -14,6 +14,7 @@ impl Keyboard
{
/// Returns whether the given key was just pressed this frame. This function will
/// return `false` if the key was also pressed the previous frame.
+ #[must_use]
pub fn just_pressed(&self, key: Key) -> bool
{
self.get_key_state(key) == KeyState::Pressed
@@ -22,6 +23,7 @@ impl Keyboard
/// Returns whether the given key was just released this frame. This function will
/// return `false` if the key was also released the previous frame.
+ #[must_use]
pub fn just_released(&self, key: Key) -> bool
{
self.get_key_state(key) == KeyState::Released
@@ -29,12 +31,14 @@ impl Keyboard
}
/// Returns whether the given key is currently pressed.
+ #[must_use]
pub fn pressed(&self, key: Key) -> bool
{
self.get_key_state(key) == KeyState::Pressed
}
/// Returns whether the given key is currently released.
+ #[must_use]
pub fn released(&self, key: Key) -> bool
{
self.get_key_state(key) == KeyState::Released
@@ -66,13 +70,11 @@ impl Keyboard
{
let bits = self.keys.get(key as usize);
- let state = match bits & KEY_CURR_PRESSED_BITS {
+ match bits & KEY_CURR_PRESSED_BITS {
KEY_CURR_PRESSED_BITS => KeyState::Pressed,
0 => KeyState::Released,
_ => unreachable!(),
- };
-
- state
+ }
}
#[must_use]
@@ -80,15 +82,14 @@ impl Keyboard
{
let bits = self.keys.get(key as usize);
- let state = match bits & KEY_PREV_PRESSED_BITS {
+ match bits & KEY_PREV_PRESSED_BITS {
KEY_PREV_PRESSED_BITS => KeyState::Pressed,
0 => KeyState::Released,
_ => unreachable!(),
- };
-
- state
+ }
}
+ #[must_use]
pub fn text_keys(&self) -> &str
{
&self.text_keys
@@ -109,7 +110,7 @@ impl Keyboard
pub fn make_key_states_previous(&mut self)
{
for byte in self.keys.bytes_mut() {
- *byte = (*byte >> 1) & 0b01010101 | *byte & 0b10101010;
+ *byte = (*byte >> 1) & 0b0101_0101 | *byte & 0b1010_1010;
}
}
@@ -125,7 +126,7 @@ impl Keyboard
#[non_exhaustive]
pub enum Key
{
- /// <kbd>`</kbd> on a US keyboard. This is also called a backtick or grave.
+ #[doc = "<kbd>`</kbd> on a US keyboard. This is also called a backtick or grave."]
/// This is the <kbd>半角</kbd>/<kbd>全角</kbd>/<kbd>漢字</kbd>
/// (hankaku/zenkaku/kanji) key on Japanese keyboards
Backquote,
@@ -243,12 +244,12 @@ pub enum Key
/// <kbd>Alt</kbd>, <kbd>Option</kbd>, or <kbd>⌥</kbd>.
AltLeft,
/// <kbd>Alt</kbd>, <kbd>Option</kbd>, or <kbd>⌥</kbd>.
- /// This is labeled <kbd>AltGr</kbd> on many keyboard layouts.
+ /// This is labeled <kbd>`AltGr`</kbd> on many keyboard layouts.
AltRight,
/// <kbd>Backspace</kbd> or <kbd>⌫</kbd>.
/// Labeled <kbd>Delete</kbd> on Apple keyboards.
Backspace,
- /// <kbd>CapsLock</kbd> or <kbd>⇪</kbd>
+ /// <kbd>`CapsLock`</kbd> or <kbd>⇪</kbd>
CapsLock,
/// The application context menu key, which is typically found between the right
/// <kbd>Super</kbd> key and the right <kbd>Control</kbd> key.
@@ -276,7 +277,7 @@ pub enum Key
/// Japanese: <kbd>カタカナ</kbd>/<kbd>ひらがな</kbd>/<kbd>ローマ字</kbd>
/// (katakana/hiragana/romaji)
KanaMode,
- /// Korean: HangulMode <kbd>한/영</kbd> (han/yeong)
+ /// Korean: `HangulMode` <kbd>한/영</kbd> (han/yeong)
///
/// Japanese (Mac keyboard): <kbd>か</kbd> (kana)
Lang1,
@@ -306,9 +307,9 @@ pub enum Key
Home,
/// <kbd>Insert</kbd> or <kbd>Ins</kbd>. Not present on Apple keyboards.
Insert,
- /// <kbd>Page Down</kbd>, <kbd>PgDn</kbd>, or <kbd>⇟</kbd>
+ /// <kbd>Page Down</kbd>, <kbd>`PgDn`</kbd>, or <kbd>⇟</kbd>
PageDown,
- /// <kbd>Page Up</kbd>, <kbd>PgUp</kbd>, or <kbd>⇞</kbd>
+ /// <kbd>Page Up</kbd>, <kbd>`PgUp`</kbd>, or <kbd>⇞</kbd>
PageUp,
/// <kbd>↓</kbd>
ArrowDown,
@@ -327,7 +328,7 @@ pub enum Key
Numpad1,
/// <kbd>2 ↓</kbd> on a keyboard. <kbd>2 ABC</kbd> on a phone or remote control
Numpad2,
- /// <kbd>3 PgDn</kbd> on a keyboard. <kbd>3 DEF</kbd> on a phone or remote control
+ /// <kbd>3 `PgDn`</kbd> on a keyboard. <kbd>3 DEF</kbd> on a phone or remote control
Numpad3,
/// <kbd>4 ←</kbd> on a keyboard. <kbd>4 GHI</kbd> on a phone or remote control
Numpad4,
@@ -340,15 +341,15 @@ pub enum Key
Numpad7,
/// <kbd>8 ↑</kbd> on a keyboard. <kbd>8 TUV</kbd> on a phone or remote control
Numpad8,
- /// <kbd>9 PgUp</kbd> on a keyboard. <kbd>9 WXYZ</kbd> or <kbd>9 WXY</kbd> on a phone
- /// or remote control
+ /// <kbd>9 `PgUp`</kbd> on a keyboard. <kbd>9 WXYZ</kbd> or <kbd>9 WXY</kbd> on a
+ /// phone or remote control
Numpad9,
/// <kbd>+</kbd>
NumpadAdd,
/// Found on the Microsoft Natural Keyboard.
NumpadBackspace,
/// <kbd>C</kbd> or <kbd>A</kbd> (All Clear). Also for use with numpads that have a
- /// <kbd>Clear</kbd> key that is separate from the <kbd>NumLock</kbd> key. On the
+ /// <kbd>Clear</kbd> key that is separate from the <kbd>`NumLock`</kbd> key. On the
/// Mac, the numpad <kbd>Clear</kbd> key is encoded as [`NumLock`].
///
/// [`NumLock`]: Self::NumLock
@@ -393,7 +394,7 @@ pub enum Key
/// This key is typically found below the <kbd>7</kbd> key and to the left of
/// the <kbd>0</kbd> key.
///
- /// Use <kbd>"NumpadMultiply"</kbd> for the <kbd>*</kbd> key on
+ /// Use <kbd>"`NumpadMultiply`"</kbd> for the <kbd>*</kbd> key on
/// numeric keypads.
NumpadStar,
/// <kbd>-</kbd>
@@ -403,10 +404,10 @@ pub enum Key
/// <kbd>Fn</kbd> This is typically a hardware key that does not generate a separate
/// code.
Fn,
- /// <kbd>FLock</kbd> or <kbd>FnLock</kbd>. Function Lock key. Found on the Microsoft
- /// Natural Keyboard.
+ /// <kbd>`FLock`</kbd> or <kbd>`FnLock`</kbd>. Function Lock key. Found on the
+ /// Microsoft Natural Keyboard.
FnLock,
- /// <kbd>PrtScr SysRq</kbd> or <kbd>Print Screen</kbd>
+ /// <kbd>`PrtScr` `SysRq`</kbd> or <kbd>Print Screen</kbd>
PrintScreen,
/// <kbd>Scroll Lock</kbd>
ScrollLock,
@@ -577,6 +578,7 @@ impl TryFrom<winit::keyboard::KeyCode> for Key
{
type Error = UnknownKeyCodeError;
+ #[allow(clippy::too_many_lines)]
fn try_from(key_code: winit::keyboard::KeyCode) -> Result<Self, Self::Error>
{
match key_code {
diff --git a/engine/src/windowing/monitor.rs b/engine/src/windowing/monitor.rs
index 894448a..392489d 100644
--- a/engine/src/windowing/monitor.rs
+++ b/engine/src/windowing/monitor.rs
@@ -11,6 +11,7 @@ impl Handle
{
/// Returns a human-readable name of the monitor.
#[inline]
+ #[must_use]
pub fn name(&self) -> Option<String>
{
self.inner.name()
@@ -18,6 +19,7 @@ impl Handle
/// Returns the monitor's resolution.
#[inline]
+ #[must_use]
pub fn size(&self) -> PhysicalSize<u32>
{
self.inner.size().into()
@@ -26,6 +28,7 @@ impl Handle
/// Returns the top-left corner position of the monitor relative to the larger full
/// screen area.
#[inline]
+ #[must_use]
pub fn position(&self) -> PhysicalPosition<i32>
{
self.inner.position().into()
@@ -33,6 +36,7 @@ impl Handle
/// Returns the scale factor of the underlying monitor.
#[inline]
+ #[must_use]
pub fn scale_factor(&self) -> f64
{
self.inner.scale_factor()
diff --git a/engine/src/windowing/mouse.rs b/engine/src/windowing/mouse.rs
index 3a43e79..a5e4cc8 100644
--- a/engine/src/windowing/mouse.rs
+++ b/engine/src/windowing/mouse.rs
@@ -31,6 +31,7 @@ pub struct ScrollDelta
impl ScrollDelta
{
+ #[must_use]
pub fn is_zero(&self) -> bool
{
self.vert_lines == 0.0 && self.hor_lines == 0.0
@@ -46,6 +47,7 @@ pub struct Buttons
impl Buttons
{
+ #[must_use]
pub fn get(&self, button: Button) -> ButtonState
{
let Some(button_data) = self.map.get(&button) else {
@@ -55,6 +57,7 @@ impl Buttons
button_data.current_state
}
+ #[must_use]
pub fn get_previous(&self, button: Button) -> ButtonState
{
let Some(button_data) = self.map.get(&button) else {
@@ -70,7 +73,7 @@ impl Buttons
{
self.map
.iter()
- .map(|(button, button_data)| (button.clone(), button_data.current_state))
+ .map(|(button, button_data)| (*button, button_data.current_state))
}
pub fn set(&mut self, button: Button, button_state: ButtonState)
@@ -120,7 +123,7 @@ impl intmap::IntKey for Button
Self::Middle => 2,
Self::Back => 3,
Self::Forward => 4,
- Self::Other(other) => 5 + *other as u32,
+ Self::Other(other) => 5 + u32::from(*other),
}
}
}
diff --git a/engine/src/windowing/window.rs b/engine/src/windowing/window.rs
index 6d7a464..ae4a36b 100644
--- a/engine/src/windowing/window.rs
+++ b/engine/src/windowing/window.rs
@@ -59,6 +59,7 @@ macro_rules! gen_creation_attrs_with_fn {
paste::paste! {
impl CreationAttributes
{
+ #[must_use]
pub fn [<with_ $field>](mut self, new: impl Into<$field_type>) -> Self
{
self.$field = new.into();
@@ -93,12 +94,9 @@ impl CreationAttributes
.with_title(self.title.into_owned())
.with_transparent(self.transparent)
.with_maximized(self.maximized)
- .with_fullscreen(match self.fullscreen {
- Some(Fullscreen::Borderless) => {
- Some(winit::window::Fullscreen::Borderless(None))
- }
- None => None,
- })
+ .with_fullscreen(self.fullscreen.map(|Fullscreen::Borderless| {
+ winit::window::Fullscreen::Borderless(None)
+ }))
.with_visible(self.visible)
.with_resizable(self.resizable)
.with_window_icon(match self.icon {
@@ -212,11 +210,13 @@ pub struct Window
impl Window
{
+ #[must_use]
pub fn wid(&self) -> Id
{
self.wid
}
+ #[must_use]
pub fn scale_factor(&self) -> f64
{
self.scale_factor
@@ -243,11 +243,11 @@ impl Window
winit_window.set_title(&self.title);
winit_window.set_cursor_visible(self.cursor_visible);
- let curr_inner_size = winit_window.inner_size().clone().into();
+ let curr_inner_size = winit_window.inner_size().into();
- let inner_size_request_result = match winit_window.request_inner_size(
- winit::dpi::Size::Physical(self.inner_size.clone().into()),
- ) {
+ let inner_size_request_result = match winit_window
+ .request_inner_size(winit::dpi::Size::Physical(self.inner_size.into()))
+ {
// The comparison of curr_inner_size is in case the user's windowing system
// lies about using the requested inner size
None if curr_inner_size == self.inner_size => Ok(()),