From da8eb97f546f3fa19c77836610ee10c8e1744ad1 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sun, 6 Nov 2016 08:55:19 -0700 Subject: [PATCH] Use the animating hint --- src/ol/view.js | 24 +++++--------- test/spec/ol/view.test.js | 66 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 16 deletions(-) diff --git a/src/ol/view.js b/src/ol/view.js index 1e71a1b597..483b00c680 100644 --- a/src/ol/view.js +++ b/src/ol/view.js @@ -85,12 +85,6 @@ ol.View = function(opt_options) { */ this.hints_ = [0, 0]; - /** - * @private - * @type {boolean} - */ - this.animating_ = false; - /** * @private * @type {Array.>} @@ -236,7 +230,7 @@ ol.View.prototype.animate = function(var_args) { series.push(animation); } this.animations_.push(series); - this.animating_ = true; + this.setHint(ol.View.Hint.ANIMATING, 1); this.updateAnimations_(); }; @@ -246,7 +240,7 @@ ol.View.prototype.animate = function(var_args) { * @return {boolean} The view is being animated. */ 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.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_); this.updateAnimationKey_ = undefined; } - if (!this.animating_) { + if (!this.getAnimating()) { return; } var now = Date.now(); @@ -324,10 +318,8 @@ ol.View.prototype.updateAnimations_ = function() { more = true; } if (seriesComplete) { + this.setHint(ol.View.Hint.ANIMATING, -1); var completed = this.animations_.pop(); - if (this.animations_.length === 0) { - this.animating_ = false; - } var callback = completed[0].callback; if (callback) { callback(true); @@ -787,7 +779,7 @@ ol.View.prototype.rotate = function(rotation, opt_anchor) { */ ol.View.prototype.setCenter = function(center) { this.set(ol.View.Property.CENTER, center); - if (this.animating_) { + if (this.getAnimating()) { this.cancelAnimations_(); } }; @@ -816,7 +808,7 @@ ol.View.prototype.setHint = function(hint, delta) { */ ol.View.prototype.setResolution = function(resolution) { this.set(ol.View.Property.RESOLUTION, resolution); - if (this.animating_) { + if (this.getAnimating()) { this.cancelAnimations_(); } }; @@ -830,7 +822,7 @@ ol.View.prototype.setResolution = function(resolution) { */ ol.View.prototype.setRotation = function(rotation) { this.set(ol.View.Property.ROTATION, rotation); - if (this.animating_) { + if (this.getAnimating()) { this.cancelAnimations_(); } }; diff --git a/test/spec/ol/view.test.js b/test/spec/ol/view.test.js index 4421b5cab9..04517fc93b 100644 --- a/test/spec/ol/view.test.js +++ b/test/spec/ol/view.test.js @@ -372,6 +372,72 @@ describe('ol.View', function() { 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() {