|
| 1 | +/** |
| 2 | +* @author Richard Davey <rich@photonstorm.com> |
| 3 | +* @copyright 2016 Photon Storm Ltd. |
| 4 | +* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | +*/ |
| 6 | + |
| 7 | +/** |
| 8 | +* A Frame is a section of a Texture. |
| 9 | +* |
| 10 | +* Called TextureFrame during integration, will rename to Frame later. |
| 11 | +* |
| 12 | +* @class Phaser.TextureFrame |
| 13 | +* @constructor |
| 14 | +* @param {Phaser.Texture} texture - The Texture this Frame belongs to. |
| 15 | +* @param {string} name - The unique (within the Texture) name of this Frame. |
| 16 | +* @param {number} x - X position of the frame within the Texture. |
| 17 | +* @param {number} y - Y position of the frame within the Texture. |
| 18 | +* @param {number} width - Width of the frame within the Texture. |
| 19 | +* @param {number} height - Height of the frame within the Texture. |
| 20 | +*/ |
| 21 | +var Frame = function (texture, name, sourceIndex, x, y, width, height) |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @property {Phaser.Texture} texture - The Texture this frame belongs to. |
| 25 | + */ |
| 26 | + this.texture = texture; |
| 27 | + |
| 28 | + /** |
| 29 | + * @property {string} name - The name of this frame within the Texture. |
| 30 | + */ |
| 31 | + this.name = name; |
| 32 | + |
| 33 | + this.source = texture.source[sourceIndex]; |
| 34 | + |
| 35 | + this.sourceIndex = sourceIndex; |
| 36 | + |
| 37 | + /** |
| 38 | + * @property {number} cutX - X position within the source image to cut from. |
| 39 | + */ |
| 40 | + this.cutX = x; |
| 41 | + |
| 42 | + /** |
| 43 | + * @property {number} cutY - Y position within the source image to cut from. |
| 44 | + */ |
| 45 | + this.cutY = y; |
| 46 | + |
| 47 | + /** |
| 48 | + * @property {number} cutWidth - The width of the area in the source image to cut. |
| 49 | + */ |
| 50 | + this.cutWidth = width; |
| 51 | + |
| 52 | + /** |
| 53 | + * @property {number} cutHeight - The height of the area in the source image to cut. |
| 54 | + */ |
| 55 | + this.cutHeight = height; |
| 56 | + |
| 57 | + /** |
| 58 | + * @property {number} x - The X rendering offset of this Frame, taking trim into account. |
| 59 | + */ |
| 60 | + this.x = 0; |
| 61 | + |
| 62 | + /** |
| 63 | + * @property {number} y - The Y rendering offset of this Frame, taking trim into account. |
| 64 | + */ |
| 65 | + this.y = 0; |
| 66 | + |
| 67 | + /** |
| 68 | + * @property {number} width - The rendering width of this Frame, taking trim into account. |
| 69 | + */ |
| 70 | + this.width = width; |
| 71 | + |
| 72 | + /** |
| 73 | + * @property {number} height - The rendering height of this Frame, taking trim into account. |
| 74 | + */ |
| 75 | + this.height = height; |
| 76 | + |
| 77 | + /** |
| 78 | + * Is this frame is rotated or not in the Texture? |
| 79 | + * Rotation allows you to use rotated frames in texture atlas packing. |
| 80 | + * It has nothing to do with Sprite rotation. |
| 81 | + * |
| 82 | + * @property {boolean} rotated |
| 83 | + * @default |
| 84 | + */ |
| 85 | + this.rotated = false; |
| 86 | + |
| 87 | + /** |
| 88 | + * Is this a tiling texture? As used by the likes of a TilingSprite. |
| 89 | + * TODO: Try and remove this, it shouldn't be here |
| 90 | + * |
| 91 | + * @property {boolean} isTiling |
| 92 | + * @default |
| 93 | + */ |
| 94 | + this.isTiling = false; |
| 95 | + |
| 96 | + /** |
| 97 | + * This will let a renderer know that a tinted parent has updated its texture. |
| 98 | + * TODO: Try and remove this, it shouldn't be here |
| 99 | + * |
| 100 | + * @property {boolean} requiresReTint |
| 101 | + * @default |
| 102 | + */ |
| 103 | + this.requiresReTint = false; |
| 104 | + |
| 105 | + // Over-rides the Renderer setting? -1 = use Renderer Setting, 0 = No rounding, 1 = Round |
| 106 | + this.autoRound = -1; |
| 107 | + |
| 108 | + /** |
| 109 | + * The un-modified source frame, trim and UV data. |
| 110 | + * |
| 111 | + * @private |
| 112 | + * @property {object} data |
| 113 | + */ |
| 114 | + this.data = { |
| 115 | + cut: { |
| 116 | + x: x, |
| 117 | + y: y, |
| 118 | + w: width, |
| 119 | + h: height, |
| 120 | + r: x + width, |
| 121 | + b: y + height |
| 122 | + }, |
| 123 | + trim: false, |
| 124 | + sourceSize: { |
| 125 | + w: width, |
| 126 | + h: height |
| 127 | + }, |
| 128 | + spriteSourceSize: { |
| 129 | + x: 0, |
| 130 | + y: 0, |
| 131 | + w: width, |
| 132 | + h: height |
| 133 | + }, |
| 134 | + uvs: { |
| 135 | + x0: 0, |
| 136 | + y0: 0, |
| 137 | + x1: 0, |
| 138 | + y1: 0, |
| 139 | + x2: 0, |
| 140 | + y2: 0, |
| 141 | + x3: 0, |
| 142 | + y3: 0 |
| 143 | + } |
| 144 | + }; |
| 145 | + |
| 146 | + this.updateUVs(); |
| 147 | +}; |
| 148 | + |
| 149 | +Frame.prototype.constructor = Frame; |
| 150 | + |
| 151 | +Frame.prototype = { |
| 152 | + |
| 153 | + /** |
| 154 | + * If the frame was trimmed when added to the Texture Atlas, this records the trim and source data. |
| 155 | + * |
| 156 | + * @method Phaser.TextureFrame#setTrim |
| 157 | + * @param {number} actualWidth - The width of the frame before being trimmed. |
| 158 | + * @param {number} actualHeight - The height of the frame before being trimmed. |
| 159 | + * @param {number} destX - The destination X position of the trimmed frame for display. |
| 160 | + * @param {number} destY - The destination Y position of the trimmed frame for display. |
| 161 | + * @param {number} destWidth - The destination width of the trimmed frame for display. |
| 162 | + * @param {number} destHeight - The destination height of the trimmed frame for display. |
| 163 | + */ |
| 164 | + setTrim: function (actualWidth, actualHeight, destX, destY, destWidth, destHeight) |
| 165 | + { |
| 166 | + // Store actual values |
| 167 | + |
| 168 | + this.data.trim = true; |
| 169 | + |
| 170 | + this.data.sourceSize.w = actualWidth; |
| 171 | + this.data.sourceSize.h = actualHeight; |
| 172 | + |
| 173 | + this.data.spriteSourceSize.x = destX; |
| 174 | + this.data.spriteSourceSize.y = destY; |
| 175 | + this.data.spriteSourceSize.w = destWidth; |
| 176 | + this.data.spriteSourceSize.h = destHeight; |
| 177 | + |
| 178 | + // Adjust properties |
| 179 | + this.x = destX; |
| 180 | + this.y = destY; |
| 181 | + this.width = destWidth; |
| 182 | + this.height = destHeight; |
| 183 | + |
| 184 | + this.updateUVs(); |
| 185 | + |
| 186 | + return this; |
| 187 | + }, |
| 188 | + |
| 189 | + /** |
| 190 | + * Updates the internal WebGL UV cache. |
| 191 | + * |
| 192 | + * @method updateUVs |
| 193 | + * @private |
| 194 | + */ |
| 195 | + updateUVs: function () |
| 196 | + { |
| 197 | + var tw = this.source.width; |
| 198 | + var th = this.source.height; |
| 199 | + var uvs = this.data.uvs; |
| 200 | + |
| 201 | + uvs.x0 = this.cutX / tw; |
| 202 | + uvs.y0 = this.cutY / th; |
| 203 | + |
| 204 | + uvs.x1 = (this.cutX + this.cutWidth) / tw; |
| 205 | + uvs.y1 = this.cutY / th; |
| 206 | + |
| 207 | + uvs.x2 = (this.cutX + this.cutWidth) / tw; |
| 208 | + uvs.y2 = (this.cutY + this.cutHeight) / th; |
| 209 | + |
| 210 | + uvs.x3 = this.cutX / tw; |
| 211 | + uvs.y3 = (this.cutY + this.cutHeight) / th; |
| 212 | + |
| 213 | + return this; |
| 214 | + }, |
| 215 | + |
| 216 | + /** |
| 217 | + * Updates the internal WebGL UV cache. |
| 218 | + * |
| 219 | + * @method updateUVsInverted |
| 220 | + * @private |
| 221 | + */ |
| 222 | + updateUVsInverted: function () |
| 223 | + { |
| 224 | + var tw = this.source.width; |
| 225 | + var th = this.source.height; |
| 226 | + var uvs = this.data.uvs; |
| 227 | + |
| 228 | + uvs.x0 = this.cutX / tw; |
| 229 | + uvs.y0 = this.cutY / th; |
| 230 | + |
| 231 | + uvs.x1 = (this.cutX + this.cutHeight) / tw; |
| 232 | + uvs.y1 = this.cutY / th; |
| 233 | + |
| 234 | + uvs.x2 = (this.cutX + this.cutHeight) / tw; |
| 235 | + uvs.y2 = (this.cutY + this.cutWidth) / th; |
| 236 | + |
| 237 | + uvs.x3 = this.cutX / tw; |
| 238 | + uvs.y3 = (this.cutY + this.cutWidth) / th; |
| 239 | + |
| 240 | + return this; |
| 241 | + }, |
| 242 | + |
| 243 | + clone: function () |
| 244 | + { |
| 245 | + var clone = new Phaser.TextureFrame(this.texture, this.name, this.sourceIndex); |
| 246 | + |
| 247 | + clone.cutX = this.cutX; |
| 248 | + clone.cutY = this.cutY; |
| 249 | + clone.cutWidth = this.cutWidth; |
| 250 | + clone.cutHeight = this.cutHeight; |
| 251 | + |
| 252 | + clone.x = this.x; |
| 253 | + clone.y = this.y; |
| 254 | + clone.width = this.width; |
| 255 | + clone.height = this.height; |
| 256 | + |
| 257 | + clone.rotated = this.rotated; |
| 258 | + |
| 259 | + clone.data = Phaser.Utils.extend(true, clone.data, this.data); |
| 260 | + |
| 261 | + clone.updateUVs(); |
| 262 | + |
| 263 | + return clone; |
| 264 | + }, |
| 265 | + |
| 266 | + destroy: function () |
| 267 | + { |
| 268 | + } |
| 269 | + |
| 270 | +}; |
| 271 | + |
| 272 | +Object.defineProperties(Frame.prototype, { |
| 273 | + |
| 274 | + /** |
| 275 | + * The width of the Frame in its un-trimmed, un-padded state, as prepared in the art package, |
| 276 | + * before being packed. |
| 277 | + * |
| 278 | + * @name Phaser.TextureFrame#realWidth |
| 279 | + * @property {any} realWidth |
| 280 | + */ |
| 281 | + realWidth: { |
| 282 | + |
| 283 | + enumerable: true, |
| 284 | + |
| 285 | + get: function () |
| 286 | + { |
| 287 | + return this.data.sourceSize.w; |
| 288 | + } |
| 289 | + |
| 290 | + }, |
| 291 | + |
| 292 | + /** |
| 293 | + * The height of the Frame in its un-trimmed, un-padded state, as prepared in the art package, |
| 294 | + * before being packed. |
| 295 | + * |
| 296 | + * @name Phaser.TextureFrame#realHeight |
| 297 | + * @property {any} realHeight |
| 298 | + */ |
| 299 | + realHeight: { |
| 300 | + |
| 301 | + enumerable: true, |
| 302 | + |
| 303 | + get: function () |
| 304 | + { |
| 305 | + return this.data.sourceSize.h; |
| 306 | + } |
| 307 | + |
| 308 | + }, |
| 309 | + |
| 310 | + /** |
| 311 | + * UVs |
| 312 | + * |
| 313 | + * @name Phaser.TextureFrame#uvs |
| 314 | + * @property {Object} uvs |
| 315 | + */ |
| 316 | + uvs: { |
| 317 | + |
| 318 | + enumerable: true, |
| 319 | + |
| 320 | + get: function () |
| 321 | + { |
| 322 | + return this.data.uvs; |
| 323 | + } |
| 324 | + |
| 325 | + } |
| 326 | + |
| 327 | +}); |
| 328 | + |
| 329 | +module.exports = Frame; |
0 commit comments