Skip to content

Commit 6dcc191

Browse files
authored
Make bevy_winit::converters module public (#22336)
# Objective - Fixes: #5980 Some of these converters are useful to users. I found myself needing to convert to a bevy KeyCode so i looked at all places its mentioned and found `convert_physical_key_code`. Then i looked up why its not in a pub module and found #5980 and #5986 In #5986 @mockersf expressed wanting these to be turned into From implementations, which Cart later argued against and instead wanted as Into implementations in bevy_winit. But the orphan rule disallows this, as the releavant types are implemented in bevy_input, bevy_window, bevy_math, etc. Because of this, and the fact its been 3 years, i'm proposing just leaving them as converter functions. The usecases are rare enough that users can deal with some non-idiomatic code and unintuitive function names. ## Solution `use converters;` -> `pub use converters;` I've also written basic docstrings. ## Testing - cargo check - cargo doc --- ## Showcase `bevy_winit::converters` is now public, allowing access to the following functions: <details><summary>Newly exposed functions</summary> <p> - `convert_element_state` Converts a `winit::event::ElementState` to a Bevy `ButtonState` - `convert_enabled_buttons` Converts a Bevy `EnabledButtons` to a `winit::window::WindowButtons` - `convert_keyboard_input` Converts a `winit::event::KeyEvent` and a window Entity to a Bevy `KeyboardInput` - `convert_logical_key` Converts a `winit::keyboard::Key` to a Bevy `bevy_input`::keyboard::Key - `convert_mouse_button` Converts a `winit::event::MouseButton` to a Bevy `MouseButton` - `convert_native_key` Converts a `winit::keyboard::NativeKey` to a Bevy `NativeKey` - `convert_physical_key_code` Converts a `winit::keyboard::PhysicalKey` to a Bevy `KeyCode` - `convert_physical_native_key_code` Converts a `winit::keyboard::NativeKeyCode` to a Bevy `NativeKeyCode` - `convert_resize_direction` Converts a Bevy `CompassOctant` to a `winit::window::ResizeDirection` - `convert_system_cursor_icon` Converts a Bevy `SystemCursorIcon` to a `winit::window::CursorIcon.` - `convert_touch_input` Converts a `winit::event::Touch`, `winit::dpi::LogicalPosition<f64>` and window `Entity` to a Bevy `TouchInput` - `convert_window_level` Converts a Bevy `WindowLevel` to a `winit::window::WindowLevel` - `convert_window_theme` Converts a Bevy `WindowTheme` to a `winit::window::Theme` - `convert_winit_theme` Converts a `winit::window::Theme` to a Bevy `WindowTheme` </p> </details>
1 parent e4eb916 commit 6dcc191

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

crates/bevy_winit/src/converters.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Helpers for mapping between winit and bevy types
12
use bevy_ecs::entity::Entity;
23
use bevy_input::{
34
keyboard::{KeyCode, KeyboardInput, NativeKeyCode},
@@ -13,6 +14,7 @@ use winit::keyboard::{Key, NamedKey, NativeKey};
1314
#[cfg(target_os = "ios")]
1415
use bevy_window::ScreenEdge;
1516

17+
/// Converts a [`winit::event::KeyEvent`] and a window [`Entity`] to a Bevy [`KeyboardInput`]
1618
pub fn convert_keyboard_input(
1719
keyboard_input: &winit::event::KeyEvent,
1820
window: Entity,
@@ -27,13 +29,15 @@ pub fn convert_keyboard_input(
2729
}
2830
}
2931

32+
/// Converts a [`winit::event::ElementState`] to a Bevy [`ButtonState`]
3033
pub fn convert_element_state(element_state: winit::event::ElementState) -> ButtonState {
3134
match element_state {
3235
winit::event::ElementState::Pressed => ButtonState::Pressed,
3336
winit::event::ElementState::Released => ButtonState::Released,
3437
}
3538
}
3639

40+
/// Converts a [`winit::event::MouseButton`] to a Bevy [`MouseButton`]
3741
pub fn convert_mouse_button(mouse_button: winit::event::MouseButton) -> MouseButton {
3842
match mouse_button {
3943
winit::event::MouseButton::Left => MouseButton::Left,
@@ -45,6 +49,7 @@ pub fn convert_mouse_button(mouse_button: winit::event::MouseButton) -> MouseBut
4549
}
4650
}
4751

52+
/// Converts a [`winit::event::Touch`], [`winit::dpi::LogicalPosition<f64>`] and window [`Entity`] to a Bevy [`TouchInput`]
4853
pub fn convert_touch_input(
4954
touch_input: winit::event::Touch,
5055
location: winit::dpi::LogicalPosition<f64>,
@@ -75,6 +80,7 @@ pub fn convert_touch_input(
7580
}
7681
}
7782

83+
/// Converts a [`winit::keyboard::NativeKeyCode`] to a Bevy [`NativeKeyCode`]
7884
pub fn convert_physical_native_key_code(
7985
native_key_code: winit::keyboard::NativeKeyCode,
8086
) -> NativeKeyCode {
@@ -86,6 +92,7 @@ pub fn convert_physical_native_key_code(
8692
winit::keyboard::NativeKeyCode::Xkb(key_code) => NativeKeyCode::Xkb(key_code),
8793
}
8894
}
95+
/// Converts a [`winit::keyboard::PhysicalKey`] to a Bevy [`KeyCode`]
8996
pub fn convert_physical_key_code(virtual_key_code: winit::keyboard::PhysicalKey) -> KeyCode {
9097
match virtual_key_code {
9198
winit::keyboard::PhysicalKey::Unidentified(native_key_code) => {
@@ -291,6 +298,7 @@ pub fn convert_physical_key_code(virtual_key_code: winit::keyboard::PhysicalKey)
291298
}
292299
}
293300

301+
///Converts a [`winit::keyboard::Key`] to a Bevy [`bevy_input::keyboard::Key`]
294302
pub fn convert_logical_key(logical_key_code: &Key) -> bevy_input::keyboard::Key {
295303
match logical_key_code {
296304
Key::Character(s) => bevy_input::keyboard::Key::Character(s.clone()),
@@ -622,6 +630,7 @@ pub fn convert_logical_key(logical_key_code: &Key) -> bevy_input::keyboard::Key
622630
}
623631
}
624632

633+
///Converts a [`winit::keyboard::NativeKey`] to a Bevy [`NativeKey`](bevy_input::keyboard::NativeKey)
625634
pub fn convert_native_key(native_key: &NativeKey) -> bevy_input::keyboard::NativeKey {
626635
match native_key {
627636
NativeKey::Unidentified => bevy_input::keyboard::NativeKey::Unidentified,
@@ -633,7 +642,7 @@ pub fn convert_native_key(native_key: &NativeKey) -> bevy_input::keyboard::Nativ
633642
}
634643
}
635644

636-
/// Converts a [`SystemCursorIcon`] to a [`winit::window::CursorIcon`].
645+
/// Converts a Bevy [`SystemCursorIcon`] to a [`winit::window::CursorIcon`].
637646
pub fn convert_system_cursor_icon(cursor_icon: SystemCursorIcon) -> winit::window::CursorIcon {
638647
match cursor_icon {
639648
SystemCursorIcon::Crosshair => winit::window::CursorIcon::Crosshair,
@@ -673,6 +682,7 @@ pub fn convert_system_cursor_icon(cursor_icon: SystemCursorIcon) -> winit::windo
673682
}
674683
}
675684

685+
/// Converts a Bevy [`WindowLevel`] to a [`winit::window::WindowLevel`]
676686
pub fn convert_window_level(window_level: WindowLevel) -> winit::window::WindowLevel {
677687
match window_level {
678688
WindowLevel::AlwaysOnBottom => winit::window::WindowLevel::AlwaysOnBottom,
@@ -681,20 +691,23 @@ pub fn convert_window_level(window_level: WindowLevel) -> winit::window::WindowL
681691
}
682692
}
683693

694+
/// Converts a [`winit::window::Theme`] to a Bevy [`WindowTheme`]
684695
pub fn convert_winit_theme(theme: winit::window::Theme) -> WindowTheme {
685696
match theme {
686697
winit::window::Theme::Light => WindowTheme::Light,
687698
winit::window::Theme::Dark => WindowTheme::Dark,
688699
}
689700
}
690701

702+
/// Converts a Bevy [`WindowTheme`] to a [`winit::window::Theme`]
691703
pub fn convert_window_theme(theme: WindowTheme) -> winit::window::Theme {
692704
match theme {
693705
WindowTheme::Light => winit::window::Theme::Light,
694706
WindowTheme::Dark => winit::window::Theme::Dark,
695707
}
696708
}
697709

710+
/// Converts a Bevy [`EnabledButtons`] to a [`winit::window::WindowButtons`]
698711
pub fn convert_enabled_buttons(enabled_buttons: EnabledButtons) -> winit::window::WindowButtons {
699712
let mut window_buttons = winit::window::WindowButtons::empty();
700713
if enabled_buttons.minimize {
@@ -709,6 +722,7 @@ pub fn convert_enabled_buttons(enabled_buttons: EnabledButtons) -> winit::window
709722
window_buttons
710723
}
711724

725+
/// Converts a Bevy [`CompassOctant`] to a [`winit::window::ResizeDirection`]
712726
pub fn convert_resize_direction(resize_direction: CompassOctant) -> winit::window::ResizeDirection {
713727
match resize_direction {
714728
CompassOctant::West => winit::window::ResizeDirection::West,

crates/bevy_winit/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use crate::{
4242
};
4343

4444
pub mod accessibility;
45-
mod converters;
45+
pub mod converters;
4646
mod cursor;
4747
mod state;
4848
mod system;

0 commit comments

Comments
 (0)