Comparing version 0.4.24 to 0.5.0
12
index.js
const { squish, unsquish } = require('./src/squish'); | ||
const GameNode = require('./src/GameNode'); | ||
const { gameNode, GameNode } = require('./src/GameNode'); | ||
const InternalGameNode = require('./src/InternalGameNode'); | ||
const Colors = require('./src/Colors'); | ||
const Game = require('./src/Game'); | ||
const Shapes = require('./src/Shapes'); | ||
const shapeUtils = require('./src/util/shapes'); | ||
@@ -8,4 +12,8 @@ module.exports = { | ||
unsquish, | ||
gameNode, | ||
Game, | ||
GameNode, | ||
Colors | ||
Colors, | ||
Shapes, | ||
ShapeUtils: shapeUtils | ||
}; |
{ | ||
"name": "squishjs", | ||
"version": "0.4.24", | ||
"version": "0.5.0", | ||
"description": "squish & unsquish stuff", | ||
"scripts": { | ||
"test": "node testRunner.js && node ./test/test.js" | ||
"test": "node testRunner.js" | ||
}, | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -13,2 +13,6 @@ const COLORS = { | ||
GREEN: [0, 255, 0, 255], | ||
HG_BLACK: [26, 26, 26, 255], | ||
HG_BLUE: [148, 210, 230, 255], | ||
HG_RED: [241, 112, 112, 255], | ||
HG_YELLOW: [255, 247, 143, 255], | ||
LAVENDER: [230, 230, 250, 255], | ||
@@ -15,0 +19,0 @@ MAGENTA: [255, 0, 255, 255], |
const listenable = require("./util/listenable"); | ||
const InternalGameNode = require('./InternalGameNode'); | ||
const Shapes = require('./Shapes'); | ||
let id = 0; | ||
const gameNode = (color, onClick, coordinates2d, border, fill, text, asset, playerId, effects, input) => { | ||
const node = new InternalGameNode(color, onClick, coordinates2d, border, fill, text, asset, playerId, effects, input); | ||
return listenable(node, node.onStateChange.bind(node)); | ||
}; | ||
class GameNode { | ||
constructor(color, onClick, pos, size, text, asset, playerId = 0) { | ||
this.id = id++; | ||
this.children = new Array(); | ||
this.color = color; | ||
this.handleClick = onClick; | ||
this.pos = pos; | ||
this.size = size; | ||
this.text = text; | ||
this.asset = asset; | ||
this.listeners = new Set(); | ||
this.playerId = Number(playerId); | ||
class Shape { | ||
constructor(color, shapeType, shapeInfo, playerId, onClick, effects, input) { | ||
this.node = gameNode(color, onClick, shapeInfo.coordinates2d, shapeInfo.border, shapeInfo.fill, null, null, playerId, effects, input); | ||
this.id = this.node.id; | ||
} | ||
addChild(node) { | ||
this.children.push(node); | ||
this.onStateChange(); | ||
addChild(child) { | ||
this.node.addChild(child); | ||
} | ||
addChildren(...nodes) { | ||
for (let nodeIndex = 0; nodeIndex < nodes.length; nodeIndex++) { | ||
this.addChild(nodes[nodeIndex]); | ||
} | ||
} | ||
removeChild(nodeId) { | ||
const removeIndex = this.children.findIndex(child => child.id == nodeId); | ||
removeIndex >= 0 && this.children.splice(removeIndex, 1); | ||
// hack to invoke update listener | ||
this.id = this.id; | ||
this.node.removeChild(nodeId); | ||
} | ||
addListener(listener) { | ||
this.listeners.add(listener); | ||
this.node.addListener(listener); | ||
} | ||
onStateChange() { | ||
for (const listener of this.listeners) { | ||
listener.handleStateChange(this); | ||
clearChildren(excludedNodeIds) { | ||
this.node.clearChildren(excludedNodeIds); | ||
} | ||
} | ||
class Text { | ||
constructor(textInfo, playerId, input) { | ||
this.node = gameNode(null, null, null, null, null, textInfo, null, playerId, null, input); | ||
this.id = this.node.id; | ||
} | ||
addChild(child) { | ||
this.node.addChild(child); | ||
} | ||
addChildren(...nodes) { | ||
for (let nodeIndex = 0; nodeIndex < nodes.length; nodeIndex++) { | ||
this.addChild(nodes[nodeIndex]); | ||
} | ||
} | ||
removeChild(nodeId) { | ||
this.node.removeChild(nodeId); | ||
} | ||
addListener(listener) { | ||
this.node.addListener(listener); | ||
} | ||
clearChildren(excludedNodeIds) { | ||
if (!excludedNodeIds) { | ||
this.children = new Array(); | ||
} else { | ||
const newChildren = this.children.filter(child => { | ||
return excludedNodeIds.includes(child.id); | ||
}); | ||
this.children = newChildren; | ||
this.node.clearChildren(excludedNodeIds); | ||
} | ||
} | ||
class Asset { | ||
constructor(onClick, coordinates2d, assetInfo, playerId) { | ||
this.node = gameNode(null, onClick, coordinates2d, null, null, null, assetInfo, playerId); | ||
this.id = this.node.id; | ||
} | ||
addChild(child) { | ||
this.node.addChild(child); | ||
} | ||
addChildren(...nodes) { | ||
for (let nodeIndex = 0; nodeIndex < nodes.length; nodeIndex++) { | ||
this.addChild(nodes[nodeIndex]); | ||
} | ||
} | ||
removeChild(nodeId) { | ||
this.node.removeChild(nodeId); | ||
} | ||
addListener(listener) { | ||
this.node.addListener(listener); | ||
} | ||
clearChildren(excludedNodeIds) { | ||
this.node.clearChildren(excludedNodeIds); | ||
} | ||
} | ||
const gameNode = (color, onClick, pos, size, text, asset, playerId) => { | ||
const node = new GameNode(color, onClick, pos, size, text, asset, playerId); | ||
return listenable(node, node.onStateChange.bind(node)); | ||
const GameNode = { | ||
Asset, | ||
Shape, | ||
Text | ||
}; | ||
module.exports = gameNode; | ||
// todo: fix this hack | ||
module.exports = {gameNode, GameNode}; |
@@ -1,2 +0,4 @@ | ||
const GameNode = require("./GameNode"); | ||
const InternalGameNode = require("./InternalGameNode"); | ||
const Colors = require('./Colors'); | ||
const assert = require('assert'); | ||
@@ -13,2 +15,8 @@ | ||
const ASSET_SUBTYPE = 48; | ||
const EFFECTS_SUBTYPE = 49; | ||
const ONCLICK_SUBTYPE = 50; | ||
const INPUT_SUBTYPE = 51; | ||
const COORDINATES_2D_SUBTYPE = 52; | ||
const FILL_SUBTYPE = 53; | ||
const BORDER_SUBTYPE = 54; | ||
@@ -55,2 +63,32 @@ const squishSpec = { | ||
}, | ||
coordinates2d: { | ||
type: COORDINATES_2D_SUBTYPE, | ||
squish: (p) => { | ||
const originalCoords = p.flat(); | ||
const squished = new Array(originalCoords.length * 2); | ||
for (const i in originalCoords) { | ||
Math.floor(p.x), Math.round(100 * (p.x - Math.floor(p.x))) | ||
squished[2 * i] = Math.floor(originalCoords[i]); | ||
squished[(2 * i) + 1] = Math.round(100 * (originalCoords[i] - Math.floor(originalCoords[i]))); | ||
} | ||
return squished; | ||
}, | ||
unsquish: (squished) => { | ||
const unsquished = new Array(squished.length / 2); | ||
for (let i = 0; i < squished.length; i += 2) { | ||
const value = squished[i] + (squished[i + 1] / 100); | ||
unsquished[i / 2] = value; | ||
} | ||
return unsquished; | ||
} | ||
}, | ||
fill: { | ||
type: FILL_SUBTYPE, | ||
squish: (c) => { | ||
return [c[0], c[1], c[2], c[3]]; | ||
}, | ||
unsquish: (squished) => { | ||
return [squished[0], squished[1], squished[2], squished[3]]; | ||
} | ||
}, | ||
size: { | ||
@@ -71,3 +109,5 @@ type: SIZE_SUBTYPE, | ||
squish: (t) => { | ||
const squishedText = new Array(t.text.length + 6); | ||
const align = t.align || 'left'; | ||
const squishedText = new Array(t.text.length + 10 + align.length); | ||
squishedText[0] = Math.floor(t.x); | ||
@@ -83,6 +123,19 @@ squishedText[1] = Math.round(100 * (t.x - Math.floor(t.x))); | ||
const textColor = t.color || Colors.BLACK; | ||
const squishedTextColor = squishSpec.color.squish(textColor); | ||
for (let i = 0; i < squishedTextColor.length; i++) { | ||
squishedText[6 + i] = squishedTextColor[i]; | ||
} | ||
squishedText[6 + squishedTextColor.length] = align.length; | ||
for (let i = 0; i < align.length; i++) { | ||
squishedText[6 + squishedTextColor.length + 1 + i] = align.charCodeAt(i); | ||
} | ||
for (let i = 0; i < t.text.length; i++) { | ||
squishedText[6 + i] = t.text.charCodeAt(i); | ||
squishedText[6 + squishedTextColor.length + align.length + 1 + i] = t.text.charCodeAt(i); | ||
} | ||
return squishedText; | ||
@@ -94,4 +147,7 @@ }, | ||
const textSize = squished[4] + squished[5] / 100; | ||
const textColor = squished.slice(6, 10); | ||
const textAlignLength = squished[10]; | ||
const align = String.fromCharCode.apply(null, squished.slice(11, 11 + textAlignLength)); | ||
const text = String.fromCharCode.apply(null, squished.slice(6)); | ||
const text = String.fromCharCode.apply(null, squished.slice(11 + textAlignLength)); | ||
@@ -102,3 +158,5 @@ return { | ||
text: text, | ||
size: textSize | ||
size: textSize, | ||
color: textColor, | ||
align | ||
}; | ||
@@ -152,2 +210,83 @@ } | ||
} | ||
}, | ||
effects: { | ||
type: EFFECTS_SUBTYPE, | ||
squish: (a) => { | ||
if (a['shadow']) { | ||
const assetKey = 'shadow'; | ||
let squishedLength = assetKey.length + 4; // + 4 for color | ||
if (a['shadow'].blur) { | ||
squishedLength += 2; | ||
} | ||
const squishedEffects = new Array(squishedLength); | ||
for (let i = 0; i < assetKey.length; i++) { | ||
squishedEffects[i] = assetKey.charCodeAt(i); | ||
} | ||
squishedEffects[assetKey.length] = a.shadow.color[0]; | ||
squishedEffects[assetKey.length + 1] = a.shadow.color[1]; | ||
squishedEffects[assetKey.length + 2] = a.shadow.color[2]; | ||
squishedEffects[assetKey.length + 3] = a.shadow.color[3]; | ||
if (a.shadow.blur) { | ||
squishedEffects[assetKey.length + 4] = Math.floor(a.shadow.blur / 10) | ||
squishedEffects[assetKey.length + 5] = a.shadow.blur % 10 | ||
} | ||
return squishedEffects; | ||
} | ||
}, | ||
unsquish: (squished) => { | ||
// 'shadow' is all (for now) | ||
const assetKey = String.fromCharCode.apply(null, squished.slice(0, 6)); | ||
const color = squished.slice(6, 10); | ||
let blur; | ||
if (squished.length > 10) { | ||
blur = squished[10] * 10 + squished[11]; | ||
} | ||
const unsquished = { | ||
[assetKey]: { | ||
color | ||
} | ||
}; | ||
if (blur) { | ||
unsquished[assetKey].blur = blur; | ||
} | ||
return unsquished; | ||
} | ||
}, | ||
handleClick: { | ||
type: ONCLICK_SUBTYPE, | ||
squish: (a) => { | ||
return a ? [1] : [0]; | ||
}, | ||
unsquish: (a) => { | ||
return a[0] === 1; | ||
} | ||
}, | ||
border: { | ||
type: BORDER_SUBTYPE, | ||
squish: (a) => { | ||
return [a]; | ||
}, | ||
unsquish: (s) => { | ||
return s[0]; | ||
} | ||
}, | ||
input: { | ||
type: INPUT_SUBTYPE, | ||
squish: (a) => { | ||
const squished = new Array(a.type.length); | ||
for (let i = 0; i < a.type.length; i++) { | ||
squished[i] = a.type.charCodeAt(i); | ||
} | ||
return squished; | ||
}, | ||
unsquish: (squished) => { | ||
return { | ||
type: String.fromCharCode.apply(null, squished) | ||
} | ||
} | ||
} | ||
@@ -160,6 +299,12 @@ }; | ||
'playerId', | ||
'coordinates2d', | ||
'fill', | ||
'pos', | ||
'size', | ||
'text', | ||
'asset' | ||
'asset', | ||
'effects', | ||
'border', | ||
'handleClick', | ||
'input' | ||
]; | ||
@@ -180,3 +325,3 @@ | ||
let constructedGameNode = GameNode(); | ||
let constructedGameNode = new InternalGameNode(); | ||
@@ -183,0 +328,0 @@ while(squishedIndex < squished.length) { |
@@ -0,0 +0,0 @@ const listenable = function(obj, onChange) { |
@@ -0,0 +0,0 @@ const { COLORS, randomColor } = require("../src/Colors"); |
@@ -31,6 +31,7 @@ // this came from: https://www.sohamkamani.com/blog/javascript/making-a-node-js-test-runner/ | ||
const filename = path.join(startPath,file); | ||
const filenamePieces = filename.split('/'); | ||
if (fs.lstatSync(filename).isDirectory()) { | ||
const values = searchForFiles(filename, filter); | ||
toReturn = toReturn.concat(values); | ||
} else if (filename.indexOf(filter) > -1) { | ||
} else if (filenamePieces[filenamePieces.length - 1].charAt(0) != '.' && filename.indexOf(filter) > -1) { | ||
toReturn.push(`./${filename}`); | ||
@@ -53,2 +54,2 @@ } | ||
run(); | ||
run(); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
28394
14
834
0