Skip to content

Instantly share code, notes, and snippets.

@rogerscg
Created June 29, 2015 02:03
Show Gist options
  • Save rogerscg/33b666d72f4b45284194 to your computer and use it in GitHub Desktop.
Save rogerscg/33b666d72f4b45284194 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name TagPro Drag and Drop Texture Replacer
// @description read texture files from hard disk
// @version 5
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @grant GM_addStyle
// @include http://tagpro-*.koalabeast.com*
// @include http://tangent.jukejuice.com*
// @include http://*.newcompte.fr*
// @author ballparts
// ==/UserScript==
//******** USER DEFINED VARIABLES ********//
// Whether or not to make the canvas background transparent
var makeTransparent = true;
//****** END USER DEFINED VARIABLES ******//
// IF WE ARE IN A GAME
if(document.URL.search(/:[0-9]{4}/) > -1) {
var images = {
tiles: GM_getValue('tiles'),
speedpad: GM_getValue('speedpad'),
speedpadred: GM_getValue('speedpadred'),
speedpadblue: GM_getValue('speedpadblue'),
portal: GM_getValue('portal'),
splats: GM_getValue('splats')
}
if(images.tiles || images.speedpad || images.speedpadred || images.speedpadblue || images.portal || images.splats) {
tagpro.loadAssets(images);
}
tagpro.ready(function() {
if(makeTransparent) {
var oldCanvas = $(tagpro.renderer.canvas);
var newCanvas = $('<canvas id="viewport" width="1280" height="800"></canvas>');
oldCanvas.after(newCanvas);
oldCanvas.remove();
tagpro.renderer.canvas = newCanvas.get(0);
tagpro.renderer.options.transparent = true;
tagpro.renderer.renderer = tagpro.renderer.createRenderer();
tagpro.renderer.resizeAndCenterView();
newCanvas.show();
}
});
// IF WE ARE NOT IN A GAME
} else {
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
files = evt.dataTransfer.files;
for (var i = 0; i < files.length; i++) {
var thisFile = files[i];
var type = determineType(thisFile);
if(type) {
handleFile(thisFile, type);
}
}
}
function handleFile(file, type) {
var reader = new FileReader();
reader.onload = function(theFile) {
URL = theFile.target.result;
GM_setValue(type, URL);
//console.log(type, theFile.target.result);
Notification.requestPermission(function (permission) {
// If the user is okay, let's create a notification
if (permission === "granted") {
var notification = new Notification('Loaded ' + type + ' image!', {icon:URL});
}
});
}
reader.readAsDataURL(file);
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy';
}
function determineType(file) {
var basename = file.name.slice(0, file.name.length - 4).toLowerCase(),
extension = file.name.substring(file.name.length - 4).toLowerCase(),
fileTypes = ['tiles', 'speedpad', 'speedpadred', 'speedpadblue', 'portal', 'splats'],
type;
if(extension !== '.png') {
//handle non-image input
return;
}
fileTypes.forEach(function(fileType) {
if(basename.search(fileType) > -1) {
type = fileType;
}
});
if(!type) type = "background";
return type;
}
var dropZone = document.body;
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment