Skip to content

Commit

Permalink
Implemented preview handling and end-user restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyEdmonds committed Jan 26, 2025
1 parent aa172bc commit 067e1da
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 50 deletions.
21 changes: 16 additions & 5 deletions AuraRingApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,26 @@ export class AuraRingApi
*/
static set(tokenDocument, auraRing)
{
const isPreview = tokenDocument.object.hasPreview === true;

if (isPreview === true) {
tokenDocument = tokenDocument.object._preview.document;
}

const auraRings = AuraRingFlags.getAuraRings(tokenDocument);
const index = AuraRingApi.getAuraRingIndex(auraRings, auraRing.id);

index !== false
? auraRings.splice(index, 1)
: auraRing.id = AuraRingFlags.nextAvailableId(auraRings);
if (auraRing.id === null) {
auraRing.id = AuraRingFlags.nextAvailableId(auraRings);
} else {
const index = AuraRingApi.getAuraRingIndex(auraRings, auraRing.id);

index !== false
? auraRings.splice(index, 1)
: auraRing.id = AuraRingFlags.nextAvailableId(auraRings);
}

auraRings.push(auraRing);
AuraRingFlags.setAuraRings(tokenDocument, auraRings);
AuraRingFlags.setAuraRings(tokenDocument, auraRings, isPreview);
}

/**
Expand Down
80 changes: 46 additions & 34 deletions AuraRingDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,8 @@ export class AuraRingDirectory extends HandlebarsApplicationMixin(ApplicationV2)

static hook = 'directory-update';

hooks = {
createToken: null,
destroyToken: null,
settings: null,
};

selectedTokenId = null;

// TODO Limit directory scroll height

// Setup
constructor(tokenDocument = null, options={}) {
super(options);
Expand All @@ -59,6 +51,30 @@ export class AuraRingDirectory extends HandlebarsApplicationMixin(ApplicationV2)
this.selectedTokenId = tokenDocument?.id ?? null;
}

async close(options = {})
{
this.deregisterHooks();
return super.close(options);
}

registerHooks()
{
Hooks.on('createToken', this.renderDirectory);
Hooks.on('destroyToken', this.renderDirectory);
Hooks.on(AuraRingDirectory.hook, this.renderDirectory);
}

renderDirectory = () => {
this.render();
}

deregisterHooks()
{
Hooks.off('createToken', this.renderDirectory);
Hooks.off('destroyToken', this.renderDirectory);
Hooks.off(AuraRingDirectory.hook, this.renderDirectory);
}

_onRender(context, options)
{
super._onRender(context, options);
Expand All @@ -72,10 +88,16 @@ export class AuraRingDirectory extends HandlebarsApplicationMixin(ApplicationV2)
const tokens = [];

for (let index = 0; index < tokenDocuments.length; ++index) {
const token = tokenDocuments[index];

if (token.isOwner === false) {
continue;
}

tokens.push({
id: tokenDocuments[index].id,
name: tokenDocuments[index].name,
selected: tokenDocuments[index].id === this.selectedTokenId,
id: token.id,
name: token.name,
selected: token.id === this.selectedTokenId,
})
}

Expand All @@ -89,12 +111,6 @@ export class AuraRingDirectory extends HandlebarsApplicationMixin(ApplicationV2)
};
}

async close(options = {})
{
this.deregisterHooks();
super.close(options);
}

static register()
{
game.settings.register(AuraRingFlags.namespace, AuraRingDirectory.name, {
Expand All @@ -107,20 +123,14 @@ export class AuraRingDirectory extends HandlebarsApplicationMixin(ApplicationV2)
Hooks.call(AuraRingDirectory.hook);
},
});
}

registerHooks()
{
this.hooks.createToken = Hooks.on('createToken', this.render.bind(this));
this.hooks.destroyToken = Hooks.on('destroyToken', this.render.bind(this));
this.hooks.settings = Hooks.on(AuraRingDirectory.hook, this.render.bind(this));
}

deregisterHooks()
{
Hooks.off('createToken', this.hooks.createToken);
Hooks.off('destroyToken', this.hooks.destroyToken);
Hooks.off(AuraRingDirectory.hook, this.hooks.settings);
game.settings.registerMenu(AuraRingFlags.namespace, 'open-directory', {
name: "Aura Ring Directory",
label: "Open...",
hint: "Open the Aura Ring Directory to apply and manage your stored Aura Rings.",
icon: "fas fa-folder-open",
type: AuraRingDirectory,
});
}

// Handlers
Expand Down Expand Up @@ -175,9 +185,6 @@ export class AuraRingDirectory extends HandlebarsApplicationMixin(ApplicationV2)
const tokenDocument = game.scenes.current.tokens.get(tokenId);

AuraRingApi.set(tokenDocument, auraRing);

// TODO May need to handle previews differently
// Correct. Currently causes preview to desync from original
}

/**
Expand All @@ -196,11 +203,16 @@ export class AuraRingDirectory extends HandlebarsApplicationMixin(ApplicationV2)
*/
static get(name)
{
return AuraRingApi.getAuraRing(
let auraRing = AuraRingApi.getAuraRing(
AuraRingDirectory.all(),
name,
'name',
);

auraRing = foundry.utils.deepClone(auraRing);
auraRing.id = null;

return auraRing;
}

/**
Expand Down
16 changes: 14 additions & 2 deletions AuraRingFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export class AuraRingFlags
{
static auraRingsKey = 'aura-rings';

static hook = 'flags-updated';

static namespace = 'token-aura-ring';

// Flags
Expand All @@ -26,9 +28,19 @@ export class AuraRingFlags
return tokenDocument.flags[AuraRingFlags.namespace].hasOwnProperty(AuraRingFlags.auraRingsKey) === true;
}

static setAuraRings(tokenDocument, auraRings)
/**
* Set the TokenDocument's Aura Ring Flag
* @param {TokenDocument} tokenDocument
* @param {AuraRing} auraRings
* @param {boolean} directly Whether to set the flag directly, to avoid re-rendering
*/
static setAuraRings(tokenDocument, auraRings, directly = false)
{
tokenDocument.setFlag(AuraRingFlags.namespace, AuraRingFlags.auraRingsKey, auraRings);
directly === true
? tokenDocument.flags[AuraRingFlags.namespace][AuraRingFlags.auraRingsKey] = auraRings
: tokenDocument.setFlag(AuraRingFlags.namespace, AuraRingFlags.auraRingsKey, auraRings);

Hooks.call(AuraRingFlags.hook);
}

// Auras
Expand Down
23 changes: 22 additions & 1 deletion AuraRingSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,33 @@ export class AuraRingSettings extends HandlebarsApplicationMixin(ApplicationV2)
}

