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
|
import * as React from "react";
import "./Keyframe.scss";
import "./Timeline.scss";
import "./../globalCssVariables.scss";
import { observer, Observer } from "mobx-react";
import { observable, reaction, action, IReactionDisposer, observe, IObservableArray, computed, toJS, isComputedProp } from "mobx";
import { Doc, DocListCast } from "../../../new_fields/Doc";
import { Cast, FieldValue, StrCast, NumCast } from "../../../new_fields/Types";
import { List } from "../../../new_fields/List";
import { createSchema, defaultSpec, makeInterface, listSpec } from "../../../new_fields/Schema";
import { any } from "bluebird";
import { FlyoutProps } from "./Timeline";
import { number } from "prop-types";
import { CollectionSchemaView, CollectionSchemaPreview } from "../collections/CollectionSchemaView";
import { faDiceOne, faFirstAid } from "@fortawesome/free-solid-svg-icons";
export namespace KeyframeFunc{
export enum KeyframeType{
fade = "fade",
default = "default",
}
export enum Direction{
left = "left",
right = "right"
}
export const findAdjacentRegion = (dir: KeyframeFunc.Direction, currentRegion:Doc, regions:List<Doc>): (RegionData | undefined) => {
let leftMost: (RegionData | undefined) = undefined;
let rightMost: (RegionData | undefined) = undefined;
regions.forEach(region => {
let neighbor = RegionData(region as Doc);
if (currentRegion.position! > neighbor.position) {
if (!leftMost || neighbor.position > leftMost.position) {
leftMost = neighbor;
}
} else if (currentRegion.position! < neighbor.position) {
if (!rightMost || neighbor.position < rightMost.position) {
rightMost = neighbor;
}
}
});
if (dir === Direction.left) {
return leftMost;
} else if (dir === Direction.right) {
return rightMost;
}
};
export const defaultKeyframe = () => {
let regiondata = new Doc(); //creating regiondata
regiondata.duration = 200;
regiondata.position = 0;
regiondata.fadeIn = 20;
regiondata.fadeOut = 20;
return regiondata;
};
}
export const RegionDataSchema = createSchema({
position: defaultSpec("number", 0),
duration: defaultSpec("number", 0),
keyframes: listSpec(Doc),
fadeIn: defaultSpec("number", 0),
fadeOut: defaultSpec("number", 0)
});
export type RegionData = makeInterface<[typeof RegionDataSchema]>;
export const RegionData = makeInterface(RegionDataSchema);
interface IProps {
node: Doc;
RegionData: Doc;
changeCurrentBarX: (x: number) => void;
setFlyout:(props:FlyoutProps) => any;
}
@observer
export class Keyframe extends React.Component<IProps> {
@observable private _bar = React.createRef<HTMLDivElement>();
@observable private _firstkf: (Doc | undefined) = undefined;
@observable private _lastkf: (Doc | undefined) = undefined;
@computed
private get regiondata() {
let index = this.regions.indexOf(this.props.RegionData);
return RegionData(this.regions[index] as Doc);
}
@computed
private get regions() {
return Cast(this.props.node.regions, listSpec(Doc)) as List<Doc>;
}
@computed
private get firstKeyframe(){
let first: (Doc | undefined) = undefined;
DocListCast(this.regiondata.keyframes!).forEach(kf => {
if (kf.type !== KeyframeFunc.KeyframeType.fade){
if (!first || first && NumCast(kf.time) < NumCast(first.time)){
first = kf;
}
}
});
return first;
}
@computed
private get lastKeyframe(){
let last: (Doc | undefined) = undefined;
DocListCast(this.regiondata.keyframes!).forEach(kf => {
if (kf.type !== KeyframeFunc.KeyframeType.fade){
if (!last || last && NumCast(kf.time) > NumCast(last.time)){
last = kf;
}
}
});
return last;
}
componentWillMount(){
if (!this.regiondata.keyframes){
this.regiondata.keyframes = new List<Doc>();
}
}
@action
componentDidMount() {
let fadeIn = this.makeKeyData(this.regiondata.position + this.regiondata.fadeIn, KeyframeFunc.KeyframeType.fade)!;
let fadeOut = this.makeKeyData(this.regiondata.position + this.regiondata.duration - this.regiondata.fadeOut, KeyframeFunc.KeyframeType.fade)!;
let start = this.makeKeyData(this.regiondata.position, KeyframeFunc.KeyframeType.fade)!;
let finish = this.makeKeyData(this.regiondata.position + this.regiondata.duration, KeyframeFunc.KeyframeType.fade)!;
(fadeIn.key! as Doc).opacity = 1;
(fadeOut.key! as Doc).opacity = 1;
(start.key! as Doc) .opacity = 0.1;
(finish.key! as Doc).opacity = 0.1;
observe(this.regiondata, change => {
if (change.type === "update"){
fadeIn.time = this.regiondata.position + this.regiondata.fadeIn;
fadeOut.time = this.regiondata.position + this.regiondata.duration - this.regiondata.fadeOut;
start.time = this.regiondata.position;
finish.time = this.regiondata.position + this.regiondata.duration;
let fadeInIndex = this.regiondata.keyframes!.indexOf(fadeIn);
let fadeOutIndex = this.regiondata.keyframes!.indexOf(fadeOut);
let startIndex = this.regiondata.keyframes!.indexOf(start);
let finishIndex = this.regiondata.keyframes!.indexOf(finish);
this.regiondata.keyframes![fadeInIndex] = fadeIn;
this.regiondata.keyframes![fadeOutIndex] = fadeOut;
this.regiondata.keyframes![startIndex] = start;
this.regiondata.keyframes![finishIndex] = finish;
this.forceUpdate();
}
});
}
@action
makeKeyData = (kfpos: number, type:KeyframeFunc.KeyframeType = KeyframeFunc.KeyframeType.default) => { //Kfpos is mouse offsetX, representing time
DocListCast(this.regiondata.keyframes!).forEach(TK => { //TK is TimeAndKey
if (TK.time === kfpos) {
return TK;
}
});
let TK: Doc = new Doc();
TK.time = kfpos;
TK.key = Doc.MakeCopy(this.props.node, true);
TK.type = type;
this.regiondata.keyframes!.push(TK);
return TK;
}
@action
onBarPointerDown = (e: React.PointerEvent) => {
e.preventDefault();
e.stopPropagation();
document.addEventListener("pointermove", this.onBarPointerMove);
document.addEventListener("pointerup", (e: PointerEvent) => {
document.removeEventListener("pointermove", this.onBarPointerMove);
});
}
@action
onBarPointerMove = (e: PointerEvent) => {
e.preventDefault();
e.stopPropagation();
let left = KeyframeFunc.findAdjacentRegion(KeyframeFunc.Direction.left, this.regiondata, this.regions)!;
let right = KeyframeFunc.findAdjacentRegion(KeyframeFunc.Direction.right, this.regiondata, this.regions!);
// let bar = this._bar.current!;
// let barX = bar.getBoundingClientRect().left;
// let offset = e.clientX - barX;
let prevX = this.regiondata.position;
let futureX = this.regiondata.position + e.movementX;
if (futureX <= 0) {
this.regiondata.position = 0;
} else if ((left && left.position + left.duration >= futureX)) {
this.regiondata.position = left.position + left.duration;
} else if ((right && right.position <= futureX + this.regiondata.duration)) {
this.regiondata.position = right.position - this.regiondata.duration;
} else {
this.regiondata.position = futureX;
}
for (let i = 0; i < this.regiondata.keyframes!.length; i++) {
if ((this.regiondata.keyframes![i] as Doc).type !== KeyframeFunc.KeyframeType.fade){
let movement = this.regiondata.position - prevX;
(this.regiondata.keyframes![i] as Doc).time = NumCast((this.regiondata.keyframes![i] as Doc).time) + movement;
}
}
this.forceUpdate();
}
@action
onResizeLeft = (e: React.PointerEvent) => {
e.preventDefault();
e.stopPropagation();
document.addEventListener("pointermove", this.onDragResizeLeft);
document.addEventListener("pointerup", () => {
document.removeEventListener("pointermove", this.onDragResizeLeft);
});
}
@action
onResizeRight = (e: React.PointerEvent) => {
e.preventDefault();
e.stopPropagation();
document.addEventListener("pointermove", this.onDragResizeRight);
document.addEventListener("pointerup", () => {
document.removeEventListener("pointermove", this.onDragResizeRight);
});
}
@action
onDragResizeLeft = (e: PointerEvent) => {
e.preventDefault();
e.stopPropagation();
let bar = this._bar.current!;
let barX = bar.getBoundingClientRect().left;
let offset = e.clientX - barX;
let firstkf: (Doc | undefined) = this.firstKeyframe;
console.log(offset);
if (firstkf && this.regiondata.position + this.regiondata.fadeIn + offset>= NumCast(firstkf!.time)) {
this.regiondata.position = NumCast(firstkf!.time) - this.regiondata.fadeIn;
}else if (this.regiondata.duration - offset < this.regiondata.fadeIn + this.regiondata.fadeOut){ // no keyframes, just fades
this.regiondata.position -= (this.regiondata.fadeIn + this.regiondata.fadeOut - this.regiondata.duration);
this.regiondata.duration = this.regiondata.fadeIn + this.regiondata.fadeOut;
} else {
this.regiondata.duration -= offset;
this.regiondata.position += offset;
}
}
@action
onDragResizeRight = (e: PointerEvent) => {
e.preventDefault();
e.stopPropagation();
let bar = this._bar.current!;
let barX = bar.getBoundingClientRect().right;
let offset = e.clientX - barX;
if (this.regiondata.duration + offset < this.regiondata.fadeIn + this.regiondata.fadeOut){
this.regiondata.duration = this.regiondata.fadeIn + this.regiondata.fadeOut;
}else {
this.regiondata.duration += offset;
}
}
createDivider = (type?: KeyframeFunc.Direction): JSX.Element => {
if (type === "left") {
return <div className="divider" style={{ right: "0px" }}></div>;
} else if (type === "right") {
return <div className="divider" style={{ left: "0px" }}> </div>;
}
return <div className="divider"></div>;
}
@action
createKeyframe = (e: React.MouseEvent) => {
e.preventDefault();
e.stopPropagation();
let bar = this._bar.current!;
let offset = e.clientX - bar.getBoundingClientRect().left;
let position = NumCast(this.regiondata.position);
let tk = this.makeKeyData(Math.round(position + offset));
this.props.changeCurrentBarX(NumCast(Math.round(position + offset))); //first move the keyframe to the correct location and make a copy so the correct file gets coppied
}
@action
moveKeyframe = (e: React.MouseEvent, kf:Doc) => {
e.preventDefault();
e.stopPropagation();
this.props.changeCurrentBarX(NumCast(kf.time!));
}
@action
private createKeyframeJSX = (kf:Doc, type = KeyframeFunc.KeyframeType.default) => {
if (type === KeyframeFunc.KeyframeType.default){
return (
<div className="keyframe" style={{ left: `${NumCast(kf.time) - this.regiondata.position}px` }}>
{this.createDivider()}
<div className="keyframeCircle" onPointerDown={(e) => {this.moveKeyframe(e, kf as Doc);} } onContextMenu={(e:React.MouseEvent)=>{
e.preventDefault();
e.stopPropagation();
}}></div>
</div>);
}
return (
<div className="keyframe" style={{ left: `${NumCast(kf.time) - this.regiondata.position}px` }}>
{this.createDivider()}
</div>
);
}
render() {
return (
<div>
<div className="bar" ref={this._bar} style={{ transform: `translate(${this.regiondata.position}px)`, width: `${this.regiondata.duration}px` }}
onPointerDown={this.onBarPointerDown}
onDoubleClick={this.createKeyframe}
onContextMenu={action((e:React.MouseEvent)=>{
e.preventDefault();
e.stopPropagation();
let offsetLeft = this._bar.current!.getBoundingClientRect().left - this._bar.current!.parentElement!.getBoundingClientRect().left;
let offsetTop = this._bar.current!.getBoundingClientRect().top; //+ this._bar.current!.parentElement!.getBoundingClientRect().top;
this.props.setFlyout({x:offsetLeft, y: offsetTop, display:"block", regiondata:this.regiondata, regions:this.regions}); })}>
<div className="leftResize" onPointerDown={this.onResizeLeft} ></div>
<div className="rightResize" onPointerDown={this.onResizeRight}></div>
{this.regiondata.keyframes!.map(kf => {
return this.createKeyframeJSX(kf as Doc, (kf! as Doc).type as KeyframeFunc.KeyframeType);
})}
</div>
</div>
);
}
}
|