Advertisement
Guest User

Untitled

a guest
Jan 19th, 2016
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Gray squares behind floor tiles
  3. // @namespace http://www.reddit.com/u/snaps_/
  4. // @description Tints every other floor tile a slightly different shade.
  5. // @include http://tagpro-*.koalabeast.com:*
  6. // @include http://tangent.jukejuice.com:*
  7. // @include http://*.newcompte.fr:*
  8. // @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
  9. // @author snaps
  10. // @version 0.1.0
  11. // ==/UserScript==
  12.  
  13. /* User-Defined Variables */
  14.  
  15. // The color to tint the floor tiles.
  16. var TINT_COLOR = "#000000";
  17.  
  18. // How much to tint the floor tiles. Should be a number from 0 to 1.
  19. // In practice try values from 0.05 to 1 in increments of .2 to start,
  20. // then smaller amounts to hone in on what you like.
  21. var OPACITY = .25;
  22.  
  23. /* End of User-Defined Variables */
  24.  
  25.  
  26. /**
  27. * Executes `fn` when the relevant parts of the `tagpro` object have
  28. * been initialized.
  29. * @param {Function} fn - The function to execute.
  30. */
  31. function waitForInitialized(fn) {
  32. if (!tagpro || !tagpro.tiles || !tagpro.tiles.draw || !tagpro.renderer) {
  33. setTimeout(function() {
  34. waitForInitialized(fn);
  35. }, 10);
  36. } else {
  37. // Only override if we load early enough.
  38. if (!tagpro.renderer.layers.backgroundDrawn) {
  39. fn();
  40. }
  41. }
  42. }
  43.  
  44. waitForInitialized(function() {
  45. var stdDraw = tagpro.tiles.draw;
  46. // ids of the tiles we're interested in changing.
  47. var floorTiles = [2, 11, 12, 17, 18];
  48. var prefix = "__tinted__";
  49.  
  50. /**
  51. * Set tint of a given canvas element.
  52. * @param {HTMLElement} image - Canvas element holding the image to tint.
  53. * @param {string} [color="#000000"] - Color string to tint the tiles, like "#FF0000".
  54. * @param {number} [opacity="0.01"] - How much to preserve the original image.
  55. */
  56. var setTint = function(image, color, opacity) {
  57. // Adapted from: http://stackoverflow.com/a/4231508/1698058
  58. var tint = document.createElement("canvas");
  59. tint.width = image.width;
  60. tint.height = image.height;
  61.  
  62. var tintCtx = tint.getContext("2d");
  63. tintCtx.fillStyle = color || "#000000";
  64. tintCtx.fillRect(0, 0, tint.width, tint.height);
  65. tintCtx.globalCompositeOperation = "source-over";
  66. tintCtx.drawImage(image, 0, 0);
  67.  
  68. var imageCtx = image.getContext("2d");
  69. imageCtx.globalAlpha = opacity || 0.01;
  70. imageCtx.drawImage(tint, 0, 0);
  71. }
  72.  
  73. /**
  74. * Creates the tinted texture for the tile of the given id and sets
  75. * the relevant values such that the returned value will function in
  76. * the original `tagpro.tiles.draw` function.
  77. * @param {(number|string)} tileId - The original id of the tile to set information for.
  78. * @return {string} - The new id to use for the tile.
  79. */
  80. var setTintedTexture = function(tileId) {
  81. var originalTileId = tileId;
  82. tileId = prefix + originalTileId;
  83. if (!tagpro.tiles[tileId] || !PIXI.TextureCache[tileId]) {
  84. var tile = tagpro.tiles[originalTileId];
  85. tagpro.tiles[tileId] = tile;
  86.  
  87. var spread = tile.spread || 0;
  88. var elt = document.createElement("canvas");
  89. elt.width = tile.size || 40;
  90. elt.height = tile.size || 40;
  91.  
  92. var ctx = elt.getContext("2d");
  93. var sx = tile.x * 40 - spread;
  94. var sy = tile.y * 40 - spread;
  95. var size = (tile.size || 40) + spread * 2;
  96. ctx.drawImage(tagpro.tiles.image, sx, sy, size, size, 0, 0, size, size);
  97. setTint(elt, TINT_COLOR, OPACITY);
  98. PIXI.TextureCache[tileId] = PIXI.Texture.fromCanvas(elt);
  99. }
  100. return tileId;
  101. }
  102.  
  103. // Override `tagpro.tiles.draw`.
  104. tagpro.tiles.draw = function() {
  105. // Only make changes when drawing background tiles.
  106. if (tagpro.tiles.draw.caller === tagpro.tiles.drawLayers) {
  107. var loc = arguments[2];
  108.  
  109. var floorTile = floorTiles.indexOf(arguments[1]) !== -1;
  110. if (loc && !(typeof arguments[1] == "object") && floorTile) {
  111. var arrayLoc = {
  112. x: Math.floor(loc.x / 20),
  113. y: Math.floor(loc.y / 20)
  114. };
  115. if ((arrayLoc.x % 2 == 0 && arrayLoc.y % 2 == 0) ||
  116. (arrayLoc.x % 2 == 1 && arrayLoc.y % 2 == 1)) {
  117. arguments[1] = setTintedTexture(arguments[1]);
  118. }
  119. }
  120. }
  121. return stdDraw.apply(null, arguments);
  122. }
  123. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement