Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize SwingMouseEventSource.fromRelativeMouseMotion #863

Merged
merged 1 commit into from
Feb 12, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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