2016-01-21 18:25:03 +08:00
/ *
* @ Author : prpr
* @ Date : 2016 - 01 - 21 13 : 56 : 40
* @ Last Modified by : prpr
2016-02-05 18:53:17 +08:00
* @ Last Modified time : 2016 - 02 - 05 18 : 46 : 35
2016-01-21 18:25:03 +08:00
* /
'use strict' ;
2016-01-21 22:14:26 +08:00
2016-02-05 15:56:17 +08:00
$ ( 'body' ) . on ( 'change' , '#skininput' , function ( ) {
var files = $ ( '#skininput' ) . prop ( 'files' ) ;
handleFiles ( files , 'skin' ) ;
2016-01-21 22:14:26 +08:00
} ) ;
2016-02-05 15:56:17 +08:00
$ ( 'body' ) . on ( 'change' , '#capeinput' , function ( ) {
var files = $ ( '#capeinput' ) . prop ( 'files' ) ;
handleFiles ( files , 'cape' ) ;
2016-01-21 22:14:26 +08:00
} ) ;
2016-02-05 15:56:17 +08:00
// Real-time preview
2016-02-05 11:41:37 +08:00
function handleFiles ( files , type ) {
2016-02-05 15:56:17 +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-01-21 22:14:26 +08:00
} ;
2016-02-05 11:41:37 +08:00
function init3dCanvas ( ) {
2016-02-05 15:56:17 +08:00
if ( $ ( window ) . width ( ) < 800 ) {
var canvas = MSP . get3dSkinCanvas ( $ ( '#skinpreview' ) . width ( ) , $ ( '#skinpreview' ) . width ( ) ) ;
$ ( "#skinpreview" ) . append ( $ ( canvas ) . prop ( "id" , "canvas3d" ) ) ;
} else {
var canvas = MSP . get3dSkinCanvas ( 400 , 400 ) ;
$ ( "#skinpreview" ) . append ( $ ( canvas ) . prop ( "id" , "canvas3d" ) ) ;
}
2016-02-05 11:08:45 +08:00
}
2016-02-05 15:56:17 +08:00
$ ( document ) . ready ( init3dCanvas ) ;
// Auto resize canvas to fit responsive design
$ ( window ) . resize ( init3dCanvas ) ;
2016-01-21 22:14:26 +08:00
2016-02-05 15:56:17 +08:00
// Change 3D preview status
2016-02-04 23:49:31 +08:00
$ ( "[title='Movements']" ) . click ( function ( ) {
2016-02-05 15:56:17 +08:00
MSP . setStatus ( "movements" , ! MSP . getStatus ( "movements" ) ) ;
2016-01-21 22:14:26 +08:00
} ) ;
2016-02-04 23:49:31 +08:00
$ ( "[title='Running']" ) . click ( function ( ) {
2016-02-05 15:56:17 +08:00
MSP . setStatus ( "running" , ! MSP . getStatus ( "running" ) ) ;
2016-01-21 22:14:26 +08:00
} ) ;
2016-02-04 23:49:31 +08:00
$ ( "[title='Rotation']" ) . click ( function ( ) {
2016-02-05 15:56:17 +08:00
MSP . setStatus ( "rotation" , ! MSP . getStatus ( "rotation" ) ) ;
2016-01-21 22:14:26 +08:00
} ) ;
2016-02-05 11:41:37 +08:00
function show2dPreview ( ) {
2016-02-05 15:56:17 +08:00
$ ( '#canvas3d' ) . remove ( ) ;
2016-02-05 18:53:17 +08:00
$ ( "#skinpreview" ) . html ( $ ( '<p>Skin for Steve model:</p>' ) . append ( $ ( '<img />' ) . addClass ( 'skin2d' ) . attr ( 'src' , '../skin/' + docCookies . getItem ( 'uname' ) + '-steve.png?v=' + Math . random ( ) ) ) ) ;
$ ( "#skinpreview" ) . append ( $ ( '<p>Skin for Alex model:</p>' ) . append ( $ ( '<img />' ) . addClass ( 'skin2d' ) . attr ( 'src' , '../skin/' + docCookies . getItem ( 'uname' ) + '-alex.png?v=' + Math . random ( ) ) ) ) ;
$ ( "#skinpreview" ) . append ( $ ( '<p>Cape:</p>' ) . append ( $ ( '<img />' ) . addClass ( 'skin2d' ) . attr ( 'src' , '../cape/' + docCookies . getItem ( 'uname' ) + '.png?v=' + Math . random ( ) ) ) ) ;
2016-02-05 18:35:02 +08:00
$ ( '#preview' ) . html ( '3D Preview' ) . attr ( 'href' , 'javascript:show3dPreview();' ) ;
}
function show3dPreview ( ) {
$ ( '#skinpreview' ) . html ( '' ) ;
init3dCanvas ( )
$ ( '#preview' ) . html ( '2D Preview' ) . attr ( 'href' , 'javascript:show2dPreview();' ) ;
2016-02-05 11:41:37 +08:00
}
2016-01-21 22:14:26 +08:00
$ ( "#upload" ) . click ( function ( ) {
2016-02-05 15:56:17 +08:00
var model = $ ( '#model-steve' ) . prop ( 'checked' ) ? "steve" : "alex" ;
var skin _file = $ ( '#skininput' ) . get ( 0 ) . files [ 0 ] ;
var cape _file = $ ( '#capeinput' ) . get ( 0 ) . files [ 0 ] ;
var form _data = new FormData ( ) ;
if ( skin _file ) form _data . append ( 'skin_file' , skin _file ) ;
if ( cape _file ) form _data . append ( 'cape_file' , cape _file ) ;
form _data . append ( 'uname' , docCookies . getItem ( 'uname' ) ) ;
// Ajax file upload
if ( skin _file || cape _file ) {
$ . ajax ( {
type : 'POST' ,
url : '../ajax.php?action=upload&model=' + model ,
contentType : false ,
dataType : "json" ,
data : form _data ,
processData : false ,
beforeSend : function ( ) {
showMsg ( 'alert-info' , 'Uploading...' ) ;
} ,
success : function ( json ) {
console . log ( json ) ;
if ( json . skin . errno == 0 && json . cape . errno == 0 ) {
showMsg ( 'alert-success' , 'Successfully uploaded.' ) ;
}
if ( json . skin . errno != 0 ) {
showMsg ( 'alert-danger' , 'Error when uploading skin:\n' + json . skin . msg ) ;
}
if ( json . cape . errno != 0 ) {
showMsg ( 'alert-danger' , 'Error when uploading cape:\n' + json . cape . msg ) ;
}
}
} ) ;
} else {
showMsg ( 'alert-warning' , 'No input file selected' ) ;
}
2016-01-21 22:14:26 +08:00
} ) ;
2016-02-05 15:56:17 +08:00
function changeModel ( uname ) {
showAlert ( "Sure to change?" , function ( ) {
$ . ajax ( {
type : "POST" ,
url : "../ajax.php?action=model" ,
data : { "uname" : docCookies . getItem ( 'uname' ) } ,
dataType : "json" ,
success : function ( json ) {
if ( json . errno == 0 ) {
showAlert ( json . msg , function ( ) {
location . reload ( ) ;
} ) ;
} else {
showAlert ( json . msg ) ;
}
}
} ) ;
} ) ;
}
2016-01-21 22:14:26 +08:00