Last active
November 3, 2017 01:14
-
-
Save shdwjk/81080064e232a97cc59d78d8de108f15 to your computer and use it in GitHub Desktop.
Normalize Page Scale. !nps for the current page or !nps-all for all pages.
This file contains hidden or 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('subtype')==='token' && | |
o.get('represents')===''; | |
}), | |
otherGraphics = filterObjs((o)=>{ | |
return o.get('pageid')===page.id && | |
o.get('type')==='graphic' && | |
( | |
o.get('represents')!=='' || | |
o.get('subtype')!=='card' | |
); | |
}), | |
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)); | |
otherGraphics=_.without(otherGraphics,mapGraphics); | |
// 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,cmds,pages,player; | |
if('api' === msg.type && msg.content.match(/^!nps(?:-all)?\b/) && playerIsGM(msg.playerid)){ | |
cmds=msg.content.split(/\s+/); | |
player=getObj('player',msg.playerid); | |
switch(cmds.shift()){ | |
case '!nps-all': | |
pages=filterObjs((o)=>{ | |
return o.get('type')==='page' && | |
o.get('scale_units')==='ft' && | |
o.get('scale_number')>5 ; | |
}); | |
/* falls through */ | |
case '!nps': | |
pages = pages || filterObjs((o)=>{ | |
return o.get('type')==='page' && | |
o.get('scale_units')==='ft' && | |
o.get('scale_number')>5 && | |
o.id === player.get('lastpage'); | |
}); | |
if(pages.length){ | |
scgm( `Operating on pages: <ul><li>${_.map(pages,(p)=>p.get('name')).join('</li> <li>')}</li></ul>`); | |
_.delay(adjustPages,50,pages); | |
} else { | |
scgm( `No work to do on the specified pages.`); | |
} | |
break; | |
} | |
} | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment