diff --git a/classes/terrain.js b/classes/terrain.js index 475f366..f175dc3 100644 --- a/classes/terrain.js +++ b/classes/terrain.js @@ -1,4 +1,4 @@ -import { log, setting } from "./terrainlayer.js"; +import { log, setting, makeid } from "./terrainlayer.js"; export class Terrain extends PlaceableObject { constructor(...args) { @@ -86,16 +86,6 @@ export class Terrain extends PlaceableObject { * @private */ _cleanData() { - let makeid = function () { - var result = ''; - var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; - var charactersLength = characters.length; - for (var i = 0; i < 16; i++) { - result += characters.charAt(Math.floor(Math.random() * charactersLength)); - } - return result; - } - if (this.data._id == undefined) this.data._id = makeid(); diff --git a/classes/terrainlayer.js b/classes/terrainlayer.js index b637efc..8ca9e16 100644 --- a/classes/terrainlayer.js +++ b/classes/terrainlayer.js @@ -18,6 +18,10 @@ export let setting = key => { return game.settings.get("TerrainLayer", key); }; +export let makeid = () => { + return TerrainLayer.makeid(); +} + export class TerrainLayer extends PlaceablesLayer { constructor() { super(); @@ -42,6 +46,16 @@ export class TerrainLayer extends PlaceablesLayer { return [0.5, 2, 3, 4]; } + static makeid = function () { + var result = ''; + var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + var charactersLength = characters.length; + for (var i = 0; i < 16; i++) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + return result; + } + static multipleText(multiple) { return (parseInt(multiple) == 1 || parseInt(multiple) == 0.5 ? '1/2' : multiple); } @@ -100,7 +114,12 @@ export class TerrainLayer extends PlaceablesLayer { let grid = canvas.scene.getFlag('TerrainLayer', 'costGrid'); for (let y in grid) { for (let x in grid[y]) { - this.createTerrain({ x: parseInt(x), y: parseInt(y), multiple: grid[y][x].multiple }); + if (canvas.scene.data.terrain.find(t => { return t.x == x && t.y == y }) == undefined) { + let id = makeid(); + let data = { _id: id, x: parseInt(x), y: parseInt(y), multiple: grid[y][x].multiple }; + canvas.scene.data.terrain.push(data); + await canvas.scene.setFlag('TerrainLayer', 'terrain' + id, data); + } } } //canvas.scene.unsetFlag('TerrainLayer', 'costGrid'); @@ -217,16 +236,19 @@ export class TerrainLayer extends PlaceablesLayer { for (let id of ids) { const object = this.get(id); log('Removing terrain', object.data.x, object.data.y); - originals.push(object); + if(!options.isUndo) + originals.push(object.data); this.objects.removeChild(object); delete this._controlled[id]; object._onDelete(options, game.user.id); object.destroy({ children: true }); + canvas.scene.data.terrain.findSplice(t => { return t._id == id; }); let key = `flags.TerrainLayer.-=terrain${id}`; updates[key] = null; } - this.storeHistory("delete", originals); + if (!options.isUndo) + this.storeHistory("delete", originals); this._costGrid = null; @@ -236,6 +258,7 @@ export class TerrainLayer extends PlaceablesLayer { _onClickLeft(event) { super._onClickLeft(event); if (game.activeTool == 'addterrain') { + this.originals = []; let pos = event.data.getLocalPosition(canvas.app.stage); let gridPt = canvas.grid.grid.getGridPositionFromPixels(pos.x, pos.y); let [y, x] = gridPt; //Normalize the returned data because it's in [y,x] format @@ -304,10 +327,11 @@ export class TerrainLayer extends PlaceablesLayer { _onDragLeftDrop(e) { if (game.activeTool == "select") { - canvas._onDragLeftDrop(event); + canvas._onDragLeftDrop(e); } - else if (game.activeTool != 'addterrain') { - super._onDragLeftDrop(event); + else if (game.activeTool == 'addterrain') { + this.storeHistory("create", this.originals); + delete this.originals; } } @@ -343,7 +367,10 @@ export class TerrainLayer extends PlaceablesLayer { createTerrain(data, options = { }) { if (!this.terrainExists(data.x, data.y)) { data.multiple = data.multiple || this.defaultmultiple; - this.constructor.placeableClass.create(data, options); + this.constructor.placeableClass.create(data, options).then((terrain) => { + if (this.originals != undefined) + this.originals.push(terrain); + }); } this._costGrid = null; } diff --git a/module.json b/module.json index 0881a2a..6b54b92 100644 --- a/module.json +++ b/module.json @@ -3,7 +3,7 @@ "title": "Terrain Layer", "description": "A base module that adds a Terrain Layer to Foundry to paint difficult terrain squares and to be used as a dependency for other mods who might integrate it with their functionality.", "authors": [{"name":"Will Saunders","email":"willsaunders1014@gmail.com" }, {"name":"IronMonk, ironmonk88#4075" }], - "version": "1.0.3", + "version": "1.0.4", "minimumCoreVersion": "0.6.6", "compatibleCoreVersion":"0.7.9", "scripts":[ @@ -31,6 +31,6 @@ "socket":true, "url":"https://github.com/ironmonk88/TerrainLayer", "changelog":"https://raw.githubusercontent.com/ironmonk88/TerrainLayer/main/README.md", - "download": "https://github.com/ironmonk88/TerrainLayer/archive/1.0.3.zip", + "download": "https://github.com/ironmonk88/TerrainLayer/archive/1.0.4.zip", "manifest": "https://github.com/ironmonk88/TerrainLayer/releases/latest/download/module.json" } diff --git a/terrain-main.js b/terrain-main.js index 74a8acc..e5c2284 100644 --- a/terrain-main.js +++ b/terrain-main.js @@ -2,16 +2,16 @@ import { TerrainLayer } from './classes/terrainlayer.js'; import { TerrainHUD } from './classes/terrainhud.js'; import { registerSettings } from "./js/settings.js"; -let theLayers = Canvas.layers; -theLayers.terrain = TerrainLayer; - -/* -let oldConfig = Scene.prototype.constructor.config; -Scene.prototype.constructor.config = function () { - let result = oldConfig.call(this); - result.embeddedEntities.Terrain = "terrains"; - return result; -}*/ +function registerLayer() { + const layers = mergeObject(Canvas.layers, { + terrain: TerrainLayer + }); + Object.defineProperty(Canvas, 'layers', { + get: function () { + return layers; + } + }); +} Hooks.on('canvasInit', () => { canvas.hud.terrain = new TerrainHUD(); @@ -25,6 +25,7 @@ Hooks.on('init', () => { }); registerSettings(); + registerLayer(); let oldOnDragLeftStart = Token.prototype._onDragLeftStart; Token.prototype._onDragLeftStart = function (event) { @@ -47,7 +48,7 @@ Hooks.on('init', () => { oldOnDragLeftCancel.apply(this, [event]) } - let handleDragCancel = MouseInteractionManager.prototype._handleDragCancel; + //let handleDragCancel = MouseInteractionManager.prototype._handleDragCancel; /* MouseInteractionManager.prototype._handleDragCancel = function (event) { @@ -56,9 +57,3 @@ Hooks.on('init', () => { handleDragCancel.apply(this, [event]) }*/ }) - -Object.defineProperty(Canvas, 'layers', { - get: function () { - return theLayers; - } -})