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

Fire 'rotate' and 'pitch' events only when their value changed #8872

Merged
merged 1 commit into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions src/ui/handler/drag_rotate.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,19 @@ class DragRotateHandler {
this._drainInertiaBuffer();
inertia.push([browser.now(), this._map._normalizeBearing(bearing, last[1])]);

const prevBearing = tr.bearing;
tr.bearing = bearing;
if (this._pitchWithRotate) {
this._fireEvent('pitch', e);
const prevPitch = tr.pitch;
tr.pitch = pitch;
if (tr.pitch !== prevPitch) {
this._fireEvent('pitch', e);
}
}

this._fireEvent('rotate', e);
if (tr.bearing !== prevBearing) {
this._fireEvent('rotate', e);
}
this._fireEvent('move', e);

delete this._lastMoveEvent;
Expand Down
64 changes: 61 additions & 3 deletions test/unit/ui/handler/drag_rotate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ test('DragRotateHandler pitches in response to a right-click drag by default', (
map.on('pitchend', pitchend);

simulate.mousedown(map.getCanvas(), {buttons: 2, button: 2});
simulate.mousemove(map.getCanvas(), {buttons: 2, clientX: 10, clientY: 10});
simulate.mousemove(map.getCanvas(), {buttons: 2, clientX: 10, clientY: -10});
map._renderTaskQueue.run();
t.equal(pitchstart.callCount, 1);
t.equal(pitch.callCount, 1);
Expand All @@ -136,6 +136,33 @@ test('DragRotateHandler pitches in response to a right-click drag by default', (
t.end();
});

test('DragRotateHandler doesn\'t fire pitch event when rotating only', (t) => {
const map = createMap(t);

// Prevent inertial rotation.
t.stub(browser, 'now').returns(0);

const pitchstart = t.spy();
const pitch = t.spy();
const pitchend = t.spy();

map.on('pitchstart', pitchstart);
map.on('pitch', pitch);
map.on('pitchend', pitchend);

simulate.mousedown(map.getCanvas(), {buttons: 2, button: 2});
simulate.mousemove(map.getCanvas(), {buttons: 2, clientX: 10, clientY: 10});
map._renderTaskQueue.run();
t.equal(pitchstart.callCount, 1);
t.equal(pitch.callCount, 0);

simulate.mouseup(map.getCanvas(), {buttons: 0, button: 2});
t.equal(pitchend.callCount, 1);

map.remove();
t.end();
});

test('DragRotateHandler pitches in response to a control-left-click drag', (t) => {
const map = createMap(t);

Expand All @@ -151,7 +178,7 @@ test('DragRotateHandler pitches in response to a control-left-click drag', (t) =
map.on('pitchend', pitchend);

simulate.mousedown(map.getCanvas(), {buttons: 1, button: 0, ctrlKey: true});
simulate.mousemove(map.getCanvas(), {buttons: 1, ctrlKey: true, clientX: 10, clientY: 10});
simulate.mousemove(map.getCanvas(), {buttons: 1, ctrlKey: true, clientX: 10, clientY: -10});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving downwards only rotates, and doesn't pitch. Change the value to move upwards so that the map gets pitched.

map._renderTaskQueue.run();
t.equal(pitchstart.callCount, 1);
t.equal(pitch.callCount, 1);
Expand Down Expand Up @@ -256,6 +283,37 @@ test('DragRotateHandler fires move events', (t) => {
t.end();
});

test('DragRotateHandler doesn\'t fire rotate event when pitching only', (t) => {
// The bearingSnap option here ensures that the moveend event is sent synchronously.
const map = createMap(t, {bearingSnap: 0});

// Prevent inertial rotation.
t.stub(browser, 'now').returns(0);

const rotatestart = t.spy();
const rotate = t.spy();
const pitch = t.spy();
const rotateend = t.spy();

map.on('movestart', rotatestart);
map.on('rotate', rotate);
map.on('pitch', pitch);
map.on('rotateend', rotateend);

simulate.mousedown(map.getCanvas(), {buttons: 2, button: 2});
simulate.mousemove(map.getCanvas(), {buttons: 2, clientX: 0, clientY: -10});
map._renderTaskQueue.run();
t.equal(rotatestart.callCount, 1);
t.equal(rotate.callCount, 0);
t.equal(pitch.callCount, 1);

simulate.mouseup(map.getCanvas(), {buttons: 0, button: 2});
t.equal(rotateend.callCount, 1);

map.remove();
t.end();
});

test('DragRotateHandler includes originalEvent property in triggered events', (t) => {
// The bearingSnap option here ensures that the moveend event is sent synchronously.
const map = createMap(t, {bearingSnap: 0});
Expand Down Expand Up @@ -285,7 +343,7 @@ test('DragRotateHandler includes originalEvent property in triggered events', (t
map.on('moveend', moveend);

simulate.mousedown(map.getCanvas(), {buttons: 2, button: 2});
simulate.mousemove(map.getCanvas(), {buttons: 2, clientX: 10, clientY: 10});
simulate.mousemove(map.getCanvas(), {buttons: 2, clientX: 10, clientY: -10});
map._renderTaskQueue.run();
simulate.mouseup(map.getCanvas(), {buttons: 0, button: 2});

Expand Down