Skip to content

Instantly share code, notes, and snippets.

@sgrif
Created September 24, 2014 22:28
Show Gist options
  • Select an option

  • Save sgrif/6fbf1a63ef47f34e39a6 to your computer and use it in GitHub Desktop.

Select an option

Save sgrif/6fbf1a63ef47f34e39a6 to your computer and use it in GitHub Desktop.
val swipe = List(
DownEvent(Seq(Vector2(0, 0))),
MoveEvent(Seq(Vector2(10, 5))),
MoveEvent(Seq(Vector2(15, 10))),
MoveEvent(Seq(Vector2(0, 0))),
UpEvent(Seq(Vector2(10, 5)))
)
val events = PublishSubject[MotionEvent]
def handleSwipe(rotation: => SphericCoordinates)(swipe: Observable[MotionEvent], initialTouch: Vector2) = {
val initialState = (rotation, initialTouch)
swipe.collect{case MoveEvent(Seq(touch)) => touch}.scan(initialState) { (state, touch) =>
val (rotation, lastTouch) = state
val touchDelta = touch - lastTouch
val rotationDelta = SphericCoordinates(0, touchDelta.y, touchDelta.x)
val nextRotation = rotation + rotationDelta / 100
(nextRotation, touch)
}.map(_._1)
}
def isEndOfSwipe(e: MotionEvent) = e match {
case MoveEvent(Seq(singleFinger)) => false
case _ => true
}
val beginningOfSwipes = events.collect { case DownEvent(Seq(singleFinger)) => singleFinger }
val endOfSwipes = events.filter(isEndOfSwipe)
val swipes = events.sliding(beginningOfSwipes)(const(endOfSwipes))
var rotation = SphericCoordinates(0, 0, 0)
val result = swipes.zip(beginningOfSwipes).map((handleSwipe(rotation) _).tupled)
result.map(_.tail).switch.foreach(println)
swipe foreach events.onNext
rotation = SphericCoordinates(10, 10, 10)
swipe foreach events.onNext
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment