1 /**************************************************************************** 2 Copyright (c) 2008-2010 Ricardo Quesada 3 Copyright (c) 2011-2012 cocos2d-x.org 4 Copyright (c) 2013-2014 Chukong Technologies Inc. 5 Copyright (C) 2010 Lam Pham 6 7 http://www.cocos2d-x.org 8 9 Permission is hereby granted, free of charge, to any person obtaining a copy 10 of this software and associated documentation files (the "Software"), to deal 11 in the Software without restriction, including without limitation the rights 12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 copies of the Software, and to permit persons to whom the Software is 14 furnished to do so, subject to the following conditions: 15 16 The above copyright notice and this permission notice shall be included in 17 all copies or substantial portions of the Software. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 THE SOFTWARE. 26 ****************************************************************************/ 27 28 /** 29 * Progress to percentage 30 * @class 31 * @extends cc.ActionInterval 32 */ 33 cc.ProgressTo = cc.ActionInterval.extend(/** @lends cc.ProgressTo# */{ 34 _to:0, 35 _from:0, 36 37 /** 38 * Creates a ProgressTo action with a duration and a percent 39 * Constructor of cc.ProgressTo 40 * @param {Number} duration duration in seconds 41 * @param {Number} percent 42 * @example 43 * var to = new cc.ProgressTo(2, 100); 44 */ 45 ctor: function(duration, percent){ 46 cc.ActionInterval.prototype.ctor.call(this); 47 this._to = 0; 48 this._from = 0; 49 50 percent !== undefined && this.initWithDuration(duration, percent); 51 }, 52 53 /** Initializes with a duration and a percent 54 * @param {Number} duration duration in seconds 55 * @param {Number} percent 56 * @return {Boolean} 57 */ 58 initWithDuration:function (duration, percent) { 59 if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) { 60 this._to = percent; 61 return true; 62 } 63 return false; 64 }, 65 66 clone:function(){ 67 var action = new cc.ProgressTo(); 68 action.initWithDuration(this._duration, this._to); 69 return action; 70 }, 71 72 reverse: function(){ 73 cc.log("cc.ProgressTo.reverse(): reverse hasn't been supported."); 74 return null; 75 }, 76 77 /** 78 * @param {cc.Node} target 79 */ 80 startWithTarget:function (target) { 81 cc.ActionInterval.prototype.startWithTarget.call(this, target); 82 this._from = target.percentage; 83 84 // XXX: Is this correct ? 85 // Adding it to support CCRepeat 86 if (this._from == 100) 87 this._from = 0; 88 }, 89 90 /** 91 * @param {Number} time time in seconds 92 */ 93 update:function (time) { 94 if (this.target instanceof cc.ProgressTimer) 95 this.target.percentage = this._from + (this._to - this._from) * time; 96 } 97 }); 98 99 /** Creates and initializes with a duration and a percent 100 * @param {Number} duration duration in seconds 101 * @param {Number} percent 102 * @return {cc.ProgressTo} 103 * @example 104 * // example 105 * var to = cc.ProgressTo.create(2, 100); 106 */ 107 cc.ProgressTo.create = function (duration, percent) { 108 return new cc.ProgressTo(duration, percent); 109 }; 110 111 /** 112 * Progress from a percentage to another percentage 113 * @class 114 * @extends cc.ActionInterval 115 */ 116 cc.ProgressFromTo = cc.ActionInterval.extend(/** @lends cc.ProgressFromTo# */{ 117 _to:0, 118 _from:0, 119 120 /** 121 * Creates and initializes the action with a duration, a "from" percentage and a "to" percentage 122 * Constructor of cc.ProgressFromTo 123 * @param {Number} duration duration in seconds 124 * @param {Number} fromPercentage 125 * @param {Number} toPercentage 126 * @example 127 * var fromTo = new cc.ProgressFromTo(2, 100.0, 0.0); 128 */ 129 ctor:function(duration, fromPercentage, toPercentage){ 130 cc.ActionInterval.prototype.ctor.call(this); 131 this._to = 0; 132 this._from = 0; 133 134 toPercentage !== undefined && this.initWithDuration(duration, fromPercentage, toPercentage); 135 }, 136 137 /** Initializes the action with a duration, a "from" percentage and a "to" percentage 138 * @param {Number} duration duration in seconds 139 * @param {Number} fromPercentage 140 * @param {Number} toPercentage 141 * @return {Boolean} 142 */ 143 initWithDuration:function (duration, fromPercentage, toPercentage) { 144 if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) { 145 this._to = toPercentage; 146 this._from = fromPercentage; 147 return true; 148 } 149 return false; 150 }, 151 152 clone:function(){ 153 var action = new cc.ProgressFromTo(); 154 action.initWithDuration(this._duration, this._from, this._to); 155 return action; 156 }, 157 158 /** 159 * @return {cc.ActionInterval} 160 */ 161 reverse:function () { 162 return cc.ProgressFromTo.create(this._duration, this._to, this._from); 163 }, 164 165 /** 166 * @param {cc.Node} target 167 */ 168 startWithTarget:function (target) { 169 cc.ActionInterval.prototype.startWithTarget.call(this, target); 170 }, 171 172 /** 173 * @param {Number} time time in seconds 174 */ 175 update:function (time) { 176 if (this.target instanceof cc.ProgressTimer) 177 this.target.percentage = this._from + (this._to - this._from) * time; 178 } 179 }); 180 181 /** Creates and initializes the action with a duration, a "from" percentage and a "to" percentage 182 * @param {Number} duration duration in seconds 183 * @param {Number} fromPercentage 184 * @param {Number} toPercentage 185 * @return {cc.ProgressFromTo} 186 * @example 187 * // example 188 * var fromTO = cc.ProgressFromTo.create(2, 100.0, 0.0); 189 */ 190 cc.ProgressFromTo.create = function (duration, fromPercentage, toPercentage) { 191 return new cc.ProgressFromTo(duration, fromPercentage, toPercentage); 192 }; 193