From e3a6b70cd7516a84be4aad62d21d024ed0158ee0 Mon Sep 17 00:00:00 2001 From: Shark Date: Sun, 29 Dec 2024 18:58:33 +0100 Subject: [PATCH] pass more events to the `EngineInstance` (vello_renderer) --- .../gosub_web_platform/src/event_listeners.rs | 1 + examples/vello-renderer/event_loop.rs | 192 +++++++++++------- 2 files changed, 114 insertions(+), 79 deletions(-) diff --git a/crates/gosub_web_platform/src/event_listeners.rs b/crates/gosub_web_platform/src/event_listeners.rs index 0b52329f..95480d74 100644 --- a/crates/gosub_web_platform/src/event_listeners.rs +++ b/crates/gosub_web_platform/src/event_listeners.rs @@ -39,6 +39,7 @@ pub struct EventListener { impl EventListener { pub fn handle_event(&mut self, event: D, e: &mut E) { + info!("Handling event: {:?}", event); for listener in self.listeners.iter_mut() { listener.execute(e, event.clone()); } diff --git a/examples/vello-renderer/event_loop.rs b/examples/vello-renderer/event_loop.rs index d3aafd94..1ad43ca2 100644 --- a/examples/vello-renderer/event_loop.rs +++ b/examples/vello-renderer/event_loop.rs @@ -1,9 +1,10 @@ use crate::window::{Window, WindowState}; use gosub_instance::{DebugEvent, InstanceMessage}; use gosub_interface::config::ModuleConfiguration; -use gosub_interface::input::InputEvent; +use gosub_interface::input::{InputEvent, MouseButton}; use gosub_interface::render_backend::{Point, RenderBackend, SizeU32, FP}; use gosub_shared::types::Result; +use log::info; use winit::event::{ElementState, MouseScrollDelta, WindowEvent}; use winit::event_loop::ActiveEventLoop; use winit::keyboard::{KeyCode, ModifiersState, PhysicalKey}; @@ -72,108 +73,141 @@ impl Window<'_, C> { } WindowEvent::KeyboardInput { event, .. } => { - if event.repeat || event.state == ElementState::Released { + let Some(tab) = self.tabs.get_current_tab() else { return Ok(()); - } + }; - let Some(tab) = self.tabs.get_current_tab() else { + let winit::keyboard::Key::Character(code) = event.logical_key else { return Ok(()); }; - if let PhysicalKey::Code(code) = event.physical_key { - match code { - KeyCode::KeyD => { - tab.tx.blocking_send(InstanceMessage::Debug(DebugEvent::Toggle))?; - self.window.request_redraw(); - } - KeyCode::KeyC => { - tab.tx.blocking_send(InstanceMessage::Debug(DebugEvent::ClearBuffers))?; - self.window.request_redraw(); - } - KeyCode::F5 => { - tab.tx.blocking_send(InstanceMessage::Reload)?; - } - KeyCode::ArrowRight => { - if self.mods.state().contains(ModifiersState::CONTROL) { - self.tabs.next_tab(); + let Some(code) = code.to_string().chars().next() else { + return Ok(()); + }; + + let input = match event.state { + ElementState::Pressed => InputEvent::KeyboardDown, + ElementState::Released => InputEvent::KeyboardUp, + }(code); + + tab.tx.blocking_send(InstanceMessage::Input(input))?; + + if !event.repeat && event.state != ElementState::Released { + if let PhysicalKey::Code(code) = event.physical_key { + match code { + KeyCode::KeyD => { + tab.tx.blocking_send(InstanceMessage::Debug(DebugEvent::Toggle))?; self.window.request_redraw(); } - } - KeyCode::ArrowLeft => { - if self.mods.state().contains(ModifiersState::CONTROL) { - self.tabs.previous_tab(); + KeyCode::KeyC => { + tab.tx.blocking_send(InstanceMessage::Debug(DebugEvent::ClearBuffers))?; self.window.request_redraw(); } - } - KeyCode::Digit0 => { - if self.mods.state().contains(ModifiersState::CONTROL) { - self.tabs.activate_idx(0); - self.window.request_redraw(); + KeyCode::F5 => { + tab.tx.blocking_send(InstanceMessage::Reload)?; } - } - KeyCode::Digit1 => { - if self.mods.state().contains(ModifiersState::CONTROL) { - self.tabs.activate_idx(1); - self.window.request_redraw(); + KeyCode::ArrowRight => { + if self.mods.state().contains(ModifiersState::CONTROL) { + self.tabs.next_tab(); + self.window.request_redraw(); + } } - } - KeyCode::Digit2 => { - if self.mods.state().contains(ModifiersState::CONTROL) { - self.tabs.activate_idx(2); - self.window.request_redraw(); + KeyCode::ArrowLeft => { + if self.mods.state().contains(ModifiersState::CONTROL) { + self.tabs.previous_tab(); + self.window.request_redraw(); + } } - } - KeyCode::Digit3 => { - if self.mods.state().contains(ModifiersState::CONTROL) { - self.tabs.activate_idx(3); - self.window.request_redraw(); + KeyCode::Digit0 => { + if self.mods.state().contains(ModifiersState::CONTROL) { + self.tabs.activate_idx(0); + self.window.request_redraw(); + } } - } - KeyCode::Digit4 => { - if self.mods.state().contains(ModifiersState::CONTROL) { - self.tabs.activate_idx(4); - self.window.request_redraw(); + KeyCode::Digit1 => { + if self.mods.state().contains(ModifiersState::CONTROL) { + self.tabs.activate_idx(1); + self.window.request_redraw(); + } } - } - KeyCode::Digit5 => { - if self.mods.state().contains(ModifiersState::CONTROL) { - self.tabs.activate_idx(5); - self.window.request_redraw(); + KeyCode::Digit2 => { + if self.mods.state().contains(ModifiersState::CONTROL) { + self.tabs.activate_idx(2); + self.window.request_redraw(); + } } - } - KeyCode::Digit6 => { - if self.mods.state().contains(ModifiersState::CONTROL) { - self.tabs.activate_idx(6); - self.window.request_redraw(); + KeyCode::Digit3 => { + if self.mods.state().contains(ModifiersState::CONTROL) { + self.tabs.activate_idx(3); + self.window.request_redraw(); + } } - } - KeyCode::Digit7 => { - if self.mods.state().contains(ModifiersState::CONTROL) { - self.tabs.activate_idx(7); - self.window.request_redraw(); + KeyCode::Digit4 => { + if self.mods.state().contains(ModifiersState::CONTROL) { + self.tabs.activate_idx(4); + self.window.request_redraw(); + } } - } - KeyCode::Digit8 => { - if self.mods.state().contains(ModifiersState::CONTROL) { - self.tabs.activate_idx(8); - self.window.request_redraw(); + KeyCode::Digit5 => { + if self.mods.state().contains(ModifiersState::CONTROL) { + self.tabs.activate_idx(5); + self.window.request_redraw(); + } } - } - KeyCode::Digit9 => { - if self.mods.state().contains(ModifiersState::CONTROL) { - self.tabs.activate_idx(9); - self.window.request_redraw(); + KeyCode::Digit6 => { + if self.mods.state().contains(ModifiersState::CONTROL) { + self.tabs.activate_idx(6); + self.window.request_redraw(); + } + } + KeyCode::Digit7 => { + if self.mods.state().contains(ModifiersState::CONTROL) { + self.tabs.activate_idx(7); + self.window.request_redraw(); + } + } + KeyCode::Digit8 => { + if self.mods.state().contains(ModifiersState::CONTROL) { + self.tabs.activate_idx(8); + self.window.request_redraw(); + } + } + KeyCode::Digit9 => { + if self.mods.state().contains(ModifiersState::CONTROL) { + self.tabs.activate_idx(9); + self.window.request_redraw(); + } } - } - // KeyCode::F6 => self.el.open_tab(Url::parse("https://news.ycombinator.com")?), - // KeyCode::F7 => self.el.open_tab(Url::parse("https://archlinux.org")?), - // KeyCode::F8 => self.el.open_tab(Url::parse("file://resources/test.html")?), - _ => {} + // KeyCode::F6 => self.el.open_tab(Url::parse("https://news.ycombinator.com")?), + // KeyCode::F7 => self.el.open_tab(Url::parse("https://archlinux.org")?), + // KeyCode::F8 => self.el.open_tab(Url::parse("file://resources/test.html")?), + _ => {} + } } } } + WindowEvent::MouseInput { state, button, .. } => { + let Some(tab) = self.tabs.get_current_tab() else { + return Ok(()); + }; + + let button = match button { + winit::event::MouseButton::Left => MouseButton::Left, + winit::event::MouseButton::Right => MouseButton::Right, + winit::event::MouseButton::Middle => MouseButton::Middle, + _ => return Ok(()), + }; + + let event = match state { + ElementState::Pressed => InputEvent::MouseDown, + ElementState::Released => InputEvent::MouseUp, + }(button); + + tab.tx.blocking_send(InstanceMessage::Input(event))?; + } + WindowEvent::ModifiersChanged(mods) => { self.mods = mods; }