Advertisement
DayDun

OWOP Copy tool

Aug 3rd, 2018
4,098
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Very useful copy tool, allows you to easily copy and paste art. This makes moving art a lot easier.
  3.  
  4. By yours truly, DayDun.
  5.  
  6. P.S. only admins can use it lol.
  7. */
  8.  
  9. OWOP.tool.addToolObject(new OWOP.tool.class("Copy", OWOP.cursors.select, OWOP.fx.player.NONE, OWOP.RANK.NONE, function (tool) {
  10.     function drawText(ctx, str, x, y, centered) {
  11.         ctx.strokeStyle = "#000000", ctx.fillStyle = "#FFFFFF", ctx.lineWidth = 2.5, ctx.globalAlpha = 0.5;
  12.         if (centered) {
  13.             x -= ctx.measureText(str).width >> 1;
  14.         }
  15.         ctx.strokeText(str, x, y);
  16.         ctx.globalAlpha = 1;
  17.         ctx.fillText(str, x, y);
  18.     }
  19.    
  20.     tool.setFxRenderer(function (fx, ctx, time) {
  21.         if (!fx.extra.isLocalPlayer) return 1;
  22.         var x = fx.extra.player.x;
  23.         var y = fx.extra.player.y;
  24.         var fxx = (Math.floor(x / 16) - OWOP.camera.x) * OWOP.camera.zoom;
  25.         var fxy = (Math.floor(y / 16) - OWOP.camera.y) * OWOP.camera.zoom;
  26.         var oldlinew = ctx.lineWidth;
  27.         ctx.lineWidth = 1;
  28.         if (tool.extra.end) {
  29.             var s = tool.extra.start;
  30.             var e = tool.extra.end;
  31.             var x = (s[0] - OWOP.camera.x) * OWOP.camera.zoom + 0.5;
  32.             var y = (s[1] - OWOP.camera.y) * OWOP.camera.zoom + 0.5;
  33.             var w = e[0] - s[0];
  34.             var h = e[1] - s[1];
  35.             ctx.beginPath();
  36.             ctx.rect(x, y, w * OWOP.camera.zoom, h * OWOP.camera.zoom);
  37.             ctx.globalAlpha = 1;
  38.             ctx.strokeStyle = "#FFFFFF";
  39.             ctx.stroke();
  40.             ctx.setLineDash([3, 4]);
  41.             ctx.strokeStyle = "#000000";
  42.             ctx.stroke();
  43.             ctx.globalAlpha = 0.25 + Math.sin(time / 500) / 4;
  44.             ctx.fillStyle = OWOP.renderer.patterns.unloaded;
  45.             ctx.fill();
  46.             ctx.setLineDash([]);
  47.             var oldfont = ctx.font;
  48.             ctx.font = "16px sans-serif";
  49.             var txt = (!tool.extra.clicking ? "Right click to copy " : "") + '(' + Math.abs(w) + 'x' + Math.abs(h) + ')';
  50.             var txtx = window.innerWidth >> 1;
  51.             var txty = window.innerHeight >> 1;
  52.             txtx = Math.max(x, Math.min(txtx, x + w * OWOP.camera.zoom));
  53.             txty = Math.max(y, Math.min(txty, y + h * OWOP.camera.zoom));
  54.  
  55.             drawText(ctx, txt, txtx, txty, true);
  56.             ctx.font = oldfont;
  57.             ctx.lineWidth = oldlinew;
  58.             return 0;
  59.         } else {
  60.             ctx.beginPath();
  61.             ctx.moveTo(0, fxy + 0.5);
  62.             ctx.lineTo(window.innerWidth, fxy + 0.5);
  63.             ctx.moveTo(fxx + 0.5, 0);
  64.             ctx.lineTo(fxx + 0.5, window.innerHeight);
  65.  
  66.             //ctx.lineWidth = 1;
  67.             ctx.globalAlpha = 1;
  68.             ctx.strokeStyle = "#FFFFFF";
  69.             ctx.stroke();
  70.             ctx.setLineDash([3]);
  71.             ctx.strokeStyle = "#000000";
  72.             ctx.stroke();
  73.  
  74.             ctx.setLineDash([]);
  75.             ctx.lineWidth = oldlinew;
  76.             return 1;
  77.         }
  78.     });
  79.  
  80.     tool.extra.start = null;
  81.     tool.extra.end = null;
  82.     tool.extra.clicking = false;
  83.  
  84.     tool.setEvent('mousedown', function (mouse, event) {
  85.         var s = tool.extra.start;
  86.         var e = tool.extra.end;
  87.         var isInside = function isInside() {
  88.             return mouse.tileX >= s[0] && mouse.tileX < e[0] && mouse.tileY >= s[1] && mouse.tileY < e[1];
  89.         };
  90.         if (mouse.buttons === 1 && !tool.extra.end) {
  91.             tool.extra.start = [mouse.tileX, mouse.tileY];
  92.             tool.extra.clicking = true;
  93.             tool.setEvent('mousemove', function (mouse, event) {
  94.                 if (tool.extra.start && mouse.buttons === 1) {
  95.                     tool.extra.end = [mouse.tileX, mouse.tileY];
  96.                     return 1;
  97.                 }
  98.             });
  99.             var finish = function finish() {
  100.                 tool.setEvent('mousemove mouseup deselect', null);
  101.                 tool.extra.clicking = false;
  102.                 var s = tool.extra.start;
  103.                 var e = tool.extra.end;
  104.                 if (e) {
  105.                     if (s[0] === e[0] || s[1] === e[1]) {
  106.                         tool.extra.start = null;
  107.                         tool.extra.end = null;
  108.                     }
  109.                     if (s[0] > e[0]) {
  110.                         var tmp = e[0];
  111.                         e[0] = s[0];
  112.                         s[0] = tmp;
  113.                     }
  114.                     if (s[1] > e[1]) {
  115.                         var tmp = e[1];
  116.                         e[1] = s[1];
  117.                         s[1] = tmp;
  118.                     }
  119.                 }
  120.                 OWOP.renderer.render(OWOP.renderer.rendertype.FX);
  121.             };
  122.             tool.setEvent('deselect', finish);
  123.             tool.setEvent('mouseup', function (mouse, event) {
  124.                 if (!(mouse.buttons & 1)) {
  125.                     finish();
  126.                 }
  127.             });
  128.         } else if (mouse.buttons === 1 && tool.extra.end) {
  129.             if (isInside()) {
  130.                 var offx = mouse.tileX;
  131.                 var offy = mouse.tileY;
  132.                 tool.setEvent('mousemove', function (mouse, event) {
  133.                     var dx = mouse.tileX - offx;
  134.                     var dy = mouse.tileY - offy;
  135.                     tool.extra.start = [s[0] + dx, s[1] + dy];
  136.                     tool.extra.end = [e[0] + dx, e[1] + dy];
  137.                 });
  138.                 var end = function end() {
  139.                     tool.setEvent('mouseup deselect mousemove', null);
  140.                 };
  141.                 tool.setEvent('deselect', end);
  142.                 tool.setEvent('mouseup', function (mouse, event) {
  143.                     if (!(mouse.buttons & 1)) {
  144.                         end();
  145.                     }
  146.                 });
  147.             } else {
  148.                 tool.extra.start = null;
  149.                 tool.extra.end = null;
  150.             }
  151.         } else if (mouse.buttons === 2 && tool.extra.end && isInside()) {
  152.             tool.extra.start = null;
  153.             tool.extra.end = null;
  154.             var x = s[0];
  155.             var y = s[1];
  156.             var w = e[0] - s[0];
  157.             var h = e[1] - s[1];
  158.             var c = document.createElement('canvas');
  159.             c.width = w;
  160.             c.height = h;
  161.             var ctx = c.getContext('2d');
  162.             var d = ctx.createImageData(w, h);
  163.             for (var i = y; i < y + h; i++) {
  164.                 for (var j = x; j < x + w; j++) {
  165.                     var pix = OWOP.world.getPixel(j, i);
  166.                     if (!pix) continue;
  167.                     d.data[4 * ((i - y) * w + (j - x))] = pix[0];
  168.                     d.data[4 * ((i - y) * w + (j - x)) + 1] = pix[1];
  169.                     d.data[4 * ((i - y) * w + (j - x)) + 2] = pix[2];
  170.                     d.data[4 * ((i - y) * w + (j - x)) + 3] = 255;
  171.                 }
  172.             }
  173.             ctx.putImageData(d, 0, 0);
  174.             var paste = OWOP.tool.allTools.paste;
  175.             paste.extra.canvas = c;
  176.             var oldSelect = paste.events.select;
  177.             paste.events.select = function() {
  178.                 paste.events.select = oldSelect;
  179.             };
  180.             OWOP.player.tool = "paste";
  181.         }
  182.     });
  183. }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement