-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuraRingApi.js
236 lines (209 loc) · 6.89 KB
/
AuraRingApi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { AuraRingDataModel } from "./AuraRingDataModel.js";
import { AuraRingDirectory } from "./AuraRingDirectory.js";
import { AuraRingFlags } from "./AuraRingFlags.js";
export class AuraRingApi
{
/**
* TODO Make an actual class
* A Token Aura Ring
* @typedef {Object} AuraRing
* @property {number} angle How wide the Aura Ring should be, from 5 to 360 degrees
* @property {number} direction Which way the Aura Ring should face, from -180 to 180 degrees
* @property {string} fill_colour The fill colour in hex
* @property {number} fill_opacity The fill opacity as a fraction between 0 and 1
* @property {boolean} hide Whether the Aura Ring should be shown
* @property {number} id The unique numeric identifier of the Aura Ring
* @property {string} name The display name of the Aura Ring
* @property {number} radius The radius of the Aura Ring, from 0
* @property {boolean} respect_fog Whether to hide the Aura Ring when the eminating token cannot be seen
* @property {boolean} stroke_close Whether to stroke the complete outline of the Aura Ring
* @property {string} stroke_colour The stroke colour in hex
* @property {number} stroke_opacity The stroke opacity as a fraction between 0 and 1
* @property {number} stroke_weight The stroke weight in pixels, from 0
* @property {boolean} use_grid_shapes Whether to use grid shapes, if enabled
* @property {string} visibility Which user roles can see the Aura Ring
*/
/**
* Retrieve all Aura Rings
*
* @param {TokenDocument} tokenDocument
*
* @returns {Array[AuraRing]}
*/
static all(tokenDocument)
{
return AuraRingFlags.getAuraRings(tokenDocument);
}
/**
* Get an unsaved empty Aura Ring without an ID
*
* @returns {AuraRing}
*/
static blank()
{
return AuraRingDataModel.defaultSettings();
}
/**
* Remove an Aura Ring
*
* @param {TokenDocument*} tokenDocument
* @param {number} id
*/
static delete(tokenDocument, id)
{
const auraRings = AuraRingFlags.getAuraRings(tokenDocument);
const index = AuraRingApi.getAuraRingIndex(auraRings, id);
if (index !== false) {
auraRings.splice(index, 1);
AuraRingFlags.setAuraRings(tokenDocument, auraRings);
}
}
/**
* Remove all Aura Rings
*
* @param {TokenDocument} tokenDocument
*/
static deleteAll(tokenDocument)
{
AuraRingFlags.setAuraRings(tokenDocument, []);
}
/**
* Open the Aura Ring Directory
*
* @param {TokenDocument|null} tokenDocument
*/
static directory(tokenDocument = null)
{
AuraRingDirectory.open(tokenDocument);
}
/**
* Retrieve a specific Aura Ring
*
* @param {TokenDocument} tokenDocument
* @param {number|string} term
* @param {string} field
*
* @returns {AuraRing|false}
*/
static get(tokenDocument, term, field = 'id')
{
const auraRings = AuraRingFlags.getAuraRings(tokenDocument);
return AuraRingApi.getAuraRing(auraRings, term, field);
}
/**
* Retrieve an Aura Ring by any field
*
* @param {AuraRing[]} auraRings
* @param {number|string} term
* @param {string} field
* @returns {AuraRing|false}
*/
static getAuraRing(auraRings, term, field = 'id')
{
for (const auraRing of auraRings) {
if (auraRing[field] == term) {
return auraRing;
}
}
return false;
}
/**
* Retrieve the index of an Aura Ring by any field
*
* @param {AuraRing[]} auraRings
* @param {number|string} term
* @param {string} field
* @returns {AuraRing|false}
*/
static getAuraRingIndex(auraRings, term, field = 'id')
{
for (let index = 0; index < auraRings.length; ++index) {
if (auraRings[index][field] == term) {
return index;
}
}
return false;
}
/**
* Retrieve a list of Aura Ring names keyed by their ID
*
* @param {TokenDocument} tokenDocument
*
* @returns {Object}
*/
static index(tokenDocument)
{
const auraRings = AuraRingFlags.getAuraRings(tokenDocument);
const index = {};
for (const auraRing of auraRings) {
index[auraRing.id] = auraRing.name;
}
return index;
}
/**
* Create a new Aura Ring from the default settings
* The ID of the Aura Ring will be set to the next available
*
* @param {TokenDocument} tokenDocument
*
* @returns {AuraRing} As given, but with the new ID
*/
static new(tokenDocument)
{
const auraRings = AuraRingFlags.getAuraRings(tokenDocument);
const auraRing = AuraRingDataModel.defaultSettings();
auraRing.id = AuraRingFlags.nextAvailableId(auraRings);
auraRings.push(auraRing);
AuraRingFlags.setAuraRings(tokenDocument, auraRings);
return auraRing;
}
/**
* Add or overwrite an Aura Ring
*
* @param {TokenDocument} tokenDocument
* @param {AuraRing} auraRing
*/
static set(tokenDocument, auraRing)
{
const isPreview = tokenDocument.object.hasPreview === true;
if (isPreview === true) {
tokenDocument = tokenDocument.object._preview.document;
}
const auraRings = AuraRingFlags.getAuraRings(tokenDocument);
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, isPreview);
}
/**
* Overwrite all Aura Rings with a new set
*
* @param {TokenDocument} tokenDocument
* @param {Array[AuraRing]} auraRings
*/
static setAll(tokenDocument, auraRings)
{
AuraRingFlags.setAuraRings(tokenDocument, auraRings);
}
/**
* Update a specific Aura Ring value directly
*
* @param {TokenDocument} tokenDocument
* @param {number} id
* @param {string} key
* @param {number|string|boolean} value
*/
static setValue(tokenDocument, id, key, value)
{
const auraRings = AuraRingFlags.getAuraRings(tokenDocument);
const index = this.getAuraRingIndex(auraRings, id);
auraRings[index][key] = value;
AuraRingFlags.setAuraRings(tokenDocument, auraRings);
}
}