@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 3.54 kB
JavaScript
import*as UI from"../../../ui/legacy/legacy.js";import{BottomPaddingWithoutParam,BottomPaddingWithParam,LeftMarginOfText,LeftSideTopPadding,NodeLabelFontStyle,ParamLabelFontStyle,RightMarginOfText,TotalInputPortHeight,TotalOutputPortHeight,TotalParamPortHeight}from"./GraphStyle.js";import{calculateInputPortXY,calculateOutputPortXY,calculateParamPortXY}from"./NodeRendererUtility.js";export class NodeView{id;type;numberOfInputs;numberOfOutputs;label;size;position;layout;ports;constructor(t,e){this.id=t.nodeId,this.type=t.nodeType,this.numberOfInputs=t.numberOfInputs,this.numberOfOutputs=t.numberOfOutputs,this.label=e,this.size={width:0,height:0},this.position=null,this.layout={inputPortSectionHeight:0,outputPortSectionHeight:0,maxTextLength:0,totalHeight:0},this.ports=new Map,this.initialize(t)}initialize(t){this.updateNodeLayoutAfterAddingNode(t),this.setupInputPorts(),this.setupOutputPorts()}addParamPort(t,e){const o=this.getPortsByType("Param").length,{x:a,y:i}=calculateParamPortXY(o,this.layout.inputPortSectionHeight);this.addPort({id:generateParamPortId(this.id,t),type:"Param",label:e,x:a,y:i}),this.updateNodeLayoutAfterAddingParam(o+1,e),this.setupOutputPorts()}getPortsByType(t){const e=[];return this.ports.forEach((o=>{o.type===t&&e.push(o)})),e}updateNodeLayoutAfterAddingNode(t){const e=TotalInputPortHeight*Math.max(1,t.numberOfInputs)+LeftSideTopPadding;this.layout.inputPortSectionHeight=e,this.layout.outputPortSectionHeight=TotalOutputPortHeight*t.numberOfOutputs,this.layout.totalHeight=Math.max(e+BottomPaddingWithoutParam,this.layout.outputPortSectionHeight);const o=measureTextWidth(this.label,NodeLabelFontStyle);this.layout.maxTextLength=Math.max(this.layout.maxTextLength,o),this.updateNodeSize()}updateNodeLayoutAfterAddingParam(t,e){const o=this.layout.inputPortSectionHeight+t*TotalParamPortHeight+BottomPaddingWithParam;this.layout.totalHeight=Math.max(o,this.layout.outputPortSectionHeight);const a=measureTextWidth(e,ParamLabelFontStyle);this.layout.maxTextLength=Math.max(this.layout.maxTextLength,a),this.updateNodeSize()}updateNodeSize(){this.size={width:Math.ceil(LeftMarginOfText+this.layout.maxTextLength+RightMarginOfText),height:this.layout.totalHeight}}setupInputPorts(){for(let t=0;t<this.numberOfInputs;t++){const{x:e,y:o}=calculateInputPortXY(t);this.addPort({id:generateInputPortId(this.id,t),type:"In",x:e,y:o,label:void 0})}}setupOutputPorts(){for(let t=0;t<this.numberOfOutputs;t++){const e=generateOutputPortId(this.id,t),{x:o,y:a}=calculateOutputPortXY(t,this.size,this.numberOfOutputs);if(this.ports.has(e)){const t=this.ports.get(e);if(!t)throw new Error(`Unable to find port with id ${e}`);t.x=o,t.y=a}else this.addPort({id:e,type:"Out",x:o,y:a,label:void 0})}}addPort(t){this.ports.set(t.id,t)}}export const generateInputPortId=(t,e)=>`${t}-input-${e||0}`;export const generateOutputPortId=(t,e)=>`${t}-output-${e||0}`;export const generateParamPortId=(t,e)=>`${t}-param-${e}`;export class NodeLabelGenerator{totalNumberOfNodes;constructor(){this.totalNumberOfNodes=0}generateLabel(t){t.endsWith("Node")&&(t=t.slice(0,t.length-4)),this.totalNumberOfNodes+=1;return`${t} ${this.totalNumberOfNodes}`}}let _contextForFontTextMeasuring;export const measureTextWidth=(t,e)=>{if(!_contextForFontTextMeasuring){const t=document.createElement("canvas").getContext("2d");if(!t)throw new Error("Unable to create canvas context.");_contextForFontTextMeasuring=t}const o=_contextForFontTextMeasuring;o.save(),e&&(o.font=e);const a=UI.UIUtils.measureTextWidth(o,t);return o.restore(),a};