mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-18 10:01:23 +01:00
Add drag to upload functionality
Allow users to drag and drop .map files onto the browser to load existing maps
This commit is contained in:
parent
7055f392c0
commit
82458aac26
3 changed files with 79 additions and 4 deletions
BIN
index.css
BIN
index.css
Binary file not shown.
|
|
@ -742,6 +742,14 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div id="map-dragged" style="display: none">
|
||||||
|
<p>Drop to upload</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="uploading-map" class="dialog" style="display: none">
|
||||||
|
<p>Uploading map...</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div id="alert" title="Warning!" style="display: none">
|
<div id="alert" title="Warning!" style="display: none">
|
||||||
<p id="alertMessage">Warning!</p>
|
<p id="alertMessage">Warning!</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
73
script.js
73
script.js
|
|
@ -133,11 +133,68 @@ function fantasyMap() {
|
||||||
$("#templateBody").sortable({items: "div:not(div[data-type='Mountain'])"});
|
$("#templateBody").sortable({items: "div:not(div[data-type='Mountain'])"});
|
||||||
$("#mapLayers, #templateBody").disableSelection();
|
$("#mapLayers, #templateBody").disableSelection();
|
||||||
|
|
||||||
|
addDragToUpload();
|
||||||
|
|
||||||
var drag = d3.drag()
|
var drag = d3.drag()
|
||||||
.container(function() {return this;})
|
.container(function() {return this;})
|
||||||
.subject(function() {var p=[d3.event.x, d3.event.y]; return [p, p];})
|
.subject(function() {var p=[d3.event.x, d3.event.y]; return [p, p];})
|
||||||
.on("start", dragstarted);
|
.on("start", dragstarted);
|
||||||
|
|
||||||
|
function addDragToUpload()
|
||||||
|
{
|
||||||
|
document.addEventListener('dragover', function(e) {
|
||||||
|
e.stopPropagation();
|
||||||
|
e.preventDefault();
|
||||||
|
$('#map-dragged').show();
|
||||||
|
});
|
||||||
|
|
||||||
|
document.addEventListener('dragleave', function(e) {
|
||||||
|
$('#map-dragged').hide();
|
||||||
|
});
|
||||||
|
|
||||||
|
document.addEventListener('drop', function(e) {
|
||||||
|
e.stopPropagation();
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
$('#map-dragged').hide();
|
||||||
|
|
||||||
|
// no files or more than one
|
||||||
|
if (e.dataTransfer.items == null || e.dataTransfer.items.length != 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var file = e.dataTransfer.items[0].getAsFile();
|
||||||
|
|
||||||
|
// not a .map file
|
||||||
|
if (file.name.indexOf('.map') == -1) {
|
||||||
|
alertMessage.innerHTML = 'Invalid map file - please upload the <b>.map</b> file you have previously downloaded.';
|
||||||
|
$("#alert").dialog({
|
||||||
|
resizable: false,
|
||||||
|
title: "Invalid map file",
|
||||||
|
width: 400,
|
||||||
|
buttons: {
|
||||||
|
Close: function() { $(this).dialog("close"); }
|
||||||
|
},
|
||||||
|
position: {my: "center", at: "center", of: "svg"}
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// all good - show uploading dialog and load the map
|
||||||
|
$('#uploading-map').dialog({
|
||||||
|
resizable: false,
|
||||||
|
title: "Uploading Map...",
|
||||||
|
width: 400,
|
||||||
|
position: {my: "center", at: "center", of: "svg"}
|
||||||
|
});
|
||||||
|
|
||||||
|
uploadFile(file, function onUploadFinish() {
|
||||||
|
$('#uploading-map').dialog("close");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function zoomed() {
|
function zoomed() {
|
||||||
var scaleDiff = Math.abs(scale - d3.event.transform.k);
|
var scaleDiff = Math.abs(scale - d3.event.transform.k);
|
||||||
scale = d3.event.transform.k;
|
scale = d3.event.transform.k;
|
||||||
|
|
@ -3643,9 +3700,14 @@ function fantasyMap() {
|
||||||
|
|
||||||
// Map Loader based on FileSystem API
|
// Map Loader based on FileSystem API
|
||||||
$("#fileToLoad").change(function() {
|
$("#fileToLoad").change(function() {
|
||||||
console.time("loadMap");
|
|
||||||
var fileToLoad = this.files[0];
|
var fileToLoad = this.files[0];
|
||||||
this.value = "";
|
this.value = "";
|
||||||
|
uploadFile(fileToLoad);
|
||||||
|
});
|
||||||
|
|
||||||
|
function uploadFile(file, callback) {
|
||||||
|
console.time("loadMap");
|
||||||
|
|
||||||
var fileReader = new FileReader();
|
var fileReader = new FileReader();
|
||||||
fileReader.onload = function(fileLoadedEvent) {
|
fileReader.onload = function(fileLoadedEvent) {
|
||||||
var dataLoaded = fileLoadedEvent.target.result;
|
var dataLoaded = fileLoadedEvent.target.result;
|
||||||
|
|
@ -3765,9 +3827,14 @@ function fantasyMap() {
|
||||||
});
|
});
|
||||||
invokeActiveZooming();
|
invokeActiveZooming();
|
||||||
console.timeEnd("loadMap");
|
console.timeEnd("loadMap");
|
||||||
|
|
||||||
|
if (callback) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
fileReader.readAsText(fileToLoad, "UTF-8");
|
|
||||||
});
|
fileReader.readAsText(file, "UTF-8");
|
||||||
|
}
|
||||||
|
|
||||||
// Poisson-disc sampling for a points
|
// Poisson-disc sampling for a points
|
||||||
// Source: bl.ocks.org/mbostock/99049112373e12709381; Based on https://www.jasondavies.com/poisson-disc
|
// Source: bl.ocks.org/mbostock/99049112373e12709381; Based on https://www.jasondavies.com/poisson-disc
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue