actually sane sensor handling
This commit is contained in:
1 parent
f4aef3a983
commit
11188f2951
9 files changed
+306
-117
No files matched your search
+16
-33
@@ -1,61 +1,44 @@
|
||||
use gui::{Sense, SenseCtx, SenseTrigger, Vec2};
|
||||
use gui::{CursorState, Vec2};
|
||||
use winit::event::WindowEvent;
|
||||
|
||||
use crate::testing::Client;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Input {
|
||||
size: Vec2,
|
||||
mouse_pos: Vec2,
|
||||
mouse_pressed: bool,
|
||||
mouse_just_pressed: bool,
|
||||
mouse_just_released: bool,
|
||||
mouse_in: bool,
|
||||
mouse_exists: bool,
|
||||
}
|
||||
|
||||
impl Input {
|
||||
pub fn event(&mut self, event: &WindowEvent) {
|
||||
self.mouse_just_pressed = false;
|
||||
self.mouse_just_released = false;
|
||||
match event {
|
||||
WindowEvent::Resized(size) => {
|
||||
self.size = Vec2::new(size.width as f32, size.height as f32);
|
||||
}
|
||||
WindowEvent::CursorMoved { position, .. } => {
|
||||
self.mouse_pos = Vec2::new(position.x as f32, position.y as f32);
|
||||
self.mouse_in = true;
|
||||
self.mouse_exists = true;
|
||||
}
|
||||
WindowEvent::MouseInput { state, button, .. } => match button {
|
||||
winit::event::MouseButton::Left => {
|
||||
if state.is_pressed() {
|
||||
self.mouse_just_pressed = !self.mouse_pressed;
|
||||
self.mouse_pressed = true;
|
||||
} else {
|
||||
self.mouse_just_released = self.mouse_pressed;
|
||||
self.mouse_pressed = false;
|
||||
}
|
||||
self.mouse_pressed = state.is_pressed();
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
fn active(&mut self, trigger: &SenseTrigger) -> bool {
|
||||
let region = trigger.shape.to_screen(self.size);
|
||||
if !self.mouse_in || !region.contains(self.mouse_pos) {
|
||||
return trigger.sense == Sense::NoHover;
|
||||
}
|
||||
match trigger.sense {
|
||||
Sense::Press => self.mouse_just_pressed,
|
||||
Sense::Held => self.mouse_pressed,
|
||||
Sense::Hover => true,
|
||||
Sense::NoHover => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SenseCtx for Client {
|
||||
fn active(&mut self, trigger: &SenseTrigger) -> bool {
|
||||
self.input.active(trigger)
|
||||
impl Client {
|
||||
pub fn window_size(&self) -> Vec2 {
|
||||
let size = self.renderer.window().inner_size();
|
||||
(size.width, size.height).into()
|
||||
}
|
||||
|
||||
pub fn cursor_state(&self) -> CursorState {
|
||||
CursorState {
|
||||
pos: self.input.mouse_pos,
|
||||
exists: self.input.mouse_exists,
|
||||
pressed: self.input.mouse_pressed,
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
-7
@@ -84,17 +84,18 @@ impl Client {
|
||||
let main = main.clone();
|
||||
let to = to.clone().erase_type();
|
||||
Rect::new(color)
|
||||
.sense(Sense::Press, move |ui, _| {
|
||||
.id_on(Sense::PressStart, move |id, ui, _| {
|
||||
ui[&main].inner = to.clone();
|
||||
ui[id].color = color.add_rgb(-0.2);
|
||||
})
|
||||
.sense_and_edit(Sense::Hover, move |r, _| {
|
||||
.edit_on(Sense::HoverStart, move |r, _| {
|
||||
r.color = color.add_rgb(0.1);
|
||||
})
|
||||
.sense_and_edit(Sense::NoHover, move |r, _| {
|
||||
r.color = color;
|
||||
.edit_on(Sense::PressEnd, move |r, _| {
|
||||
r.color = color.add_rgb(0.1);
|
||||
})
|
||||
.sense_and_edit(Sense::Held, move |r, _| {
|
||||
r.color = color.add_rgb(-0.1);
|
||||
.edit_on(Sense::HoverEnd, move |r, _| {
|
||||
r.color = color;
|
||||
})
|
||||
}
|
||||
|
||||
@@ -135,7 +136,9 @@ impl Client {
|
||||
|
||||
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop, ui: &mut Ui<Self>) {
|
||||
self.input.event(&event);
|
||||
ui.run_sensors(self);
|
||||
let cursor_state = self.cursor_state();
|
||||
let window_size = self.window_size();
|
||||
ui.run_sensors(self, &cursor_state, window_size);
|
||||
match event {
|
||||
WindowEvent::CloseRequested => event_loop.exit(),
|
||||
WindowEvent::RedrawRequested => {
|
||||
|
||||
Reference in new issue
Block a user