From e0420606d97950083bcf5d5e2307be260e2e1331 Mon Sep 17 00:00:00 2001 From: synth-ruiner Date: Fri, 29 Dec 2017 17:25:27 +0000 Subject: [PATCH] don't create duplicate tiles --- includes/script.js | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/includes/script.js b/includes/script.js index 78323ad..65d2241 100644 --- a/includes/script.js +++ b/includes/script.js @@ -289,7 +289,17 @@ $(document).ready(function() { var tilesForMatch = bitsyData.tiles; // if we want to always create new tiles, don't bother trying to check matches - if (tileMatchThreshold < 64) { + if (tileMatchThreshold === 64) { + // even if we want to "always create new tiles" we still don't want to create duplicates + var bestMatch = _.find(bitsyData.tiles, function(tile) { + return tile.bitmap === pseudoTile; + }); + + if (bestMatch) { + console.debug(pseudoTile === bestMatch.bitmap); + bestMatch.match = 64; + } + } else { _.each(tilesForMatch, function(tile) { tile.match = 0; @@ -313,18 +323,18 @@ $(document).ready(function() { tile.match /= 2; } }); + + // what if there are several equally good matches? + // find highest match amount and find all of them + var bestMatchAmount = _.last(_.sortBy(tilesForMatch, ['match'])).match; + var bestMatches = _.filter(tilesForMatch, {'match': bestMatchAmount}); + + // sort by name in ascending order + // earlier names are preferable + var bestMatch = _.first(_.sortBy(bestMatches, 'name')); } - // what if there are several equally good matches? - // find highest match amount and find all of them - var bestMatchAmount = _.last(_.sortBy(tilesForMatch, ['match'])).match; - var bestMatches = _.filter(tilesForMatch, {'match': bestMatchAmount}); - - // sort by name in ascending order - // earlier names are preferable - var bestMatch = _.first(_.sortBy(bestMatches, 'name')); - - if (tileMatchThreshold === 64 || bestMatch.match <= tileMatchThreshold) { + if ( ! bestMatch || bestMatch.match < tileMatchThreshold) { // turn pseudo-tile into a real tile and add it to the tile data var name = newTileName(); @@ -386,10 +396,11 @@ $(document).ready(function() { } var renderDebounced = _.debounce(render, 30); + var renderThrottled = _.throttle(render, 30); $croppie.on('update', renderDebounced); - $('#brightness').on('change', renderDebounced); + $('#brightness').on('change', renderThrottled); $('#brightness').on('dblclick', function() { $(this).val(0); @@ -441,7 +452,7 @@ $(document).ready(function() { tileMatchThreshold = newValue; - renderDebounced(); + renderThrottled(); }); $('#save').on('click touchend', function() {