Kaynağa Gözat

Clean code a bit

David Catuhe 6 yıl önce
ebeveyn
işleme
4122eabf4b
2 değiştirilmiş dosya ile 116 ekleme ve 128 silme
  1. 112 0
      nodeEditor/src/blockTools.ts
  2. 4 128
      nodeEditor/src/graphEditor.tsx

+ 112 - 0
nodeEditor/src/blockTools.ts

@@ -0,0 +1,112 @@
+import { AlphaTestBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/alphaTestBlock';
+import { BonesBlock } from 'babylonjs/Materials/Node/Blocks/Vertex/bonesBlock';
+import { InstancesBlock } from 'babylonjs/Materials/Node/Blocks/Vertex/instancesBlock';
+import { MorphTargetsBlock } from 'babylonjs/Materials/Node/Blocks/Vertex/morphTargetsBlock';
+import { ImageProcessingBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/imageProcessingBlock';
+import { RGBAMergerBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/rgbaMergerBlock';
+import { RGBASplitterBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/rgbaSplitterBlock';
+import { RGBMergerBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/rgbMergerBlock';
+import { RGBSplitterBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/rgbSplitterBlock';
+import { TextureBlock } from 'babylonjs/Materials/Node/Blocks/Dual/textureBlock';
+import { LightBlock } from 'babylonjs/Materials/Node/Blocks/Dual/lightBlock';
+import { FogBlock } from 'babylonjs/Materials/Node/Blocks/Dual/fogBlock';
+import { VertexOutputBlock } from 'babylonjs/Materials/Node/Blocks/Vertex/vertexOutputBlock';
+import { FragmentOutputBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/fragmentOutputBlock';
+import { AddBlock } from 'babylonjs/Materials/Node/Blocks/addBlock';
+import { ClampBlock } from 'babylonjs/Materials/Node/Blocks/clampBlock';
+import { CrossBlock } from 'babylonjs/Materials/Node/Blocks/crossBlock';
+import { DotBlock } from 'babylonjs/Materials/Node/Blocks/dotBlock';
+import { MultiplyBlock } from 'babylonjs/Materials/Node/Blocks/multiplyBlock';
+import { TransformBlock } from 'babylonjs/Materials/Node/Blocks/transformBlock';
+import { NodeMaterialBlockConnectionPointTypes } from 'babylonjs/Materials/Node/nodeMaterialBlockConnectionPointTypes';
+
+export class BlockTools {
+    public static GetBlockFromString(data: string) {
+        switch (data) {
+            case "BonesBlock":
+                return new BonesBlock("Bones");
+            case "InstancesBlock":
+                return new InstancesBlock("Instances");
+            case "MorphTargetsBlock":
+                return new MorphTargetsBlock("MorphTargets");
+            case "AlphaTestBlock":
+                return new AlphaTestBlock("AlphaTest");
+            case "ImageProcessingBlock":
+                return new ImageProcessingBlock("ImageProcessing");
+            case "RGBAMergerBlock":
+                return new RGBAMergerBlock("RGBAMerger");
+            case "RGBASplitterBlock":
+                return new RGBASplitterBlock("RGBASplitter");
+            case "RGBMergerBlock":
+                return new RGBMergerBlock("RGBMerger");
+            case "RGBSplitterBlock":
+                return new RGBSplitterBlock("RGBSplitter");
+            case "TextureBlock":
+                return new TextureBlock("Texture");
+            case "LightBlock":
+                return new LightBlock("Lights");
+            case "FogBlock":
+                return new FogBlock("Fog");
+            case "VertexOutputBlock":
+                return new VertexOutputBlock("VertexOutput");
+            case "FragmentOutputBlock":
+                return new FragmentOutputBlock("FragmentOutput");
+            case "AddBlock":
+                return new AddBlock("Add");
+            case "ClampBlock":
+                return new ClampBlock("Clamp");
+            case "CrossBlock":
+                return new CrossBlock("Dot");
+            case "DotBlock":
+                return new DotBlock("Dot");
+            case "MultiplyBlock":
+                return new MultiplyBlock("Multiply");
+            case "TransformBlock":
+                return new TransformBlock("Transform");
+        }
+
+        return null;
+    }
+
+    public static GetConnectionNodeTypeFromString(type: string) {
+        switch (type) {
+            case "Float":
+                return NodeMaterialBlockConnectionPointTypes.Float;
+            case "Vector2":
+                return NodeMaterialBlockConnectionPointTypes.Vector2;
+            case "Vector3":
+                return NodeMaterialBlockConnectionPointTypes.Vector3;
+            case "Vector4":
+                return NodeMaterialBlockConnectionPointTypes.Vector4;
+            case "Matrix":
+                return NodeMaterialBlockConnectionPointTypes.Matrix;
+            case "Color3":
+                return NodeMaterialBlockConnectionPointTypes.Color3;
+            case "Color4":
+                return NodeMaterialBlockConnectionPointTypes.Color4;
+        }
+
+        return NodeMaterialBlockConnectionPointTypes.AutoDetect;
+    }
+
+    public static GetStringFromConnectionNodeType(type: NodeMaterialBlockConnectionPointTypes) {
+        switch (type){
+            case NodeMaterialBlockConnectionPointTypes.Float:
+                return "Float";
+            case NodeMaterialBlockConnectionPointTypes.Vector2:
+                return "Vector2";
+            case NodeMaterialBlockConnectionPointTypes.Vector3:
+                return "Vector3";
+            case NodeMaterialBlockConnectionPointTypes.Vector4:
+                return "Vector4";
+            case NodeMaterialBlockConnectionPointTypes.Color3:
+                return "Color3";
+            case NodeMaterialBlockConnectionPointTypes.Color4:
+                return "Color4";
+            case NodeMaterialBlockConnectionPointTypes.Matrix:
+                return "Matrix";
+        }
+
+        return "";
+    }
+}

+ 4 - 128
nodeEditor/src/graphEditor.tsx

@@ -31,25 +31,8 @@ import { DataStorage } from './dataStorage';
 import { NodeMaterialBlockConnectionPointTypes } from 'babylonjs/Materials/Node/nodeMaterialBlockConnectionPointTypes';
 import { InputBlock } from 'babylonjs/Materials/Node/Blocks/Input/inputBlock';
 import { Nullable } from 'babylonjs/types';
-import { BonesBlock } from 'babylonjs/Materials/Node/Blocks/Vertex/bonesBlock';
-import { InstancesBlock } from 'babylonjs/Materials/Node/Blocks/Vertex/instancesBlock';
-import { MorphTargetsBlock } from 'babylonjs/Materials/Node/Blocks/Vertex/morphTargetsBlock';
-import { AlphaTestBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/alphaTestBlock';
-import { ImageProcessingBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/imageProcessingBlock';
-import { RGBAMergerBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/rgbaMergerBlock';
-import { RGBASplitterBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/rgbaSplitterBlock';
-import { FogBlock } from 'babylonjs/Materials/Node/Blocks/Dual/fogBlock';
-import { VertexOutputBlock } from 'babylonjs/Materials/Node/Blocks/Vertex/vertexOutputBlock';
-import { FragmentOutputBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/fragmentOutputBlock';
-import { AddBlock } from 'babylonjs/Materials/Node/Blocks/addBlock';
-import { ClampBlock } from 'babylonjs/Materials/Node/Blocks/clampBlock';
-import { CrossBlock } from 'babylonjs/Materials/Node/Blocks/crossBlock';
-import { DotBlock } from 'babylonjs/Materials/Node/Blocks/dotBlock';
-import { MultiplyBlock } from 'babylonjs/Materials/Node/Blocks/multiplyBlock';
-import { TransformBlock } from 'babylonjs/Materials/Node/Blocks/transformBlock';
 import { MessageDialogComponent } from './sharedComponents/messageDialog';
-import { RGBMergerBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/rgbMergerBlock';
-import { RGBSplitterBlock } from 'babylonjs/Materials/Node/Blocks/Fragment/rgbSplitterBlock';
+import { BlockTools } from './blockTools';
 
 require("storm-react-diagrams/dist/style.min.css");
 require("./main.scss");
@@ -255,27 +238,6 @@ export class GraphEditor extends React.Component<IGraphEditorProps> {
         }
     }
 
-    getStringFromConnectionNodeType(type: NodeMaterialBlockConnectionPointTypes) {
-        switch (type){
-            case NodeMaterialBlockConnectionPointTypes.Float:
-                return "Float";
-            case NodeMaterialBlockConnectionPointTypes.Vector2:
-                return "Vector2";
-            case NodeMaterialBlockConnectionPointTypes.Vector3:
-                return "Vector3";
-            case NodeMaterialBlockConnectionPointTypes.Vector4:
-                return "Vector4";
-            case NodeMaterialBlockConnectionPointTypes.Color3:
-                return "Color3";
-            case NodeMaterialBlockConnectionPointTypes.Color4:
-                return "Color4";
-            case NodeMaterialBlockConnectionPointTypes.Matrix:
-                return "Matrix";
-        }
-
-        return "";
-    }
-
     build(needToWait = false) {
         // setup the diagram model
         this._model = new DiagramModel();
@@ -314,7 +276,7 @@ export class GraphEditor extends React.Component<IGraphEditorProps> {
                         if (!e.link.targetPort && e.link.sourcePort) {
                             // Drag from input port, we are going to build an input for it                            
                             let input = e.link.sourcePort as DefaultPortModel;
-                            let nodeModel = this.addValueNode(this.getStringFromConnectionNodeType(input.connection!.type));
+                            let nodeModel = this.addValueNode(BlockTools.GetStringFromConnectionNodeType(input.connection!.type));
                             let link = nodeModel.ports.output.link(input);
 
                             nodeModel.x = e.link.points[1].x - this.NodeWidth;
@@ -417,30 +379,7 @@ export class GraphEditor extends React.Component<IGraphEditorProps> {
     }
 
     addValueNode(type: string) {
-        let nodeType: NodeMaterialBlockConnectionPointTypes = NodeMaterialBlockConnectionPointTypes.Vector3;
-        switch (type) {
-            case "Float":
-                nodeType = NodeMaterialBlockConnectionPointTypes.Float;
-                break;
-            case "Vector2":
-                nodeType = NodeMaterialBlockConnectionPointTypes.Vector2;
-                break;
-            case "Vector3":
-                nodeType = NodeMaterialBlockConnectionPointTypes.Vector3;
-                break;
-            case "Vector4":
-                nodeType = NodeMaterialBlockConnectionPointTypes.Vector4;
-                break;
-            case "Matrix":
-                nodeType = NodeMaterialBlockConnectionPointTypes.Matrix;
-                break;
-            case "Color3":
-                nodeType = NodeMaterialBlockConnectionPointTypes.Color3;
-                break;
-            case "Color4":
-                nodeType = NodeMaterialBlockConnectionPointTypes.Color4;
-                break;
-        }
+        let nodeType: NodeMaterialBlockConnectionPointTypes = BlockTools.GetConnectionNodeTypeFromString(type);
 
         let newInputBlock = new InputBlock(type, undefined, nodeType);
         newInputBlock.setDefaultValue();
@@ -495,70 +434,7 @@ export class GraphEditor extends React.Component<IGraphEditorProps> {
         if (data.indexOf("Block") === -1) {
             nodeModel = this.addValueNode(data);
         } else {
-            let block: Nullable<NodeMaterialBlock> = null;
-
-            switch (data) {
-                case "BonesBlock":
-                    block = new BonesBlock("Bones");
-                    break;
-                case "InstancesBlock":
-                    block = new InstancesBlock("Instances");
-                    break;
-                case "MorphTargetsBlock":
-                    block = new MorphTargetsBlock("MorphTargets");
-                    break;
-                case "AlphaTestBlock":
-                    block = new AlphaTestBlock("AlphaTest");
-                    break;
-                case "ImageProcessingBlock":
-                    block = new ImageProcessingBlock("ImageProcessing");
-                    break;
-                case "RGBAMergerBlock":
-                    block = new RGBAMergerBlock("RGBAMerger");
-                    break;
-                case "RGBASplitterBlock":
-                    block = new RGBASplitterBlock("RGBASplitter");
-                    break;
-                case "RGBMergerBlock":
-                    block = new RGBMergerBlock("RGBMerger");
-                    break;
-                case "RGBSplitterBlock":
-                    block = new RGBSplitterBlock("RGBSplitter");
-                    break;                    
-                case "TextureBlock":
-                    block = new TextureBlock("Texture");
-                    break;
-                case "LightBlock":
-                    block = new LightBlock("Lights");
-                    break;
-                case "FogBlock":
-                    block = new FogBlock("Fog");
-                    break;
-                case "VertexOutputBlock":
-                    block = new VertexOutputBlock("VertexOutput");
-                    break;
-                case "FragmentOutputBlock":
-                    block = new FragmentOutputBlock("FragmentOutput");
-                    break;
-                case "AddBlock":
-                    block = new AddBlock("Add");
-                    break;
-                case "ClampBlock":
-                    block = new ClampBlock("Clamp");
-                    break;
-                case "CrossBlock":
-                    block = new CrossBlock("Dot");
-                    break;
-                case "DotBlock":
-                    block = new DotBlock("Dot");
-                    break;
-                case "MultiplyBlock":
-                    block = new MultiplyBlock("Multiply");
-                    break;
-                case "TransformBlock":
-                    block = new TransformBlock("Transform");
-                    break;
-            }
+            let block = BlockTools.GetBlockFromString(data);          
 
             if (block) {
                 nodeModel = this.createNodeFromObject({ nodeMaterialBlock: block });