diff --git a/Tile/Tile.js b/Tile/Tile.js new file mode 100644 index 0000000000..dadf4ec5d7 --- /dev/null +++ b/Tile/Tile.js @@ -0,0 +1,107 @@ +// Github: https://github.com/shdwjk/Roll20API/blob/master/Tile/Tile.js +// By: The Aaron, Arcane Scriptomancer +// Contact: https://app.roll20.net/users/104025/the-aaron + +var Tile = Tile || (function() { + 'use strict'; + + var version = 0.2, + tileNextMove = false, + + getCleanImgsrc = function (imgsrc) { + var parts = imgsrc.match(/(.*\/images\/.*)(thumb|max)(.*)$/); + if(parts) { + return parts[1]+'thumb'+parts[3]; + } + return; + }, + + handleInput = function(msg) { + var args; + + if (msg.type !== "api" || !isGM(msg.playerid)) { + return; + } + + args = msg.content.split(/\s+/); + switch(args[0]) { + case '!tile-next-move': + tileNextMove=true; + sendChat('Tile', '/w gm Tiling next move on the map layer.'); + break; + } + }, + + handleMove = function(obj, prev) { + var img, + ax,ay, + sx,sy, + cx,cy, + ix,iy, + t + ; + + if( tileNextMove + && 'map' === obj.get('layer') + && ( obj.get('left') !== prev.left || obj.get('top') !== prev.top) + ) { + img = getCleanImgsrc(obj.get('imgsrc')); + if(img) { + ax = prev.left; + ay = prev.top; + sx = obj.get('width') * (prev.left < obj.get('left') ? 1 : -1 ); + sy = obj.get('height') * (prev.top < obj.get('top') ? 1 : -1 ); + cx = Math.round(Math.abs((obj.get('left')-prev.left)/sx)); + cy = Math.round(Math.abs((obj.get('top')-prev.top)/sy)); + t = { + imgsrc: img, + pageid: obj.get('pageid'), + layer: 'map', + width: prev.width, + height: prev.height + }; + for(ix=0;ix<=cx;++ix) { + for(iy=0;iy<=cy;++iy) { + t.left = (ax + (sx * ix) ); + t.top = (ay + (sy * iy) ); + if(ix === cx && iy === cy ) { + obj.set({ + left: t.left, + top: t.top + }); + } else { + createObj('graphic',t); + } + } + } + + } else { + sendChat('Tile','/w gm Can only tile with images in a User Library.'); + } + tileNextMove = false; + } + }, + + registerEventHandlers = function() { + on('change:graphic', handleMove); + on('chat:message', handleInput); + }; + + return { + RegisterEventHandlers: registerEventHandlers + }; + +}()); + +on('ready',function() { + 'use strict'; + + if("undefined" !== typeof isGM && _.isFunction(isGM)) { + Tile.RegisterEventHandlers(); + } else { + log('--------------------------------------------------------------'); + log('Tile requires the isGM module to work.'); + log('isGM GIST: https://gist.github.com/shdwjk/8d5bb062abab18463625'); + log('--------------------------------------------------------------'); + } +}); diff --git a/Tile/package.json b/Tile/package.json new file mode 100644 index 0000000000..4018f77640 --- /dev/null +++ b/Tile/package.json @@ -0,0 +1,22 @@ +{ + "name": "Tile", + "version": "0.2", + "description": "Create tiled arrays of grpahics on the map layer.", + "authors": "The Aaron", + "roll20userid": "104025", + "dependencies": { + "isGMModule": "0.x" + }, + "modifies": { + "graphic": "create", + "graphic.height": "read", + "graphic.imgsrc": "read", + "graphic.layer": "read", + "graphic.left": "read,write", + "graphic.pageid": "read", + "graphic.top": "read,write", + "graphic.width": "read" + }, + "conflicts": [ + ] +}