fix mouse buttons on wayland

This commit is contained in:
griffi-gh 2024-09-01 21:14:49 +02:00
parent bd299bc7a3
commit be1e24ba0c
2 changed files with 30 additions and 3 deletions

View file

@ -50,7 +50,8 @@ winapi = { version = "0.3", features = ["wincon"] }
[features] [features]
default = ["raw-evt-mouse"] default = ["raw-evt-mouse"]
raw-evt-keyboard = [] # use raw input for keyboard. works on x11 and windows, breaks keyboard on android and wayland raw-evt-keyboard = [] # use raw input for keyboard. works on x11 and windows, breaks keyboard on android and wayland
raw-evt-mouse = [] # required for mouse input raw-evt-mouse = [] # use raw input for mouse movement events. *required* for mouse input
raw-evt-button = [] # use raw input for mouse button events. used to be the default, but breaks on wayland.
c-ffi = [] # generate a C-ffi-compatible `kubi_extern_main` entry point (useful if building as a shared library) c-ffi = [] # generate a C-ffi-compatible `kubi_extern_main` entry point (useful if building as a shared library)
generate_visualizer_data = ["dep:serde_json", "shipyard/serde1"] generate_visualizer_data = ["dep:serde_json", "shipyard/serde1"]
safe_lz4 = ["lz4_flex/safe-encode", "lz4_flex/safe-decode"] safe_lz4 = ["lz4_flex/safe-encode", "lz4_flex/safe-decode"]

View file

@ -1,6 +1,6 @@
use glam::UVec2; use glam::UVec2;
use shipyard::{World, Component, AllStoragesViewMut, SparseSet}; use shipyard::{World, Component, AllStoragesViewMut, SparseSet};
use winit::event::{Event, DeviceEvent, DeviceId, WindowEvent, Touch}; use winit::event::{Event, DeviceEvent, DeviceId, WindowEvent, Touch, MouseButton};
pub mod player_actions; pub mod player_actions;
@ -52,6 +52,28 @@ pub fn process_winit_events(world: &mut World, event: &Event<()>) {
)); ));
} }
#[cfg(not(feature = "raw-evt-button"))]
WindowEvent::MouseInput { device_id, state, button } => {
// HACK: translate MouseInput events to raw device events
world.add_entity((
EventComponent,
InputDeviceEvent {
device_id: *device_id,
event: DeviceEvent::Button {
button: match button {
MouseButton::Left => 0,
MouseButton::Right => 1,
MouseButton::Middle => 2,
MouseButton::Back => 3,
MouseButton::Forward => 4,
MouseButton::Other(id) => *id as u32,
},
state: *state
}
}
));
}
WindowEvent::Touch(touch) => { WindowEvent::Touch(touch) => {
// if matches!(touch.phase, TouchPhase::Started | TouchPhase::Cancelled | TouchPhase::Ended) { // if matches!(touch.phase, TouchPhase::Started | TouchPhase::Cancelled | TouchPhase::Ended) {
// println!("TOUCH ==================== {:#?}", touch); // println!("TOUCH ==================== {:#?}", touch);
@ -70,6 +92,7 @@ pub fn process_winit_events(world: &mut World, event: &Event<()>) {
#[cfg(any( #[cfg(any(
feature = "raw-evt-keyboard", feature = "raw-evt-keyboard",
feature = "raw-evt-mouse", feature = "raw-evt-mouse",
feature = "raw-evt-button",
))] ))]
Event::DeviceEvent { device_id, event } => { Event::DeviceEvent { device_id, event } => {
// Filter out events we don't care about // Filter out events we don't care about
@ -78,7 +101,10 @@ pub fn process_winit_events(world: &mut World, event: &Event<()>) {
DeviceEvent::Key(_) => (), DeviceEvent::Key(_) => (),
#[cfg(feature = "raw-evt-mouse")] #[cfg(feature = "raw-evt-mouse")]
DeviceEvent::MouseMotion { .. } | DeviceEvent::Button { .. } => (), DeviceEvent::MouseMotion { .. } => (),
#[cfg(feature = "raw-evt-button")]
DeviceEvent::Button { .. } => (),
_ => return, _ => return,
}; };