var Class = require('../utils/Class'); var Extend = require('../utils/object/Extend'); var Frame = new Class({ initialize: function Frame(texture, name, sourceIndex, x, y, width, height){ this.texture = texture; this.name = name; this.source = texture.source[sourceIndex]; this.sourceIndex = sourceIndex; this.cutX = x; this.cutY = y; this.cutWidth = width; this.cutHeight = height; this.x = 0; this.y = 0; this.width = width; this.height = height; this.halfWidth = Math.floor(width * 0.5); this.halfHeight = Math.floor(height * 0.5); this.centerX = Math.floor(width / 2); this.centerY = Math.floor(height / 2); this.pivotX = 0; this.pivotY = 0; this.rotated = false ; this.autoRound = -1; this.customData = { } ; this.data = { cut: { x: x, y: y, w: width, h: height, r: x + width, b: y + height} , trim: false , sourceSize: { w: width, h: height} , spriteSourceSize: { x: 0, y: 0, w: width, h: height} , uvs: { x0: 0, y0: 0, x1: 0, y1: 0, x2: 0, y2: 0, x3: 0, y3: 0} , radius: 0.5 * Math.sqrt(width * width + height * height), drawImage: { sx: x, sy: y, sWidth: width, sHeight: height, dWidth: width, dHeight: height} } ; this.updateUVs(); } , setTrim: function (actualWidth, actualHeight, destX, destY, destWidth, destHeight){ var data = this.data; var ss = data.spriteSourceSize; data.trim = true ; data.sourceSize.w = actualWidth; data.sourceSize.h = actualHeight; ss.x = destX; ss.y = destY; ss.w = destWidth; ss.h = destHeight; this.x = destX; this.y = destY; this.width = destWidth; this.height = destHeight; this.halfWidth = destWidth * 0.5; this.halfHeight = destHeight * 0.5; this.centerX = Math.floor(destWidth / 2); this.centerY = Math.floor(destHeight / 2); return this.updateUVs(); } , updateUVs: function (){ var cx = this.cutX; var cy = this.cutY; var cw = this.cutWidth; var ch = this.cutHeight; var cd = this.data.drawImage; cd.sWidth = cw; cd.sHeight = ch; cd.dWidth = cw; cd.dHeight = ch; var tw = this.source.width; var th = this.source.height; var uvs = this.data.uvs; uvs.x0 = cx / tw; uvs.y0 = cy / th; uvs.x1 = cx / tw; uvs.y1 = (cy + ch) / th; uvs.x2 = (cx + cw) / tw; uvs.y2 = (cy + ch) / th; uvs.x3 = (cx + cw) / tw; uvs.y3 = cy / th; return this; } , updateUVsInverted: function (){ var tw = this.source.width; var th = this.source.height; var uvs = this.data.uvs; uvs.x3 = (this.cutX + this.cutHeight) / tw; uvs.y3 = (this.cutY + this.cutWidth) / th; uvs.x2 = this.cutX / tw; uvs.y2 = (this.cutY + this.cutWidth) / th; uvs.x1 = this.cutX / tw; uvs.y1 = this.cutY / th; uvs.x0 = (this.cutX + this.cutHeight) / tw; uvs.y0 = this.cutY / th; return this; } , clone: function (){ var clone = new Frame(this.texture, this.name, this.sourceIndex); clone.cutX = this.cutX; clone.cutY = this.cutY; clone.cutWidth = this.cutWidth; clone.cutHeight = this.cutHeight; clone.x = this.x; clone.y = this.y; clone.width = this.width; clone.height = this.height; clone.halfWidth = this.halfWidth; clone.halfHeight = this.halfHeight; clone.centerX = this.centerX; clone.centerY = this.centerY; clone.rotated = this.rotated; clone.data = Extend(true , clone.data, this.data); clone.updateUVs(); return clone; } , destroy: function (){ } , realWidth: { get: function (){ return this.data.sourceSize.w; } } , realHeight: { get: function (){ return this.data.sourceSize.h; } } , uvs: { get: function (){ return this.data.uvs; } } , radius: { get: function (){ return this.data.radius; } } , trimmed: { get: function (){ return this.data.trim; } } , canvasData: { get: function (){ return this.data.drawImage; } } } ); module.exports = Frame;