From 0c8e2d2163db9314412f24be03670e25bcc75e09 Mon Sep 17 00:00:00 2001 From: synth-ruiner Date: Sat, 3 Mar 2018 12:40:19 +0000 Subject: [PATCH] tidy up script --- includes/script.js | 64 ++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/includes/script.js b/includes/script.js index ed3e599..91cba22 100644 --- a/includes/script.js +++ b/includes/script.js @@ -10,24 +10,22 @@ $(document).ready(function() { background: { red: 62, green: 43, - blue: 32, + blue: 32 }, tile: { red: 208, green: 112, - blue: 56, + blue: 56 }, sprite: { red: 229, green: 92, - blue: 68, + blue: 68 } }; var room = []; - var tiles = []; - var tileMatchThreshold = 64; var croptions = { @@ -35,22 +33,20 @@ $(document).ready(function() { viewport: {width: 128, height: 128, type: 'square'}, boundary: {width: 256, height: 256}, zoom: 0 - } + }; var $croppie = $('#croppie'); - var croppie = $croppie.croppie(croptions); + + $croppie.croppie(croptions); function colourDifference(colour1, colour2) { - difference = {}; + var difference = {}; _.each(['red', 'green', 'blue'], function(key) { difference[key] = Math.abs(colour1[key] - colour2[key]); }); - // sum rgb differences - return _.reduce(difference, function(sum, n) { - return sum + n; - }, 0); + return _.toInteger(_.sum(_.toArray(difference))); } function zeroPad(input, desiredLength) { @@ -73,7 +69,7 @@ $(document).ready(function() { return { red: parseInt(rgb[0], 16), green: parseInt(rgb[1], 16), - blue: parseInt(rgb[2], 16), + blue: parseInt(rgb[2], 16) }; } @@ -120,7 +116,6 @@ $(document).ready(function() { // do palettes always go 0..n? // will this cause problems if not? _.each(palettes, function(palette, n) { - var thisPalette = {}; var name = ""; if (palette.match(/NAME (.+)\n/)) { @@ -141,7 +136,7 @@ $(document).ready(function() { id: n, background: colours[0], tile: colours[1], - sprite: colours[2], + sprite: colours[2] } }); @@ -156,11 +151,12 @@ $(document).ready(function() { bitmap: _.chunk(_.times(64, _.constant(0)), 8), new: false // this could also be used to stop it from being added to the game data, wooo }); - - // todo: handle animated tiles properly instead of discarding the second animation frame - var tiles = input.match(/TIL (.*)\n([01]{8}\n){8}(>\n([01]{8}\n){8})?/g); // everything after > is an optional second animation frame - _.each(tiles, function(tile, i) { + // everything after > is an optional second animation frame + // todo: handle multiple animation frames! more than 2 are allowed (but not via the standard editor) + var tiles = input.match(/TIL (.*)\n([01]{8}\n){8}(>\n([01]{8}\n){8})?/g); + + _.each(tiles, function(tile) { var name = tile.match(/TIL .*/)[0].replace('TIL ', ''); tile = tile.replace(/TIL .*\n/, ''); @@ -230,7 +226,7 @@ $(document).ready(function() { url: e.target.result, zoom: 0 }); - } + }; reader.readAsDataURL(input.files[0]); } @@ -253,7 +249,7 @@ $(document).ready(function() { var pixel = { red: _.clamp(rawData[i ] + brightnessAdjustment, 0, 255), green: _.clamp(rawData[i + 1] + brightnessAdjustment, 0, 255), - blue: _.clamp(rawData[i + 2] + brightnessAdjustment, 0, 255), + blue: _.clamp(rawData[i + 2] + brightnessAdjustment, 0, 255) }; var targetColour = getClosestColour(pixel, palette); @@ -290,10 +286,12 @@ $(document).ready(function() { ); }); + var bestMatch; + // if we want to always create new tiles, don't bother trying to check matches 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) { + bestMatch = _.find(bitsyData.tiles, function(tile) { return _.isEqual(tile.bitmap, pseudoTile); }); @@ -332,7 +330,7 @@ $(document).ready(function() { // sort by name in ascending order // earlier names are preferable - var bestMatch = _.first(_.sortBy(bestMatches, 'name')); + bestMatch = _.first(_.sortBy(bestMatches, 'name')); } if ( ! bestMatch || bestMatch.match < tileMatchThreshold) { @@ -401,9 +399,11 @@ $(document).ready(function() { $croppie.on('update', renderDebounced); - $('#brightness').on('change', renderThrottled); + var $brightness = $('#brightness'); - $('#brightness').on('dblclick', function() { + $brightness.on('change', renderThrottled); + + $brightness.on('dblclick', function() { $(this).val(0); renderDebounced(); @@ -413,7 +413,13 @@ $(document).ready(function() { $('#brightness').trigger('dblclick'); }); - $('#bitsy-data').on('change blur keyup', handleBitsyGameData); + var $bitsyData = $('#bitsy-data'); + + $bitsyData.on('change blur keyup', handleBitsyGameData); + + $bitsyData.on('focus', function() { + $(this).select(); + }); handleBitsyGameData(); @@ -465,7 +471,9 @@ $(document).ready(function() { }); $('#save').on('click touchend', function() { - var newGameData = $('textarea').val(); + $textArea = $('textarea'); + + var newGameData = $textArea.val(); // handle rooms @@ -511,7 +519,7 @@ $(document).ready(function() { newGameData = newGameData.replace(/(TIL.*(.*\n)*)SPR/g, '$1\n\n' + tileText + '\nSPR'); // write - $('textarea').val(newGameData); + $textArea.val(newGameData); // todo: give the user some nice "yay! it worked!" kinda feedback? });