diff --git a/src/event.rs b/src/event.rs index 44b704d..a1d1478 100644 --- a/src/event.rs +++ b/src/event.rs @@ -15,6 +15,7 @@ pub enum Event { /// Identifies a touch so we can track it. id: usize, position: LocalPoint, + delta: LocalOffset, }, /// Touch went up or mouse button released. @@ -38,9 +39,9 @@ impl Event { pub fn offset(&self, offset: LocalOffset) -> Event { let mut event = self.clone(); match &mut event { - Event::TouchBegin { id: _, position } => *position += offset, - Event::TouchMove { id: _, position } => *position += offset, - Event::TouchEnd { id: _, position } => *position += offset, + Event::TouchBegin { position, .. } => *position += offset, + Event::TouchMove { position, .. } => *position += offset, + Event::TouchEnd { position, .. } => *position += offset, _ => (), } event diff --git a/src/view.rs b/src/view.rs index 9e28366..83a4636 100644 --- a/src/view.rs +++ b/src/view.rs @@ -44,7 +44,7 @@ pub trait View: private::Sealed + 'static { fn draw(&self, id: ViewId, args: &mut DrawArgs); /// Gets IDs for views currently in use. - /// + /// /// Push onto map if the view stores layout or state info. fn gc(&self, _id: ViewId, _cx: &mut Context, _map: &mut Vec) {} diff --git a/src/views/canvas.rs b/src/views/canvas.rs index 657088d..b8a5b8d 100644 --- a/src/views/canvas.rs +++ b/src/views/canvas.rs @@ -42,7 +42,6 @@ where fn gc(&self, id: ViewId, _cx: &mut Context, map: &mut Vec) { map.push(id); } - } /// Canvas for GPU drawing with Vger. See https://github.com/audulus/vger-rs. diff --git a/src/views/drag.rs b/src/views/drag.rs index ea9b55b..a2ac848 100644 --- a/src/views/drag.rs +++ b/src/views/drag.rs @@ -21,14 +21,18 @@ where F: Fn(&mut Context, LocalOffset, GestureState, Option) -> A + 'static, { pub fn new(v: V, f: F) -> Self { - Self { child: v, func: f, grab: false } + Self { + child: v, + func: f, + grab: false, + } } pub fn grab_cursor(self) -> Self { Self { child: self.child, func: self.func, - grab: true + grab: true, } } } @@ -55,25 +59,28 @@ where cx.grab_cursor = self.grab; } } - Event::TouchMove { id, position } => { + Event::TouchMove { + id, + position, + delta, + } => { if cx.touches[*id] == vid { - let delta = *position - cx.previous_position[*id]; actions.push(Box::new((self.func)( cx, - delta, + *delta, GestureState::Changed, cx.mouse_button, ))); cx.previous_position[*id] = *position; } } - Event::TouchEnd { id, position } => { + Event::TouchEnd { id, .. } => { if cx.touches[*id] == vid { cx.touches[*id] = ViewId::default(); cx.grab_cursor = false; actions.push(Box::new((self.func)( cx, - *position - cx.previous_position[*id], + LocalOffset::zero(), GestureState::Ended, cx.mouse_button, ))); @@ -142,7 +149,7 @@ where func: f, binding: b, phantom: std::marker::PhantomData::default(), - grab: false + grab: false, } } @@ -152,7 +159,7 @@ where func: self.func, binding: self.binding, phantom: std::marker::PhantomData::default(), - grab: true + grab: true, } } } @@ -181,28 +188,30 @@ where cx.grab_cursor = self.grab; } } - Event::TouchMove { id, position } => { + Event::TouchMove { + id, + position, + delta, + } => { if cx.touches[*id] == vid { - let delta = *position - cx.previous_position[*id]; let button = cx.mouse_button; actions.push(Box::new((self.func)( self.binding.get_mut(cx), - delta, + *delta, GestureState::Changed, button, ))); cx.previous_position[*id] = *position; } } - Event::TouchEnd { id, position } => { + Event::TouchEnd { id, .. } => { if cx.touches[*id] == vid { cx.touches[*id] = ViewId::default(); cx.grab_cursor = false; - let delta = *position - cx.previous_position[*id]; let button = cx.mouse_button; actions.push(Box::new((self.func)( self.binding.get_mut(cx), - delta, + LocalOffset::zero(), GestureState::Ended, button, ))); diff --git a/src/views/hover.rs b/src/views/hover.rs index 9a73bb5..ff8dc81 100644 --- a/src/views/hover.rs +++ b/src/views/hover.rs @@ -30,7 +30,7 @@ where cx: &mut Context, actions: &mut Vec>, ) { - if let Event::TouchMove { id: _, position } = &event { + if let Event::TouchMove { position, .. } = &event { if cx.mouse_button.is_none() { let inside = self.hittest(vid, *position, cx).is_some(); actions.push(Box::new((self.func)(cx, inside))); diff --git a/src/views/knob.rs b/src/views/knob.rs index ed66d8c..6b6c252 100644 --- a/src/views/knob.rs +++ b/src/views/knob.rs @@ -14,7 +14,8 @@ pub fn knob(value: impl Binding) -> impl View { .color(CLEAR_COLOR) .drag_s(value, move |v, delta, _, _| { *v = (*v + (delta.x + delta.y) / 400.0).clamp(0.0, 1.0) - }).grab_cursor(), + }) + .grab_cursor(), canvas(move |cx, sz, vger| { let c = sz.center(); let r = sz.width().min(sz.height()) / 2.0; @@ -68,6 +69,7 @@ mod tests { Event::TouchMove { id: 0, position: [100.0, 50.0].into(), + delta: [50.0, 0.0].into(), }, Event::TouchEnd { id: 0, diff --git a/src/winit_event_loop.rs b/src/winit_event_loop.rs index 8026452..5fdee83 100644 --- a/src/winit_event_loop.rs +++ b/src/winit_event_loop.rs @@ -316,10 +316,16 @@ pub fn rui(view: impl View) { ] .into(); + let delta = position - cx.previous_position[0]; + // TODO: Multi-Touch management let event = match phase { TouchPhase::Started => Some(Event::TouchBegin { id: 0, position }), - TouchPhase::Moved => Some(Event::TouchMove { id: 0, position }), + TouchPhase::Moved => Some(Event::TouchMove { + id: 0, + position, + delta, + }), TouchPhase::Ended | TouchPhase::Cancelled => { Some(Event::TouchEnd { id: 0, position }) } @@ -339,11 +345,11 @@ pub fn rui(view: impl View) { (config.height as f32 - position.y as f32) / scale, ] .into(); - let event = Event::TouchMove { - id: 0, - position: mouse_position, - }; - process_event(&mut cx, &view, &event, &window) + // let event = Event::TouchMove { + // id: 0, + // position: mouse_position, + // }; + // process_event(&mut cx, &view, &event, &window) } WEvent::WindowEvent { @@ -496,8 +502,20 @@ pub fn rui(view: impl View) { }; } - WEvent::DeviceEvent { event: winit::event::DeviceEvent::MouseMotion{ delta }, .. } => { - println!("mouse delta: {} {}", delta.0, delta.1); + WEvent::DeviceEvent { + event: winit::event::DeviceEvent::MouseMotion { delta }, + .. + } => { + // Flip y coordinate. + let d: LocalOffset = [delta.0 as f32, -delta.1 as f32].into(); + + let event = Event::TouchMove { + id: 0, + position: mouse_position, + delta: d, + }; + + process_event(&mut cx, &view, &event, &window); } _ => (), }