-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathninepatch.js
More file actions
375 lines (336 loc) · 13.6 KB
/
ninepatch.js
File metadata and controls
375 lines (336 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
* Creates a NinePatch image.
* @constructor
* @param {string} src - The source image path. If ending with .9.png, the patches do not need to be specified.
* @param {string} fillMode - The way that the image is filled. It can be "stretch" or "tile". Stretch will stretch the borders and center, while tile will tile the texture, truncating at the sides.
* @param {number} patch1 - If only patch 1 is specified, all sides will have an equal patch size. Defaults to 0.
* @param {number} patch2 - If patch 2 is also specified, patch 1 will be the x patch size and patch 2 will be the y patch size.
* @param {number} patch3 - If patches 3-4 are specified, patches will be specifying each side, clockwise starting at the top.
* @param {number} patch4 - If patches 3-4 are specified, patches will be specifying each side, clockwise starting at the top.
*/
// later, it'd be nice to have multiple constructors or something for the different amount of patches
class NinePatch {
constructor(src, fillMode="stretch", patch1, patch2, patch3, patch4) {
this.fillMode = fillMode;
this.patches = new Array(9).fill(0);
// this is almost definitely not the best way, but it's the most obvious
// can't do default values because the others should be based on the previous ones.
this.topPatch = patch1;
this.rightPatch = patch2;
this.bottomPatch = patch3;
this.leftPatch = patch4;
if (this.topPatch === undefined) {
this.topPatch = 0;
}
if (this.rightPatch === undefined) {
this.rightPatch = this.topPatch;
}
if (this.bottomPatch === undefined && this.leftPatch === undefined) {
this.bottomPatch = this.topPatch;
this.leftPatch = this.rightPatch;
} else if (this.bottomPatch === undefined || this.leftPatch === undefined) {
console.warn("Patch 3 and 4 should be specified together or not at all.\nIf they aren't both specified, they are ignored.");
}
// todo: apparently img.pixels is faster than img.get, so use that primarily later
// however, this only runs on load, so it doesn't matter too much.
// also, it probably isn't that big of a difference.
loadImage(src, (img) => {
if (src.endsWith(".9.png")) {
let vertLayer = img.get(0, 0, img.width, 1);
let horizLayer = img.get(0, 0, 1, img.height);
for (let i = 0; i < vertLayer.width; i++) {
if (vertLayer.get(i, 0)[3] === 0) {
this.leftPatch = i;
}
if (this.leftPatch !== undefined && vertLayer.get(i, 0)[3] !== 0) {
this.rightPatch = i - 1;
break;
}
}
for (let i = 0; i < horizLayer.height; i++) {
if (horizLayer.get(0, i)[3] === 0) {
this.topPatch = i;
}
if (this.topPatch !== undefined && horizLayer.get(0, i)[3] !== 0) {
this.bottomPatch = i - 1;
break;
}
}
img = img.get(1, 1, img.width - 1, img.height - 1); // remove the borders
}
this.patches[0] = img.get(
0,
0,
this.leftPatch,
this.topPatch
); // top left
this.patches[1] = img.get(
this.leftPatch,
0,
img.width - this.rightPatch - this.leftPatch,
this.topPatch
); // top middle
this.patches[2] = img.get(
img.width - this.rightPatch,
0,
this.rightPatch,
this.topPatch
); // top right
this.patches[3] = img.get(
img.width - this.rightPatch,
this.topPatch,
this.rightPatch,
img.height - this.topPatch - this.bottomPatch
); // middle right
this.patches[4] = img.get(
img.width - this.rightPatch,
img.height - this.bottomPatch,
this.rightPatch,
this.bottomPatch
); // bottom right
this.patches[5] = img.get(
this.leftPatch,
img.height - this.bottomPatch,
img.width - this.rightPatch - this.leftPatch,
this.bottomPatch
); // bottom middle
this.patches[6] = img.get(
0,
img.height - this.bottomPatch,
this.leftPatch,
this.bottomPatch
); // bottom left
this.patches[7] = img.get(
0,
this.topPatch,
this.leftPatch,
img.height - this.topPatch - this.bottomPatch
); // middle left
this.patches[8] = img.get(
this.leftPatch,
this.topPatch,
img.width - this.rightPatch - this.leftPatch,
img.height - this.topPatch - this.bottomPatch
); // middle
}, (err) => {
console.error(`Failed to load image ${src}: ${err}\nMake sure the file exists and is not corrupted.`);
});
}
gen(wo, ho, segmentScale = 1) {
let scaleTopPatch = this.topPatch * segmentScale;
let scaleRightPatch = this.rightPatch * segmentScale;
let scaleBottomPatch = this.bottomPatch * segmentScale;
let scaleLeftPatch = this.leftPatch * segmentScale;
let scaleMiddleWidth = this.patches[8].width * segmentScale;
let scaleMiddleHeight = this.patches[8].height * segmentScale;
// make the size a multiple of the segment scale
// this isn't ideal, but it's much easier to do this than actually fix it
wo = Math.round(wo / segmentScale) * segmentScale;
ho = Math.round(ho / segmentScale) * segmentScale;
// make sure at least the corners fit
let w = max(scaleLeftPatch + scaleRightPatch, wo);
let h = max(scaleTopPatch + scaleBottomPatch, ho);
// make overflows for tiling
let overflowX = ((w / segmentScale) - this.leftPatch - this.rightPatch) % this.patches[8].width;
let overflowY = ((h / segmentScale) - this.topPatch - this.bottomPatch) % this.patches[8].height;
// make the graphics object
let temp = createGraphics(w, h);
temp.noSmooth();
// draw the middle
if (this.fillMode === "stretch") {
temp.image(
this.patches[8],
scaleLeftPatch,
scaleTopPatch,
w - scaleLeftPatch - scaleRightPatch,
h - scaleTopPatch - scaleBottomPatch
); // middle
} else if (this.fillMode === "tile") {
let imgSrc = this.patches[8];
let imgR, imgB, imgC;
if (overflowX > 0) {
imgR = imgSrc.get(
0,
0,
overflowX,
imgSrc.height
);
}
if (overflowY > 0) {
imgB = imgSrc.get(
0,
0,
imgSrc.width,
overflowY
);
}
if (overflowX > 0 && overflowY > 0) {
imgC = imgSrc.get(
0,
0,
overflowX,
overflowY
);
}
for (let x = scaleLeftPatch; x < w - scaleRightPatch; x += scaleMiddleWidth) {
for (let y = scaleTopPatch; y < h - scaleBottomPatch; y += scaleMiddleHeight) {
let img = imgSrc;
if (x >= w - scaleRightPatch - scaleMiddleWidth && y >= h - scaleBottomPatch - scaleMiddleHeight && overflowX > 0 && overflowY > 0) {
img = imgC;
} else if (x >= w - scaleRightPatch - scaleMiddleWidth && overflowX > 0) {
img = imgR;
} else if (y > h - scaleBottomPatch - scaleMiddleHeight && overflowY > 0) {
img = imgB;
}
temp.image(img, x, y, img.width * segmentScale, img.height * segmentScale);
}
}
}
// draw the sides
if (this.fillMode === "stretch") {
// need to only draw sides if they exist
if (wo === w) {
temp.image(
this.patches[1],
scaleLeftPatch,
0,
w - scaleLeftPatch - scaleRightPatch,
scaleTopPatch
); // top middle
temp.image(
this.patches[5],
scaleLeftPatch,
h - scaleBottomPatch,
w - scaleLeftPatch - scaleRightPatch,
scaleBottomPatch
); // bottom middle
}
if (ho === h) {
temp.image(
this.patches[3],
w - scaleRightPatch,
scaleBottomPatch - 1,
scaleTopPatch,
h - scaleTopPatch - scaleBottomPatch + 1
); // right middle -- i don't know why i need to offset by one, but this works.
temp.image(
this.patches[7],
0,
scaleTopPatch,
scaleLeftPatch,
h - scaleTopPatch - scaleBottomPatch
); // left middle
}
} else if (this.fillMode === "tile") {
let overflowPatch1, overflowPatch3, overflowPatch5, overflowPatch7;
if (overflowX > 0) {
overflowPatch1 = this.patches[1].get(0, 0, overflowX, this.topPatch)
overflowPatch5 = this.patches[5].get(0, 0, overflowX, this.bottomPatch)
}
if (overflowY > 0) {
overflowPatch3 = this.patches[3].get(0, 0, this.rightPatch, overflowY)
overflowPatch7 = this.patches[7].get(0, 0, this.leftPatch, overflowY)
}
for (let x = scaleLeftPatch; x < w - scaleRightPatch; x += scaleMiddleWidth) {
// top and bottom
if (overflowX > 0 && x >= w - scaleRightPatch - scaleMiddleWidth) {
temp.image(
overflowPatch1,
x,
0,
overflowPatch1.width * segmentScale,
scaleTopPatch
)
temp.image(
overflowPatch5,
x,
h - scaleBottomPatch,
overflowPatch5.width * segmentScale,
scaleBottomPatch
)
} else {
temp.image(
this.patches[1],
x,
0,
scaleMiddleWidth,
scaleTopPatch
)
temp.image(
this.patches[5],
x,
h - scaleBottomPatch,
scaleMiddleWidth,
scaleBottomPatch
)
}
}
for (let y = scaleTopPatch; y < h - scaleBottomPatch; y += scaleMiddleHeight) {
// right and left
if (overflowY > 0 && y >= h - scaleBottomPatch - scaleMiddleHeight) {
temp.image(
overflowPatch7,
0,
y,
scaleLeftPatch,
overflowPatch7.height * segmentScale
)
temp.image(
overflowPatch3,
w - scaleRightPatch,
y,
scaleRightPatch,
overflowPatch3.height * segmentScale
)
} else {
temp.image(
this.patches[7],
0,
y,
scaleLeftPatch,
scaleMiddleHeight
)
temp.image(
this.patches[3],
w - scaleRightPatch,
y,
scaleRightPatch,
scaleMiddleHeight
)
}
}
}
// draw the corners first (they're easy)
temp.image(
this.patches[0],
0,
0,
scaleLeftPatch,
scaleTopPatch
); // top left
temp.image(
this.patches[2],
w - scaleRightPatch,
0,
scaleRightPatch,
scaleTopPatch
); // top right
temp.image(
this.patches[4],
w - scaleRightPatch,
h - scaleBottomPatch,
scaleRightPatch,
scaleBottomPatch
); // bottom right
temp.image(
this.patches[6],
0,
h - scaleBottomPatch,
scaleLeftPatch,
scaleBottomPatch
); // bottom left
let out = temp.get();
temp.remove();
return out;
}
}