Skip to content

Commit

Permalink
wip: return if input was captured by Ui
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Oct 21, 2024
1 parent fdeca4f commit 2004103
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
31 changes: 26 additions & 5 deletions violet-core/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);

Expand Down Expand Up @@ -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(
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -154,7 +167,11 @@ impl InputState {
},
);
}

return true;
}

false
}

pub fn on_modifiers_change(&mut self, modifiers: ModifiersState) {
Expand All @@ -167,7 +184,7 @@ impl InputState {
key: Key,
state: ElementState,
text: Option<SmolStr>,
) {
) -> 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);
Expand All @@ -181,7 +198,11 @@ impl InputState {
},
);
}

return true;
}

false
}

fn focused<'a>(&self, world: &'a World) -> Option<EntityRef<'a>> {
Expand Down
10 changes: 5 additions & 5 deletions violet-wgpu/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand All @@ -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::<f32>(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));
}
}
}
Expand Down

0 comments on commit 2004103

Please sign in to comment.