Skip to content

Commit

Permalink
Merge branch 'release/0.0.3' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
caewok committed Oct 11, 2023
2 parents 073bf37 + 37f8e55 commit bab67d8
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 83 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 0.0.3
Add checks for when the terrain item is not yet defined. Closes issue #3.
Fix for the terrain item getting initialized too late.
Fix hook that ensure terrain item is invisible.
Add check to determine if terrain data file exists, to avoid 404 error message.

## 0.0.2
Correct import issue where "Settings" filename was incorrectly capitalized. Closes issue #2.

Expand Down
8 changes: 7 additions & 1 deletion scripts/EffectHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export class EffectHelper {
*/
async duplicate() {
const item = Settings.terrainEffectsItem;
if ( !item ) return;
const effectData = foundry.utils.deepClone(this.effect.toObject());
delete effectData._id;
const effect = new CONFIG.ActiveEffect.documentClass(effectData);
Expand Down Expand Up @@ -144,6 +145,8 @@ export class EffectHelper {

static async deleteEffectById(id) {
const item = Settings.terrainEffectsItem;
if ( !item ) return;

const activeEffect = this.getTerrainEffectById(id);
if ( !activeEffect ) return;
await item.deleteEmbeddedDocuments("ActiveEffect", [activeEffect.id]);
Expand All @@ -155,7 +158,7 @@ export class EffectHelper {
*/
static getAll() {
const item = Settings.terrainEffectsItem;
return item.effects;
return item?.effects ?? [];
}

/**
Expand All @@ -165,6 +168,7 @@ export class EffectHelper {
*/
static terrainEffectExists(effect) {
const item = Settings.terrainEffectsItem;
if ( !item ) return false;
return item.effects.has(effect.id);
}

Expand All @@ -175,6 +179,7 @@ export class EffectHelper {
*/
static getTerrainEffectByName(name) {
const item = Settings.terrainEffectsItem;
if ( !item ) return undefined;
return item.effects.find(e => e.name === name);
}

Expand All @@ -185,6 +190,7 @@ export class EffectHelper {
*/
static getTerrainEffectById(id) {
const item = Settings.terrainEffectsItem;
if ( !item ) return undefined;
return item.effects.find(e => e.id === id);
}
}
4 changes: 2 additions & 2 deletions scripts/PixelCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ export class PixelCache extends PIXI.Rectangle {
tmpPt.translate(center.x, center.y, tmpPt);
Draw.point(tmpPt, { radius: 1 })
if ( !rect.contains(tmpPt.x, tmpPt.y) )
console.debug(`Rectangle does not contain {tmpPt.x},${tmpPt.y} (${offsets[i]},${offsets[i+1]})`)
// Debug: console.debug(`Rectangle does not contain {tmpPt.x},${tmpPt.y} (${offsets[i]},${offsets[i+1]})`)
}
Draw.shape(rect)
Expand Down Expand Up @@ -1225,7 +1225,7 @@ export class PixelCache extends PIXI.Rectangle {
tmpPt.translate(center.x, center.y, tmpPt);
Draw.point(tmpPt, { radius: 1 })
if ( !poly.contains(tmpPt.x, tmpPt.y) )
console.debug(`Poly does not contain {tmpPt.x},${tmpPt.y} (${offsets[i]},${offsets[i+1]})`)
// Debug: console.debug(`Poly does not contain {tmpPt.x},${tmpPt.y} (${offsets[i]},${offsets[i+1]})`)
}
Draw.shape(poly)
*/
Expand Down
12 changes: 6 additions & 6 deletions scripts/Terrain.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class Terrain {
await this.constructor.lock.acquire();
let currTerrains = new Set(this.constructor.allOnToken(token));
if ( duplicate || !currTerrains.has(this) ) {
console.debug(`Adding ${this.name} terrain to ${token.name}.`);
// Debug: console.debug(`Adding ${this.name} terrain to ${token.name}.`);
await SOCKETS.socket.executeAsGM("addTerrainEffect", token.document.uuid, this.id);
}

Expand All @@ -274,7 +274,7 @@ export class Terrain {
if ( removeOtherSceneTerrains || removeAllOtherTerrains ) {
currTerrains.delete(this);
for ( const terrain of currTerrains ) {
console.debug(`Removing ${terrain.name} terrain from ${token.name}.`);
// Debug: console.debug(`Removing ${terrain.name} terrain from ${token.name}.`);
await SOCKETS.socket.executeAsGM("removeTerrainEffect", token.document.uuid, terrain.id);
}
}
Expand All @@ -290,7 +290,7 @@ export class Terrain {
await this.constructor.lock.acquire();
const currTerrains = new Set(this.constructor.allOnToken(token));
if ( currTerrains.has(this) ) {
console.debug(`Removing ${this.name} terrain from ${token.name}.`);
// Debug: console.debug(`Removing ${this.name} terrain from ${token.name}.`);
await SOCKETS.socket.executeAsGM("removeTerrainEffect", token.document.uuid, this.id);
}
await this.constructor.lock.release();
Expand All @@ -306,7 +306,7 @@ export class Terrain {
const promises = [];
const uuid = token.document.uuid;
for ( const terrain of terrains ) {
console.debug(`removeAllFromToken|Removing ${terrain.name} from ${token.name}.`);
// Debug: console.debug(`removeAllFromToken|Removing ${terrain.name} from ${token.name}.`);
promises.push(SOCKETS.socket.executeAsGM("removeTerrainEffect", uuid, terrain.id));
}
await Promise.allSettled(promises);
Expand All @@ -323,7 +323,7 @@ export class Terrain {
const promises = [];
const uuid = token.document.uuid;
for ( const terrain of terrains ) {
console.debug(`removeAllFromToken|Removing ${terrain.name} from ${token.name}.`);
// Debug: console.debug(`removeAllFromToken|Removing ${terrain.name} from ${token.name}.`);
promises.push(SOCKETS.socket.executeAsGM("removeTerrainEffect", uuid, terrain.id));
}
await Promise.allSettled(promises);
Expand All @@ -336,7 +336,7 @@ export class Terrain {
* @returns {Terrain[]}
*/
static allOnToken(token) {
console.debug(`Getting all terrains on ${token.name}.`);
// Debug: console.debug(`Getting all terrains on ${token.name}.`);
const allEffects = token.actor?.appliedEffects;
if ( !allEffects ) return [];
const terrainEffects = allEffects.filter(e => {
Expand Down
44 changes: 22 additions & 22 deletions scripts/TerrainEffectsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ export class TerrainEffectsController {
}

_fetchFavorites(terrains) {
console.debug("TerrainEffectsController|_fetchFavorites");
// Debug: console.debug("TerrainEffectsController|_fetchFavorites");
const favorites = new Set(Settings.getByName("FAVORITES"));
return terrains.filter(t => favorites.has(t.id));
}

_fetchSceneTerrains(terrains) {
console.debug("TerrainEffectsController|_fetchSceneTerrains");
// Debug: console.debug("TerrainEffectsController|_fetchSceneTerrains");
const map = canvas.terrain.sceneMap;
const ids = new Set([...map.values()].map(terrain => terrain.id));
return terrains.filter(t => ids.has(t.id));
Expand Down Expand Up @@ -130,7 +130,7 @@ export class TerrainEffectsController {
* pixel values. Allows re-assignment of pixel values to different terrains.
*/
async onEditSceneTerrains() {
console.debug("TerrainEffectsController|onEditSceneTerrains");
// Debug: console.debug("TerrainEffectsController|onEditSceneTerrains");
new TerrainSceneConfig().render(true);
}

Expand All @@ -139,7 +139,7 @@ export class TerrainEffectsController {
* Displays a mini-configuration that lists all terrains, allows for quick editing.
*/
async onListTerrains() {
console.debug("TerrainEffectsController|onListTerrains");
// Debug: console.debug("TerrainEffectsController|onListTerrains");
new TerrainListConfig().render(true);
}

Expand All @@ -148,7 +148,7 @@ export class TerrainEffectsController {
* @param {MouseEvent} event
*/
async onCreateEffectClick(_event) {
console.debug("TerrainEffectsController|onCreateEffectClick");
// Debug: console.debug("TerrainEffectsController|onCreateEffectClick");
const terrain = new Terrain();
await terrain.initialize();
this._viewMvc.render();
Expand All @@ -159,7 +159,7 @@ export class TerrainEffectsController {
* @param {jQuery} effectItem - jQuery element representing the effect list item
*/
async onEditEffectClick(_effectItem) {
console.debug("TerrainEffectsController|onEditEffectClick");
// Debug: console.debug("TerrainEffectsController|onEditEffectClick");
const effectId = this._findNearestEffectId(event);
const activeEffect = EffectHelper.getTerrainEffectById(effectId);
activeEffect.sheet.render(true);
Expand All @@ -170,7 +170,7 @@ export class TerrainEffectsController {
* @param {jQuery} effectItem - jQuery element representing the effect list item
*/
async onDeleteEffectClick(_effectItem) {
console.debug("TerrainEffectsController|onDeleteEffectClick");
// Debug: console.debug("TerrainEffectsController|onDeleteEffectClick");
const effectId = this._findNearestEffectId(event);
const view = this._viewMvc;

Expand All @@ -179,7 +179,7 @@ export class TerrainEffectsController {
content:
"<h4>Are You Sure?</h4><p>This will remove the terrain from all scenes.",
yes: async () => {
console.debug("TerrainEffectsController|onDeleteEffectClick yes");
// Debug: console.debug("TerrainEffectsController|onDeleteEffectClick yes");
await EffectHelper.deleteEffectById(effectId);
view.render();
}
Expand All @@ -204,7 +204,7 @@ export class TerrainEffectsController {
content:
"<h4>Are You Sure?</h4><p>This will reset all configured terrain effects to the module defaults and reload Foundry.",
yes: async () => {
console.debug("TerrainEffectsController|onResetStatusEffectsClick");
// Debug: console.debug("TerrainEffectsController|onResetStatusEffectsClick");
// await this._settings.resetStatusEffects();
window.location.reload();
}
Expand Down Expand Up @@ -245,7 +245,7 @@ export class TerrainEffectsController {
* @param {MouseEvent} event - event that corresponds to clicking an effect item
*/
async onEffectClick(event) {
console.debug("TerrainEffectsController|onEffectClick");
// Debug: console.debug("TerrainEffectsController|onEffectClick");
await this.onEditEffectClick(event);
}

Expand All @@ -260,7 +260,7 @@ export class TerrainEffectsController {
* @param {jQuery} effectItem - jQuery element representing the effect list item
*/
async onAddFavorite(effectItem) {
console.debug("TerrainEffectsController|onAddFavorite");
// Debug: console.debug("TerrainEffectsController|onAddFavorite");
const effectId = effectItem.data().effectId;
await Settings.addToFavorites(effectId);
this._viewMvc.render();
Expand All @@ -271,7 +271,7 @@ export class TerrainEffectsController {
* @param {jQuery} effectItem - jQuery element representing the effect list item
*/
async onRemoveFavorite(effectItem) {
console.debug("TerrainEffectsController|onRemoveFavorite");
// Debug: console.debug("TerrainEffectsController|onRemoveFavorite");
const effectId = effectItem.data().effectId;
await Settings.removeFromFavorites(effectId);
this._viewMvc.render();
Expand All @@ -283,7 +283,7 @@ export class TerrainEffectsController {
* @returns true if the effect is favorited
*/
isFavoritedEffect(effectItem) {
console.debug("TerrainEffectsController|isFavoritedEffect");
// Debug: console.debug("TerrainEffectsController|isFavoritedEffect");
const effectId = effectItem.data().effectId;
return Settings.isFavorite(effectId);

Expand All @@ -297,7 +297,7 @@ export class TerrainEffectsController {
* @returns true if the effect is in the scene map.
*/
isInScene(effectItem) {
console.debug("TerrainEffectsController|isInScene");
// Debug: console.debug("TerrainEffectsController|isInScene");
const effectId = effectItem.data().effectId;
return canvas.terrain.sceneMap.hasTerrainId(effectId);
}
Expand All @@ -307,7 +307,7 @@ export class TerrainEffectsController {
* @param {jQuery} effectItem - jQuery element representing the effect list item
*/
async onImportTerrain(effectItem) {
console.debug("TerrainEffectsController|onImportTerrain");
// Debug: console.debug("TerrainEffectsController|onImportTerrain");
const effectId = effectItem.data().effectId;
const terrain = Terrain.fromEffectId(effectId);
await terrain.importFromJSONDialog();
Expand All @@ -319,7 +319,7 @@ export class TerrainEffectsController {
* @param {jQuery} effectItem - jQuery element representing the effect list item
*/
onExportTerrain(effectItem) {
console.debug("TerrainEffectsController|onExportTerrain");
// Debug: console.debug("TerrainEffectsController|onExportTerrain");
const effectId = effectItem.data().effectId;
const terrain = Terrain.fromEffectId(effectId);
terrain.exportToJSON();
Expand All @@ -330,7 +330,7 @@ export class TerrainEffectsController {
* @param {jQuery} effectFolder
*/
async onImportAllTerrains(event) {
console.debug("TerrainEffectsController|onImportAllTerrains", event);
// Debug: console.debug("TerrainEffectsController|onImportAllTerrains", event);
event.stopPropagation();
await Terrain.importFromJSONDialog();
this._viewMvc.render();
Expand All @@ -341,7 +341,7 @@ export class TerrainEffectsController {
* @param {jQuery} effectFolder
*/
async onReplaceAllTerrains(event) {
console.debug("TerrainEffectsController|onReplaceAllTerrains", event);
// Debug: console.debug("TerrainEffectsController|onReplaceAllTerrains", event);
event.stopPropagation();
await Terrain.replaceFromJSONDialog();
this._viewMvc.render();
Expand All @@ -352,7 +352,7 @@ export class TerrainEffectsController {
* @param {jQuery} effectFolder
*/
onExportAllTerrains(event) {
console.debug("TerrainEffectsController|onExportAllTerrains", event);
// Debug: console.debug("TerrainEffectsController|onExportAllTerrains", event);
event.stopPropagation();
Terrain.saveToJSON();
}
Expand All @@ -362,7 +362,7 @@ export class TerrainEffectsController {
* @param {jQuery} effectItem - jQuery element representing the effect list item
*/
async onToggleStatusEffect(_effectItem) {
console.debug("TerrainEffectsController|onToggleStatusEffect");
// Debug: console.debug("TerrainEffectsController|onToggleStatusEffect");
// const effectId = effectItem.data().effectId;

// const effectName = effectItem.data().effectName;
Expand All @@ -382,7 +382,7 @@ export class TerrainEffectsController {
* @param {jQuery} effectItem - jQuery element representing the effect list item
*/
async onDuplicate(effectItem) {
console.debug("TerrainEffectsController|onDuplicate");
// Debug: console.debug("TerrainEffectsController|onDuplicate");
const effectId = effectItem.data().effectId;
const eHelper = EffectHelper.fromId(effectId);
const dupe = await eHelper.duplicate();
Expand All @@ -397,7 +397,7 @@ export class TerrainEffectsController {
* @param {DragEvent} event - event that corresponds to the drag start
*/
onEffectDragStart(_event) {
console.debug(`TerrainEffectsController|onEffectDragStart for ${event.target.dataset.effectName}`);
// Debug: console.debug(`TerrainEffectsController|onEffectDragStart for ${event.target.dataset.effectName}`);

const terrain = Terrain.fromEffectId(event.target.dataset.effectId);
event.dataTransfer.setData(
Expand Down
29 changes: 25 additions & 4 deletions scripts/TerrainFileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class TerrainFileManager {
if ( filePath.startsWith("https://")
|| filePath.startsWith("http://") ) filePath = `${filePath}?v=${Math.random()}`;

console.debug(`Loading ${filePath}`);
// Debug: console.debug(`Loading ${filePath}`);
try {
const baseTexture = await TextureLoader.loader.loadTexture(filePath);
const texture = new PIXI.Texture(baseTexture);
Expand All @@ -105,7 +105,7 @@ export class TerrainFileManager {
* @returns {PIXI.Texture}
*/
async loadTextureFromFile(file) {
console.debug("Loading from file");
// Debug: console.debug("Loading from file");
try {
const texture = await PIXI.Texture.fromURL(file);
return this._formatTexture(texture);
Expand Down Expand Up @@ -149,21 +149,24 @@ export class TerrainFileManager {
* @returns {Promise<object>} The response object from FilePicker.upload.
*/
async saveTexture(texture) {
console.debug(`Saving texture to ${this.#filePath}/${this.#textureFileName}.webp`);
// Debug: console.debug(`Saving texture to ${this.#filePath}/${this.#textureFileName}.webp`);
const base64image = await this.convertTextureToImage(texture);
return this.constructor.uploadBase64(base64image, `${this.#textureFileName}.webp`, this.#filePath, { type: "image", notify: false });
}

/**
* Load a json file with terrain data.
* @returns {object|undefined} The data object unless an error occurs, then undefined.
* @returns {object|undefined} The data object unless an error occurs or file does not exist, then undefined.
*/
async loadData() {
if ( !(await doesFileExist(this.#filePath, `${this.#dataFileName}.json`)) ) return undefined;

const filePath = `${this.#filePath}/${this.#dataFileName}.json`;
let data;
try {
data = await foundry.utils.fetchJsonWithTimeout(foundry.utils.getRoute(filePath, {prefix: ROUTE_PREFIX}));
} catch (err) {
return undefined;
}
return data;
}
Expand Down Expand Up @@ -259,6 +262,24 @@ export class TerrainFileManager {
}
}


/**
* Determine if a file exists in a folder structure using FilePicker
* @param {string[]} dirs Array of folder names, representing a folder hierarchy
* @param {string} fileName Name of file to locate
* @returns {boolean} True if file exists
*/
export async function doesFileExist(dirPath, fileName) {
let res;
try {
res = await FilePicker.browse("data", dirPath);
} catch(error) {
return false;
}
const path = `${dirPath}/${fileName}`;
return res?.files.some(str => str === path)
}

/**
* Recursively construct a directory path from an array of folders.
* Based in the "data" path.
Expand Down
Loading

0 comments on commit bab67d8

Please sign in to comment.