Created
August 27, 2016 21:22
-
-
Save shdwjk/92f41797209be0be6129439ac5e9f932 to your computer and use it in GitHub Desktop.
NormalizePageSize -- !nps by gm to adjust scale of all tactical maps to 5ft.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on('ready',function(){ | |
"use strict"; | |
var scgm=(msg)=>{ | |
sendChat('NPS',`/w gm <div style=" margin-left: -40px; border: 1px solid #ccc; border-radius: .5em; padding: .1em .5em; background-color: #eee; font-size: 10px; font-weight: bold; "> ${msg} </div> `); | |
}, | |
scaleGraphic = function(scale){ | |
return function(graphic){ | |
graphic.set({ | |
left: graphic.get('left')*scale, | |
top: graphic.get('top')*scale, | |
width: graphic.get('width')*scale, | |
height: graphic.get('height')*scale | |
}); | |
}; | |
}, | |
repositionGraphic = function(scale){ | |
return function(graphic){ | |
graphic.set({ | |
left: graphic.get('left')*scale, | |
top: graphic.get('top')*scale, | |
}); | |
}; | |
}, | |
scaleText = function(scale){ | |
return function(text){ | |
text.set({ | |
left: text.get('left')*scale, | |
top: text.get('top')*scale, | |
font_size: text.get('font_size')*scale, | |
}); | |
}; | |
}, | |
simpleObject = function(o){ | |
return JSON.parse(JSON.stringify(o)); | |
}, | |
scalePathString = function(pathstring,scale){ | |
return JSON.stringify(_.map(JSON.parse(pathstring),(n)=> _.map(n,(i)=> _.isNumber(i) ? scale*i : i ))); | |
}, | |
scaleDrawing = function(scale){ | |
return function(drawing){ | |
let newpath=_.omit(simpleObject(drawing),['_id','_type']); | |
if(_.contains(['','[]'],newpath._path)){ | |
return; | |
} | |
newpath.path=scalePathString(newpath._path,scale); | |
delete newpath._path; | |
newpath.top*=scale; | |
newpath.left*=scale; | |
newpath.width*=scale; | |
newpath.height*=scale; | |
let newPathObj = createObj('path',newpath); | |
drawing.remove(); | |
}; | |
}, | |
adjustPages = function(pages){ | |
let page=pages.shift(), | |
scale=page.get('scale_number')/5; | |
let mapGraphics = filterObjs((o)=>{ | |
return o.get('pageid')===page.id && | |
o.get('type')==='graphic' && | |
o.get('layer')==='map'; | |
}), | |
otherGraphics = filterObjs((o)=>{ | |
return o.get('pageid')===page.id && | |
o.get('type')==='graphic' && | |
o.get('layer')!=='map'; | |
}), | |
allText = filterObjs((o)=>{ | |
return o.get('pageid')===page.id && | |
o.get('type')==='text'; | |
}), | |
allDrawings = filterObjs((o)=>{ | |
return o.get('pageid')===page.id && | |
o.get('type')==='path'; | |
}) | |
; | |
scgm(`Adjusting page: <u>${page.get('name')}</u>`); | |
// scale page | |
scgm(`-- Scale from ${page.get('scale_number')}ft to 5ft (x${scale} size)`); | |
page.set({ | |
width: page.get('width')*scale, | |
height: page.get('height')*scale, | |
scale_number: 5 | |
}); | |
// scale all graphics on the map layer | |
scgm(`-- Scaling map images: ${mapGraphics.length}`); | |
_.each(mapGraphics,scaleGraphic(scale)); | |
// reposition all graphics on the other layers | |
scgm(`-- Repositioning other layer images: ${otherGraphics.length}`); | |
_.each(otherGraphics,repositionGraphic(scale)); | |
// reposition and scale all text on all layers | |
scgm(`-- Scaling and Repositioning text: ${allText.length}`); | |
_.each(allText,scaleText(scale)); | |
// redraw all drawings | |
scgm(`-- Redrawing paths to scale: ${allDrawings.length}`); | |
_.each(allDrawings,scaleDrawing(scale)); | |
if(pages.length){ | |
_.delay(adjustPages,100,pages); | |
} | |
}; | |
on('chat:message',function(msg){ | |
var args,errors=[],who,cmd,characterid,createAttrs,character; | |
if('api' === msg.type && msg.content.match(/^!nps\b/) && playerIsGM(msg.playerid)){ | |
let pages=filterObjs((o)=>{ | |
return o.get('type')==='page' && | |
o.get('scale_units')==='ft' && | |
o.get('scale_number')>5 ; | |
}); | |
scgm( `Operating on pages: <ul><li>${_.map(pages,(p)=>p.get('name')).join('</li> <li>')}</li></ul>`); | |
_.delay(adjustPages,50,pages); | |
} | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment