-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_Layer.js.html
377 lines (316 loc) · 15 KB
/
base_Layer.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: base/Layer.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/Layer.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/>.
*/
/**
* Represents a Wick Layer.
*/
Wick.Layer = class extends Wick.Base {
/**
* Called when creating a Wick Layer.
* @param {boolean} locked - Is the layer locked?
* @param {boolean} hideen - Is the layer hidden?
*/
constructor (args) {
if(!args) args = {};
super(args);
this.locked = args.locked === undefined ? false : args.locked;
this.hidden = args.hidden === undefined ? false : args.hidden;
this.name = args.name || null;
}
_serialize (args) {
var data = super._serialize(args);
data.locked = this.locked;
data.hidden = this.hidden;
return data;
}
_deserialize (data) {
super._deserialize(data);
this.locked = data.locked;
this.hidden = data.hidden;
}
get classname () {
return 'Layer';
}
/**
* The frames belonging to this layer.
* @type {Wick.Frame[]}
*/
get frames () {
return this.getChildren('Frame');
}
/**
* The order of the Layer in the timeline.
* @type {number}
*/
get index () {
return this.parent && this.parent.layers.indexOf(this);
}
/**
* Set this layer to be the active layer in its timeline.
*/
activate () {
this.parent.activeLayerIndex = this.index;
}
/**
* True if this layer is the active layer in its timeline.
* @type {boolean}
*/
get isActive () {
return this.parent && this === this.parent.activeLayer;
}
/**
* The length of the layer in frames.
* @type {number}
*/
get length () {
var end = 0;
this.frames.forEach(function (frame) {
if(frame.end > end) {
end = frame.end;
}
});
return end;
}
/**
* The active frame on the layer.
* @type {Wick.Frame}
*/
get activeFrame () {
if(!this.parent) return null;
return this.getFrameAtPlayheadPosition(this.parent.playheadPosition);
}
/**
* Moves this layer to a different position, inserting it before/after other layers if needed.
* @param {number} index - the new position to move the layer to.
*/
move (index) {
this.parentTimeline.moveLayer(this, index);
}
/**
* Remove this layer from its timeline.
*/
remove () {
this.parentTimeline.removeLayer(this);
}
/**
* Adds a frame to the layer.
* @param {Wick.Frame} frame - The frame to add to the Layer.
*/
addFrame (frame) {
this.addChild(frame);
this.resolveOverlap([frame]);
this.resolveGaps([frame]);
}
/**
* Adds a tween to the active frame of this layer (if one exists).
* @param {Wick.Tween} tween - the tween to add
*/
addTween (tween) {
this.activeFrame && this.activeFrame.addChild(tween);
}
/**
* Adds a frame to the layer. If there is an existing frame where the new frame is
* inserted, then the existing frame will be cut, and the new frame will fill the
* gap created by that cut.
* @param {number} playheadPosition - Where to add the blank frame.
*/
insertBlankFrame (playheadPosition) {
if(!playheadPosition) {
throw new Error('insertBlankFrame: playheadPosition is required');
}
var frame = new Wick.Frame({start: playheadPosition});
this.addChild(frame);
// If there is is overlap with an existing frame
var existingFrame = this.getFrameAtPlayheadPosition(playheadPosition);
if (existingFrame) {
// Make sure the new frame fills the empty space
frame.end = existingFrame.end;
}
this.resolveOverlap([frame]);
this.resolveGaps([frame]);
return frame;
}
/**
* Removes a frame from the Layer.
* @param {Wick.Frame} frame Frame to remove.
*/
removeFrame (frame) {
this.removeChild(frame);
this.resolveGaps();
}
/**
* Gets the frame at a specific playhead position.
* @param {number} playheadPosition - Playhead position to search for frame at.
* @return {Wick.Frame} The frame at the given playheadPosition.
*/
getFrameAtPlayheadPosition (playheadPosition) {
return this.frames.find(frame => {
return frame.inPosition(playheadPosition);
}) || null;
}
/**
* Gets all frames in the layer that are between the two given playhead positions.
* @param {number} playheadPositionStart - The start of the range to search
* @param {number} playheadPositionEnd - The end of the range to search
* @return {Wick.Frame[]} The frames in the given range.
*/
getFramesInRange (playheadPositionStart, playheadPositionEnd) {
return this.frames.filter(frame => {
return frame.inRange(playheadPositionStart, playheadPositionEnd);
});
}
/**
* Gets all frames in the layer that are contained within the two given playhead positions.
* @param {number} playheadPositionStart - The start of the range to search
* @param {number} playheadPositionEnd - The end of the range to search
* @return {Wick.Frame[]} The frames contained in the given range.
*/
getFramesContainedWithin (playheadPositionStart, playheadPositionEnd) {
return this.frames.filter(frame => {
return frame.containedWithin(playheadPositionStart, playheadPositionEnd);
});
}
/**
* Prevents frames from overlapping each other by removing pieces of frames that are touching.
* @param {Wick.Frame[]} newOrModifiedFrames - the frames that should take precedence when determining which frames should get "eaten".
*/
resolveOverlap (newOrModifiedFrames) {
newOrModifiedFrames = newOrModifiedFrames || [];
// Ensure that frames never go beyond the beginning of the timeline
newOrModifiedFrames.forEach(frame => {
if(frame.start <= 1) {
frame.start = 1;
}
});
var isEdible = existingFrame => {
return newOrModifiedFrames.indexOf(existingFrame) === -1;
};
newOrModifiedFrames.forEach(frame => {
// "Full eat"
// The frame completely eats the other frame.
var containedFrames = this.getFramesContainedWithin(frame.start, frame.end);
containedFrames.filter(isEdible).forEach(existingFrame => {
existingFrame.remove();
});
// "Right eat"
// The frame takes a chunk out of the right side of another frame.
this.frames.filter(isEdible).forEach(existingFrame => {
if(existingFrame.inPosition(frame.start) && existingFrame.start !== frame.start) {
existingFrame.end = frame.start - 1;
}
});
// "Left eat"
// The frame takes a chunk out of the left side of another frame.
this.frames.filter(isEdible).forEach(existingFrame => {
if(existingFrame.inPosition(frame.end) && existingFrame.end !== frame.end) {
existingFrame.start = frame.end + 1;
}
});
});
}
/**
* Prevents gaps between frames by extending frames to fill empty space between themselves.
*/
resolveGaps (newOrModifiedFrames) {
if(this.parentTimeline && this.parentTimeline.waitToFillFrameGaps) return;
newOrModifiedFrames = newOrModifiedFrames || [];
var fillGapsMethod = this.parentTimeline && this.parentTimeline.fillGapsMethod;
if(!fillGapsMethod) fillGapsMethod = 'blank_frames';
this.findGaps().forEach(gap => {
// Method 1: Use the frame on the left (if there is one) to fill the gap
if(fillGapsMethod === 'auto_extend') {
var frameOnLeft = this.getFrameAtPlayheadPosition(gap.start-1);
if(!frameOnLeft || newOrModifiedFrames.indexOf(frameOnLeft) !== -1 || gap.start === 1) {
// If there is no frame on the left, create a blank one
var empty = new Wick.Frame({
start: gap.start,
end: gap.end,
});
this.addFrame(empty);
} else {
// Otherwise, extend the frame to the left to fill the gap
frameOnLeft.end = gap.end;
}
}
// Method 2: Always create empty frames to fill gaps
if(fillGapsMethod === 'blank_frames') {
var empty = new Wick.Frame({
start: gap.start,
end: gap.end,
});
this.addFrame(empty);
}
});
}
/**
* Generate a list of positions where there is empty space between frames.
* @returns {Object[]} An array of objects with start/end positions describing gaps.
*/
findGaps () {
var gaps = [];
var currentGap = null;
for(var i = 1; i <= this.length; i++) {
var frame = this.getFrameAtPlayheadPosition(i);
// Found the start of a gap
if(!frame && !currentGap) {
currentGap = {};
currentGap.start = i;
}
// Found the end of a gap
if(frame && currentGap) {
currentGap.end = i-1;
gaps.push(currentGap);
currentGap = null;
}
}
return gaps;
}
}
</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:13 GMT-0400 (Eastern Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>