// Setup
constructor(simpleTokenDocument, options={}) {
constructor(simpleTokenDocument, options = {}) {
super(options);
this.registerHooks();
this.preview = simpleTokenDocument;
this.auraRings = AuraRingFlags.getAuraRings(this.preview);
}

async close(options = {})
{
this.deregisterHooks();
return super.close(options);
}

registerHooks()
{
Hooks.on(AuraRingFlags.hook, this.renderDirectory);
}

renderDirectory = () => {
this.render();
}

deregisterHooks()
{
Hooks.off(AuraRingFlags.hook, this.renderDirectory);
}

_onChangeForm(context, event)
{
if (event.target.form !== null) {
Expand Down
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,33 @@ For example, a standard medium creature (1 square = 5 foot) can have a minimum r

## Directory

A global Aura Ring Directory is provided to allow for defining and reusing common Aura Rings which you may want to use across many tokens.
A global Aura Ring Directory is provided to allow for defining Aura Rings for use across many tokens.

![An example of the Aura Ring Directory](images/directory.jpg)

You can pick which token to add Aura Rings to from the dropdown, which lists all Tokens in the current scene.
### Opening the Directory

If the Directory was opened from an Aura Ring Configuration screen, the current Token will be automatically selected.
A button to open the Aura Ring Directory is provided in the main "Configure Settings" and on Aura Ring Configuration screens.

You can then add any number of Aura Rings to that token.
You can also open the Directory using a macro, which is provided in the `API` section.

### Applying Aura Rings

You can pick which token to add Aura Rings to from the dropdown, which lists all tokens and token previews in the current scene.

If the Directory was opened from an Aura Ring Configuration screen, the current token preview will be automatically selected.

Pressing the "Add" button will copy the Aura Ring from the directory onto the selected token.

You can also rename and remove any Aura Rings from the directory.

To overwrite an existing Aura Ring, simply save an Aura Ring with the same name.
### Library Management

To overwrite an existing Aura Ring, save an Aura Ring with the same name.

You will be prompted to change the name, in case you did not intend to overwrite an existing Aura Ring.

You can open the Directory without going through a Token using a macro, which is provided in the `API` section.
Pressing the "Remove" button will immediately delete that Aura Ring from the Directory.

## API

Expand Down
4 changes: 2 additions & 2 deletions directory.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ <h4 style="border-bottom: 1px solid var(--color-underline-active);">Selected Tok

<p class="hint">Select the token to add Aura Rings to.</p>
{{else}}
<p>There are no Tokens in the current scene.</p>
<p>Add a Token to the scene and reload the Directory.</p>
<p>You do not control any Tokens in the current scene.</p>
<p>Add an owned Token to the scene to add Aura Rings.</p>
{{/if}}

<h4 style="border-bottom: 1px solid var(--color-underline-active);">Saved Aura Rings</h4>
Expand Down
Binary file added images/directory.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 067e1da

Please sign in to comment.