Skip to content

Commit

Permalink
pass more events to the EngineInstance (vello_renderer)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharktheone committed Jan 14, 2025
1 parent 7f4f806 commit e3a6b70
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 79 deletions.
1 change: 1 addition & 0 deletions crates/gosub_web_platform/src/event_listeners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct EventListener<D, E: FutureExecutor> {

impl<D: Clone + Debug, E: FutureExecutor> EventListener<D, E> {
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());
}
Expand Down
192 changes: 113 additions & 79 deletions examples/vello-renderer/event_loop.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -72,108 +73,141 @@ impl<C: ModuleConfiguration> 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;
}
Expand Down

0 comments on commit e3a6b70

Please sign in to comment.