From 97870da0480ffad534cafd6267c108aef78254e7 Mon Sep 17 00:00:00 2001 From: cyantree Date: Sun, 26 Jul 2020 23:11:06 +0200 Subject: [PATCH] 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 --- src/js/game/shape_definition.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/js/game/shape_definition.js b/src/js/game/shape_definition.js index d94f335a..3a777f6f 100644 --- a/src/js/game/shape_definition.js +++ b/src/js/game/shape_definition.js @@ -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 }); }