Skip to content

Commit

Permalink
Merge pull request #863 from Acardiac/swing-rel-mouse-motion
Browse files Browse the repository at this point in the history
Optimize SwingMouseEventSource.fromRelativeMouseMotion
  • Loading branch information
benjchristensen committed Feb 12, 2014
2 parents b0d975c + d764c1b commit b8551f3
Showing 1 changed file with 5 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,36 +120,13 @@ public void call() {
* @see rx.observables.SwingObservable#fromRelativeMouseMotion
*/
public static Observable<Point> fromRelativeMouseMotion(final Component component) {
class OldAndRelative {
public final Point old;
public final Point relative;

private OldAndRelative(Point old, Point relative) {
this.old = old;
this.relative = relative;
}
}

class Relativize implements Func2<OldAndRelative, MouseEvent, OldAndRelative> {
final Observable<MouseEvent> events = fromMouseMotionEventsOf(component);
return Observable.zip(events, events.skip(1), new Func2<MouseEvent, MouseEvent, Point>() {
@Override
public OldAndRelative call(OldAndRelative last, MouseEvent event) {
Point current = new Point(event.getX(), event.getY());
Point relative = new Point(current.x - last.old.x, current.y - last.old.y);
return new OldAndRelative(current, relative);
public Point call(MouseEvent ev1, MouseEvent ev2) {
return new Point(ev2.getX() - ev1.getX(), ev2.getY() - ev1.getY());
}
}

class OnlyRelative implements Func1<OldAndRelative, Point> {
@Override
public Point call(OldAndRelative oar) {
return oar.relative;
}
}

return fromMouseMotionEventsOf(component)
.scan(new OldAndRelative(new Point(0, 0), new Point(0, 0)), new Relativize())
.map(new OnlyRelative())
.skip(2); // skip the useless initial value and the invalid first computation
});
}

public static class UnitTest {
Expand Down

0 comments on commit b8551f3

Please sign in to comment.