From 200410398cbce6c0633b14179e17a64a9b0a431e Mon Sep 17 00:00:00 2001 From: Tei Leelo Roberts Date: Tue, 22 Oct 2024 01:06:59 +0200 Subject: [PATCH] wip: return if input was captured by Ui --- violet-core/src/input.rs | 31 ++++++++++++++++++++++++++----- violet-wgpu/src/app.rs | 10 +++++----- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/violet-core/src/input.rs b/violet-core/src/input.rs index b779ec5..671b484 100644 --- a/violet-core/src/input.rs +++ b/violet-core/src/input.rs @@ -46,7 +46,7 @@ impl InputState { pos: Vec2, mut filter: impl FnMut(&EntityRef) -> bool, ) -> Option<(Entity, Vec2)> { - let query = (screen_transform(), rect()).filtered(focusable().with()); + let query = (screen_transform(), rect()); OrderedDfsIterator::new(&frame.world, frame.world.entity(self.root).unwrap()) .filter_map(|entity| { if !filter(&entity) { @@ -67,7 +67,12 @@ impl InputState { .last() } - pub fn on_mouse_input(&mut self, frame: &mut Frame, state: ElementState, button: MouseButton) { + pub fn on_mouse_input( + &mut self, + frame: &mut Frame, + state: ElementState, + button: MouseButton, + ) -> bool { let cursor_pos = self.pos; let intersect = self.find_intersect(frame, cursor_pos, |_| true); @@ -103,6 +108,7 @@ impl InputState { absolute_pos: self.pos, local_pos, }; + if let Ok(mut on_input) = entity.get_mut(on_mouse_input()) { let s = ScopeRef::new(frame, entity); on_input( @@ -115,10 +121,14 @@ impl InputState { }, ); } + + return true; } + + false } - pub fn on_cursor_move(&mut self, frame: &mut Frame, pos: Vec2) { + pub fn on_cursor_move(&mut self, frame: &mut Frame, pos: Vec2) -> bool { self.pos = pos; if let Some(entity) = &self.focused(&frame.world) { @@ -136,10 +146,13 @@ impl InputState { }, ); } + + return true; } + false } - pub fn on_scroll(&mut self, frame: &mut Frame, delta: Vec2) { + pub fn on_scroll(&mut self, frame: &mut Frame, delta: Vec2) -> bool { let intersect = self.find_intersect(frame, self.pos, |v| v.has(on_scroll())); if let Some((id, _)) = intersect { @@ -154,7 +167,11 @@ impl InputState { }, ); } + + return true; } + + false } pub fn on_modifiers_change(&mut self, modifiers: ModifiersState) { @@ -167,7 +184,7 @@ impl InputState { key: Key, state: ElementState, text: Option, - ) { + ) -> bool { if let Some(entity) = &self.focused(frame.world()) { if let Ok(mut on_input) = entity.get_mut(on_keyboard_input()) { let s = ScopeRef::new(frame, *entity); @@ -181,7 +198,11 @@ impl InputState { }, ); } + + return true; } + + false } fn focused<'a>(&self, world: &'a World) -> Option> { diff --git a/violet-wgpu/src/app.rs b/violet-wgpu/src/app.rs index bdf9762..42aba94 100644 --- a/violet-wgpu/src/app.rs +++ b/violet-wgpu/src/app.rs @@ -20,7 +20,7 @@ use violet_core::{ input::InputState, io::{self, Clipboard}, layout::cache::LayoutUpdateEvent, - style::{primary_surface, setup_stylesheet, stylesheet, Background, SizeExt}, + style::{setup_stylesheet, stylesheet, SizeExt}, systems::{ hydrate_text, invalidate_cached_layout_system, layout_system, templating_system, transform_system, @@ -408,14 +408,14 @@ impl ApplicationHandler for WindowEventHandler { event.logical_key, event.state, event.text, - ) + ); } WindowEvent::CursorMoved { position, .. } => { puffin::profile_scope!("CursorMoved"); instance.input_state.on_cursor_move( &mut instance.frame, vec2(position.x as f32, position.y as f32), - ) + ); } WindowEvent::MouseWheel { delta, .. } => { puffin::profile_scope!("MouseWheel"); @@ -424,13 +424,13 @@ impl ApplicationHandler for WindowEventHandler { const LINE_SIZE: f32 = 16.0; instance .input_state - .on_scroll(&mut instance.frame, vec2(x * LINE_SIZE, y * LINE_SIZE)) + .on_scroll(&mut instance.frame, vec2(x * LINE_SIZE, y * LINE_SIZE)); } winit::event::MouseScrollDelta::PixelDelta(pos) => { let pos = pos.to_logical::(instance.scale_factor); instance .input_state - .on_scroll(&mut instance.frame, vec2(pos.x, pos.y)) + .on_scroll(&mut instance.frame, vec2(pos.x, pos.y)); } } }