Use the animating hint

This commit is contained in:
Tim Schaub
2016-11-06 08:55:19 -07:00
parent f6ad883db6
commit da8eb97f54
2 changed files with 74 additions and 16 deletions

View File

@@ -85,12 +85,6 @@ ol.View = function(opt_options) {
*/ */
this.hints_ = [0, 0]; this.hints_ = [0, 0];
/**
* @private
* @type {boolean}
*/
this.animating_ = false;
/** /**
* @private * @private
* @type {Array.<Array.<ol.ViewAnimation>>} * @type {Array.<Array.<ol.ViewAnimation>>}
@@ -236,7 +230,7 @@ ol.View.prototype.animate = function(var_args) {
series.push(animation); series.push(animation);
} }
this.animations_.push(series); this.animations_.push(series);
this.animating_ = true; this.setHint(ol.View.Hint.ANIMATING, 1);
this.updateAnimations_(); this.updateAnimations_();
}; };
@@ -246,7 +240,7 @@ ol.View.prototype.animate = function(var_args) {
* @return {boolean} The view is being animated. * @return {boolean} The view is being animated.
*/ */
ol.View.prototype.getAnimating = function() { ol.View.prototype.getAnimating = function() {
return this.animating_; return this.getHints()[ol.View.Hint.ANIMATING] > 0;
}; };
@@ -261,7 +255,7 @@ ol.View.prototype.cancelAnimations_ = function() {
} }
} }
this.animations_.length = 0; this.animations_.length = 0;
this.animating_ = false; this.setHint(ol.View.Hint.ANIMATING, -this.getHints()[ol.View.Hint.ANIMATING]);
}; };
/** /**
@@ -272,7 +266,7 @@ ol.View.prototype.updateAnimations_ = function() {
cancelAnimationFrame(this.updateAnimationKey_); cancelAnimationFrame(this.updateAnimationKey_);
this.updateAnimationKey_ = undefined; this.updateAnimationKey_ = undefined;
} }
if (!this.animating_) { if (!this.getAnimating()) {
return; return;
} }
var now = Date.now(); var now = Date.now();
@@ -324,10 +318,8 @@ ol.View.prototype.updateAnimations_ = function() {
more = true; more = true;
} }
if (seriesComplete) { if (seriesComplete) {
this.setHint(ol.View.Hint.ANIMATING, -1);
var completed = this.animations_.pop(); var completed = this.animations_.pop();
if (this.animations_.length === 0) {
this.animating_ = false;
}
var callback = completed[0].callback; var callback = completed[0].callback;
if (callback) { if (callback) {
callback(true); callback(true);
@@ -787,7 +779,7 @@ ol.View.prototype.rotate = function(rotation, opt_anchor) {
*/ */
ol.View.prototype.setCenter = function(center) { ol.View.prototype.setCenter = function(center) {
this.set(ol.View.Property.CENTER, center); this.set(ol.View.Property.CENTER, center);
if (this.animating_) { if (this.getAnimating()) {
this.cancelAnimations_(); this.cancelAnimations_();
} }
}; };
@@ -816,7 +808,7 @@ ol.View.prototype.setHint = function(hint, delta) {
*/ */
ol.View.prototype.setResolution = function(resolution) { ol.View.prototype.setResolution = function(resolution) {
this.set(ol.View.Property.RESOLUTION, resolution); this.set(ol.View.Property.RESOLUTION, resolution);
if (this.animating_) { if (this.getAnimating()) {
this.cancelAnimations_(); this.cancelAnimations_();
} }
}; };
@@ -830,7 +822,7 @@ ol.View.prototype.setResolution = function(resolution) {
*/ */
ol.View.prototype.setRotation = function(rotation) { ol.View.prototype.setRotation = function(rotation) {
this.set(ol.View.Property.ROTATION, rotation); this.set(ol.View.Property.ROTATION, rotation);
if (this.animating_) { if (this.getAnimating()) {
this.cancelAnimations_(); this.cancelAnimations_();
} }
}; };

View File

@@ -372,6 +372,72 @@ describe('ol.View', function() {
view.setCenter([1, 2]); // interrupt the animation view.setCenter([1, 2]); // interrupt the animation
}); });
it('properly sets the ANIMATING hint', function(done) {
var view = new ol.View({
center: [0, 0],
zoom: 0,
rotation: 0
});
var count = 3;
function decrement() {
--count;
if (count === 0) {
expect(view.getHints()[ol.View.Hint.ANIMATING]).to.be(0);
done();
}
}
view.animate({
center: [1, 2],
duration: 25
}, decrement);
expect(view.getHints()[ol.View.Hint.ANIMATING]).to.be(1);
view.animate({
zoom: 1,
duration: 25
}, decrement);
expect(view.getHints()[ol.View.Hint.ANIMATING]).to.be(2);
view.animate({
rotate: Math.PI,
duration: 25
}, decrement);
expect(view.getHints()[ol.View.Hint.ANIMATING]).to.be(3);
});
it('clears the ANIMATING hint when animations are cancelled', function() {
var view = new ol.View({
center: [0, 0],
zoom: 0,
rotation: 0
});
view.animate({
center: [1, 2],
duration: 25
});
expect(view.getHints()[ol.View.Hint.ANIMATING]).to.be(1);
view.animate({
zoom: 1,
duration: 25
});
expect(view.getHints()[ol.View.Hint.ANIMATING]).to.be(2);
view.animate({
rotate: Math.PI,
duration: 25
});
expect(view.getHints()[ol.View.Hint.ANIMATING]).to.be(3);
// cancel animations
view.setCenter([10, 20]);
expect(view.getHints()[ol.View.Hint.ANIMATING]).to.be(0);
});
}); });
describe('#getResolutions', function() { describe('#getResolutions', function() {