portHelper.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import * as React from "react";
  2. import { DefaultNodeModel } from './defaultNodeModel';
  3. import { DefaultPortModel } from './port/defaultPortModel';
  4. import { Nullable } from 'babylonjs/types';
  5. import { NodeMaterialConnectionPoint } from 'babylonjs/Materials/Node/nodeMaterialBlockConnectionPoint';
  6. import { NodeMaterialBlockConnectionPointTypes } from 'babylonjs/Materials/Node/nodeMaterialBlockConnectionPointTypes';
  7. import { DefaultPortWidget } from './port/defaultPortWidget';
  8. import { BlockTools } from '../../blockTools';
  9. export class PortHelper {
  10. private static _GetPortTypeIndicator(connection: NodeMaterialConnectionPoint): Nullable<JSX.Element> {
  11. switch (connection.type) {
  12. case NodeMaterialBlockConnectionPointTypes.Float:
  13. case NodeMaterialBlockConnectionPointTypes.Int:
  14. return <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAABESURBVDhPY/z//z8DJYAJSpMNRg3Ab4APEL+GYhAbK8AXjSCNIhAmwxsgFoUwUQFNvZAJxCCbQRjExgpGU+LAG8DAAAA+ghAthzG18wAAAABJRU5ErkJggg=="></img>;
  15. case NodeMaterialBlockConnectionPointTypes.Vector2:
  16. return <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAABHSURBVDhPY/z//z8DJYAJSpMN8BngA8SvoRjExgrweQGkUQTCZHgDxKIQJiqgqRcygRhkMwiD2FjB4I4FosBoNA6DaGRgAABqpx09dHGG2QAAAABJRU5ErkJggg=="></img>;
  17. case NodeMaterialBlockConnectionPointTypes.Vector3:
  18. case NodeMaterialBlockConnectionPointTypes.Color3:
  19. return <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAABHSURBVDhPY/z//z8DJYAJSmMDPkD8GopBbKwAnwtAGkUgTIY3QCwKYaICfC4gCuAzIBOIQTaDMIiNFYwG4mgggsCQD0QGBgD0QypNGzDYqQAAAABJRU5ErkJggg=="></img>;
  20. case NodeMaterialBlockConnectionPointTypes.Vector4:
  21. case NodeMaterialBlockConnectionPointTypes.Color4:
  22. return <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAABRSURBVDhPY/z//z8DJYAJSpMN0A3wAeLXUAxiwwAucQYGkBeQ8GsghgEQm5A45V6AmwTFIUAMsgGEQWxC4gyDLxZIBqPpAAiQTQPiIZcOGBgAyCDrTTmX3gcAAAAASUVORK5CYII="></img>;
  23. case NodeMaterialBlockConnectionPointTypes.Matrix:
  24. return <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAABuSURBVDhPY/z//z8DJYAJSpMN0A3wAeLXUAxiwwAucQZ0L4AUiECYOMEbIBaFMGnghUwoDQKhQMwIxSA2DCCrwfACCMAEQBqRAVZxqnuBZEB1A9DjHgZwsQdpOgDZAMLo6QAmTjAdkAQo9AIDAwD62SHFU/Hk8QAAAABJRU5ErkJggg=="></img>;
  25. }
  26. return null;
  27. }
  28. static _GetPortStyle(type: NodeMaterialBlockConnectionPointTypes) {
  29. return {
  30. background: BlockTools.GetColorFromConnectionNodeType(type)
  31. };
  32. }
  33. public static GenerateOutputPorts(node: Nullable<DefaultNodeModel>, ignoreLabel: boolean) {
  34. if (!node) {
  35. return new Array<JSX.Element>();
  36. }
  37. let outputPorts = [];
  38. for (var key in node.ports) {
  39. let port = node.ports[key] as DefaultPortModel;
  40. if (port.position === "output") {
  41. let typeIndicator = this._GetPortTypeIndicator(port.connection!);
  42. let style = this._GetPortStyle(port.connection!.type);
  43. let isConnected = port.connection && port.connection.endpoints.length > 0;
  44. outputPorts.push(
  45. <div key={key} className="output-port">
  46. {
  47. !ignoreLabel &&
  48. <div className="output-port-label">
  49. {port.name}
  50. </div>
  51. }
  52. <div className="output-port-plug">
  53. <div className="output-port-border">
  54. </div>
  55. <DefaultPortWidget key={key} name={port.name} node={node} style={style} />
  56. {
  57. !isConnected &&
  58. <div className="output-port-connection">
  59. </div>
  60. }
  61. <div className="output-port-type">
  62. {
  63. typeIndicator
  64. }
  65. </div>
  66. </div>
  67. </div>
  68. );
  69. }
  70. }
  71. return outputPorts;
  72. }
  73. public static GenerateInputPorts(node: Nullable<DefaultNodeModel>, includeOnly?: string[]) {
  74. if (!node) {
  75. return new Array<JSX.Element>();
  76. }
  77. let inputPorts = [];
  78. for (var key in node.ports) {
  79. let port = node.ports[key] as DefaultPortModel;
  80. if (port.position === "input") {
  81. let typeIndicator = this._GetPortTypeIndicator(port.connection!);
  82. let style = this._GetPortStyle(port.connection!.type);
  83. let isConnected = port.connection && port.connection.endpoints.length > 0;
  84. if (!includeOnly || includeOnly.indexOf(port.name) !== -1) {
  85. inputPorts.push(
  86. <div key={key} className="input-port">
  87. <div className="input-port-plug">
  88. <div className="input-port-border">
  89. </div>
  90. <DefaultPortWidget key={key} name={port.name} node={node} style={style}/>
  91. {
  92. !isConnected &&
  93. <div className="output-port-connection">
  94. </div>
  95. }
  96. <div className="input-port-type">
  97. {
  98. typeIndicator
  99. }
  100. </div>
  101. </div>
  102. <div className="input-port-label">
  103. {port.name}
  104. </div>
  105. </div>
  106. );
  107. }
  108. }
  109. }
  110. return inputPorts;
  111. }
  112. }