1 goog.provide('lime.animation.MoveTo'); 2 3 4 goog.require('goog.math.Coordinate'); 5 goog.require('lime.Sprite'); 6 goog.require('lime.animation.Animation'); 7 8 /** 9 * Move element to specific position 10 * Also accepts two numbers (x and y) 11 * @constructor 12 * @param {(goog.math.Coordinate|number)} position New position value. 13 * @param {number=} opt_y Optionaly use x,y 14 * @extends lime.animation.Animation 15 */ 16 lime.animation.MoveTo = function(position, opt_y) { 17 lime.animation.Animation.call(this); 18 19 if (arguments.length == 2) { 20 this.position_ = new goog.math.Coordinate(arguments[0], arguments[1]); 21 } 22 else this.position_ = position; 23 24 }; 25 goog.inherits(lime.animation.MoveTo, lime.animation.Animation); 26 27 /** 28 * @inheritDoc 29 */ 30 lime.animation.MoveTo.prototype.scope = 'move'; 31 32 /** 33 * Helper function that sets tha animation duration 34 * based on the size of the delta. 1 unit means 100px/sec. 35 * Calculation is based on first target node. 36 * @param {number} speed Speed value. 37 * @return {lime.animation.MoveTo} Object itself. 38 */ 39 lime.animation.MoveTo.prototype.setSpeed = function(speed) { 40 this.speed_ = speed; 41 delete this.speedCalcDone_; 42 return this; 43 }; 44 45 /** 46 * @inheritDoc 47 * @see lime.animation.Animation#makeTargetProp 48 */ 49 lime.animation.MoveTo.prototype.makeTargetProp = function(target) { 50 var start = target.getPosition(); 51 var delta = new goog.math.Coordinate( 52 this.position_.x - start.x, 53 this.position_.y - start.y); 54 55 if (this.useTransitions()) { 56 target.addTransition(lime.Transition.POSITION, 57 this.position_, 58 this.duration_, this.getEasing()); 59 target.setDirty(lime.Dirty.POSITION); 60 } 61 62 return {startpos: start, delta: delta}; 63 }; 64 65 66 /** 67 * Calculate animations duration based on its speed. 68 * @private 69 */ 70 lime.animation.MoveTo.prototype.calcDurationFromSpeed_ = function(){ 71 if(!this.speed_ || !this.targets.length) return; 72 73 var start = this.targets[0].getPosition(); 74 var delta = new goog.math.Coordinate( 75 this.position_.x - start.x, 76 this.position_.y - start.y); 77 78 this.setDuration(this.speed_ * goog.math.Coordinate.distance( 79 delta, new goog.math.Coordinate(0, 0)) / 100); 80 this.speedCalcDone_ = 1; 81 } 82 83 /** 84 * @inheritDoc 85 * @see lime.animation.Animation#update 86 */ 87 lime.animation.MoveTo.prototype.update = function(t, target) { 88 if (this.status_ == 0) return; 89 var prop = this.getTargetProp(target); 90 91 target.setPosition( 92 prop.startpos.x + prop.delta.x * t, 93 prop.startpos.y + prop.delta.y * t 94 ); 95 }; 96 97 /** 98 * @inheritDoc 99 * @see lime.animation.Animation#clearTransition 100 */ 101 lime.animation.MoveTo.prototype.clearTransition = function(target) { 102 103 if (this.useTransitions()) { 104 target.clearTransition(lime.Transition.POSITION); 105 target.setDirty(lime.Dirty.POSITION); 106 } 107 108 }; 109 110