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
|
import * as React from "react";
import * as ReactDOM from "react-dom";
import { observer } from "mobx-react";
import { observable, reaction, action, IReactionDisposer, observe, IObservableArray } from "mobx";
import "./Timeline.scss";
import { KeyFrame } from "./KeyFrame";
import { CollectionViewProps } from "../collections/CollectionBaseView";
import { CollectionSubView, SubCollectionViewProps } from "../collections/CollectionSubView";
import { DocumentViewProps, DocumentView } from "./DocumentView";
import { CollectionFreeFormView } from "../collections/collectionFreeForm/CollectionFreeFormView";
import { Doc, DocListCastAsync } from "../../../new_fields/Doc";
import { Document, listSpec, createSchema, makeInterface, defaultSpec } from "../../../new_fields/Schema";
import { FieldValue, Cast, NumCast } from "../../../new_fields/Types";
import { emptyStatement } from "babel-types";
import { DocumentManager } from "../../util/DocumentManager";
import { SelectionManager } from "../../util/SelectionManager";
import { List } from "../../../new_fields/List";
import { Self } from "../../../new_fields/FieldSymbols";
type IndividualDocKeyFrame = List<Doc>; //data?
type KeyframeList = List<List<Doc>>;
const PositionSchema = createSchema({
x: defaultSpec("number", 0),
y: defaultSpec("number", 0)
});
type Position = makeInterface<[typeof PositionSchema]>;
const Position = makeInterface(PositionSchema);
const TimeAndPositionSchema = createSchema({
time: defaultSpec("number", 0),
position: Doc //Position
});
type TimeAndPosition = makeInterface<[typeof TimeAndPositionSchema]>;
const TimeAndPosition = makeInterface(TimeAndPositionSchema);
@observer
export class Timeline extends CollectionSubView(Document) {
@observable private _inner = React.createRef<HTMLDivElement>();
@observable private _timeInput = React.createRef<HTMLInputElement>();
@observable private _isRecording: Boolean = false;
@observable private _currentBar: any = null;
@observable private _newBar: any = null;
private _reactionDisposers: IReactionDisposer[] = [];
private _keyFrames: Doc[] = []; //private _keyFrames: KeyFrame[] = [];
private _keyBars: HTMLDivElement[] = [];
private _currentBarX: number = 0;
@observable private _onBar: Boolean = false;
@observable private _keys = ["x", "y"];
@observable private _data: Doc[] = [];
private temp1: any = null;
private temp2: any = null;
private tempdoc: any = null;
@action
onRecord = (e: React.MouseEvent) => {
this._isRecording = true;
let children = Cast(this.props.Document[this.props.fieldKey], listSpec(Doc));
if (!children) {
return;
}
let childrenList = (children[Self] as any).__fields;
// let keys = ["x", "y"]; //maybe make this an instance var?
const addReaction = (element: Doc) => {
element = (element as any).value();
this.tempdoc = element;
return reaction(() => {
return this._keys.map(key => FieldValue(element[key]));
}, async data => { //where is the data index actually being set?
if (this._inner.current) {
let keyFrame: Doc;
const kf = new Doc;
this._data.push(kf);
let info = [];
let time: number = this._currentBarX; //time that indicates what frame you are in. Whole numbers.
info.push(time); //index 0: should be x position of mouse relative to inner
info.push(kf.X); //index 1
info.push(kf.Y); //index 2
this._keyFrames.push(kf);
let exists: boolean = false;
// let frames: List<Doc>; //don't know if this should be an instance...
this._keyFrames.forEach(async kfs => { //checks if there is a keyframe that is tracking this document.
if (kfs === element) {
keyFrame = kfs;
this._data = (await DocListCastAsync(keyFrame.frames))!; //keyFrame.doc.frames
exists = true;
}
});
if (!exists) {
keyFrame = new Doc();
let bar: HTMLDivElement = this.createBar(5, time, "yellow");
this._inner.current.appendChild(bar);
keyFrame.frames = new List<Doc>();
// this._keyFrames.push(keyFrame);
}
this._keys.forEach((key, index) => {
console.log(data[index]);
if (keyFrame.frames !== undefined) {
this._data.forEach(frame => {
if (frame.time === this._currentBarX) {
frame[key] = data[index];
}
});
}
});
}
});
};
observe(childrenList as IObservableArray<Doc>, change => {
if (change.type === "update") {
this._reactionDisposers[change.index]();
this._reactionDisposers[change.index] = addReaction(change.newValue);
} else {
let removed = this._reactionDisposers.splice(change.index, change.removedCount, ...change.added.map(addReaction));
removed.forEach(disp => disp());
}
}, true);
}
//maybe to be called in innerPointerDown
//this is the only method that should have access to time changes
//should run every time the time has been changed
//should look through the array of times and positions and set docs to the positions associated with the specific time
//if there is not an entry in the array associated with the time (for now) choose the next existing lower/higher time entry, etc.
//eventually there will be interpolation between these existing times
//will be similar to last block of code in the onRecord reaction, but kind of doing the opposite...
@action
timeChange = async (time: number) => { //x position of mouse relative to inner box can be passed in?
//data is list of docs
let keyFrame: TimeAndPosition; //keyframe reference
const docs = (await DocListCastAsync(this.props.Document[this.props.fieldKey]))!;
const kfList: KeyframeList = Cast(this.props.Document.keyframes, listSpec(Doc), []) as any;
//^currently KeyframeList is undefined (because no keyframes have been added to the list)
//to fix this problem add keyframes to the List
//this could either happen by moving this outside of the method
//or make list outside of this method and then copy it here (recommended)
const list = await Promise.all(kfList.map(l => Promise.all(l)));
let isFrame: boolean = false;
for (let i in docs) {
let oneDoc = docs[i];
let oneKf: TimeAndPosition[] = list[i].map(TimeAndPosition);
let leftKf!: TimeAndPosition;
let rightKf!: TimeAndPosition;
for (let j in oneKf) {
let singleKf: TimeAndPosition = oneKf[j];
let leftMin: Number = Infinity;
let rightMin: Number = Infinity;
if (singleKf.time !== time) { //choose closest time neighbors
for (let k in oneKf) {
if (oneKf[k].time < time) {
const diff: Number = Math.abs(oneKf[k].time - time);
if (diff < leftMin) {
leftMin = diff;
leftKf = oneKf[k];
}
} else {
const diff: Number = Math.abs(oneKf[k].time - time);
if (diff < rightMin) {
rightMin = diff;
rightKf = oneKf[k];
}
}
}
} else {
isFrame = true;
}
}
this.interpolate(oneDoc, leftKf, rightKf, time);
if (isFrame) {
oneDoc.X = oneKf[i].X;
oneDoc.Y = oneKf[i].Y;
}
isFrame = false;
}
}
/**
* Linearly interpolates a document from time1 to time2
* @param Doc that needs to be modified
* @param
*/
@action
interpolate = async (doc: Doc, kf1: TimeAndPosition, kf2: TimeAndPosition, time: number) => {
const keyFrame1 = Position(await kf1.position);
const keyFrame2 = Position(await kf2.position);
const dif_X = NumCast(keyFrame2.X) - NumCast(keyFrame1.X);
const dif_Y = NumCast(keyFrame2.Y) - NumCast(keyFrame1.Y);
const dif_time = kf2.time - kf1.time;
const ratio = (time - kf1.time) / dif_time;
const adjusted_X = dif_X * ratio;
const adjusted_Y = dif_Y * ratio;
console.log(doc.X);
doc.X = keyFrame1.x + adjusted_X;
doc.Y = keyFrame1.y + adjusted_Y;
}
/**
* TEMPORARY
*/
@action
onInterpolate = (e: React.MouseEvent) => {
}
@action
displayKeyFrames = (dv: DocumentView) => {
console.log(dv);
dv.props.Document;
}
@action
onInnerPointerUp = (e: React.PointerEvent) => {
if (this._inner.current) {
this._inner.current.removeEventListener("pointermove", this.onInnerPointerMove);
}
}
@action
onInnerPointerDown = (e: React.PointerEvent) => {
e.preventDefault();
e.stopPropagation();
if (this._isRecording) {
if (this._inner.current) {
let mouse = e.nativeEvent;
let offsetX = Math.round(mouse.offsetX);
this._currentBarX = offsetX;
this._currentBar.style.transform = `translate(${offsetX}px)`;
this._inner.current.removeEventListener("pointermove", this.onInnerPointerMove); //reset
this._inner.current.addEventListener("pointermove", this.onInnerPointerMove);
this.timeChange(this._currentBarX);
}
}
}
@action
onInnerPointerMove = (e: PointerEvent) => {
e.preventDefault();
e.stopPropagation();
let offsetX = Math.round(e.offsetX);
this._currentBarX = offsetX;
this._currentBar.style.transform = `translate(${offsetX}px)`; //styling should not have to be done this way...maybe done through react??
this.timeChange(this._currentBarX);
}
createBar = (width: number, pos: number = 0, color: string = "green"): HTMLDivElement => {
let bar = document.createElement("div");
bar.style.height = "100%";
bar.style.width = `${width}px`;
bar.style.backgroundColor = color;
bar.style.transform = `translate(${pos}px)`; //repeated code from previous method
bar.style.position = "absolute";
bar.style.pointerEvents = "none";
return bar;
}
onTimeEntered = (e: React.KeyboardEvent) => {
if (this._timeInput.current) {
if (e.keyCode === 13) {
let input = parseInt(this._timeInput.current.value) || 0;
this._currentBar.style.transform = `translate(${input}px)`;
this._currentBarX = input;
}
}
}
componentDidMount() {
if (this._inner.current && this._currentBar === null) {
this._currentBar = this.createBar(5);
this._inner.current.appendChild(this._currentBar);
}
let doc: Doc = this.props.Document;
let test = this.props.Document[this.props.fieldKey];
}
componentWillUnmount() {
this._reactionDisposers.forEach(disp => disp());
this._reactionDisposers = [];
}
render() {
return (
<div>
<div className="timeline-container">
<div className="timeline">
<div className="inner" ref={this._inner} onPointerDown={this.onInnerPointerDown} onPointerUp={this.onInnerPointerUp}>
{
SelectionManager.SelectedDocuments().map((dv) => {
this.displayKeyFrames(dv);
})
}
</div>
</div>
<button onClick={this.onRecord}>Record</button>
<button onClick={this.onInterpolate}>Inter</button>
<input placeholder="Time" ref={this._timeInput} onKeyDown={this.onTimeEntered}></input>
</div>
</div>
);
}
}
|