2017-12-22 17:22:08 +00:00
|
|
|
$(document).ready(function() {
|
2017-12-27 17:38:13 +00:00
|
|
|
// todo define things like 16x16, 128x128 etc. as constants?
|
|
|
|
// also script debounce/throttle times
|
2018-05-08 20:29:37 +00:00
|
|
|
var animationTime = 400; // defined in bitsy.js
|
2017-12-27 17:38:13 +00:00
|
|
|
|
2017-12-22 17:22:08 +00:00
|
|
|
var bitsyData = {};
|
|
|
|
|
|
|
|
var palette = {
|
2017-12-24 00:47:00 +00:00
|
|
|
id: 0,
|
2017-12-22 17:22:08 +00:00
|
|
|
background: {
|
|
|
|
red: 62,
|
|
|
|
green: 43,
|
2018-03-03 12:40:19 +00:00
|
|
|
blue: 32
|
2017-12-22 17:22:08 +00:00
|
|
|
},
|
|
|
|
tile: {
|
|
|
|
red: 208,
|
|
|
|
green: 112,
|
2018-03-03 12:40:19 +00:00
|
|
|
blue: 56
|
2017-12-22 17:22:08 +00:00
|
|
|
},
|
|
|
|
sprite: {
|
|
|
|
red: 229,
|
|
|
|
green: 92,
|
2018-03-03 12:40:19 +00:00
|
|
|
blue: 68
|
2017-12-22 17:22:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-23 00:57:52 +00:00
|
|
|
var room = [];
|
|
|
|
|
2018-02-27 23:18:51 +00:00
|
|
|
var tileMatchThreshold = 64;
|
2017-12-29 15:49:18 +00:00
|
|
|
|
2017-12-22 17:22:08 +00:00
|
|
|
var croptions = {
|
|
|
|
url: 'https://i.imgur.com/ThQZ94v.jpg',
|
|
|
|
viewport: {width: 128, height: 128, type: 'square'},
|
|
|
|
boundary: {width: 256, height: 256},
|
|
|
|
zoom: 0
|
2018-03-03 12:40:19 +00:00
|
|
|
};
|
2017-12-22 17:22:08 +00:00
|
|
|
|
|
|
|
var $croppie = $('#croppie');
|
2018-03-03 12:40:19 +00:00
|
|
|
|
|
|
|
$croppie.croppie(croptions);
|
2017-12-22 17:22:08 +00:00
|
|
|
|
|
|
|
function colourDifference(colour1, colour2) {
|
2018-03-03 12:40:19 +00:00
|
|
|
var difference = {};
|
2017-12-22 17:22:08 +00:00
|
|
|
|
|
|
|
_.each(['red', 'green', 'blue'], function(key) {
|
|
|
|
difference[key] = Math.abs(colour1[key] - colour2[key]);
|
|
|
|
});
|
|
|
|
|
2018-03-03 12:40:19 +00:00
|
|
|
return _.toInteger(_.sum(_.toArray(difference)));
|
2017-12-22 17:22:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-23 16:34:16 +00:00
|
|
|
function zeroPad(input, desiredLength) {
|
|
|
|
while (input.length < desiredLength) {
|
|
|
|
input = "0" + input;
|
|
|
|
}
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2017-12-22 17:22:08 +00:00
|
|
|
function colourToHex(colour) {
|
2017-12-23 16:34:16 +00:00
|
|
|
return '#' + zeroPad(Number(colour.red ).toString(16), 2)
|
|
|
|
+ zeroPad(Number(colour.green).toString(16), 2)
|
|
|
|
+ zeroPad(Number(colour.blue ).toString(16), 2);
|
2017-12-22 17:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hexToColour(hex) {
|
2017-12-24 18:14:16 +00:00
|
|
|
var rgb = hex.match(/[\da-f]{2}/gi);
|
2017-12-22 17:22:08 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
red: parseInt(rgb[0], 16),
|
|
|
|
green: parseInt(rgb[1], 16),
|
2018-03-03 12:40:19 +00:00
|
|
|
blue: parseInt(rgb[2], 16)
|
2017-12-22 17:22:08 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getClosestColour(initialColour, colourOptions) {
|
|
|
|
// ditch sprite colour as we're not using it atm
|
|
|
|
delete colourOptions.sprite;
|
|
|
|
|
|
|
|
_.each(palette, function(colour, name) {
|
2017-12-23 00:57:52 +00:00
|
|
|
colourOptions[name].name = name;
|
2017-12-22 17:22:08 +00:00
|
|
|
colourOptions[name].difference = colourDifference(initialColour, colour);
|
|
|
|
});
|
|
|
|
|
|
|
|
// lowest difference (closest) wins
|
|
|
|
return _.first(_.sortBy(colourOptions, 'difference'));
|
|
|
|
}
|
|
|
|
|
2017-12-29 15:49:18 +00:00
|
|
|
function newTileName() {
|
|
|
|
var tileNames = _.map(bitsyData.tiles, 'name');
|
|
|
|
|
|
|
|
var i = 1; // start with 1 as 0 is an implicit tile
|
|
|
|
|
|
|
|
while (tileNames.indexOf(i.toString(36)) > -1) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// base 36 = 0-9a-z
|
|
|
|
return i.toString(36);
|
|
|
|
}
|
|
|
|
|
2017-12-22 17:22:08 +00:00
|
|
|
function handleBitsyGameData() {
|
|
|
|
var input = $('#bitsy-data').val();
|
|
|
|
|
2018-03-03 09:21:28 +00:00
|
|
|
if ( ! input) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bitsyData = {};
|
|
|
|
|
2017-12-22 17:22:08 +00:00
|
|
|
// get palettes
|
2019-03-31 13:23:58 +00:00
|
|
|
var palettes = input.match(/PAL ([^\n]*)\n(NAME ([^\n]*)\n)?(([0-9,]+){3}\n){3,}/g);
|
2017-12-22 17:22:08 +00:00
|
|
|
|
|
|
|
bitsyData.palettes = {};
|
|
|
|
|
2017-12-27 20:39:35 +00:00
|
|
|
// do palettes always go 0..n?
|
|
|
|
// will this cause problems if not?
|
2017-12-22 17:22:08 +00:00
|
|
|
_.each(palettes, function(palette, n) {
|
|
|
|
var name = "";
|
|
|
|
|
|
|
|
if (palette.match(/NAME (.+)\n/)) {
|
|
|
|
name = palette.match(/NAME (.+)\n/)[0].replace('NAME ', '');
|
|
|
|
} else if (palette.match(/PAL (\d+)\n/)) {
|
|
|
|
name = palette.match(/PAL (\d+)\n/)[0].replace("PAL", "palette");
|
|
|
|
}
|
|
|
|
|
|
|
|
var colours = palette.match(/\d+,\d+,\d+/g);
|
|
|
|
|
|
|
|
colours = _.map(colours, function(colour) {
|
|
|
|
var rgb = colour.split(',');
|
|
|
|
|
|
|
|
return {red: rgb[0], green: rgb[1], blue: rgb[2]};
|
|
|
|
});
|
|
|
|
|
|
|
|
bitsyData.palettes[name] = {
|
2017-12-24 00:47:00 +00:00
|
|
|
id: n,
|
2017-12-22 17:22:08 +00:00
|
|
|
background: colours[0],
|
|
|
|
tile: colours[1],
|
2018-03-03 12:40:19 +00:00
|
|
|
sprite: colours[2]
|
2017-12-22 17:22:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-12-23 00:57:52 +00:00
|
|
|
// get tiles
|
2017-12-24 18:14:16 +00:00
|
|
|
|
2017-12-29 15:49:18 +00:00
|
|
|
bitsyData.tiles = [];
|
2017-12-23 00:57:52 +00:00
|
|
|
|
2017-12-24 18:12:46 +00:00
|
|
|
// tile 0 (background colour only) is implicit in bitsy rather than being stored in the game data
|
|
|
|
// so, make our own version
|
2017-12-29 15:49:18 +00:00
|
|
|
bitsyData.tiles.push({
|
|
|
|
name: "0",
|
|
|
|
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
|
|
|
|
});
|
2017-12-23 00:57:52 +00:00
|
|
|
|
2018-03-03 12:40:19 +00:00
|
|
|
// 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) {
|
2017-12-24 18:14:16 +00:00
|
|
|
var name = tile.match(/TIL .*/)[0].replace('TIL ', '');
|
|
|
|
|
|
|
|
tile = tile.replace(/TIL .*\n/, '');
|
2017-12-23 00:57:52 +00:00
|
|
|
|
2017-12-29 15:49:18 +00:00
|
|
|
var bitmap = _.map(tile.match(/[01]/g), _.toInteger);
|
|
|
|
|
|
|
|
var newTile = {
|
|
|
|
name: name,
|
|
|
|
new: false
|
|
|
|
};
|
2017-12-23 00:57:52 +00:00
|
|
|
|
2017-12-29 15:49:18 +00:00
|
|
|
// todo make this agnostic? i.e. tile.frames = _.chunk(bitmap, 64)
|
2017-12-27 17:38:13 +00:00
|
|
|
|
|
|
|
if (bitmap.length === 64) { // normal tile
|
|
|
|
newTile.bitmap = _.chunk(bitmap, 8);
|
|
|
|
} else if (bitmap.length === 128) { // animated tile
|
|
|
|
newTile.bitmap = _.chunk(_.take( bitmap, 64), 8);
|
|
|
|
newTile.secondAnimationFrame = _.chunk(_.takeRight(bitmap, 64), 8);
|
|
|
|
}
|
|
|
|
|
2017-12-29 15:49:18 +00:00
|
|
|
bitsyData.tiles.push(newTile);
|
2017-12-24 18:14:16 +00:00
|
|
|
});
|
2017-12-23 00:57:52 +00:00
|
|
|
|
2017-12-27 20:39:35 +00:00
|
|
|
if (_.find(bitsyData.palettes, {'id': palette.id})) {
|
|
|
|
// user has already selected a palette, leave it be
|
|
|
|
|
2017-12-27 20:44:16 +00:00
|
|
|
// in case this is the first run:
|
|
|
|
palette = _.find(bitsyData.palettes, {'id': palette.id})
|
|
|
|
|
2017-12-27 20:39:35 +00:00
|
|
|
// if we just set the palette to the newly imported palette with the same ID,
|
|
|
|
// we will lose any changes the user has made to the palettes
|
|
|
|
// is this a big issue considering that the palettes cannot be currently saved anyway?
|
|
|
|
} else {
|
|
|
|
// set palette to first imported palette and redraw
|
|
|
|
palette = _.first(_.sortBy(bitsyData.palettes, 'id'));
|
|
|
|
}
|
2017-12-22 17:22:08 +00:00
|
|
|
|
2017-12-29 15:49:18 +00:00
|
|
|
renderDebounced();
|
2017-12-22 17:22:08 +00:00
|
|
|
|
|
|
|
// update palette picker
|
|
|
|
$('tr.palette').remove();
|
|
|
|
|
|
|
|
_.each(bitsyData.palettes, function(palette, name) {
|
|
|
|
$('#palette tbody').append(
|
|
|
|
'<tr class="palette">'
|
2017-12-24 00:47:00 +00:00
|
|
|
+ '<td>'
|
|
|
|
+ '<input type="radio" name="palette" id="palette-' + name + '">'
|
|
|
|
+ '<input type="hidden" name="id" value="' + palette.id + '">'
|
|
|
|
+ '</td>'
|
2017-12-22 19:45:33 +00:00
|
|
|
+ '<td><label for="palette-' + name + '">' + name + '</label></td>'
|
2017-12-22 17:22:08 +00:00
|
|
|
+ '<td><input type="color" name="background" value="' + colourToHex(palette.background) + '"></td>'
|
|
|
|
+ '<td><input type="color" name="tile" value="' + colourToHex(palette.tile) + '"></td>'
|
2017-12-22 19:45:33 +00:00
|
|
|
+ '<td><input type="color" name="sprite" value="' + colourToHex(palette.sprite) + '" disabled></td>'
|
2017-12-22 17:22:08 +00:00
|
|
|
+ '</tr>'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-12-27 20:39:35 +00:00
|
|
|
$('input[name="id"][value="' + palette.id + '"]').siblings(':radio').trigger('click');
|
2017-12-22 17:22:08 +00:00
|
|
|
}
|
|
|
|
|
2018-03-03 14:40:57 +00:00
|
|
|
function readFile(input, callback) {
|
2017-12-22 17:22:08 +00:00
|
|
|
if (input.files && input.files[0]) {
|
|
|
|
var reader = new FileReader();
|
|
|
|
|
2018-03-03 14:40:57 +00:00
|
|
|
reader.onload = callback;
|
2017-12-22 17:22:08 +00:00
|
|
|
|
|
|
|
reader.readAsDataURL(input.files[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 14:40:57 +00:00
|
|
|
function readTextFile(input, callback) {
|
|
|
|
if (input.files && input.files[0]) {
|
|
|
|
var reader = new FileReader();
|
|
|
|
|
|
|
|
reader.onload = callback;
|
|
|
|
|
|
|
|
reader.readAsText(input.files[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-29 15:49:18 +00:00
|
|
|
function render() {
|
2017-12-22 17:22:08 +00:00
|
|
|
$croppie.croppie('result', {
|
|
|
|
type: 'rawcanvas',
|
|
|
|
size: 'viewport'
|
|
|
|
}).then(function (result) {
|
2017-12-23 00:57:52 +00:00
|
|
|
var imageData = result.getContext('2d').getImageData(0, 0, 128, 128);
|
|
|
|
var rawData = imageData.data;
|
|
|
|
var monochrome = [];
|
2017-12-22 17:22:08 +00:00
|
|
|
|
2017-12-24 18:14:16 +00:00
|
|
|
var brightnessAdjustment = parseFloat($('#brightness').val());
|
2017-12-22 17:22:08 +00:00
|
|
|
|
|
|
|
// for each pixel
|
|
|
|
for (var i = 0; i < rawData.length; i += 4) {
|
|
|
|
// this brightness adjustment is pretty crude but whatever
|
|
|
|
var pixel = {
|
2017-12-27 17:03:27 +00:00
|
|
|
red: _.clamp(rawData[i ] + brightnessAdjustment, 0, 255),
|
|
|
|
green: _.clamp(rawData[i + 1] + brightnessAdjustment, 0, 255),
|
2018-03-03 12:40:19 +00:00
|
|
|
blue: _.clamp(rawData[i + 2] + brightnessAdjustment, 0, 255)
|
2017-12-22 17:22:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var targetColour = getClosestColour(pixel, palette);
|
|
|
|
|
2017-12-29 15:49:18 +00:00
|
|
|
if (targetColour.name === "background") {
|
|
|
|
monochrome.push(0);
|
2017-12-23 00:57:52 +00:00
|
|
|
} else { // tile
|
2017-12-29 15:49:18 +00:00
|
|
|
monochrome.push(1)
|
2017-12-23 00:57:52 +00:00
|
|
|
}
|
|
|
|
|
2017-12-22 17:22:08 +00:00
|
|
|
rawData[i ] = targetColour.red;
|
|
|
|
rawData[i + 1] = targetColour.green;
|
|
|
|
rawData[i + 2] = targetColour.blue;
|
2017-12-27 17:38:13 +00:00
|
|
|
rawData[i + 3] = 255; // alpha
|
2017-12-22 17:22:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-23 00:57:52 +00:00
|
|
|
// split monochrome bitmap into equal chunks for easier x:y access
|
|
|
|
monochrome = _.chunk(monochrome, 128);
|
|
|
|
|
|
|
|
document.getElementById('preview').getContext('2d').putImageData(imageData, 0, 0);
|
|
|
|
|
|
|
|
// tiled output
|
|
|
|
|
2017-12-23 20:20:55 +00:00
|
|
|
room = [];
|
|
|
|
|
2017-12-23 00:57:52 +00:00
|
|
|
_.times(16, function(tileY) {
|
|
|
|
_.times(16, function(tileX) {
|
|
|
|
// make pseudo-tile from monochrome bitmap
|
|
|
|
var pseudoTile = [];
|
|
|
|
|
|
|
|
_.times(8, function(y) {
|
|
|
|
pseudoTile.push(
|
|
|
|
_.slice(monochrome[(tileY * 8) + y], (tileX * 8), (tileX * 8) + 8)
|
|
|
|
);
|
2017-12-29 15:49:18 +00:00
|
|
|
});
|
2017-12-23 00:57:52 +00:00
|
|
|
|
2018-03-03 12:40:19 +00:00
|
|
|
var bestMatch;
|
|
|
|
|
2017-12-29 15:49:18 +00:00
|
|
|
// if we want to always create new tiles, don't bother trying to check matches
|
2017-12-29 17:25:27 +00:00
|
|
|
if (tileMatchThreshold === 64) {
|
|
|
|
// even if we want to "always create new tiles" we still don't want to create duplicates
|
2018-03-03 12:40:19 +00:00
|
|
|
bestMatch = _.find(bitsyData.tiles, function(tile) {
|
2018-01-26 19:03:41 +00:00
|
|
|
return _.isEqual(tile.bitmap, pseudoTile);
|
2017-12-29 17:25:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (bestMatch) {
|
|
|
|
bestMatch.match = 64;
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-26 19:11:41 +00:00
|
|
|
_.each(bitsyData.tiles, function(tile) {
|
2017-12-29 15:49:18 +00:00
|
|
|
tile.match = 0;
|
2017-12-27 17:38:13 +00:00
|
|
|
|
2017-12-29 15:49:18 +00:00
|
|
|
_.each(tile.bitmap, function(row, y) {
|
2017-12-27 17:38:13 +00:00
|
|
|
_.each(row, function(pixel, x) {
|
|
|
|
if (parseInt(pixel) === parseInt(pseudoTile[y][x])) {
|
|
|
|
tile.match++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-29 15:49:18 +00:00
|
|
|
if (tile.secondAnimationFrame) {
|
|
|
|
_.each(tile.secondAnimationFrame, function(row, y) {
|
|
|
|
_.each(row, function(pixel, x) {
|
|
|
|
if (parseInt(pixel) === parseInt(pseudoTile[y][x])) {
|
|
|
|
tile.match++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
tile.match /= 2;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-12-29 17:25:27 +00:00
|
|
|
// what if there are several equally good matches?
|
|
|
|
// find highest match amount and find all of them
|
2018-01-26 19:11:41 +00:00
|
|
|
var bestMatchAmount = _.last(_.sortBy(bitsyData.tiles, ['match'])).match;
|
|
|
|
var bestMatches = _.filter(bitsyData.tiles, {'match': bestMatchAmount});
|
2017-12-23 00:57:52 +00:00
|
|
|
|
2017-12-29 17:25:27 +00:00
|
|
|
// sort by name in ascending order
|
|
|
|
// earlier names are preferable
|
2018-03-03 12:40:19 +00:00
|
|
|
bestMatch = _.first(_.sortBy(bestMatches, 'name'));
|
2017-12-29 17:25:27 +00:00
|
|
|
}
|
2017-12-23 00:57:52 +00:00
|
|
|
|
2017-12-29 17:25:27 +00:00
|
|
|
if ( ! bestMatch || bestMatch.match < tileMatchThreshold) {
|
2017-12-23 00:57:52 +00:00
|
|
|
// turn pseudo-tile into a real tile and add it to the tile data
|
2017-12-29 15:49:18 +00:00
|
|
|
|
|
|
|
var name = newTileName();
|
|
|
|
|
|
|
|
bitsyData.tiles.push({
|
|
|
|
name: name,
|
|
|
|
bitmap: pseudoTile,
|
|
|
|
new: true
|
|
|
|
});
|
|
|
|
|
|
|
|
room.push(name);
|
|
|
|
|
|
|
|
// issue with this approach:
|
|
|
|
// what if a tile we add late in the loop is a better match for an earlier "good enough" match?
|
|
|
|
// this would also cause different results if the user were to add the same room several times
|
|
|
|
// we could keep iterating until the room no longer changes
|
|
|
|
} else {
|
|
|
|
room.push(bestMatch.name);
|
|
|
|
}
|
2017-12-23 00:57:52 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
room = _.chunk(room, 16);
|
|
|
|
|
|
|
|
// write room to output
|
2017-12-29 15:49:18 +00:00
|
|
|
|
2017-12-23 00:57:52 +00:00
|
|
|
imageData = document.getElementById("room-output").getContext('2d').getImageData(0, 0, 128, 128);
|
|
|
|
rawData = imageData.data;
|
|
|
|
|
|
|
|
_.each(room, function(row, tileY) {
|
|
|
|
_.each(row, function(tileName, tileX) {
|
2017-12-29 15:49:18 +00:00
|
|
|
var tile = _.find(bitsyData.tiles, {'name' : tileName});
|
|
|
|
|
|
|
|
_.each(tile.bitmap, function(row, y) {
|
|
|
|
_.each(row, function(pixel, x) {
|
|
|
|
var position = (((tileY * 8) + y) * 128) + ((tileX * 8) + x);
|
|
|
|
|
|
|
|
position *= 4; // 4 values (rgba) per pixel
|
|
|
|
|
|
|
|
var pixelColour = {};
|
|
|
|
|
|
|
|
switch(parseInt(pixel)) {
|
|
|
|
case 0: pixelColour = palette.background; break;
|
|
|
|
case 1: pixelColour = palette.tile; break;
|
|
|
|
default: console.log("error");
|
|
|
|
}
|
|
|
|
|
|
|
|
rawData[position ] = pixelColour.red;
|
|
|
|
rawData[position + 1] = pixelColour.green;
|
|
|
|
rawData[position + 2] = pixelColour.blue;
|
|
|
|
rawData[position + 3] = 255;
|
2017-12-23 00:57:52 +00:00
|
|
|
});
|
2017-12-29 15:49:18 +00:00
|
|
|
});
|
2017-12-23 00:57:52 +00:00
|
|
|
});
|
|
|
|
});
|
2017-12-23 22:33:31 +00:00
|
|
|
|
2017-12-23 00:57:52 +00:00
|
|
|
document.getElementById('room-output').getContext('2d').putImageData(imageData, 0, 0);
|
2017-12-22 17:22:08 +00:00
|
|
|
});
|
2017-12-29 15:49:18 +00:00
|
|
|
}
|
2017-12-22 17:22:08 +00:00
|
|
|
|
2017-12-29 15:49:18 +00:00
|
|
|
var renderDebounced = _.debounce(render, 30);
|
2017-12-29 17:25:27 +00:00
|
|
|
var renderThrottled = _.throttle(render, 30);
|
2017-12-22 17:22:08 +00:00
|
|
|
|
2017-12-29 15:49:18 +00:00
|
|
|
$croppie.on('update', renderDebounced);
|
|
|
|
|
2018-03-03 12:40:19 +00:00
|
|
|
var $brightness = $('#brightness');
|
2017-12-22 17:22:08 +00:00
|
|
|
|
2018-03-03 12:40:19 +00:00
|
|
|
$brightness.on('change', renderThrottled);
|
|
|
|
|
|
|
|
$brightness.on('dblclick', function() {
|
2017-12-22 17:22:08 +00:00
|
|
|
$(this).val(0);
|
2017-12-29 15:49:18 +00:00
|
|
|
|
|
|
|
renderDebounced();
|
2017-12-22 17:22:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$('label[for="brightness"]').on('click touchdown', function() {
|
|
|
|
$('#brightness').trigger('dblclick');
|
|
|
|
});
|
|
|
|
|
2018-03-03 12:40:19 +00:00
|
|
|
var $bitsyData = $('#bitsy-data');
|
|
|
|
|
|
|
|
$bitsyData.on('change blur keyup', handleBitsyGameData);
|
|
|
|
|
|
|
|
$bitsyData.on('focus', function() {
|
|
|
|
$(this).select();
|
|
|
|
});
|
2017-12-22 17:22:08 +00:00
|
|
|
|
2017-12-22 19:45:33 +00:00
|
|
|
handleBitsyGameData();
|
|
|
|
|
2017-12-22 17:22:08 +00:00
|
|
|
$('#imageUpload').on('change', function () {
|
2018-03-03 14:40:57 +00:00
|
|
|
readFile(this, function (e) {
|
|
|
|
$croppie.croppie('bind', {
|
|
|
|
url: e.target.result,
|
|
|
|
zoom: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$('input.game-data').on('change', function() {
|
|
|
|
readTextFile(this, function (e) {
|
|
|
|
$bitsyData.val(e.target.result);
|
|
|
|
handleBitsyGameData();
|
|
|
|
});
|
2017-12-22 17:22:08 +00:00
|
|
|
});
|
|
|
|
|
2017-12-24 00:47:00 +00:00
|
|
|
// these inputs get added and removed from the DOM so the event handler needs to be on the document
|
2017-12-23 22:33:31 +00:00
|
|
|
$(document).on('change', '#palette input', function() {
|
2017-12-24 20:36:39 +00:00
|
|
|
var id = parseInt($(this).closest('.palette').find('input[name="id"]').val());
|
|
|
|
|
2017-12-22 17:22:08 +00:00
|
|
|
// if this is a colour input, update the palette
|
|
|
|
if ($(this).attr('type') === 'color') {
|
2017-12-24 20:36:39 +00:00
|
|
|
if (id === palette.id) {
|
|
|
|
palette[$(this).attr('name')] = hexToColour($(this).val());
|
|
|
|
}
|
2017-12-22 17:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if this is a radio button, pick this palette
|
2017-12-22 19:45:33 +00:00
|
|
|
if ($(this).attr('type') === 'radio') {
|
2017-12-24 20:36:39 +00:00
|
|
|
palette.id = id;
|
2017-12-24 00:47:00 +00:00
|
|
|
palette.background = hexToColour($(this).closest('.palette').find('input[name="background"]').val());
|
|
|
|
palette.tile = hexToColour($(this).closest('.palette').find('input[name="tile"]' ).val());
|
|
|
|
// sprite colour is not currently used
|
2017-12-22 19:45:33 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 15:49:18 +00:00
|
|
|
renderDebounced();
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('change', '#threshold', function() {
|
|
|
|
var newValue = parseInt($(this).val());
|
|
|
|
|
|
|
|
if (newValue < tileMatchThreshold) {
|
|
|
|
// set tiles back to default
|
|
|
|
bitsyData.tiles = _.filter(bitsyData.tiles, ['new', false]);
|
|
|
|
}
|
|
|
|
|
|
|
|
tileMatchThreshold = newValue;
|
|
|
|
|
2017-12-29 17:25:27 +00:00
|
|
|
renderThrottled();
|
2017-12-22 17:22:08 +00:00
|
|
|
});
|
2017-12-24 00:47:00 +00:00
|
|
|
|
2018-01-26 19:26:53 +00:00
|
|
|
$('#never').on('click touchend', function() {
|
|
|
|
$('#threshold').val(0).change();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#always').on('click touchend', function() {
|
|
|
|
$('#threshold').val(64).change();
|
|
|
|
});
|
|
|
|
|
2017-12-24 00:47:00 +00:00
|
|
|
$('#save').on('click touchend', function() {
|
2018-03-03 12:40:19 +00:00
|
|
|
$textArea = $('textarea');
|
|
|
|
|
|
|
|
var newGameData = $textArea.val();
|
2017-12-24 00:47:00 +00:00
|
|
|
|
2017-12-29 19:13:28 +00:00
|
|
|
// handle rooms
|
|
|
|
|
2017-12-24 00:47:00 +00:00
|
|
|
// need to import IDs so we don't give the new room a conflicting ID
|
2018-05-08 20:30:26 +00:00
|
|
|
var roomIds = newGameData.match(/ROOM \d+\n/g);
|
2017-12-24 00:47:00 +00:00
|
|
|
|
2018-05-08 20:30:26 +00:00
|
|
|
roomIds = _.map(roomIds, function(roomId) {
|
|
|
|
return parseInt(roomId.replace(/[^\d]+/g, ""));
|
|
|
|
});
|
|
|
|
|
|
|
|
var newRoomId = _.max(roomIds) + 1;
|
2017-12-24 00:47:00 +00:00
|
|
|
|
2017-12-24 12:09:54 +00:00
|
|
|
var newRoomName = $('#roomName').val();
|
|
|
|
// remove invalid chars? what's invalid? newlines? are those possible?
|
|
|
|
|
2017-12-24 00:47:00 +00:00
|
|
|
var newRoom = "ROOM " + newRoomId + "\n";
|
|
|
|
|
|
|
|
_.each(room, function(row) {
|
|
|
|
newRoom += _.toString(row) + "\n";
|
|
|
|
});
|
|
|
|
|
2017-12-24 12:09:54 +00:00
|
|
|
if (newRoomName) {
|
|
|
|
newRoom += "NAME " + newRoomName + "\n";
|
|
|
|
}
|
|
|
|
|
2017-12-24 00:47:00 +00:00
|
|
|
newRoom += "PAL " + palette.id + "\n";
|
|
|
|
|
2017-12-29 19:13:28 +00:00
|
|
|
newGameData = newGameData.replace(/(ROOM .*\n(.*\n)*PAL .*)/g, '$1\n\n' + newRoom);
|
|
|
|
|
|
|
|
// handle tiles
|
|
|
|
|
|
|
|
var newTiles = _.filter(bitsyData.tiles, 'new');
|
|
|
|
var tileText = "";
|
|
|
|
|
2018-04-28 13:17:29 +00:00
|
|
|
_.each(newTiles, function(tile, n) {
|
2017-12-29 19:13:28 +00:00
|
|
|
tileText += "TIL " + tile.name + "\n"; //again, rename tile name to id...
|
|
|
|
|
|
|
|
_.each(tile.bitmap, function(row) {
|
|
|
|
tileText += row.join('') + "\n";
|
|
|
|
});
|
|
|
|
|
2018-04-28 13:17:29 +00:00
|
|
|
tileText += "NAME " + newRoomName + " " + (n + 1) + "\n";
|
|
|
|
|
2017-12-29 19:13:28 +00:00
|
|
|
// don't need to worry about animation right now
|
|
|
|
|
|
|
|
tileText += "\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
newGameData = newGameData.replace(/(TIL.*(.*\n)*)SPR/g, '$1\n\n' + tileText + '\nSPR');
|
|
|
|
|
2017-12-24 00:47:00 +00:00
|
|
|
// write
|
2018-03-03 12:40:19 +00:00
|
|
|
$textArea.val(newGameData);
|
2017-12-29 19:13:28 +00:00
|
|
|
|
2019-03-31 13:23:58 +00:00
|
|
|
handleBitsyGameData();
|
|
|
|
|
2017-12-29 19:13:28 +00:00
|
|
|
// todo: give the user some nice "yay! it worked!" kinda feedback?
|
2017-12-24 00:47:00 +00:00
|
|
|
});
|
2017-12-24 18:14:16 +00:00
|
|
|
});
|