Skip to content

Instantly share code, notes, and snippets.

@bacalj
Last active April 6, 2023 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bacalj/1c2ab0c3b08a86e96fe2a2b5626966bb to your computer and use it in GitHub Desktop.
Save bacalj/1c2ab0c3b08a86e96fe2a2b5626966bb to your computer and use it in GitHub Desktop.
proposed break up of tick
private tick = () => {
const {tileModel, playBackIndex, isPlaying} = this.props;
const dataSet = tileModel.dataSet;
const now = Date.now();
this.setState({lastIntervalDuration: now - this.lastIntervalTime});
this.lastIntervalTime = now;
const isCleared = this.props.programRecordState === 0;
const isRecording = this.props.programRecordState === 1;
const isRecorded = this.props.programRecordState === 2;
if (isCleared){
this.updateNodes();
}
if (isRecording){
this.updateNodes();
this.recordCase();
}
if (isRecorded){
this.updateNodesWithCaseData(dataSet, playBackIndex);
isPlaying && this.props.updatePlayBackIndex(UpdateMode.Increment);
!isPlaying && this.props.updatePlayBackIndex(UpdateMode.Reset);
}
};
private recordCase = () => {
const aCase: ICaseCreation = {};
//loop through attribute (nodes) and write each value
this.programEditor.nodes.forEach((node, idx) => {
const key = this.props.tileModel.dataSet.attributes[idx].id;
aCase[key] = node.data.nodeValue as string;
});
addCanonicalCasesToDataSet(this.props.tileModel.dataSet, [aCase]);
}
private updateNodesWithCaseData = (dataSet: any, playBackIndex: number) => {
const currentCase = dataSet.getCaseAtIndex(playBackIndex);
if (currentCase){
const {__id__} = currentCase; //this is the id of the case we are looking at for each frame
this.programEditor.nodes.forEach((node, idx) => { //update each node in the frame
const attrId = dataSet.attributes[idx].id;
const valueToSendToNode = dataSet.getValue(__id__, attrId) as number;
let nodeControl;
switch (node.name){
case "Sensor":
nodeControl = node.controls.get("nodeValue") as SensorValueControl;
nodeControl.setValue(valueToSendToNode);
break;
case "Number":
nodeControl = node.controls.get("nodeValue") as NumControl;
nodeControl.setValue(valueToSendToNode);
break;
case "Generator":
nodeControl = node.controls.get("nodeValue") as ValueControl;
nodeControl.setValue(valueToSendToNode);
break;
case "Timer":
nodeControl = node.controls.get("nodeValue") as ValueControl; //not working
nodeControl.setValue(valueToSendToNode);
break;
case "Math":
break;
case "Logic":
break;
case "Transform":
break;
case "Control":
break;
case "Demo Output":
nodeControl = node.controls.get("demoOutput") as DemoOutputControl;
nodeControl.setValue(valueToSendToNode); //---> shows correct animation
nodeControl = node.inputs.get("nodeValue")?.control as InputValueControl;
nodeControl.setDisplayMessage(valueToSendToNode === 0 ? "off" : "on");
break;
case "Live Output":
nodeControl = node.inputs.get("nodeValue")?.control as InputValueControl;
nodeControl.setDisplayMessage(valueToSendToNode === 0 ? "off" : "on");
break;
default:
}
});
}
}
private updateNodes = () => {
const nodeProcessMap: { [name: string]: (n: Node) => void } = {
Generator: this.updateGeneratorNode,
Timer: this.updateTimerNode,
Sensor: (n: Node) => {
this.updateNodeChannelInfo(n);
this.updateNodeSensorValue(n);
},
"Live Output": (n: Node) => {
this.sendDataToSerialDevice(n);
}
};
let processNeeded = false;
this.programEditor.nodes.forEach((n: Node) => {
const nodeProcess = nodeProcessMap[n.name];
if (nodeProcess) {
processNeeded = true;
nodeProcess(n);
}
if (Object.prototype.hasOwnProperty.call(n.data, "nodeValue")) {
this.updateNodeRecentValues(n);
}
});
if (processNeeded) {
// if we've updated values on 1 or more nodes (such as a generator),
// we need to abort any current processing and reprocess all
// nodes so current values are up to date
(async () => {
await this.programEngine.abort();
await this.programEngine.process(this.programEditor.toJSON());
})();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment