don't create duplicate tiles

This commit is contained in:
synth-ruiner 2017-12-29 17:25:27 +00:00
parent 948fccb5a3
commit e0420606d9
1 changed files with 24 additions and 13 deletions

View File

@ -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() {