Skip to content

Commit

Permalink
feat: add setting to make "Clear Drawings" button clear only the acti…
Browse files Browse the repository at this point in the history
…ve layer
  • Loading branch information
ghost91- committed Oct 14, 2021
1 parent 97e84fd commit 9e02d37
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 30 deletions.
2 changes: 2 additions & 0 deletions src/lang/de.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"foreground-drawings.CONTROLS.ClearDrawingsOnlyOnActiveLayerSetting": "Nur Zeichnungen der aktiven Ebene löschen",
"foreground-drawings.CONTROLS.ClearDrawingsOnlyOnActiveLayerSettingHint": "Ist diese Einstellung aktiv, löscht der \"Zeichnugen Löschen\" Schalter nur die Zeichnungen der aktiven Zeichnungs-Ebene.",
"foreground-drawings.CONTROLS.DrawingForeground": "Vordergund-Zeichnungs-Ebene",
"foreground-drawings.HUD.DrawingBackground": "Hintergrund-Zeichnung",
"foreground-drawings.HUD.DrawingForeground": "Vordergrund-Zeichnung"
Expand Down
2 changes: 2 additions & 0 deletions src/lang/en.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"foreground-drawings.CONTROLS.ClearDrawingsOnlyOnActiveLayerSetting": "Only Clear Drawings of the Active Layer",
"foreground-drawings.CONTROLS.ClearDrawingsOnlyOnActiveLayerSettingHint": "If enabled, the \"Clear Drawings\" button clears only the drawings of the active drawings layer.",
"foreground-drawings.CONTROLS.DrawingForeground": "Foreground Drawings Layer",
"foreground-drawings.HUD.DrawingBackground": "Background Drawing",
"foreground-drawings.HUD.DrawingForeground": "Foreground Drawing"
Expand Down
29 changes: 29 additions & 0 deletions src/module/delete-all-drawings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT

import { getGame } from './helpers';

export async function deleteAllDrawings(
{ foreground } = { foreground: false },
): Promise<DrawingDocument[] | false | null> {
const type = 'Drawing';
const game = getGame();
if (!game.user?.isGM) {
throw new Error(`You do not have permission to delete ${type} objects from the Scene.`);
}
return Dialog.confirm({
title: game.i18n.localize('CONTROLS.ClearAll'),
content: `<p>${game.i18n.format('CONTROLS.ClearAllHint', {
type: foreground ? `Foreground ${type}` : `Background ${type}`,
})}</p>`,
yes: () =>
canvas?.scene?.deleteEmbeddedDocuments(
type,
canvas.scene.drawings
.filter((drawing) => (drawing.data.flags['foreground-drawings']?.foreground ?? false) === foreground)
.map((drawing) => drawing.id)
.filter((id): id is string => !!id),
) ?? null,
}) as Promise<DrawingDocument[] | false | null>;
}
19 changes: 15 additions & 4 deletions src/module/foreground-drawings-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// SPDX-License-Identifier: MIT

import { packageName } from './const';
import { deleteAllDrawings } from './delete-all-drawings';
import { getGame } from './helpers';

