Skip to content

Commit

Permalink
osm support, image storage
Browse files Browse the repository at this point in the history
  • Loading branch information
sirmmo committed Jun 10, 2022
1 parent d392632 commit 966cd83
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 35 deletions.
83 changes: 50 additions & 33 deletions modules/map-canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ class MapCanvas extends Application {
Hooks.once('init', () => {
MapCanvas.registerSettings(options).then(() => console.log("MapCanvas Settings Registered."));
});

Hooks.on('fileUtilsReady', (fileUtils) => {
window.fileUtils = fileUtils;
})
}

addControls(controls) {
Expand Down Expand Up @@ -218,13 +222,26 @@ class MapCanvas extends Application {
if (!window['ofmmapcanvas'].dialogActive) { window['ofmmapcanvas'].dialogActive = true } else { return }
window['ofmmapcanvas'].dialog = new MapDialog();
window['ofmmapcanvas'].dialog.render(true, {
width: 1600,
height: 1200
width: 1100,
height: 850
});
}

async updateScene(generateNewScene = false) {
const USE_STORAGE = game.settings.get("ofm-map-canvas", "USE_STORAGE");
const LICENSE = game.settings.get("ofm-map-canvas", "LICENSE");
const WORLD_TO_LOAD = game.settings.get("ofm-map-canvas", "WORLD_TO_LOAD");
let WIDTH = game.settings.get("ofm-map-canvas", "WIDTH");
let HEIGHT = game.settings.get("ofm-map-canvas", "HEIGHT");

console.log('getting walls and lights for...')
const doc = document.querySelector('#mapData').value;
const jdoc = JSON.parse(doc);
console.log(jdoc)

if(jdoc.zoom < 18.5){
alert('I cannot render a battlemap at this level of zoom');
return false;
}
const DEFAULT_SCENE = game.settings.get("ofm-map-canvas", "DEFAULT_SCENE");
const sceneName = (generateNewScene) ? DEFAULT_SCENE+"_"+new Date().getTime() : DEFAULT_SCENE;
let scene = game.scenes.find(s => s.name.startsWith(sceneName));
Expand All @@ -237,15 +254,11 @@ class MapCanvas extends Application {
});
}

const LICENSE = game.settings.get("ofm-map-canvas", "LICENSE");
const WORLD_TO_LOAD = game.settings.get("ofm-map-canvas", "WORLD_TO_LOAD");
const WIDTH = game.settings.get("ofm-map-canvas", "WIDTH");
const HEIGHT = game.settings.get("ofm-map-canvas", "HEIGHT");
if (WORLD_TO_LOAD === 'osm'){
WIDTH *= 2;
HEIGHT *= 2;
}

console.log('getting walls and lights for...')
const doc = document.querySelector('#mapData').value;
const jdoc = JSON.parse(doc);
console.log(jdoc)

const bbox = [jdoc.bounds._sw.lng, jdoc.bounds._sw.lat, jdoc.bounds._ne.lng, jdoc.bounds._ne.lat];

Expand All @@ -254,18 +267,32 @@ class MapCanvas extends Application {
console.log(vectors.tiles);


await canvas.lighting.deleteAll();
await canvas.walls.deleteAll();
await canvas.tokens.deleteAll();
await canvas.foreground.objects.destroy();
await canvas.background.objects.destroy()
await canvas.sight.resetFog();

if (!generateNewScene) {
await canvas.lighting.deleteAll();
await canvas.walls.deleteAll();
await canvas.tokens.deleteAll();
await canvas.foreground.objects.destroy();
await canvas.background.objects.destroy()
await canvas.sight.resetFog();
}

//await const fileUtils = game.modules.get('foundry-file-utils');
try{
await FilePicker.createDirectory('data', 'ofm-map-canvas');
} catch(ex){ }

const url = 'https://vectors.fantasymaps.org/render/' + WORLD_TO_LOAD + '.jpeg?width='+WIDTH+'&height='+HEIGHT+'&bbox=[' + bbox.join(',') + ']&zoom=' + jdoc.zoom + '&key=LICENSE';
const data = await fetch(url);
const fil = new File([await data.blob()], scene.id+'.jpeg');
await FilePicker.upload('data', 'ofm-map-canvas', scene.id);


let updates = {
_id: scene.id,
width: WIDTH,
height: HEIGHT,
//bgSource:
bgSource: 'ofm-map-canvas/'+scene.id+".jpeg",
//img: 'https://vectors.fantasymaps.org/render/' + WORLD_TO_LOAD + '.jpeg?width='+WIDTH+'&height='+HEIGHT+'&bbox=[' + bbox.join(',') + ']&zoom=' + jdoc.zoom + '&key=LICENSE',
padding: 0,
gridType: 1,
Expand All @@ -280,11 +307,11 @@ class MapCanvas extends Application {
tiles: vectors.tiles,
};

if(WORLD_TO_LOAD === 'osm'){
updates['bgSource'] = 'https://vectors.fantasymaps.org/render/' + WORLD_TO_LOAD + '.jpeg?width='+WIDTH+'&height='+HEIGHT+'&bbox=[' + bbox.join(',') + ']&zoom=' + jdoc.zoom + '&key=LICENSE';
} else {
updates['bgSource'] = 'https://vectors.fantasymaps.org/render/' + WORLD_TO_LOAD + '.jpeg?width='+WIDTH+'&height='+HEIGHT+'&bbox=[' + bbox.join(',') + ']&zoom=' + jdoc.zoom + '&key=LICENSE';
}
//if(WORLD_TO_LOAD === 'osm'){
// updates['bgSource'] = 'https://vectors.fantasymaps.org/render/osm.jpeg?width='+WIDTH+'&height='+HEIGHT+'&bbox=[' + bbox.join(',') + ']&zoom=' + jdoc.zoom + '&key=LICENSE';
//} else {
// updates['bgSource'] = 'https://vectors.fantasymaps.org/render/' + WORLD_TO_LOAD + '.jpeg?width='+WIDTH+'&height='+HEIGHT+'&bbox=[' + bbox.join(',') + ']&zoom=' + jdoc.zoom + '&key=LICENSE';
//}

await Scene.updateDocuments([updates]).then(() => {
ui.notifications.info(" Map Canvas | Updated Scene: " + sceneName)
Expand Down Expand Up @@ -336,16 +363,6 @@ class MapCanvas extends Application {
filePicker: false,
});

await game.settings.register('ofm-map-canvas', 'USE_STORAGE', {
name: 'Store Images [Experimental]',
hint: 'Stores images instead of embedding them in the scene document, should speed up image propagation.',
scope: 'world',
config: true,
type: Boolean,
default: false,
filePicker: false,
});

console.log(window['ofmmapcanvas'].options);
await game.settings.register('ofm-map-canvas', 'WORLD_TO_LOAD', {
name: 'Fantasy Map to load',
Expand Down
4 changes: 2 additions & 2 deletions templates/map-canvas.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
gap: 10px;
flex-direction: column;
justify-content: center;
height: 100%;
width: 100%;
height: 768px;
width: 1024px;
}

#mapCanvasZoomLevel {
Expand Down

0 comments on commit 966cd83

Please sign in to comment.