1- var GetValue = require ( '../../utils/object/GetValue ' ) ;
1+ var GetFastValue = require ( '../../utils/object/GetFastValue ' ) ;
22
33var SpriteSheetFromAtlas = function ( texture , frame , config )
44{
5- var frameWidth = GetValue ( config , 'frameWidth' , null ) ;
6- var frameHeight = GetValue ( config , 'frameHeight' , frameWidth ) ;
5+ var frameWidth = GetFastValue ( config , 'frameWidth' , null ) ;
6+ var frameHeight = GetFastValue ( config , 'frameHeight' , frameWidth ) ;
77
88 // If missing we can't proceed
99 if ( ! frameWidth )
1010 {
1111 throw new Error ( 'TextureManager.SpriteSheetFromAtlas: Invalid frameWidth given.' ) ;
1212 }
1313
14+ /*
15+ {
16+ "filename": "boomtest-notrim",
17+ "frame": {"x":4,"y":4,"w":320,"h":320},
18+ "rotated": false,
19+ "trimmed": false,
20+ "spriteSourceSize": {"x":0,"y":0,"w":320,"h":320},
21+ "sourceSize": {"w":320,"h":320},
22+ "pivot": {"x":0.5,"y":0.5}
23+ },
24+ {
25+ "filename": "boomtest",
26+ "frame": {"x":976,"y":4,"w":306,"h":305},
27+ "rotated": false,
28+ "trimmed": true,
29+ "spriteSourceSize": {"x":6,"y":5,"w":306,"h":305},
30+ "sourceSize": {"w":320,"h":320},
31+ "pivot": {"x":0.5,"y":0.5}
32+ },
33+ */
34+
35+ // The notrim version is smaller than sourceSize
36+
1437 // Add in a __BASE entry (for the entire atlas)
15- // var source = texture.source[0 ];
16- // texture.add('__BASE', 0 , 0, 0, source.width, source.height);
38+ // var source = texture.source[sourceIndex ];
39+ // texture.add('__BASE', sourceIndex , 0, 0, source.width, source.height);
1740
18- var startFrame = GetValue ( config , 'startFrame' , 0 ) ;
19- var endFrame = GetValue ( config , 'endFrame' , - 1 ) ;
20- var margin = GetValue ( config , 'margin' , 0 ) ;
21- var spacing = GetValue ( config , 'spacing' , 0 ) ;
41+ var startFrame = GetFastValue ( config , 'startFrame' , 0 ) ;
42+ var endFrame = GetFastValue ( config , 'endFrame' , - 1 ) ;
43+ var margin = GetFastValue ( config , 'margin' , 0 ) ;
44+ var spacing = GetFastValue ( config , 'spacing' , 0 ) ;
2245
2346 var x = frame . cutX ;
2447 var y = frame . cutY ;
48+
2549 var cutWidth = frame . cutWidth ;
2650 var cutHeight = frame . cutHeight ;
2751 var sheetWidth = frame . realWidth ;
2852 var sheetHeight = frame . realHeight ;
2953
54+ console . log ( 'x / y' , x , y ) ;
55+ console . log ( 'cutW / H' , cutWidth , cutHeight ) ;
56+ console . log ( 'sheetW / H' , sheetWidth , sheetHeight ) ;
57+
3058 var row = Math . floor ( ( sheetWidth - margin ) / ( frameWidth + spacing ) ) ;
3159 var column = Math . floor ( ( sheetHeight - margin ) / ( frameHeight + spacing ) ) ;
3260 var total = row * column ;
3361
34- console . log ( 'split sheet into rows/cols:' , row , column , 'total frames:' , total ) ;
62+ console . log ( 'row' , row , 'column' , column , 'total' , total ) ;
63+
64+ // trim offset
65+
66+ var leftPad = frame . x ;
67+ var leftWidth = frameWidth - leftPad ;
68+
69+ var rightWidth = frameWidth - ( ( sheetWidth - cutWidth ) - leftPad ) ;
70+
71+ var topPad = frame . y ;
72+ var topHeight = frameHeight - topPad ;
73+
74+ var bottomHeight = frameHeight - ( ( sheetHeight - cutHeight ) - topPad ) ;
75+
76+ // console.log('padding x', leftPad, 'y', topPad, 'right', rightPad, 'bottom', bottomPad);
77+ console . log ( 'LW' , leftWidth , 'RW' , rightWidth , 'TH' , topHeight , 'BH' , bottomHeight ) ;
3578
3679 if ( startFrame > total || startFrame < - total )
3780 {
@@ -49,14 +92,142 @@ var SpriteSheetFromAtlas = function (texture, frame, config)
4992 total = startFrame + ( endFrame + 1 ) ;
5093 }
5194
52- var fx = margin ;
53- var fy = margin ;
54- var ax = 0 ;
55- var ay = 0 ;
5695 var sheetFrame ;
5796
97+ var width = frame . cutWidth ;
98+ var height = frame . cutHeight ;
99+ var sourceIndex = frame . sourceIndex ;
100+
101+ var frameX = margin ;
102+ var frameY = margin ;
103+ var frameIndex = 0 ;
104+
105+ for ( var sheetY = 0 ; sheetY < column ; sheetY ++ )
106+ {
107+ var topRow = ( sheetY === 0 ) ;
108+ var bottomRow = ( sheetY === column - 1 ) ;
109+
110+ // var fy = margin + ((frameHeight + spacing) * sheetY);
111+
112+ for ( var sheetX = 0 ; sheetX < row ; sheetX ++ )
113+ {
114+ // var fx = margin + ((frameWidth + spacing) * sheetX);
115+ var leftRow = ( sheetX === 0 ) ;
116+ var rightRow = ( sheetX === row - 1 ) ;
117+
118+ // fx / fy is wrong
119+
120+ // var frame = new Frame(this, name, sourceIndex, x, y, width, height);
121+ // x/y/w/h = set to the CUT values AND normal values
122+ // we need to override the cut values and setTime does NOT do that
123+
124+ sheetFrame = texture . add ( frameIndex , sourceIndex , x + frameX , y + frameY , frameWidth , frameHeight ) ;
125+
126+ if ( leftRow || topRow || rightRow || bottomRow )
127+ {
128+ var destWidth = frameWidth ;
129+ var destHeight = frameHeight ;
130+
131+ if ( leftRow )
132+ {
133+ destWidth = leftWidth ;
134+ }
135+ else if ( rightRow )
136+ {
137+ destWidth = rightWidth ;
138+ }
139+
140+ if ( topRow )
141+ {
142+ destHeight = topHeight ;
143+ }
144+ else if ( bottomRow )
145+ {
146+ destHeight = bottomHeight ;
147+ }
148+
149+ sheetFrame . cutWidth = destWidth ;
150+ sheetFrame . cutHeight = destHeight ;
151+ }
152+
153+ frameX += spacing ;
154+
155+ if ( leftRow )
156+ {
157+ frameX += leftWidth ;
158+ }
159+ else if ( rightRow )
160+ {
161+ frameX += rightRow ;
162+ }
163+ else
164+ {
165+ frameX += frameWidth ;
166+ }
167+
168+ /*
169+ if (leftRow || topRow || rightRow || bottomRow)
170+ {
171+ var actualWidth = frameWidth;
172+ var actualHeight = frameHeight;
173+ var destX = (leftRow) ? leftPad : 0;
174+ var destY = (topRow) ? topPad : 0;
175+
176+ var destWidth = frameWidth;
177+ var destHeight = frameHeight;
178+
179+ if (leftRow)
180+ {
181+ destWidth = leftWidth;
182+ }
183+ else if (rightRow)
184+ {
185+ destWidth = rightWidth;
186+ }
187+
188+ if (topRow)
189+ {
190+ destHeight = topHeight;
191+ }
192+ else if (bottomRow)
193+ {
194+ destHeight = bottomHeight;
195+ }
196+
197+ // sheetFrame.cutWidth = destWidth;
198+ // sheetFrame.cutHeight = destHeight;
199+ // sheetFrame.updateUVs();
200+
201+ // sheetFrame.setTrim(actualWidth, actualHeight, destX, destY, destWidth, destHeight);
202+ }
203+ */
204+
205+ // console.log(sheetFrame);
206+
207+ frameIndex ++ ;
208+ }
209+
210+ frameX = margin ;
211+ frameY += spacing ;
212+
213+ if ( topRow )
214+ {
215+ frameY += topHeight ;
216+ }
217+ else if ( bottomRow )
218+ {
219+ frameY += bottomHeight ;
220+ }
221+ else
222+ {
223+ frameY += frameHeight ;
224+ }
225+ }
226+
227+ /*
58228 for (var i = 0; i < total; i++)
59229 {
230+
60231 ax = 0;
61232 ay = 0;
62233
@@ -73,12 +244,14 @@ var SpriteSheetFromAtlas = function (texture, frame, config)
73244 ay = h - height;
74245 }
75246
76- sheetFrame = texture . add ( i , sourceIndex , x + fx , y + fy , frameWidth - ax , frameHeight - ay ) ;
247+ // console.log('Add frame', i);
248+ // console.log('x', (x + fx), 'y', (y + fy), 'w', (frameWidth - ax), 'h', (frameHeight - ay));
77249
78- // sheetFrame.setTrim(sheetWidth, sheetHeight, )
250+ sheetFrame = texture.add(i, sourceIndex, x + fx, y + fy, frameWidth - ax, frameHeight - ay);
79251
80252 // setTrim: function (actualWidth, actualHeight, destX, destY, destWidth, destHeight)
81253
254+ // sheetFrame.setTrim(sheetWidth, sheetHeight, )
82255
83256 fx += frameWidth + spacing;
84257
@@ -88,6 +261,7 @@ var SpriteSheetFromAtlas = function (texture, frame, config)
88261 fy += frameHeight + spacing;
89262 }
90263 }
264+ */
91265
92266 return texture ;
93267} ;
0 commit comments