-
Notifications
You must be signed in to change notification settings - Fork 11
/
ChannelItemWithGUI.ts
88 lines (76 loc) · 2.7 KB
/
ChannelItemWithGUI.ts
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
import { AutomatonWithGUI } from './AutomatonWithGUI';
import { ChannelItem, SerializedChannelItem } from '@0b5vr/automaton';
import { CurveWithGUI } from './CurveWithGUI';
import { SerializableWithID } from './types/SerializableWithID';
import { applyMixins } from './utils/applyMixins';
import type { StateChannelItem } from './types/StateChannelItem';
export interface ChannelItemWithGUI extends SerializableWithID<SerializedChannelItem> {}
export class ChannelItemWithGUI extends ChannelItem {
protected __automaton!: AutomatonWithGUI;
public curve?: CurveWithGUI;
/**
* TODO
* @param time The timepoint you want to grab the value
* @param isFromGUI If you're poking the method from Automaton GUI, set this to true otherwise you are going to suffer in redux hell
*/
public getValue( time: number, isFromGUI?: boolean ): number {
const value = super.getValue( time );
if ( this.curve && !isFromGUI ) {
this.curve.emitPreviewTime( {
time: this.offset + time * this.speed,
value: this.curve.getValue( this.offset + time * this.speed ),
itemTime: this.time,
itemOffset: this.offset,
itemSpeed: this.speed,
} );
}
return value;
}
public deserialize( data: SerializedChannelItem | StateChannelItem ): void {
if ( 'curveId' in data && data.curveId != null ) {
super.deserialize( {
...data,
curve: this.__automaton.getCurveIndexById( data.curveId )
} );
} else {
super.deserialize( data );
}
}
/**
* Serialize its current state.
* @returns Serialized state
*/
public serialize(): SerializedChannelItem {
const data: SerializedChannelItem = {};
if ( this.time !== 0.0 ) { data.time = this.time; }
if ( this.length !== 0.0 ) { data.length = this.length; }
if ( this.value !== 0.0 ) { data.value = this.value; }
if ( this.reset ) { data.reset = true; }
if ( this.curve ) {
data.curve = this.__automaton.getCurveIndex( this.curve );
if ( this.offset !== 0.0 ) { data.offset = this.offset; }
if ( this.speed !== 1.0 ) { data.speed = this.speed; }
if ( this.amp !== 1.0 ) { data.amp = this.amp; }
}
return data;
}
/**
* Serialize its current state, but every fields are required + $id is there.
* @returns Serialized state
*/
public serializeGUI(): StateChannelItem {
const data: StateChannelItem = {
$id: this.$id,
time: this.time,
length: this.length,
value: this.value,
reset: this.reset ?? false,
offset: this.offset,
speed: this.speed,
amp: this.amp,
curveId: this.curve?.$id ?? null,
};
return data;
}
}
applyMixins( ChannelItemWithGUI, [ SerializableWithID ] );