var canvasContainer = document.querySelector("#canvasContainer");
var panzoomContainer = document.querySelector("#panzoomContainer");
var canvas = document.querySelector("#canvas");
var overlayCanvas = document.querySelector("#overlayCanvas");
var selectionCanvas = document.querySelector("#selectionCanvas");
var ctx = canvas.getContext("2d");
var overlayCtx = overlayCanvas.getContext("2d");
var selectionCtx = selectionCanvas.getContext("2d");
// var canvasContextImageData = ctx.createImageData(1,1);
// var imageData = canvasContextImageData.data;
var blockSelector = document.querySelector("#blocks");
ctx.msImageSmoothingEnabled = false;
ctx.mozImageSmoothingEnabled = false;
ctx.msImageSmoothingEnabled = false;
ctx.imageSmoothingEnabled = false;
overlayCtx.msImageSmoothingEnabled = false;
overlayCtx.mozImageSmoothingEnabled = false;
overlayCtx.msImageSmoothingEnabled = false;
overlayCtx.imageSmoothingEnabled = false;
selectionCtx.msImageSmoothingEnabled = false;
selectionCtx.mozImageSmoothingEnabled = false;
selectionCtx.msImageSmoothingEnabled = false;
selectionCtx.imageSmoothingEnabled = false;
var file;
var world;
var selectionX = 0;
var selectionY = 0;
var panzoom = $("#panzoomContainer").panzoom({
cursor: "default",
maxScale: 20,
increment: 0.3,
});
$("#status").html("Checking File APIs...");
// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
$("#file").css("visibility", "visible");
$("#file").on('change', fileNameChanged);
$("#status").html("Please choose a Terraria .wld file.");
} else {
$("#status").html("The File APIs are not fully supported in this browser.");
}
resizeCanvases();
var options = [];
addTileSelectOptions();
addItemSelectOptions();
addWallSelectOptions();
sortAndAddSelectOptions();
addSetListItems();
function addSetListItems() {
for(var i = 0; i < sets.length; i++) {
var set = sets[i];
for(var j = 0; j < set.Entries.length; j++) {
var entry = set.Entries[j];
if(entry.U || entry.V) {
var tileInfo = getTileInfoFrom(entry.Id, entry.U, entry.V);
if(tileInfo) {
set.Entries[j] = tileInfo;
}
}
}
$("#setList").append('
' + set.Name + '');
}
}
function highlightSet(setIndex) {
var set = sets[setIndex];
highlightInfos(set.Entries);
}
function sortAndAddSelectOptions() {
options.sort(compareOptions);
for(var i = 0; i < options.length; i++) {
var option = options[i];
blockSelector.add(option);
}
}
function addTileSelectOptions() {
for(var i = 0; i < settings.Tiles.length; i++) {
var tile = settings.Tiles[i];
tile.isTile = true;
var option = document.createElement("option");
option.text = tile.Name;
option.value = i;
options.push(option);
if(tile.Frames) {
for(var frameIndex = 0; frameIndex < tile.Frames.length; frameIndex++) {
var frame = tile.Frames[frameIndex];
frame.isTile = true;
option = document.createElement("option");
option.text = tile.Name;
option.value = i;
var attribute = document.createAttribute("data-u");
attribute.value = frame.U;
option.setAttributeNode(attribute);
attribute = document.createAttribute("data-v");
attribute.value = frame.V;
option.setAttributeNode(attribute);
if(frame.Name) {
option.text = `${option.text} - ${frame.Name}`;
}
if(frame.Variety) {
option.text = `${option.text} - ${frame.Variety}`;
}
option.text += " (Tile)";
options.push(option);
}
}
}
}
function addItemSelectOptions() {
for(var i = 0; i < settings.Items.length; i++) {
var item = settings.Items[i];
item.isItem = true;
var option = document.createElement("option");
option.text = `${item.Name} (Item)`;
option.value = `item${item.Id}`;
options.push(option);
}
}
function addWallSelectOptions() {
for(var i = 0; i < settings.Walls.length; i++) {
var wall = settings.Walls[i];
wall.isWall = true;
var option = document.createElement("option");
option.text = `${wall.Name} (Wall)`;
option.value = `wall${wall.Id}`;
options.push(option);
}
}
function compareOptions(a,b) {
if (a.text < b.text)
return -1;
if (a.text > b.text)
return 1;
return 0;
}
$('#chooseBlocksModal').on('shown.bs.modal', function () {
$('#blocksFilter').focus();
});
$(document).bind('keydown', 'ctrl+b', function() {
$('#chooseBlocksModal').modal();
});
// filter blocks
jQuery.fn.filterByText = function(textbox, selectSingleMatch) {
return this.each(function() {
var select = this;
var options = [];
$(select).find('option').each(function() {
options.push({value: $(this).val(), text: $(this).text(), u: $(this).attr('data-u'), v: $(this).attr('data-v')});
});
$(select).data('options', options);
$(textbox).bind('change keyup', function() {
var options = $(select).empty().data('options');
var search = $.trim($(this).val());
var regex = new RegExp(search,"gi");
$.each(options, function(i) {
var option = options[i];
if(option.text.match(regex) !== null) {
var newOption = $('