Skip to content

Commit

Permalink
Use mouse deltas
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed May 9, 2023
1 parent 4a732da commit 673b4a6
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 30 deletions.
7 changes: 4 additions & 3 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ViewId>) {}

Expand Down
1 change: 0 additions & 1 deletion src/views/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ where
fn gc(&self, id: ViewId, _cx: &mut Context, map: &mut Vec<ViewId>) {
map.push(id);
}

}

/// Canvas for GPU drawing with Vger. See https://github.com/audulus/vger-rs.
Expand Down
39 changes: 24 additions & 15 deletions src/views/drag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ where
F: Fn(&mut Context, LocalOffset, GestureState, Option<MouseButton>) -> 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,
}
}
}
Expand All @@ -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,
)));
Expand Down Expand Up @@ -142,7 +149,7 @@ where
func: f,
binding: b,
phantom: std::marker::PhantomData::default(),
grab: false
grab: false,
}
}

Expand All @@ -152,7 +159,7 @@ where
func: self.func,
binding: self.binding,
phantom: std::marker::PhantomData::default(),
grab: true
grab: true,
}
}
}
Expand Down Expand Up @@ -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,
)));
Expand Down
2 changes: 1 addition & 1 deletion src/views/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ where
cx: &mut Context,
actions: &mut Vec<Box<dyn Any>>,
) {
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)));
Expand Down
4 changes: 3 additions & 1 deletion src/views/knob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ pub fn knob(value: impl Binding<f32>) -> 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;
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 26 additions & 8 deletions src/winit_event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}
_ => (),
}
Expand Down

0 comments on commit 673b4a6

Please sign in to comment.