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