Arbybear

TagPro Drag and Drop Texture Replacer

Jul 15th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. // ==UserScript==
  2. // @name TagPro Drag and Drop Texture Replacer
  3. // @description read texture files from hard disk
  4. // @version 8
  5. // @grant GM_getValue
  6. // @grant GM_setValue
  7. // @grant GM_deleteValue
  8. // @grant GM_addStyle
  9. // @include http://tagpro-*.koalabeast.com*
  10. // @include http://tangent.jukejuice.com*
  11. // @include http://*.newcompte.fr*
  12. // @author ballparts
  13. // @updateURL https://gist.github.com/ballparts/eeffb535bb370fadf7ea/raw/TagPro_DragNDropTextureReplacer.user.js
  14. // @downloadURL https://gist.github.com/ballparts/eeffb535bb370fadf7ea/raw/TagPro_DragNDropTextureReplacer.user.js
  15. // ==/UserScript==
  16.  
  17. //******** USER DEFINED VARIABLES ********//
  18.  
  19. // Whether or not to make the canvas background transparent
  20. var makeTransparent = true;
  21.  
  22. //****** END USER DEFINED VARIABLES ******//
  23.  
  24.  
  25.  
  26.  
  27.  
  28. // IF WE ARE IN A GAME
  29. if(document.URL.search(/:[0-9]{4}/) > -1) {
  30. var images = {},
  31. tiles = GM_getValue('tiles'),
  32. speedpad = GM_getValue('speedpad'),
  33. speedpadred = GM_getValue('speedpadred'),
  34. speedpadblue = GM_getValue('speedpadblue'),
  35. portal = GM_getValue('portal'),
  36. splats = GM_getValue('splats'),
  37. gravitywell = GM_getValue('gravitywell');
  38.  
  39. if(tiles) images.tiles = tiles;
  40. if(speedpad) images.speedpad = speedpad;
  41. if(speedpadred) images.speedpadRed = speedpadred;
  42. if(speedpadblue) images.speedpadBlue = speedpadblue;
  43. if(portal) images.portal = portal;
  44. if(splats) images.splats = splats;
  45. if(gravitywell) images.gravityWell = gravitywell;
  46. if(images.tiles || images.speedpad || images.speedpadred || images.speedpadblue || images.portal || images.splats || images.gravitywell) {
  47. tagpro.loadAssets(images);
  48. }
  49.  
  50. tagpro.ready(function() {
  51. if(makeTransparent) {
  52. var oldCanvas = $(tagpro.renderer.canvas);
  53. var newCanvas = $('<canvas id="viewport" width="1280" height="800"></canvas>');
  54. oldCanvas.after(newCanvas);
  55. oldCanvas.remove();
  56. tagpro.renderer.canvas = newCanvas.get(0);
  57. tagpro.renderer.options.transparent = true;
  58. tagpro.renderer.renderer = tagpro.renderer.createRenderer();
  59. tagpro.renderer.resizeAndCenterView();
  60. newCanvas.show();
  61. }
  62. });
  63.  
  64. // IF WE ARE NOT IN A GAME
  65. } else {
  66.  
  67.  
  68.  
  69. function handleFileSelect(evt) {
  70. evt.stopPropagation();
  71. evt.preventDefault();
  72.  
  73. files = evt.dataTransfer.files;
  74.  
  75. for (var i = 0; i < files.length; i++) {
  76. var thisFile = files[i];
  77. var type = determineType(thisFile);
  78. if(type) {
  79. handleFile(thisFile, type);
  80. }
  81. }
  82. }
  83.  
  84. function handleFile(file, type) {
  85. var reader = new FileReader();
  86. reader.onload = function(theFile) {
  87. URL = theFile.target.result;
  88. GM_setValue(type, URL);
  89. //console.log(type, theFile.target.result);
  90. Notification.requestPermission(function (permission) {
  91. // If the user is okay, let's create a notification
  92. if (permission === "granted") {
  93. var notification = new Notification('Loaded ' + type + ' image!', {icon:URL});
  94. }
  95. });
  96. if(type === 'background') {
  97. replaceBackground();
  98. }
  99. }
  100. reader.readAsDataURL(file);
  101. }
  102.  
  103. function handleDragOver(evt) {
  104. evt.stopPropagation();
  105. evt.preventDefault();
  106. evt.dataTransfer.dropEffect = 'copy';
  107. }
  108.  
  109. function determineType(file) {
  110. var basename = file.name.slice(0, file.name.length - 4).toLowerCase(),
  111. extension = file.name.substring(file.name.length - 4).toLowerCase(),
  112. fileTypes = ['tiles', 'speedpad', 'speedpadred', 'speedpadblue', 'portal', 'splats', 'gravitywell'],
  113. type;
  114. if(extension !== '.png') {
  115. //handle non-image input
  116. return;
  117. }
  118.  
  119. fileTypes.forEach(function(fileType) {
  120. if(basename.search(fileType) > -1) {
  121. type = fileType;
  122. }
  123. });
  124. if(!type) type = "background";
  125.  
  126. return type;
  127. }
  128.  
  129. var dropZone = document.body;
  130. dropZone.addEventListener('dragover', handleDragOver, false);
  131. dropZone.addEventListener('drop', handleFileSelect, false);
  132. }
  133.  
  134. function replaceBackground() {
  135.  
  136. backgroundImage = GM_getValue('background');
  137. if(backgroundImage) {
  138. GM_addStyle (
  139. "html.background { background: #000000 url('"+backgroundImage+"') no-repeat fixed center; background-size: cover }"
  140. );
  141. }
  142. }
  143.  
  144. $(document).ready(replaceBackground);
Advertisement
Add Comment
Please, Sign In to add comment