-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReceive.ts
82 lines (72 loc) · 3.02 KB
/
Receive.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
import {BehaveEngineNode, IBehaviourNodeProps} from "../../BehaveEngineNode";
import {IInteractivityEvent} from "../../../types/InteractivityGraph";
export class Receive extends BehaveEngineNode {
REQUIRED_CONFIGURATIONS = {event: {}}
_defaultValues: Record<string, any> = {};
_event: number;
constructor(props: IBehaviourNodeProps) {
super(props);
this.name = "CustomEventReceiveNode";
this.validateValues(this.values);
this.validateConfigurations(this.configuration);
const {event} = this.evaluateAllConfigurations(Object.keys(this.REQUIRED_CONFIGURATIONS));
this._event = event;
this.setUpEventListener();
}
setUpEventListener() {
const {event} = this.evaluateAllConfigurations(Object.keys(this.REQUIRED_CONFIGURATIONS));
const customEventDesc: IInteractivityEvent = this.events[event];
const defaultValues: Record<string, any> = {};
Object.entries(customEventDesc.values).forEach(([key, value]) => {
const typeName = this.getType(value.type);
let defaultVal = this.getDefualtValueForType(typeName);
if (value.value) {
// if there is a given default value in the CE then use that
defaultVal = value.value;
}
defaultValues[key] = {
value: defaultVal,
type: value.type,
}
});
this._defaultValues = defaultValues;
this.outValues = JSON.parse(JSON.stringify(defaultValues));
this.graphEngine.addCustomEventListener(`KHR_INTERACTIVITY:${customEventDesc.id}`, (e: any) => {
this.graphEngine.processNodeStarted(this);
// reset values to default before processing the event
this.outValues = JSON.parse(JSON.stringify(this._defaultValues));
const ce = (e as CustomEvent).detail as { [key: string]: any };
Object.keys(ce).forEach((ceKey) => {
const typeIndex = Object.entries(customEventDesc.values).find(([key, _]) => key === ceKey)?.[1]?.type;
const typeName: string = this.getType(Number(typeIndex));
const rawVal = ce[ceKey];
const val = this.parseType(typeName, [rawVal]);
this.outValues[ceKey] = {
value: val,
type: typeIndex
}
});
super.processNode();
})
}
override parseType(type: string, val: any) {
switch (type) {
case "bool":
return [val[0] === "true"];
case "int":
return [Number(val[0])];
case "float":
return [Number(val[0])];
case "float2":
return JSON.parse(val);
case "float3":
return JSON.parse(val);
case "float4":
return JSON.parse(val);
case "float4x4":
return JSON.parse(val);
default:
return val
}
}
}