]> gerrit.simantics Code Review - simantics/3d.git/blob
c19847b5b05a702cca221877b2291a4f679b3cba
[simantics/3d.git] /
1 package org.simantics.processeditor.actions;\r
2 \r
3 import java.awt.event.MouseEvent;\r
4 import java.util.List;\r
5 \r
6 import javax.vecmath.Point3d;\r
7 import javax.vecmath.Vector3d;\r
8 \r
9 import org.simantics.db.Graph;\r
10 import org.simantics.db.Resource;\r
11 import org.simantics.proconf.g3d.actions.InteractiveAction;\r
12 import org.simantics.proconf.g3d.base.MathTools;\r
13 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
14 import org.simantics.proconf.g3d.base.VecmathJmeTools;\r
15 \r
16 import com.jme.math.Vector3f;\r
17 import com.jme.renderer.ColorRGBA;\r
18 import com.jme.scene.Line;\r
19 import com.jme.scene.state.MaterialState;\r
20 import com.jme.util.geom.BufferUtils;\r
21 \r
22 public class SelectSplitPointAction extends InteractiveAction {\r
23         \r
24         Point3d start;\r
25         Point3d end;\r
26         Vector3d dir;\r
27         \r
28     Line line;\r
29     \r
30     boolean activated;\r
31     \r
32     SplitPointListener listener;\r
33     \r
34     public SelectSplitPointAction(ThreeDimensionalEditorBase parent, SplitPointListener listener) {\r
35                 super(parent);\r
36                 this.listener = listener;\r
37         }\r
38     \r
39     \r
40     public void setSplit(Point3d start, Point3d end) {\r
41         this.start = start;\r
42         this.end = end;\r
43         dir = new Vector3d(end);\r
44                 dir.sub(start);\r
45     }\r
46         \r
47         \r
48         @Override\r
49         public void activate() {\r
50                 if (start == null) throw new RuntimeException("Starting split action without information about range");\r
51                 \r
52 //              start = new Point3d();\r
53 //              end = new Point3d();\r
54 //              PipingTools.getStraightPipeEnds(PipingTools.getPipeline(straight),\r
55 //                              straight, startStart, startEnd);\r
56     \r
57         line = new Line();\r
58         MaterialState ms = parent.getRenderingComponent().getDisplaySystem().getRenderer().createMaterialState();\r
59         ms.setEmissive(new ColorRGBA(1.f,1.f,1.f,1.f));\r
60         line.setRenderState(ms);\r
61         parent.getRenderingComponent().getNoShadowRoot().attachChild(line);\r
62         activated = true;\r
63                 \r
64         }\r
65         \r
66         @Override\r
67         public boolean usable(Graph graph, List<Resource> resources) {\r
68 //              if (resources.size() != 1)\r
69 //                      return false;\r
70 //              IEntity entity = EntityFactory.create(graph, resources.get(0));\r
71 //              if (entity.isInstanceOf(ProcessResource.plant3Dresource.VariableLengthInlineComponent)) {\r
72 //                      return true;\r
73 //              }\r
74                 // This is not a standalone action\r
75                 return false;\r
76         }\r
77         \r
78         @Override\r
79         public void deactivate() {\r
80                 line.removeFromParent();\r
81                 activated = false;\r
82                 start = null;\r
83                 end = null;\r
84                 dir = null;\r
85                 \r
86         }\r
87         \r
88         public boolean active() {\r
89                 return activated;\r
90         }\r
91         \r
92         @Override\r
93         public void update() {\r
94                 if (!activated) {\r
95                         return;\r
96                 }\r
97                 if (input.mouseClicked() && input.clickButton() == MouseEvent.BUTTON3) {\r
98                         listener.setSplitPoint(null);\r
99                         activated = false;\r
100                         return;\r
101                 }\r
102                 Vector3d o = new Vector3d();\r
103                 Vector3d d = new Vector3d();\r
104                 parent.createPickRay(o, d);\r
105 \r
106                 Vector3d normal = parent.getCamera().getUnNormalizedHeading();\r
107                 normal.normalize();\r
108                 normal.negate();\r
109                 // reference point for selection line\r
110                 Vector3d point = new Vector3d(start);\r
111                 Vector3d currentPoint = new Vector3d();\r
112                 MathTools.intersectStraightPlane(o, d, point, normal, currentPoint);\r
113                 Point3d startPoint = new Point3d();\r
114                 double mu[] = new double[2];\r
115                 MathTools.intersectStraightStraight(start, dir, o, d, startPoint,\r
116                                 new Point3d(), mu);\r
117                 // startPoint of branch must be between pipe ends\r
118                 // TODO : take account sizes of elbows (or other components)\r
119                 if (mu[0] < 0.0)\r
120                         startPoint.set(start);\r
121                 else if (mu[0] > 1.0)\r
122                         startPoint.set(end);\r
123 \r
124                 Vector3f verts[] = new Vector3f[2];\r
125                 verts[0] = VecmathJmeTools.get(startPoint);\r
126                 verts[1] = VecmathJmeTools.get(currentPoint);\r
127                 line.reconstruct(BufferUtils.createFloatBuffer(verts), null, null, null);\r
128 \r
129                 parent.setViewChanged(true);\r
130                 if (input.mouseClicked() && input.clickButton() == MouseEvent.BUTTON1) {\r
131                         listener.setSplitPoint(new Point3d(startPoint));\r
132                 }\r
133                 \r
134         }\r
135 }\r