-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_Base.js.html
616 lines (514 loc) · 21 KB
/
base_Base.js.html
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: base/Base.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: base/Base.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*
* Copyright 2020 WICKLETS LLC
*
* This file is part of Wick Engine.
*
* Wick Engine is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Wick Engine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Wick Engine. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* The base class for all objects within the Wick Engine.
*/
Wick.Base = class {
/**
* Creates a Base object.
* @parm {string} identifier - (Optional) The identifier of the object. Defaults to null.
* @parm {string} name - (Optional) The name of the object. Defaults to null.
*/
constructor (args) {
/* One instance of each Wick.Base class is created so we can access
* a list of all possible properties of each class. This is used
* to clean up custom variables after projects are stopped. */
if(!Wick._originals[this.classname]) {
Wick._originals[this.classname] = {};
Wick._originals[this.classname] = new Wick[this.classname];
}
if(!args) args = {};
this._uuid = args.uuid || uuidv4();
this._identifier = args.identifier || null;
this._name = args.name || null;
this._view = null;
this.view = this._generateView();
this._guiElement = null;
this.guiElement = this._generateGUIElement();
this._classname = this.classname;
this._children = {};
this._childrenData = null;
this._parent = null;
this._project = this.classname === 'Project' ? this : null;
this.needsAutosave = true;
this._cachedSerializeData = null;
Wick.ObjectCache.addObject(this);
}
/**
* @param {object} data - Serialized data to use to create a new object.
*/
static fromData (data) {
if(!data.classname) {
console.warn('Wick.Base.fromData(): data was missing, did you mean to deserialize something else?');
}
if(!Wick[data.classname]) {
console.warn('Tried to deserialize an object with no Wick class: ' + data.classname);
}
var object = new Wick[data.classname]({uuid: data.uuid});
object.deserialize(data);
if (data.classname === 'Project') {
object.initialize();
}
return object;
}
/**
* Converts this Wick Base object into a plain javascript object contianing raw data (no references).
* @return {object} Plain JavaScript object representing this Wick Base object.
*/
serialize (args) {
// TEMPORARY: Force the cache to never be accessed.
// This is because the cache was causing issues in the tests, and the
// performance boost that came with the cache was not signifigant enough
// to be worth fixing the bugs over...
this.needsAutosave = true;
if(this.needsAutosave || !this._cachedSerializeData) {
// If the cache is outdated or does not exist, reserialize and cache.
var data = this._serialize(args);
this._cacheSerializeData(data);
return data;
} else {
// Otherwise, just read from the cache
return this._cachedSerializeData;
}
}
/**
* Parses serialized data representing Base Objects which have been serialized using the serialize function of their class.
* @param {object} data Serialized data that was returned by a Base Object's serialize function.
*/
deserialize (data) {
this._deserialize(data);
this._cacheSerializeData(data);
}
/* The internal serialize method that actually creates the data. Every class that inherits from Base must have one of these. */
_serialize (args) {
var data = {};
data.classname = this.classname;
data.identifier = this._identifier;
data.name = this._name;
data.uuid = this._uuid;
data.children = this.getChildren().map(child => { return child.uuid });
return data;
}
/* The internal deserialize method that actually reads the data. Every class that inherits from Base must have one of these. */
_deserialize (data) {
this._uuid = data.uuid;
this._identifier = data.identifier;
this._name = data.name;
this._children = {};
this._childrenData = data.children;
// Clear any custom attributes set by scripts
var compareObj = Wick._originals[this.classname];
for (var name in this) {
if(compareObj[name] === undefined) {
delete this[name];
}
}
}
_cacheSerializeData (data) {
this._cachedSerializeData = data;
this.needsAutosave = false;
}
/**
* Returns a copy of a Wick Base object.
* @return {Wick.Base} The object resulting from the copy
*/
copy () {
var data = this.serialize();
data.uuid = uuidv4();
var copy = Wick.Base.fromData(data);
copy._childrenData = null;
// Copy children
this.getChildren().forEach(child => {
copy.addChild(child.copy());
});
return copy;
}
/**
* Returns an object containing serialied data of this object, as well as all of its children.
* Use this to copy entire Wick.Base objects between projects, and to export individual Clips as files.
* @returns {object} The exported data.
*/
export () {
var copy = this.copy();
copy._project = this.project;
// the main object
var object = copy.serialize();
// children
var children = copy.getChildrenRecursive().map(child => {
return child.serialize();
});
// assets
var assets = [];
copy.getChildrenRecursive().concat(copy).forEach(child => {
child._project = copy._project;
child.getLinkedAssets().forEach(asset => {
assets.push(asset.serialize({includeOriginalSource: true}));
});
});
return {
object: object,
children: children,
assets: assets,
};
}
/**
* Import data created using Wick.Base.export().
* @param {object} exportData - an object created from Wick.Base.export().
*/
static import (exportData, project) {
if(!exportData) console.error('Wick.Base.import(): exportData is required');
if(!exportData.object) console.error('Wick.Base.import(): exportData is missing data');
if(!exportData.children) console.error('Wick.Base.import(): exportData is missing data');
var object = Wick.Base.fromData(exportData.object);
// Import children as well
exportData.children.forEach(childData => {
// Only need to call deserialize here, we just want the object to get added to ObjectCache
var child = Wick.Base.fromData(childData);
});
// Also import linked assets
exportData.assets.forEach(assetData => {
// Don't import assets if they exist in the project already
// (Assets only get reimported when objects are pasted between projects)
if(project.getAssetByUUID(assetData.uuid)) {
return;
}
var asset = Wick.Base.fromData(assetData);
project.addAsset(asset);
});
return object;
}
/**
* Marks the object as possibly changed, so that next time autosave happens, this object is written to the save.
* @type {boolean}
*/
set needsAutosave (needsAutosave) {
if(needsAutosave) {
Wick.ObjectCache.markObjectToBeAutosaved(this);
} else {
Wick.ObjectCache.clearObjectToBeAutosaved(this);
}
}
get needsAutosave () {
return Wick.ObjectCache.objectNeedsAutosave(this);
}
/**
* Returns the classname of a Wick Base object.
* @type {string}
*/
get classname () {
return 'Base';
}
/**
* The uuid of a Wick Base object.
* @type {string}
*/
get uuid () {
return this._uuid;
}
set uuid (uuid) {
// Please try to avoid using this unless you absolutely have to ;_;
this._uuid = uuid;
Wick.ObjectCache.addObject(this);
}
/**
* The name of the object that is used to access the object through scripts. Must be a valid JS variable name.
* @type {string}
*/
get identifier () {
return this._identifier;
}
set identifier (identifier) {
// Treat empty string identifier as null
if(identifier === '' || identifier === null) {
this._identifier = null;
return;
}
// Make sure the identifier doesn't squash any attributes of the window
if(this._identifierNameExistsInWindowContext(identifier)) return;
// Make sure the identifier will not be squashed by Wick API functions
if(this._identiferNameIsPartOfWickAPI(identifier)) return;
// Make sure the identifier is a valid js variable name
if(!isVarName(identifier)) {
this.project && this.project.errorOccured('Identifier must be a valid variable name.');
return;
}
// Make sure the identifier is not a reserved word in js
if(reserved.check(identifier)) return;
// Ensure no objects with duplicate identifiers can exist
this._identifier = this._getUniqueIdentifier(identifier);
}
/**
* The name of the object.
* @type {string}
*/
get name () {
return this._name;
}
set name (name) {
if(typeof name !== 'string') return;
if(name === '') this._name = null;
this._name = name;
}
/**
* The Wick.View object that is used for rendering this object on the canvas.
*/
get view () {
return this._view;
}
set view (view) {
if(view) view.model = this;
this._view = view;
}
/**
* The object that is used for rendering this object in the timeline GUI.
*/
get guiElement () {
return this._guiElement;
}
set guiElement (guiElement) {
if(guiElement) guiElement.model = this;
this._guiElement = guiElement;
}
/**
* Returns a single child of this object with a given classname.
* @param {string} classname - the classname to use
*/
getChild (classname) {
return this.getChildren(classname)[0];
}
/**
* Gets all children with a given classname(s).
* @param {Array|string} classname - (optional) A string, or list of strings, of classnames.
*/
getChildren (classname) {
// Lazily generate children list from serialized data
if(this._childrenData) {
this._childrenData.forEach(uuid => {
this.addChild(Wick.ObjectCache.getObjectByUUID(uuid));
});
this._childrenData = null;
}
if (classname instanceof Array) {
var children = [];
classname.forEach(classnameSeek => {
children = children.concat(this.getChildren(classnameSeek));
});
return children;
} else if(classname === undefined) {
// Retrieve all children if no classname was given
var allChildren = [];
for(var classnameSeek in this._children) {
allChildren = allChildren.concat(this._children[classnameSeek]);
}
return allChildren;
} else {
// Retrieve children by classname
return this._children[classname] || [];
}
}
/**
* Get an array of all children of this object, and the children of those children, recursively.
* @type {Wick.Base[]}
*/
getChildrenRecursive () {
var children = this.getChildren();
this.getChildren().forEach(child => {
children = children.concat(child.getChildrenRecursive());
});
return children;
}
/**
* The parent of this object.
* @type {Wick.Base}
*/
get parent () {
return this._parent;
}
/**
* The parent Clip of this object.
* @type {Wick.Clip}
*/
get parentClip () {
return this._getParentByClassName('Clip');
}
/**
* The parent Layer of this object.
* @type {Wick.Layer}
*/
get parentLayer () {
return this._getParentByClassName('Layer');
}
/**
* The parent Frame of this object.
* @type {Wick.Frame}
*/
get parentFrame () {
return this._getParentByClassName('Frame');
}
/**
* The parent Timeline of this object.
* @type {Wick.Timeline}
*/
get parentTimeline () {
return this._getParentByClassName('Timeline');
}
/**
* The project that this object belongs to. Can be null if the object is not in a project.
* @type {Wick.Project}
*/
get project () {
return this._project;
}
/**
* Check if an object is selected or not.
* @type {boolean}
*/
get isSelected () {
if(!this.project) return false;
return this.project.selection.isObjectSelected(this);
}
/**
* Add a child to this object.
* @param {Wick.Base} child - the child to add.
*/
addChild (child) {
var classname = child.classname;
if(!this._children[classname]) {
this._children[classname] = [];
}
child._parent = this;
child._setProject(this.project);
this._children[classname].push(child);
}
/**
* Remove a child from this object.
* @param {Wick.Base} child - the child to remove.
*/
removeChild (child) {
var classname = child.classname;
if(!this._children[classname]) {
return;
}
child._parent = null;
child._project = null;
this._children[classname] = this._children[classname].filter(seekChild => {
return seekChild !== child;
});
}
/**
* Assets attached to this object.
* @returns {Wick.Base[]}
*/
getLinkedAssets () {
// Implemented by Wick.Frame and Wick.Clip
return [];
}
_generateView () {
var viewClass = Wick.View[this.classname];
if(viewClass) {
return new viewClass(this);
} else {
return null;
}
}
_generateGUIElement () {
var guiElementClass = Wick.GUIElement[this.classname];
if(guiElementClass && guiElementClass !== Wick.Button) {
return new guiElementClass(this);
} else {
return null;
}
}
_getParentByClassName (classname) {
if(!this.parent) return null;
if(this.parent instanceof Wick[classname]) {
return this.parent;
} else {
if(!this.parent._getParentByClassName) return null;
return this.parent._getParentByClassName(classname);
}
}
_setProject (project) {
this._project = project;
this.getChildren().forEach(child => {
child._setProject(project);
});
}
_getUniqueIdentifier (identifier) {
if(!this.parent) return identifier;
var otherIdentifiers = this.parent.getChildren(['Clip','Frame','Button']).filter(child => {
return child !== this && child.identifier;
}).map(child => {
return child.identifier;
});
if(otherIdentifiers.indexOf(identifier) === -1) {
return identifier;
} else {
return this._getUniqueIdentifier(identifier + '_copy');
}
}
_identifierNameExistsInWindowContext (identifier) {
if(window[identifier]) {
return true;
} else {
return false;
}
}
_identiferNameIsPartOfWickAPI (identifier) {
var globalAPI = new GlobalAPI(this);
if(globalAPI[identifier]) {
return true;
} else {
return false;
}
}
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BuiltinAssets.html">BuiltinAssets</a></li><li><a href="GlobalAPI.html">GlobalAPI</a></li><li><a href="paper.SelectionGUI.html">SelectionGUI</a></li><li><a href="paper-paper.Selection.html">Selection</a></li><li><a href="SelectionWidget.html">SelectionWidget</a></li><li><a href="Wick.AutoSave.html">AutoSave</a></li><li><a href="Wick.Base.html">Base</a></li><li><a href="Wick.Button.html">Button</a></li><li><a href="Wick.Clip.html">Clip</a></li><li><a href="Wick.Clipboard.html">Clipboard</a></li><li><a href="Wick.FileCache.html">FileCache</a></li><li><a href="Wick.Frame.html">Frame</a></li><li><a href="Wick.GUIElement.Project.html">Project</a></li><li><a href="Wick.GUIElement.Timeline.html">Timeline</a></li><li><a href="Wick.GUIElement-Wick.GUIElement.Breadcrumbs.html">Breadcrumbs</a></li><li><a href="Wick.GUIElement-Wick.GUIElement.Button.html">Button</a></li><li><a href="Wick.History.html">History</a></li><li><a href="Wick.HTMLExport.html">HTMLExport</a></li><li><a href="Wick.HTMLPreview.html">HTMLPreview</a></li><li><a href="Wick.ImageSequence.html">ImageSequence</a></li><li><a href="Wick.Layer.html">Layer</a></li><li><a href="Wick.Path.html">Path</a></li><li><a href="Wick.Project.html">Project</a></li><li><a href="Wick.Selection.html">Selection</a></li><li><a href="Wick.Tickable.html">Tickable</a></li><li><a href="Wick.Timeline.html">Timeline</a></li><li><a href="Wick.Tools-Wick.Tools.Brush.html">Brush</a></li><li><a href="Wick.Tools-Wick.Tools.Cursor.html">Cursor</a></li><li><a href="Wick.Tools-Wick.Tools.Ellipse.html">Ellipse</a></li><li><a href="Wick.Tools-Wick.Tools.Eraser.html">Eraser</a></li><li><a href="Wick.Tools-Wick.Tools.Eyedropper.html">Eyedropper</a></li><li><a href="Wick.Tools-Wick.Tools.FillBucket.html">FillBucket</a></li><li><a href="Wick.Tools-Wick.Tools.Interact.html">Interact</a></li><li><a href="Wick.Tools-Wick.Tools.Line.html">Line</a></li><li><a href="Wick.Tools-Wick.Tools.None.html">None</a></li><li><a href="Wick.Tools-Wick.Tools.Pan.html">Pan</a></li><li><a href="Wick.Tools-Wick.Tools.Pencil.html">Pencil</a></li><li><a href="Wick.Tools-Wick.Tools.Rectangle.html">Rectangle</a></li><li><a href="Wick.Tools-Wick.Tools.Text.html">Text</a></li><li><a href="Wick.Tools-Wick.Tools.Zoom.html">Zoom</a></li><li><a href="Wick.Transformation.html">Transformation</a></li><li><a href="Wick.Tween.html">Tween</a></li><li><a href="Wick.View-Wick.View.Clip.html">Clip</a></li><li><a href="Wick.View-Wick.View.Frame.html">Frame</a></li><li><a href="Wick.View-Wick.View.Path.html">Path</a></li><li><a href="Wick.View-Wick.View.Selection.html">Selection</a></li><li><a href="Wick.WickFile.html">WickFile</a></li><li><a href="Wick.WickObjectFile.html">WickObjectFile</a></li><li><a href="Wick.ZIPExport.html">ZIPExport</a></li><li><a href="WickObjectCache.html">WickObjectCache</a></li><li><a href="Wick-Wick.Asset.html">Asset</a></li><li><a href="Wick-Wick.AudioTrack.html">AudioTrack</a></li><li><a href="Wick-Wick.ClipAsset.html">ClipAsset</a></li><li><a href="Wick-Wick.Color.html">Color</a></li><li><a href="Wick-Wick.FileAsset.html">FileAsset</a></li><li><a href="Wick-Wick.FontAsset.html">FontAsset</a></li><li><a href="Wick-Wick.GIFAsset.html">GIFAsset</a></li><li><a href="Wick-Wick.GUIElement.html">GUIElement</a></li><li><a href="Wick-Wick.ImageAsset.html">ImageAsset</a></li><li><a href="Wick-Wick.SoundAsset.html">SoundAsset</a></li><li><a href="Wick-Wick.SVGAsset.html">SVGAsset</a></li><li><a href="Wick-Wick.Tool.html">Tool</a></li><li><a href="Wick-Wick.ToolSettings.html">ToolSettings</a></li><li><a href="Wick-Wick.View.html">View</a></li></ul><h3>Global</h3><ul><li><a href="global.html#type">type</a></li><li><a href="global.html#Wick">Wick</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Fri Apr 24 2020 15:26:12 GMT-0400 (Eastern Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>