Skip to content

Commit 0aa071f

Browse files
committed
Frame has a new method setSize which allows you to set the frame x, y, width and height and have it update all of the internal properties automatically.
1 parent 6d958c5 commit 0aa071f

1 file changed

Lines changed: 93 additions & 28 deletions

File tree

src/textures/Frame.js

Lines changed: 93 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ var Frame = new Class({
7474
* @type {integer}
7575
* @since 3.0.0
7676
*/
77-
this.cutX = x;
77+
this.cutX;
7878

7979
/**
8080
* Y position within the source image to cut from.
@@ -83,7 +83,7 @@ var Frame = new Class({
8383
* @type {integer}
8484
* @since 3.0.0
8585
*/
86-
this.cutY = y;
86+
this.cutY;
8787

8888
/**
8989
* The width of the area in the source image to cut.
@@ -92,7 +92,7 @@ var Frame = new Class({
9292
* @type {integer}
9393
* @since 3.0.0
9494
*/
95-
this.cutWidth = width;
95+
this.cutWidth;
9696

9797
/**
9898
* The height of the area in the source image to cut.
@@ -101,7 +101,7 @@ var Frame = new Class({
101101
* @type {integer}
102102
* @since 3.0.0
103103
*/
104-
this.cutHeight = height;
104+
this.cutHeight;
105105

106106
/**
107107
* The X rendering offset of this Frame, taking trim into account.
@@ -130,7 +130,7 @@ var Frame = new Class({
130130
* @type {integer}
131131
* @since 3.0.0
132132
*/
133-
this.width = width;
133+
this.width;
134134

135135
/**
136136
* The rendering height of this Frame, taking trim into account.
@@ -139,7 +139,7 @@ var Frame = new Class({
139139
* @type {integer}
140140
* @since 3.0.0
141141
*/
142-
this.height = height;
142+
this.height;
143143

144144
/**
145145
* Half the width, floored.
@@ -149,7 +149,7 @@ var Frame = new Class({
149149
* @type {integer}
150150
* @since 3.0.0
151151
*/
152-
this.halfWidth = Math.floor(width * 0.5);
152+
this.halfWidth;
153153

154154
/**
155155
* Half the height, floored.
@@ -159,7 +159,7 @@ var Frame = new Class({
159159
* @type {integer}
160160
* @since 3.0.0
161161
*/
162-
this.halfHeight = Math.floor(height * 0.5);
162+
this.halfHeight;
163163

164164
/**
165165
* The x center of this frame, floored.
@@ -168,7 +168,7 @@ var Frame = new Class({
168168
* @type {integer}
169169
* @since 3.0.0
170170
*/
171-
this.centerX = Math.floor(width / 2);
171+
this.centerX;
172172

173173
/**
174174
* The y center of this frame, floored.
@@ -177,7 +177,7 @@ var Frame = new Class({
177177
* @type {integer}
178178
* @since 3.0.0
179179
*/
180-
this.centerY = Math.floor(height / 2);
180+
this.centerY;
181181

182182
/**
183183
* The horizontal pivot point of this Frame.
@@ -255,23 +255,23 @@ var Frame = new Class({
255255
*/
256256
this.data = {
257257
cut: {
258-
x: x,
259-
y: y,
260-
w: width,
261-
h: height,
262-
r: x + width,
263-
b: y + height
258+
x: 0,
259+
y: 0,
260+
w: 0,
261+
h: 0,
262+
r: 0,
263+
b: 0
264264
},
265265
trim: false,
266266
sourceSize: {
267-
w: width,
268-
h: height
267+
w: 0,
268+
h: 0
269269
},
270270
spriteSourceSize: {
271271
x: 0,
272272
y: 0,
273-
w: width,
274-
h: height
273+
w: 0,
274+
h: 0
275275
},
276276
uvs: {
277277
x0: 0,
@@ -283,18 +283,83 @@ var Frame = new Class({
283283
x3: 0,
284284
y3: 0
285285
},
286-
radius: 0.5 * Math.sqrt(width * width + height * height),
286+
radius: 0,
287287
drawImage: {
288-
sx: x,
289-
sy: y,
290-
sWidth: width,
291-
sHeight: height,
292-
dWidth: width,
293-
dHeight: height
288+
sx: 0,
289+
sy: 0,
290+
sWidth: 0,
291+
sHeight: 0,
292+
dWidth: 0,
293+
dHeight: 0
294294
}
295295
};
296296

297-
this.updateUVs();
297+
this.setSize(width, height, x, y);
298+
},
299+
300+
/**
301+
* Sets the width, height, x and y of this Frame.
302+
*
303+
* This is called automatically by the constructor
304+
* and should rarely be changed on-the-fly.
305+
*
306+
* @method Phaser.Textures.Frame#setSize
307+
* @since 3.6.1
308+
*
309+
* @param {integer} width - The width of the frame before being trimmed.
310+
* @param {integer} height - The height of the frame before being trimmed.
311+
* @param {integer} [x=0] - The x coordinate of the top-left of this Frame.
312+
* @param {integer} [y=0] - The y coordinate of the top-left of this Frame.
313+
*
314+
* @return {Phaser.Textures.Frame} This Frame object.
315+
*/
316+
setSize: function (width, height, x, y)
317+
{
318+
if (x === undefined) { x = 0; }
319+
if (y === undefined) { y = 0; }
320+
321+
this.cutX = x;
322+
this.cutY = y;
323+
this.cutWidth = width;
324+
this.cutHeight = height;
325+
326+
this.width = width;
327+
this.height = height;
328+
329+
this.halfWidth = Math.floor(width * 0.5);
330+
this.halfHeight = Math.floor(height * 0.5);
331+
332+
this.centerX = Math.floor(width / 2);
333+
this.centerY = Math.floor(height / 2);
334+
335+
var data = this.data;
336+
var cut = data.cut;
337+
338+
cut.x = x;
339+
cut.y = y;
340+
cut.w = width;
341+
cut.h = height;
342+
cut.r = x + width;
343+
cut.b = y + height;
344+
345+
data.sourceSize.w = width;
346+
data.sourceSize.h = height;
347+
348+
data.spriteSourceSize.w = width;
349+
data.spriteSourceSize.h = height;
350+
351+
data.radius = 0.5 * Math.sqrt(width * width + height * height);
352+
353+
var drawImage = data.drawImage;
354+
355+
drawImage.sx = x;
356+
drawImage.sy = y;
357+
drawImage.sWidth = width;
358+
drawImage.sHeight = height;
359+
drawImage.dWidth = width;
360+
drawImage.dHeight = height;
361+
362+
return this.updateUVs();
298363
},
299364

300365
/**

0 commit comments

Comments
 (0)