squishjs 0.7.51 → 0.7.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/squish.js +24 -10
- package/src/squishHelpers/text.js +1 -1
- package/test/main.test.js +24 -0
package/package.json
CHANGED
package/src/squish.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
const assert = require('assert');
|
|
2
|
-
|
|
3
1
|
const InternalGameNode = require("./InternalGameNode");
|
|
4
2
|
const { CONSTRUCTOR_TO_TYPE, TYPE_TO_CONSTRUCTOR } = require('./node-types');
|
|
5
3
|
const SUBTYPE_MAPPINGS = require('./subtype-mappings');
|
|
@@ -43,19 +41,23 @@ for (const key in squishSpec) {
|
|
|
43
41
|
}
|
|
44
42
|
|
|
45
43
|
const unsquish = (squished) => {
|
|
46
|
-
|
|
44
|
+
if (squished[0] != 3) {
|
|
45
|
+
throw new Error('Squished[0] isnt 3.');
|
|
46
|
+
}
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
if (squished.length !== squished[1] + squished[2] + squished[3]) {
|
|
49
|
+
throw new Error('Bad length value');
|
|
50
|
+
}
|
|
49
51
|
|
|
50
|
-
let squishedIndex =
|
|
52
|
+
let squishedIndex = 5;
|
|
51
53
|
|
|
52
54
|
let constructedInternalNode = new InternalGameNode();
|
|
53
55
|
|
|
54
56
|
while(squishedIndex < squished.length) {
|
|
55
57
|
|
|
56
58
|
const subFrameType = squished[squishedIndex];
|
|
57
|
-
const subFrameLength = squished[squishedIndex + 1];
|
|
58
|
-
const subFrame = squished.slice(squishedIndex +
|
|
59
|
+
const subFrameLength = squished[squishedIndex + 1] + squished[squishedIndex + 2];
|
|
60
|
+
const subFrame = squished.slice(squishedIndex + 3, squishedIndex + subFrameLength);
|
|
59
61
|
|
|
60
62
|
if (!typeToSquishMap[subFrameType]) {
|
|
61
63
|
console.warn("Unknown sub frame type " + subFrameType);
|
|
@@ -72,7 +74,8 @@ const unsquish = (squished) => {
|
|
|
72
74
|
squishedIndex += subFrameLength;
|
|
73
75
|
}
|
|
74
76
|
|
|
75
|
-
const constructor = TYPE_TO_CONSTRUCTOR[squished[
|
|
77
|
+
const constructor = TYPE_TO_CONSTRUCTOR[squished[4]];
|
|
78
|
+
|
|
76
79
|
if (constructor) {
|
|
77
80
|
return new constructor({ node: constructedInternalNode });
|
|
78
81
|
}
|
|
@@ -112,7 +115,11 @@ const squish = (entity, scale = null) => {
|
|
|
112
115
|
const attr = internalNode[key];
|
|
113
116
|
if (attr !== undefined && attr !== null) {
|
|
114
117
|
const squished = squishSpec[key].squish(attr, scale, internalNode);
|
|
115
|
-
|
|
118
|
+
const totalLength = squished.length + 3;
|
|
119
|
+
const rightMost = Math.min(255, totalLength);
|
|
120
|
+
const leftMost = Math.min(255, Math.max(0, totalLength - 255));
|
|
121
|
+
|
|
122
|
+
squishedPieces.push([squishSpec[key]['type'], leftMost, rightMost, ...squished]);
|
|
116
123
|
}
|
|
117
124
|
}
|
|
118
125
|
}
|
|
@@ -125,7 +132,14 @@ const squish = (entity, scale = null) => {
|
|
|
125
132
|
}
|
|
126
133
|
|
|
127
134
|
const squished = squishedPieces.flat();
|
|
128
|
-
|
|
135
|
+
|
|
136
|
+
// length + 5 bytes for what we're inserting here - 3 for length, one for type (3), one for class code
|
|
137
|
+
const totalLength = squished.length + 5;
|
|
138
|
+
const rightMost = Math.min(255, totalLength);
|
|
139
|
+
const middle = Math.min(255, Math.max(0, totalLength - 255));
|
|
140
|
+
const leftMost = Math.min(255, Math.max(0, totalLength - 510));
|
|
141
|
+
|
|
142
|
+
return [3, leftMost, middle, rightMost, nodeClassCode, ...squished];
|
|
129
143
|
|
|
130
144
|
}
|
|
131
145
|
|
package/test/main.test.js
CHANGED
|
@@ -309,5 +309,29 @@ test("scaled asset node", () => {
|
|
|
309
309
|
assert(asset.size.y === .85 * 20);
|
|
310
310
|
});
|
|
311
311
|
|
|
312
|
+
test("big big text", () => {
|
|
313
|
+
const gameNode = new GameNode.Text({
|
|
314
|
+
textInfo: {
|
|
315
|
+
text: 'I am going to write a whoooooole lot of text like more than 64 characters which is quite a bit. Not like a tweet long but still long, you know?',
|
|
316
|
+
x: 4,
|
|
317
|
+
y: 20,
|
|
318
|
+
size: 5,
|
|
319
|
+
align: 'center',
|
|
320
|
+
color: COLORS.RED
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
const xScale = .6;
|
|
325
|
+
const yScale = .5;
|
|
326
|
+
|
|
327
|
+
const scaledTextSize = 5 * hypLength(xScale, yScale);
|
|
328
|
+
|
|
329
|
+
const squishedScaledNode = squish(gameNode, {x: xScale, y: yScale});
|
|
330
|
+
const unsquishedNode = unsquish(squishedScaledNode).node;
|
|
312
331
|
|
|
332
|
+
assert(unsquishedNode.text.x.toFixed(2) === (4 * xScale + Math.round((1 - xScale) * 100) / 2).toFixed(2));
|
|
333
|
+
assert(unsquishedNode.text.y.toFixed(2) === (20 * yScale + Math.round((1 - yScale) * 100) / 2).toFixed(2));
|
|
334
|
+
|
|
335
|
+
assert(unsquishedNode.text.size.toFixed(2) === scaledTextSize.toFixed(2));
|
|
336
|
+
});
|
|
313
337
|
|