@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
110 lines (109 loc) • 3.71 kB
JavaScript
;
import { TypedJsNode } from "./_Base";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { JsConnectionPoint, JsConnectionPointType } from "../utils/io/connections/Js";
import { NodeContext } from "../../poly/NodeContext";
import { CopType } from "../../poly/registers/nodes/types/Cop";
import { JsType } from "../../poly/registers/nodes/types/Js";
import {
VideoEvent,
VIDEO_EVENTS
} from "../../../core/VideoEvent";
import {
getConnectedOutputNodes,
getOutputIndices,
nodeMethodName,
triggerInputIndex
} from "./code/assemblers/actor/ActorAssemblerUtils";
import { setToArray } from "../../../core/SetUtils";
import { Poly } from "../../Poly";
import { InitFunctionJsDefinition, TriggeringJsDefinition } from "./utils/JsDefinition";
class OnVideoEventJsParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
/** @param video node */
this.node = ParamConfig.NODE_PATH("", {
nodeSelection: {
context: NodeContext.COP,
types: [CopType.VIDEO]
},
computeOnDirty: true
});
}
}
const ParamsConfig = new OnVideoEventJsParamsConfig();
export class OnVideoEventJsNode extends TypedJsNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return JsType.ON_VIDEO_EVENT;
}
isTriggering() {
return true;
}
initializeNode() {
this.io.outputs.setNamedOutputConnectionPoints(
VIDEO_EVENTS.map((triggerName) => new JsConnectionPoint(triggerName, JsConnectionPointType.TRIGGER))
);
}
setTriggeringLines(shadersCollectionController, triggeredMethods) {
const node = this.pv.node.node();
if (!(node && node.context() == NodeContext.COP)) {
return;
}
const nodePath = `'${node.path()}'`;
const listeners = {
[VideoEvent.PAUSE]: "",
[VideoEvent.PLAY]: "",
[VideoEvent.TIME_UPDATE]: "",
[VideoEvent.VOLUME_CHANGE]: ""
};
VIDEO_EVENTS.forEach((videoEvent) => {
const triggeredMethods2 = triggerMethod(this, videoEvent);
const _nodeMethodName = nodeMethodName(this, videoEvent);
listeners[videoEvent] = `this.${_nodeMethodName}.bind(this)`;
const value = triggeredMethods2;
const dataType = JsConnectionPointType.BOOLEAN;
shadersCollectionController.addDefinitions(this, [
new TriggeringJsDefinition(this, shadersCollectionController, dataType, _nodeMethodName, value, {
triggeringMethodName: videoEvent,
gatherable: false,
nodeMethodName: _nodeMethodName
})
]);
});
const func = Poly.namedFunctionsRegister.getFunction(
"addVideoEventListener",
this,
shadersCollectionController
);
const bodyLine = func.asString(nodePath, JSON.stringify(listeners).replace(/\"/g, ""), `this`);
shadersCollectionController.addDefinitions(this, [
new InitFunctionJsDefinition(
this,
shadersCollectionController,
JsConnectionPointType.OBJECT_3D,
this.path(),
bodyLine
)
]);
}
}
function triggerMethod(node, outputName) {
const outputIndex = getOutputIndices(node, (c) => c.name() == outputName)[0];
const triggerableNodes = /* @__PURE__ */ new Set();
getConnectedOutputNodes({
node,
triggerOutputIndices: [outputIndex],
triggerableNodes,
recursive: false
});
const triggerableMethodNames = setToArray(triggerableNodes, []).map((triggerableNode) => {
const argIndex = triggerInputIndex(node, triggerableNode);
const m = nodeMethodName(triggerableNode);
return `this.${m}(${argIndex})`;
});
return `${triggerableMethodNames.join(";")}`;
}