1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2024-10-27 20:34:29 +00:00

Fix layer handling in cutter (#352)

* Fix layer handling in cutter

* Remove unused variable in `cloneFilteredByquadrants()`

* Rework check in `isValidShortKeyInternal()` to being an early return

* Support empty layers in `isValidShortKeyInternal()` which aren't the topmost layer
This commit is contained in:
cyantree 2020-07-26 23:11:06 +02:00 committed by GitHub
parent ef574c0bfe
commit 97870da048
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -183,6 +183,11 @@ export class ShapeDefinition extends BasicSerializableObject {
*/
static isValidShortKeyInternal(key) {
const sourceLayers = key.split(":");
if (sourceLayers.length === 0 || sourceLayers.length > 4) {
return false;
}
let layers = [];
for (let i = 0; i < sourceLayers.length; ++i) {
const text = sourceLayers[i];
@ -221,15 +226,12 @@ export class ShapeDefinition extends BasicSerializableObject {
}
}
if (!anyFilled) {
// Empty layer
if (!anyFilled && i === sourceLayers.length - 1) {
// Topmost layer isn't allowed being empty
return false;
}
layers.push(quads);
}
if (layers.length === 0 || layers.length > 4) {
return false;
layers.push(quads);
}
return true;
@ -447,23 +449,23 @@ export class ShapeDefinition extends BasicSerializableObject {
*/
cloneFilteredByQuadrants(includeQuadrants) {
const newLayers = this.internalCloneLayers();
let lastNonEmptyLayer = -1;
for (let layerIndex = 0; layerIndex < newLayers.length; ++layerIndex) {
const quadrants = newLayers[layerIndex];
let anyContents = false;
for (let quadrantIndex = 0; quadrantIndex < 4; ++quadrantIndex) {
if (includeQuadrants.indexOf(quadrantIndex) < 0) {
quadrants[quadrantIndex] = null;
} else if (quadrants[quadrantIndex]) {
anyContents = true;
lastNonEmptyLayer = layerIndex;
}
}
// Check if the layer is entirely empty
if (!anyContents) {
newLayers.splice(layerIndex, 1);
layerIndex -= 1;
}
}
// Remove top most empty layers which aren't needed anymore
if (lastNonEmptyLayer !== newLayers.length - 1) {
newLayers.splice(lastNonEmptyLayer + 1);
}
return new ShapeDefinition({ layers: newLayers });
}