Skip to content

Commit

Permalink
Add isRotating(), _-prefix private properties
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Feb 16, 2018
1 parent 0276d45 commit 643f609
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 48 deletions.
62 changes: 31 additions & 31 deletions src/ui/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ type AnimationOptions = {

class Camera extends Evented {
transform: Transform;
moving: boolean;
zooming: boolean;
rotating: boolean;
pitching: boolean;
_moving: boolean;
_zooming: boolean;
_rotating: boolean;
_pitching: boolean;

_bearingSnap: number;
_onEaseEnd: number;
Expand All @@ -82,8 +82,8 @@ class Camera extends Evented {

constructor(transform: Transform, options: {bearingSnap: number}) {
super();
this.moving = false;
this.zooming = false;
this._moving = false;
this._zooming = false;
this.transform = transform;
this._bearingSnap = options.bearingSnap;
}
Expand Down Expand Up @@ -562,22 +562,22 @@ class Camera extends Evented {
aroundPoint = tr.locationPoint(around);
}

this.zooming = (zoom !== startZoom);
this.rotating = (startBearing !== bearing);
this.pitching = (pitch !== startPitch);
this._zooming = (zoom !== startZoom);
this._rotating = (startBearing !== bearing);
this._pitching = (pitch !== startPitch);

this._prepareEase(eventData, options.noMoveStart);

clearTimeout(this._onEaseEnd);

this._ease((k) => {
if (this.zooming) {
if (this._zooming) {
tr.zoom = interpolate(startZoom, zoom, k);
}
if (this.rotating) {
if (this._rotating) {
tr.bearing = interpolate(startBearing, bearing, k);
}
if (this.pitching) {
if (this._pitching) {
tr.pitch = interpolate(startPitch, pitch, k);
}

Expand Down Expand Up @@ -607,43 +607,43 @@ class Camera extends Evented {
}

_prepareEase(eventData?: Object, noMoveStart: boolean) {
this.moving = true;
this._moving = true;

if (!noMoveStart) {
this.fire('movestart', eventData);
}
if (this.zooming) {
if (this._zooming) {
this.fire('zoomstart', eventData);
}
if (this.rotating) {
if (this._rotating) {
this.fire('rotatestart', eventData);
}
if (this.pitching) {
if (this._pitching) {
this.fire('pitchstart', eventData);
}
}

_fireMoveEvents(eventData?: Object) {
this.fire('move', eventData);
if (this.zooming) {
if (this._zooming) {
this.fire('zoom', eventData);
}
if (this.rotating) {
if (this._rotating) {
this.fire('rotate', eventData);
}
if (this.pitching) {
if (this._pitching) {
this.fire('pitch', eventData);
}
}

_afterEase(eventData?: Object) {
const wasZooming = this.zooming;
const wasRotating = this.rotating;
const wasPitching = this.pitching;
this.moving = false;
this.zooming = false;
this.rotating = false;
this.pitching = false;
const wasZooming = this._zooming;
const wasRotating = this._rotating;
const wasPitching = this._pitching;
this._moving = false;
this._zooming = false;
this._rotating = false;
this._pitching = false;

if (wasZooming) {
this.fire('zoomend', eventData);
Expand Down Expand Up @@ -825,9 +825,9 @@ class Camera extends Evented {
options.duration = 0;
}

this.zooming = true;
this.rotating = (startBearing !== bearing);
this.pitching = (pitch !== startPitch);
this._zooming = true;
this._rotating = (startBearing !== bearing);
this._pitching = (pitch !== startPitch);

this._prepareEase(eventData, false);

Expand All @@ -837,10 +837,10 @@ class Camera extends Evented {
const scale = 1 / w(s);
tr.zoom = startZoom + tr.scaleZoom(scale);

if (this.rotating) {
if (this._rotating) {
tr.bearing = interpolate(startBearing, bearing, k);
}
if (this.pitching) {
if (this._pitching) {
tr.pitch = interpolate(startPitch, pitch, k);
}

Expand Down
16 changes: 12 additions & 4 deletions src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ class Map extends Camera {
* Returns true if the map is panning, zooming, rotating, or pitching due to a camera animation or user gesture.
*/
isMoving(): boolean {
return this.moving ||
return this._moving ||
this.dragPan.isActive() ||
this.dragRotate.isActive() ||
this.scrollZoom.isActive();
Expand All @@ -594,10 +594,18 @@ class Map extends Camera {
* Returns true if the map is zooming due to a camera animation or user gesture.
*/
isZooming(): boolean {
return this.zooming ||
return this._zooming ||
this.scrollZoom.isActive();
}

/**
* Returns true if the map is rotating due to a camera animation or user gesture.
*/
isRotating(): boolean {
return this._rotating ||
this.dragRotate.isActive();
}

/**
* Adds a listener for events of a specified type.
*
Expand Down Expand Up @@ -1544,8 +1552,8 @@ class Map extends Camera {
this.painter.render(this.style, {
showTileBoundaries: this.showTileBoundaries,
showOverdrawInspector: this._showOverdrawInspector,
rotating: this.rotating,
zooming: this.zooming,
rotating: this.isRotating(),
zooming: this.isZooming(),
fadeDuration: this._fadeDuration
});

Expand Down
26 changes: 13 additions & 13 deletions test/unit/ui/camera.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,9 @@ test('camera', (t) => {
.on('movestart', (d) => { movestarted = d.data; })
.on('move', (d) => { moved = d.data; })
.on('moveend', (d) => {
t.notOk(camera.zooming);
t.notOk(camera.panning);
t.notOk(camera.rotating);
t.notOk(camera._zooming);
t.notOk(camera._panning);
t.notOk(camera._rotating);

t.equal(movestarted, 'ok');
t.equal(moved, 'ok');
Expand Down Expand Up @@ -997,9 +997,9 @@ test('camera', (t) => {
.on('rotate', (d) => { rotated = d.data; })
.on('pitch', (d) => { pitched = d.data; })
.on('moveend', function(d) {
t.notOk(this.zooming);
t.notOk(this.panning);
t.notOk(this.rotating);
t.notOk(this._zooming);
t.notOk(this._panning);
t.notOk(this._rotating);

t.equal(movestarted, 'ok');
t.equal(moved, 'ok');
Expand Down Expand Up @@ -1064,9 +1064,9 @@ test('camera', (t) => {
.on('pitch', (d) => { pitched = d.data; })
.on('pitchend', (d) => { pitchended = d.data; })
.on('moveend', function(d) {
t.notOk(this.zooming);
t.notOk(this.panning);
t.notOk(this.rotating);
t.notOk(this._zooming);
t.notOk(this._panning);
t.notOk(this._rotating);

t.equal(movestarted, 'ok');
t.equal(moved, 'ok');
Expand Down Expand Up @@ -1579,19 +1579,19 @@ test('camera', (t) => {
});

t.test('#stop', (t) => {
t.test('resets camera.zooming', (t) => {
t.test('resets camera._zooming', (t) => {
const camera = createCamera();
camera.zoomTo(3.2);
camera.stop();
t.ok(!camera.zooming);
t.ok(!camera._zooming);
t.end();
});

t.test('resets camera.rotating', (t) => {
t.test('resets camera._rotating', (t) => {
const camera = createCamera();
camera.rotateTo(90);
camera.stop();
t.ok(!camera.rotating);
t.ok(!camera._rotating);
t.end();
});

Expand Down
57 changes: 57 additions & 0 deletions test/unit/ui/map/isRotating.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

const test = require('mapbox-gl-js-test').test;
const window = require('../../../../src/util/window');
const Map = require('../../../../src/ui/map');
const DOM = require('../../../../src/util/dom');
const simulate = require('mapbox-gl-js-test/simulate_interaction');

function createMap() {
return new Map({ container: DOM.create('div', '', window.document.body) });
}

test('Map#isRotating returns false by default', (t) => {
const map = createMap();
t.equal(map.isRotating(), false);
map.remove();
t.end();
});

test('Map#isRotating returns true during a camera rotate animation', (t) => {
const map = createMap();

map.on('rotatestart', () => {
t.equal(map.isRotating(), true);
});

map.on('rotateend', () => {
t.equal(map.isRotating(), false);
map.remove();
t.end();
});

map.rotateTo(5, { duration: 0 });
});

test('Map#isRotating returns true when drag rotating', (t) => {
const map = createMap();

map.on('rotatestart', () => {
t.equal(map.isRotating(), true);
});

map.on('rotateend', () => {
t.equal(map.isRotating(), false);
map.remove();
t.end();
});

simulate.mousedown(map.getCanvas(), {buttons: 2, button: 2});
map._updateCamera();

simulate.mousemove(map.getCanvas(), {buttons: 2});
map._updateCamera();

simulate.mouseup(map.getCanvas(), {buttons: 0, button: 2});
map._updateCamera();
});

0 comments on commit 643f609

Please sign in to comment.