tidy up script

This commit is contained in:
synth-ruiner 2018-03-03 12:40:19 +00:00
parent 90b33e2525
commit 0c8e2d2163
1 changed files with 36 additions and 28 deletions

View File

@ -10,24 +10,22 @@ $(document).ready(function() {
background: { background: {
red: 62, red: 62,
green: 43, green: 43,
blue: 32, blue: 32
}, },
tile: { tile: {
red: 208, red: 208,
green: 112, green: 112,
blue: 56, blue: 56
}, },
sprite: { sprite: {
red: 229, red: 229,
green: 92, green: 92,
blue: 68, blue: 68
} }
}; };
var room = []; var room = [];
var tiles = [];
var tileMatchThreshold = 64; var tileMatchThreshold = 64;
var croptions = { var croptions = {
@ -35,22 +33,20 @@ $(document).ready(function() {
viewport: {width: 128, height: 128, type: 'square'}, viewport: {width: 128, height: 128, type: 'square'},
boundary: {width: 256, height: 256}, boundary: {width: 256, height: 256},
zoom: 0 zoom: 0
} };
var $croppie = $('#croppie'); var $croppie = $('#croppie');
var croppie = $croppie.croppie(croptions);
$croppie.croppie(croptions);
function colourDifference(colour1, colour2) { function colourDifference(colour1, colour2) {
difference = {}; var difference = {};
_.each(['red', 'green', 'blue'], function(key) { _.each(['red', 'green', 'blue'], function(key) {
difference[key] = Math.abs(colour1[key] - colour2[key]); difference[key] = Math.abs(colour1[key] - colour2[key]);
}); });
// sum rgb differences return _.toInteger(_.sum(_.toArray(difference)));
return _.reduce(difference, function(sum, n) {
return sum + n;
}, 0);
} }
function zeroPad(input, desiredLength) { function zeroPad(input, desiredLength) {
@ -73,7 +69,7 @@ $(document).ready(function() {
return { return {
red: parseInt(rgb[0], 16), red: parseInt(rgb[0], 16),
green: parseInt(rgb[1], 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? // do palettes always go 0..n?
// will this cause problems if not? // will this cause problems if not?
_.each(palettes, function(palette, n) { _.each(palettes, function(palette, n) {
var thisPalette = {};
var name = ""; var name = "";
if (palette.match(/NAME (.+)\n/)) { if (palette.match(/NAME (.+)\n/)) {
@ -141,7 +136,7 @@ $(document).ready(function() {
id: n, id: n,
background: colours[0], background: colours[0],
tile: colours[1], tile: colours[1],
sprite: colours[2], sprite: colours[2]
} }
}); });
@ -156,11 +151,12 @@ $(document).ready(function() {
bitmap: _.chunk(_.times(64, _.constant(0)), 8), 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 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 ', ''); var name = tile.match(/TIL .*/)[0].replace('TIL ', '');
tile = tile.replace(/TIL .*\n/, ''); tile = tile.replace(/TIL .*\n/, '');
@ -230,7 +226,7 @@ $(document).ready(function() {
url: e.target.result, url: e.target.result,
zoom: 0 zoom: 0
}); });
} };
reader.readAsDataURL(input.files[0]); reader.readAsDataURL(input.files[0]);
} }
@ -253,7 +249,7 @@ $(document).ready(function() {
var pixel = { var pixel = {
red: _.clamp(rawData[i ] + brightnessAdjustment, 0, 255), red: _.clamp(rawData[i ] + brightnessAdjustment, 0, 255),
green: _.clamp(rawData[i + 1] + 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); 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 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 // 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); return _.isEqual(tile.bitmap, pseudoTile);
}); });
@ -332,7 +330,7 @@ $(document).ready(function() {
// sort by name in ascending order // sort by name in ascending order
// earlier names are preferable // earlier names are preferable
var bestMatch = _.first(_.sortBy(bestMatches, 'name')); bestMatch = _.first(_.sortBy(bestMatches, 'name'));
} }
if ( ! bestMatch || bestMatch.match < tileMatchThreshold) { if ( ! bestMatch || bestMatch.match < tileMatchThreshold) {
@ -401,9 +399,11 @@ $(document).ready(function() {
$croppie.on('update', renderDebounced); $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); $(this).val(0);
renderDebounced(); renderDebounced();
@ -413,7 +413,13 @@ $(document).ready(function() {
$('#brightness').trigger('dblclick'); $('#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(); handleBitsyGameData();
@ -465,7 +471,9 @@ $(document).ready(function() {
}); });
$('#save').on('click touchend', function() { $('#save').on('click touchend', function() {
var newGameData = $('textarea').val(); $textArea = $('textarea');
var newGameData = $textArea.val();
// handle rooms // handle rooms
@ -511,7 +519,7 @@ $(document).ready(function() {
newGameData = newGameData.replace(/(TIL.*(.*\n)*)SPR/g, '$1\n\n' + tileText + '\nSPR'); newGameData = newGameData.replace(/(TIL.*(.*\n)*)SPR/g, '$1\n\n' + tileText + '\nSPR');
// write // write
$('textarea').val(newGameData); $textArea.val(newGameData);
// todo: give the user some nice "yay! it worked!" kinda feedback? // todo: give the user some nice "yay! it worked!" kinda feedback?
}); });