export class ForegroundDrawingsLayer extends DrawingsLayer {
/** @override */
Expand All @@ -14,19 +16,28 @@ export class ForegroundDrawingsLayer extends DrawingsLayer {
}

/** @override */
deactivate() {
super.deactivate();
activate() {
super.activate();
this.refresh();
return this;
}

/** @override */
activate() {
super.activate();
deactivate() {
super.deactivate();
this.refresh();
return this;
}

/** @override */
async deleteAll() {
if (getGame().settings.get(packageName, 'clearDrawingsOnlyOnActiveLayer')) {
return deleteAllDrawings({ foreground: true });
} else {
return super.deleteAll();
}
}

/** @override */
_getNewDrawingData(...args: Parameters<DrawingsLayer['_getNewDrawingData']>) {
const newDrawingData = super._getNewDrawingData(...args);
Expand Down
6 changes: 6 additions & 0 deletions src/module/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ declare global {
interface Canvas {
foregroundDrawings: ForegroundDrawingsLayer | undefined;
}

namespace ClientSettings {
interface Values {
'foreground-drawings.clearDrawingsOnlyOnActiveLayer': boolean;
}
}
}
17 changes: 17 additions & 0 deletions src/module/hooks/get-scene-control-buttons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
//
// SPDX-License-Identifier: MIT

import { packageName } from '../const';
import { getGame } from '../helpers';

export default function registerForGetSceneControlButtonsHook() {
Hooks.on('getSceneControlButtons', getSceneControlButtons);
}
Expand All @@ -21,4 +24,18 @@ function getSceneControlButtons(controls: SceneControl[]) {
active: canvas?.foregroundDrawings?._active ?? false,
onClick: (toggled: boolean) => canvas?.[toggled ? 'foregroundDrawings' : 'drawings']?.activate(),
});

const game = getGame();
if (game.settings.get(packageName, 'clearDrawingsOnlyOnActiveLayer')) {
const clearTool = drawings.tools.find((tool) => tool.name === 'clear');
if (clearTool) {
clearTool.onClick = () => {
if (canvas?.drawings?._active) {
canvas.drawings.deleteAll();
} else if (canvas?.foregroundDrawings?._active) {
canvas.foregroundDrawings.deleteAll();
}
};
}
}
}
22 changes: 4 additions & 18 deletions src/module/hooks/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,16 @@

import { packageName } from '../const';
import { ForegroundDrawingsLayer } from '../foreground-drawings-layer';
import { libWrapper } from '../shims/libWrapperShim';
import { onUpdate } from '../wrappers/drawing';
import { getLayer } from '../wrappers/drawing-document';
import { onClickControl } from '../wrappers/drawing-hud';
import { getDocuments } from '../wrappers/drawings-layer';
import registerSettings from '../settings';
import registerWrappers from '../wrappers';

export default function registerForInitHook(): void {
Hooks.once('init', init);
}

function init() {
logger.info(`Initializing ${packageName}`);

CONFIG.Canvas.layers.foregroundDrawings = ForegroundDrawingsLayer;

libWrapper.register(packageName, 'DrawingsLayer.prototype.getDocuments', getDocuments, 'WRAPPER');

const layerTarget = 'DrawingDocument.prototype.layer';
try {
libWrapper.register(packageName, layerTarget, getLayer, 'OVERRIDE');
} catch (e) {
logger.warn(`Failed to override ${layerTarget}, some things might not work correctly:`, e);
}

libWrapper.register(packageName, 'DrawingHUD.prototype._onClickControl', onClickControl, 'WRAPPER');
libWrapper.register(packageName, 'Drawing.prototype._onUpdate', onUpdate, 'WRAPPER');
registerSettings();
registerWrappers();
}
19 changes: 19 additions & 0 deletions src/module/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT

import { packageName } from './const';
import { getGame } from './helpers';

export default function registerSettings() {
const game = getGame();
game.settings.register(packageName, 'clearDrawingsOnlyOnActiveLayer', {
name: 'foreground-drawings.CONTROLS.ClearDrawingsOnlyOnActiveLayerSetting',
hint: 'foreground-drawings.CONTROLS.ClearDrawingsOnlyOnActiveLayerSettingHint',
scope: 'client',
config: true,
type: Boolean,
default: true,
onChange: () => window.location.reload(),
});
}
14 changes: 13 additions & 1 deletion src/module/wrappers/drawing-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
//
// SPDX-License-Identifier: MIT

export function getLayer(this: DrawingDocument) {
import { packageName } from '../const';
import { libWrapper } from '../shims/libWrapperShim';

export default function registerDrawingDocumentWrappers() {
const layerTarget = 'DrawingDocument.prototype.layer';
try {
libWrapper.register(packageName, layerTarget, getLayer, 'OVERRIDE');
} catch (e) {
logger.warn(`Failed to override ${layerTarget}, some things might not work correctly:`, e);
}
}

function getLayer(this: DrawingDocument) {
return this.data.flags['foreground-drawings']?.foreground ? canvas?.foregroundDrawings : canvas?.drawings;
}
11 changes: 6 additions & 5 deletions src/module/wrappers/drawing-hud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
// SPDX-License-Identifier: MIT

import { packageName } from '../const';
import { libWrapper } from '../shims/libWrapperShim';

export function onClickControl(
this: DrawingHUD,
wrapped: (event: JQuery.ClickEvent) => unknown,
event: JQuery.ClickEvent,
) {
export default function registerDrawingHUDWrappers() {
libWrapper.register(packageName, 'DrawingHUD.prototype._onClickControl', onClickControl, 'WRAPPER');
}

function onClickControl(this: DrawingHUD, wrapped: (event: JQuery.ClickEvent) => unknown, event: JQuery.ClickEvent) {
wrapped(event);
if (event.isDefaultPrevented()) {
return;
Expand Down
7 changes: 6 additions & 1 deletion src/module/wrappers/drawing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
// SPDX-License-Identifier: MIT

import { packageName } from '../const';
import { libWrapper } from '../shims/libWrapperShim';

export function onUpdate(
export default function registerDrawingWrappers() {
libWrapper.register(packageName, 'Drawing.prototype._onUpdate', onUpdate, 'WRAPPER');
}

function onUpdate(
this: Drawing,
wrapped: (data: DeepPartial<foundry.data.DrawingData['_source']>) => void,
data: DeepPartial<foundry.data.DrawingData['_source']>,
Expand Down
23 changes: 22 additions & 1 deletion src/module/wrappers/drawings-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,28 @@
// SPDX-License-Identifier: MIT

import { packageName } from '../const';
import { deleteAllDrawings } from '../delete-all-drawings';
import { getGame } from '../helpers';
import { libWrapper } from '../shims/libWrapperShim';

export function getDocuments(wrapped: DrawingsLayer['getDocuments']) {
export default function registerDrawingsLayerWrappers() {
libWrapper.register(packageName, 'DrawingsLayer.prototype.getDocuments', getDocuments, 'WRAPPER');

const game = getGame();
if (game.settings.get(packageName, 'clearDrawingsOnlyOnActiveLayer')) {
const deleteAllTarget = 'DrawingsLayer.prototype.deleteAll';
try {
libWrapper.register(packageName, deleteAllTarget, deleteAll, 'OVERRIDE');
} catch (e) {
logger.warn(`Failed to override ${deleteAllTarget}, some things might not work correctly:`, e);
}
}
}

function getDocuments(wrapped: DrawingsLayer['getDocuments']) {
return wrapped().filter((doc) => !doc.data.flags[packageName]?.foreground);
}

async function deleteAll() {
return deleteAllDrawings({ foreground: false });
}
15 changes: 15 additions & 0 deletions src/module/wrappers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT

import registerDrawingWrappers from './drawing';
import registerGetLayerWrapper from './drawing-document';
import registerDrawingHUDWrappers from './drawing-hud';
import registerDrawingsLayerWrappers from './drawings-layer';

export default function registerWrappers() {
registerDrawingsLayerWrappers();
registerGetLayerWrapper();
registerDrawingHUDWrappers();
registerDrawingWrappers();
}

0 comments on commit 9e02d37

Please sign in to comment.