Skip to content

Instantly share code, notes, and snippets.

@kanageddaamen
Created October 12, 2019 17:20
Show Gist options
  • Save kanageddaamen/97545f68c5cd1e2c85d34fbd94933e76 to your computer and use it in GitHub Desktop.
Save kanageddaamen/97545f68c5cd1e2c85d34fbd94933e76 to your computer and use it in GitHub Desktop.
// JavaScript source code
function rgbstringtohtml(color)
{
var iStart = color.indexOf('(') + 1;
var iEnd = color.indexOf(')');
colors = color.substring(iStart, iEnd).split(',');
color = '#';
for(var x = 0;x<3;x++)
{
color += ("0" + parseInt(colors[x]).toString(16)).slice(-2);
}
return color;
}
class ImageObject
{
constructor(object, imageindex, zorderlist)
{
this.ImageIndex = imageindex;
this.ZOrder = zorderlist.indexOf(object.get('id'));
this.XPosition = object.get('left');
this.YPosition = object.get('top');
this.Width =object.get('width');
this.Height = object.get('height');
this.Rotation = object.get('rotation');
this.FlipHorizontal = object.get('fliph');
this.FlipVertical = object.get('flipv');
this.Tint = object.get('tint_color');
}
check()
{
return true;
}
};
class PathObject
{
constructor(object, zorderlist)
{
this.ZOrder = zorderlist.indexOf(object.get('id'));
var aaPathElements = JSON.parse(object.get('path'));
var sPath = "";
var bFirst = true;
for(var x=0;x<aaPathElements.length;x++)
{
for(var y=0;y<aaPathElements[x].length;y++)
{
if(!bFirst)
{
sPath+=' ';
}
else
{
bFirst=false;
}
sPath+=aaPathElements[x][y];
}
}
this.Path = sPath;
this.FillColor = object.get('fill');
this.StrokeColor = object.get('stroke');
this.StrokeWidth = object.get("stroke_width");
this.Rotation = object.get('rotation');
this.Width = object.get('width');
this.Height = object.get('height');
this.XPosition = object.get('left');
this.YPosition = object.get('top');
this.ScaleX = object.get('ScaleX');
this.ScaleY = object.get('ScaleY');
}
check()
{
return true;
}
};
class TextObject
{
constructor(object, zorderlist)
{
this.ZOrder = zorderlist.indexOf(object.get('id'));
this.Width = object.get('width');
this.Height = object.get('height');
this.XPosition = object.get('left');
this.YPosition = object.get('top');
this.Text = object.get('text');
this.FontSize = object.get('font_size');
this.Rotation = object.get('rotation');
this.Color = rgbstringtohtml(object.get('color'));
this.FontFamily = object.get('font_family');
}
check()
{
return this.Text!='';
}
};
class Layer
{
MakeImageObject(object, zorder, imagelibrary)
{
return new ImageObject(object, imagelibrary.GetURLIndex(object.get('imgsrc')), zorder);
}
MakePathObject(object, zorder, imagelibrary)
{
return new PathObject(object, zorder);
}
MakeTextObject(object, zorder, imagelibrary)
{
return new TextObject(object, zorder);
}
GetObjects(page, layername, zorder, imagelibrary, makeobject, objecttype)
{
var ret = [];
var objs = findObjs({type:objecttype, layer:layername, pageid:page});
for(var x = 0;x<objs.length;x++)
{
var obj = makeobject(objs[x], zorder, imagelibrary);
if(obj.check())
ret.push(obj);
}
return ret;
}
constructor(page, layername, zorder, imagelibrary)
{
this.Name = layername;
this.ImageObjects = this.GetObjects(page, layername, zorder, imagelibrary, this.MakeImageObject, 'graphic');
this.Paths = this.GetObjects(page, layername, zorder, imagelibrary, this.MakePathObject, 'path');
this.Texts = this.GetObjects(page, layername, zorder, imagelibrary, this.MakeTextObject, 'text');
}
};
class ImageLibrary
{
constructor()
{
this.ImageURLs = [];
}
GetURLIndex(URL)
{
var iRet = this.ImageURLs.indexOf(URL);
if(iRet == -1)
{
iRet = this.ImageURLs.length;
this.ImageURLs.push(URL);
}
return iRet;
}
}
class PageData
{
constructor(pageID)
{
var page = getObj('page', pageID);
this.Name = page.get("name");
this.Width = page.get("width") * 70;
this.Height = page.get("height") * 70;
this.DrawGrid = page.get("showgrid");
this.GridOpacity = page.get("grid_opacity");
this.BackgroundColor = page.get("background_color");
this.GridColor = page.get("gridcolor");
this.GridCellWidth = page.get("snapping_increment") * 70;
this.Layers = [];
this.ImageLibrary = new ImageLibrary();
var zorder = page.get('zorder').split(',');
this.Layers.push(new Layer(pageID, 'map', zorder, this.ImageLibrary));
this.Layers.push(new Layer(pageID, 'gmlayer', zorder, this.ImageLibrary));
}
};
on('ready',function(){
"use strict";
function constructPageJSON(pageID)
{
if(pageID != undefined && pageID != null)
{
var p = new PageData(pageID);
log(p);
return JSON.stringify(p);
}
else
{
return 'you have not been to a page as the GM';
}
};
on("chat:message", function(msg) {
if('api' === msg.type && playerIsGM(msg.playerid) && msg.content == '!EXPORT')
{
var player = getObj('player',msg.playerid);
var pageID = player.get('lastpage');
sendChat('Map Export JSON', '/w '+msg.who+' '+ constructPageJSON(pageID));
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment