Content-Length: 12536 | pFad | http://github.com/pguedes/gesticle/pull/8.diff

67C2B35B diff --git a/.gitignore b/.gitignore index 2ab662d..5d4feb3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ gesticle.iml /target/ **/*.rs.bk +.history/ diff --git a/project.code-workspace b/project.code-workspace new file mode 100644 index 0000000..876a149 --- /dev/null +++ b/project.code-workspace @@ -0,0 +1,8 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": {} +} \ No newline at end of file diff --git a/src/gestures.rs b/src/gestures.rs index 797bf22..eee3b4d 100644 --- a/src/gestures.rs +++ b/src/gestures.rs @@ -1,8 +1,6 @@ use std::f64::consts::PI; use std::fmt; -use input::Event::Gesture; -use input::event::gesture::{GesturePinchEndEvent, GesturePinchEvent, GesturePinchUpdateEvent}; use input::event::gesture::GestureEndEvent; use input::event::gesture::GestureEventCoordinates; use input::event::gesture::GestureEventTrait; @@ -12,23 +10,30 @@ use input::event::gesture::GestureSwipeEvent::Begin; use input::event::gesture::GestureSwipeEvent::End; use input::event::gesture::GestureSwipeEvent::Update; use input::event::gesture::GestureSwipeUpdateEvent; +use input::event::gesture::{GesturePinchEndEvent, GesturePinchEvent, GesturePinchUpdateEvent}; use input::event::GestureEvent::*; +use input::Event::Gesture; use std::fmt::Formatter; +#[derive(Copy, Clone)] struct SwipeGesture { dx: f64, dy: f64, fingers: i32, - cancelled: bool + cancelled: bool, } impl SwipeGesture { - - fn new (fingers: i32) -> SwipeGesture { - SwipeGesture { dx: 0.0, dy: 0.0, cancelled: false, fingers } + fn new(fingers: i32) -> SwipeGesture { + SwipeGesture { + dx: 0.0, + dy: 0.0, + cancelled: false, + fingers, + } } - fn add (&mut self, dx: f64, dy: f64) { + fn add(&mut self, dx: f64, dy: f64) { self.dx += dx; self.dy += dy; } @@ -38,13 +43,21 @@ impl SwipeGesture { } fn direction(&self) -> Option { - let theta = (self.dy/self.dx).atan(); + let theta = (self.dy / self.dx).atan(); let t: f64 = 180.into(); - let angle = (theta * (t/PI)).abs(); + let angle = (theta * (t / PI)).abs(); if 75.0 < angle && angle < 105.0 { - return if self.dy > 0.0 {Some(SwipeDirection::Down)} else {Some(SwipeDirection::Up)} + return if self.dy > 0.0 { + Some(SwipeDirection::Down) + } else { + Some(SwipeDirection::Up) + }; } else if 0.0 < angle && angle < 15.0 { - return if self.dx > 0.0 {Some(SwipeDirection::Right)} else {Some(SwipeDirection::Left)} + return if self.dx > 0.0 { + Some(SwipeDirection::Right) + } else { + Some(SwipeDirection::Left) + }; } warn!("unknown direction: {:?} direction = {:?}", self, angle); return None; @@ -52,27 +65,36 @@ impl SwipeGesture { } impl fmt::Debug for SwipeGesture { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "({}, {}) fingers = {} cancelled? {}", self.dx, self.dy, self.fingers, self.cancelled) + write!( + f, + "({}, {}) fingers = {} cancelled? {}", + self.dx, self.dy, self.fingers, self.cancelled + ) } } +#[derive(Copy, Clone)] struct PinchGesture { scale: f64, dx: f64, dy: f64, angle: f64, - cancelled: bool + cancelled: bool, } impl PinchGesture { - - fn new (scale: f64) -> PinchGesture { - PinchGesture { scale, dx: 0.0, dy: 0.0, angle: 0.0, cancelled: false } + fn new(scale: f64) -> PinchGesture { + PinchGesture { + scale, + dx: 0.0, + dy: 0.0, + angle: 0.0, + cancelled: false, + } } - fn add (&mut self, dx: f64, dy: f64, angle: f64) { + fn add(&mut self, dx: f64, dy: f64, angle: f64) { self.dx += dx; self.dy += dy; self.angle += angle; @@ -85,14 +107,15 @@ impl PinchGesture { fn cancel(&mut self) { self.cancelled = true; } - } impl fmt::Debug for PinchGesture { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "scale = {}, ({}, {}) angle = {} cancelled? {}", - self.scale, self.dx, self.dy, self.angle, self.cancelled) + write!( + f, + "scale = {}, ({}, {}) angle = {} cancelled? {}", + self.scale, self.dx, self.dy, self.angle, self.cancelled + ) } } @@ -100,7 +123,7 @@ impl fmt::Debug for PinchGesture { pub enum GestureType { Swipe(SwipeDirection, i32), Rotation(RotationDirection, f64), - Pinch(PinchDirection, f64) + Pinch(PinchDirection, f64), } impl GestureType { @@ -108,17 +131,28 @@ impl GestureType { match self { GestureType::Swipe(direction, fingers) => format!("swipe.{}.{}", direction, fingers), GestureType::Rotation(direction, _) => format!("rotation.{}", direction), - GestureType::Pinch(direction, _) => format!("pinch.{}", direction) + GestureType::Pinch(direction, _) => format!("pinch.{}", direction), } } } #[derive(Debug)] -pub enum SwipeDirection { Up, Down, Left, Right } +pub enum SwipeDirection { + Up, + Down, + Left, + Right, +} #[derive(Debug)] -pub enum RotationDirection { Left, Right } +pub enum RotationDirection { + Left, + Right, +} #[derive(Debug)] -pub enum PinchDirection { In, Out } +pub enum PinchDirection { + In, + Out, +} impl fmt::Display for SwipeDirection { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { @@ -137,27 +171,24 @@ impl fmt::Display for PinchDirection { } trait Identifiable { - fn gesture_type(&self) -> Option; } impl Identifiable for SwipeGesture { fn gesture_type(&self) -> Option { if self.cancelled { - return None + return None; } return match self.direction() { Some(d) => Some(GestureType::Swipe(d, self.fingers)), - None => None + None => None, }; } } impl Identifiable for PinchGesture { - fn gesture_type(&self) -> Option { - if !self.cancelled { if self.angle > 50.0 { return Some(GestureType::Rotation(RotationDirection::Right, self.angle)); @@ -172,72 +203,69 @@ impl Identifiable for PinchGesture { } } - return None + return None; } } struct SwipeBuilder { - swipe: Option + swipe: Option, } impl SwipeBuilder { - fn empty() -> SwipeBuilder { SwipeBuilder { swipe: None } } fn new(&mut self, fingers: i32) { - self.swipe = Some( SwipeGesture::new(fingers) ); + self.swipe = Some(SwipeGesture::new(fingers)); } fn update(&mut self, event: GestureSwipeUpdateEvent) { - match self.swipe { Some(ref mut g) => g.add(event.dx(), event.dy()), - None => () + None => (), } } fn build(self, event: GestureSwipeEndEvent) -> Result { - match self.swipe { Some(mut g) => { if event.cancelled() { g.cancel(); } Ok(g) - }, - None => Err("failed to produce event".to_owned()) + } + None => Err("failed to produce event".to_owned()), } } } struct PinchBuilder { - pinch: Option + pinch: Option, } impl PinchBuilder { - fn empty() -> PinchBuilder { - PinchBuilder { - pinch: None - } + PinchBuilder { pinch: None } } fn new(&mut self, scale: f64) { - self.pinch = Some( PinchGesture::new(scale) ); + self.pinch = Some(PinchGesture::new(scale)); } - fn update(&mut self, event: GesturePinchUpdateEvent) { - + fn update(&mut self, event: GesturePinchUpdateEvent) -> Result { match self.pinch { - Some(ref mut g) => g.add(event.dx(), event.dy(), event.angle_delta()), - None => () + Some(ref mut g) => { + g.add(event.dx(), event.dy(), event.angle_delta()); + g.scale(event.scale()); + + Ok(*g) + } + None => Err("failed to produce event".to_owned()), } } fn build(self, event: GesturePinchEndEvent) -> Result { - match self.pinch { Some(mut g) => { g.scale(event.scale()); @@ -245,69 +273,67 @@ impl PinchBuilder { g.cancel(); } Ok(g) - }, - None => Err("failed to produce event".to_owned()) + } + None => Err("failed to produce event".to_owned()), } } } - pub struct Listener<'a> { swipe: SwipeBuilder, pinch: PinchBuilder, - gesture_action: &'a dyn Fn(GestureType) + gesture_action: &'a dyn Fn(GestureType), } impl<'a> Listener<'a> { - pub fn new(gesture_action: &dyn Fn(GestureType)) -> Listener { Listener { swipe: SwipeBuilder::empty(), pinch: PinchBuilder::empty(), - gesture_action + gesture_action, } } pub fn event(mut self, event: input::Event) -> Self { - match event { - Gesture(Swipe(Begin(event))) => - self.swipe.new(event.finger_count()), - Gesture(Swipe(Update(event))) => - self.swipe.update(event), + Gesture(Swipe(Begin(event))) => self.swipe.new(event.finger_count()), + Gesture(Swipe(Update(event))) => { + self.swipe.update(event); + } Gesture(Swipe(End(event))) => { match self.swipe.build(event) { - Ok(g) => { - match g.gesture_type() { - Some(t) => (self.gesture_action)(t), - None => warn!("cancelled or unrecognized gesture {:?}", g) - } + Ok(g) => match g.gesture_type() { + Some(t) => (self.gesture_action)(t), + None => warn!("cancelled or unrecognized gesture {:?}", g), }, - Err(s) => error!("no Gesture {:?}", s) + Err(s) => error!("no Gesture {:?}", s), } self.swipe = SwipeBuilder::empty(); + } + + Gesture(Pinch(GesturePinchEvent::Begin(event))) => self.pinch.new(event.scale()), + Gesture(Pinch(GesturePinchEvent::Update(event))) => match self.pinch.update(event) { + Ok(p) => match p.gesture_type() { + Some(t) => (self.gesture_action)(t), + None => warn!("cancelled or unrecognized gesture {:?}", p), + }, + Err(s) => error!("no Gesture {:?}", s), }, - Gesture(Pinch(GesturePinchEvent::Begin(event))) => - self.pinch.new(event.scale()), - Gesture(Pinch(GesturePinchEvent::Update(event))) => - self.pinch.update(event), Gesture(Pinch(GesturePinchEvent::End(event))) => { let gesture = self.pinch.build(event); match gesture { - Ok(p) => { - match p.gesture_type() { - Some(t) => (self.gesture_action)(t), - None => warn!("cancelled or unrecognized gesture {:?}", p) - } + Ok(p) => match p.gesture_type() { + Some(t) => (self.gesture_action)(t), + None => warn!("cancelled or unrecognized gesture {:?}", p), }, - Err(s) => error!("no Gesture {:?}", s) + Err(s) => error!("no Gesture {:?}", s), } self.pinch = PinchBuilder::empty(); - }, + } - _ => () + _ => (), } self } -} \ No newline at end of file +}








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/pguedes/gesticle/pull/8.diff

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy