blessing-skin-server/assets/js/user.utils.js

117 lines
2.9 KiB
JavaScript
Raw Normal View History

/*
* @Author: prpr
* @Date: 2016-01-21 13:56:40
* @Last Modified by: prpr
2016-02-05 11:41:37 +08:00
* @Last Modified time: 2016-02-05 11:38:39
*/
'use strict';
$("body").on("change", "#skininput", function(){
2016-01-22 11:26:13 +08:00
var files = $("#skininput").prop("files");
handleFiles(files, "skin");
});
$("body").on("change", "#capeinput", function(){
2016-01-22 11:26:13 +08:00
var files = $("#capeinput").prop("files");
handleFiles(files, "cape");
});
2016-02-05 11:41:37 +08:00
function handleFiles(files, type) {
2016-01-22 11:26:13 +08:00
if(files.length > 0) {
var file = files[0];
if(file.type === 'image/png') {
var fr = new FileReader();
fr.onload = function (e) {
var img = new Image();
img.onload = function () {
if (type == "skin") {
MSP.changeSkin(img.src);
} else {
MSP.changeCape(img.src);
}
};
img.onerror = function () {
showMsg("alert-danger", "Error: Not an image or unknown file format");
};
img.src = this.result;
};
fr.readAsDataURL(file);
} else {
showMsg("alert-danger", "Error: This is not a PNG image!");
}
}
};
2016-02-05 11:41:37 +08:00
function init3dCanvas() {
2016-02-05 11:08:45 +08:00
if ($(window).width() < 800) {
2016-02-04 23:49:31 +08:00
var canvas = MSP.get3dSkinCanvas($('#skinpreview').width(), $('#skinpreview').width());
$("#skinpreview").append($(canvas).prop("id", "canvas3d"));
2016-01-22 11:26:13 +08:00
} else {
2016-02-04 23:49:31 +08:00
var canvas = MSP.get3dSkinCanvas(400, 400);
$("#skinpreview").append($(canvas).prop("id", "canvas3d"));
2016-01-22 11:26:13 +08:00
}
2016-02-05 11:08:45 +08:00
}
2016-02-05 11:41:37 +08:00
$(document).ready(function(){
init3dCanvas();
});
2016-02-05 11:08:45 +08:00
2016-02-05 11:41:37 +08:00
$(window).resize(function(){
init3dCanvas();
});
2016-02-04 23:49:31 +08:00
$("[title='Movements']").click(function(){
MSP.setStatus("movements", !MSP.getStatus("movements"));
});
2016-02-04 23:49:31 +08:00
$("[title='Running']").click(function(){
MSP.setStatus("running", !MSP.getStatus("running"));
});
2016-02-04 23:49:31 +08:00
$("[title='Rotation']").click(function(){
MSP.setStatus("rotation", !MSP.getStatus("rotation"));
});
2016-02-05 11:41:37 +08:00
function show2dPreview() {
$('#canvas3d').remove();
$("#skinpreview").html($('<p>Skin for Steve model:</p>').append($('<img />').css('float', 'right').attr('src', '../skin/admin.png')));
}
$("#upload").click(function(){
2016-01-22 11:26:13 +08:00
var skin_file = $("#skininput").get(0).files[0];
var cape_file = $("#capeinput").get(0).files[0];
var form_data = new FormData();
2016-02-05 11:22:35 +08:00
if (skin_file) form_data.append('skin_file', skin_file);
if (cape_file) form_data.append('cape_file', cape_file);
2016-01-22 11:26:13 +08:00
if (skin_file || cape_file) {
$.ajax({
type: 'POST',
url: '../ajax.php?action=upload',
contentType: false,
dataType: "json",
data: form_data,
processData: false,
beforeSend: function() {
showMsg("alert-info", "Uploading...");
},
success: function(json) {
console.log(json);
2016-02-03 13:28:45 +08:00
if (json.skin.errno == 0 && json.cape.errno == 0) {
2016-01-22 11:26:13 +08:00
showMsg("alert-success", "Successfully uploaded.");
}
2016-02-03 13:28:45 +08:00
if (json.skin.errno != 0) {
showMsg("alert-danger", "Error when uploading skin:\n"+json.skin.msg);
2016-01-22 11:26:13 +08:00
}
2016-02-03 13:28:45 +08:00
if (json.cape.errno != 0) {
showMsg("alert-danger", "Error when uploading cape:\n"+json.cape.msg);
2016-01-22 11:26:13 +08:00
}
}
});
} else {
showMsg("alert-warning", "No input file selected");
}
});