]> gerrit.simantics Code Review - simantics/3d.git/blob - vtk/src/vtk/vtkCanvas.java
Mesh API to use Tuple3d instead of Vector3d
[simantics/3d.git] / vtk / src / vtk / vtkCanvas.java
1 package vtk;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.awt.event.ComponentAdapter;
6 import java.awt.event.ComponentEvent;
7 import java.awt.event.InputEvent;
8 import java.awt.event.KeyEvent;
9 import java.awt.event.KeyListener;
10 import java.awt.event.MouseEvent;
11 import java.awt.event.MouseListener;
12 import java.awt.event.MouseMotionListener;
13
14 import javax.swing.Timer;
15
16 /**
17  * Java AWT component that encapsulate vtkGenericRenderWindowInteractor,
18  * vtkPlaneWidget, vtkBoxWidget and extends vtkPanel
19  *
20  * @see vtkPanel
21  * @author Kitware
22  */
23 public class vtkCanvas extends vtkPanel implements MouseListener, MouseMotionListener, KeyListener {
24     private static final long serialVersionUID = 1L;
25     protected vtkGenericRenderWindowInteractor iren = new vtkGenericRenderWindowInteractor();
26     protected Timer timer = new Timer(10, new DelayAction());
27     protected int ctrlPressed = 0;
28     protected int shiftPressed = 0;
29     protected vtkPlaneWidget pw = new vtkPlaneWidget();
30     protected vtkBoxWidget bw = new vtkBoxWidget();
31
32     static {
33         // load up hybrid for 3d widgets
34         vtkNativeLibrary.HYBRID.LoadLibrary();
35         vtkNativeLibrary.WIDGETS.LoadLibrary();
36     }
37
38     public void Delete() {
39         iren = null;
40         pw = null;
41         bw = null;
42         super.Delete();
43     }
44
45     public vtkCanvas() {
46         super();
47         Initialize();
48     }
49
50     public vtkCanvas(vtkRenderWindow renwin) {
51         super(renwin);
52         Initialize();
53     }
54
55     protected void Initialize() {
56         iren.SetRenderWindow(rw);
57         iren.TimerEventResetsTimerOff();
58         iren.AddObserver("CreateTimerEvent", this, "StartTimer");
59         iren.AddObserver("DestroyTimerEvent", this, "DestroyTimer");
60         iren.SetSize(200, 200);
61         iren.ConfigureEvent();
62         pw.AddObserver("EnableEvent", this, "BeginPlaneInteraction");
63         bw.AddObserver("EnableEvent", this, "BeginBoxInteraction");
64         pw.SetKeyPressActivationValue('l');
65         bw.SetKeyPressActivationValue('b');
66         pw.SetInteractor(iren);
67         bw.SetInteractor(iren);
68
69         addComponentListener(new ComponentAdapter() {
70             public void componentResized(ComponentEvent event) {
71                 // The Canvas is being resized, get the new size
72                 int width = getWidth();
73                 int height = getHeight();
74                 setSize(width, height);
75             }
76         });
77
78         ren.SetBackground(0.0, 0.0, 0.0);
79
80         // Setup same interactor style than vtkPanel
81         vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera();
82         iren.SetInteractorStyle(style);
83     }
84
85     public void StartTimer() {
86         if (timer.isRunning())
87             timer.stop();
88
89         timer.setRepeats(true);
90         timer.start();
91     }
92
93     public void DestroyTimer() {
94         if (timer.isRunning())
95             timer.stop();
96     }
97
98     /**
99      * Replace by getRenderWindowInteractor()
100      */
101     @Deprecated
102     public vtkGenericRenderWindowInteractor getIren() {
103         return this.iren;
104     }
105
106     public vtkGenericRenderWindowInteractor getRenderWindowInteractor() {
107         return this.iren;
108     }
109
110     public void setInteractorStyle(vtkInteractorStyle style) {
111         iren.SetInteractorStyle(style);
112     }
113
114     public void addToPlaneWidget(vtkProp3D prop) {
115         pw.SetProp3D(prop);
116         pw.PlaceWidget();
117     }
118
119     public void addToBoxWidget(vtkProp3D prop) {
120         bw.SetProp3D(prop);
121         bw.PlaceWidget();
122     }
123
124     public void BeginPlaneInteraction() {
125         System.out.println("Plane widget begin interaction");
126     }
127
128     public void BeginBoxInteraction() {
129         System.out.println("Box widget begin interaction");
130     }
131
132     public void setSize(int x, int y) {
133         super.setSize(x, y);
134         if (windowset == 1) {
135             Lock();
136             rw.SetSize(x, y);
137             iren.SetSize(x, y);
138             iren.ConfigureEvent();
139             UnLock();
140         }
141     }
142
143     public void mouseClicked(MouseEvent e) {
144     }
145
146     public void mousePressed(MouseEvent e) {
147         if (ren.VisibleActorCount() == 0)
148             return;
149         Lock();
150         rw.SetDesiredUpdateRate(5.0);
151         lastX = e.getX();
152         lastY = e.getY();
153
154         ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
155         shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
156
157         iren.SetEventInformationFlipY(e.getX(), e.getY(), ctrlPressed, shiftPressed, '0', 0, "0");
158
159         if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
160             iren.LeftButtonPressEvent();
161         } else if ((e.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
162             iren.MiddleButtonPressEvent();
163         } else if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
164             iren.RightButtonPressEvent();
165         }
166         UnLock();
167     }
168
169     public void mouseReleased(MouseEvent e) {
170         rw.SetDesiredUpdateRate(0.01);
171
172         ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
173         shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
174
175         iren.SetEventInformationFlipY(e.getX(), e.getY(), ctrlPressed, shiftPressed, '0', 0, "0");
176
177         if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
178             Lock();
179             iren.LeftButtonReleaseEvent();
180             UnLock();
181         }
182
183         if ((e.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
184             Lock();
185             iren.MiddleButtonReleaseEvent();
186             UnLock();
187         }
188
189         if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
190             Lock();
191             iren.RightButtonReleaseEvent();
192             UnLock();
193         }
194     }
195
196     public void mouseEntered(MouseEvent e) {
197         this.requestFocus();
198         iren.SetEventInformationFlipY(e.getX(), e.getY(), 0, 0, '0', 0, "0");
199         iren.EnterEvent();
200     }
201
202     public void mouseExited(MouseEvent e) {
203         iren.SetEventInformationFlipY(e.getX(), e.getY(), 0, 0, '0', 0, "0");
204         iren.LeaveEvent();
205     }
206
207     public void mouseMoved(MouseEvent e) {
208         lastX = e.getX();
209         lastY = e.getY();
210
211         ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
212         shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
213
214         iren.SetEventInformationFlipY(e.getX(), e.getY(), ctrlPressed, shiftPressed, '0', 0, "0");
215
216         Lock();
217         iren.MouseMoveEvent();
218         UnLock();
219     }
220
221     public void mouseDragged(MouseEvent e) {
222         if (ren.VisibleActorCount() == 0)
223             return;
224
225         ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
226         shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
227
228         iren.SetEventInformationFlipY(e.getX(), e.getY(), ctrlPressed, shiftPressed, '0', 0, "0");
229
230         Lock();
231         iren.MouseMoveEvent();
232         UnLock();
233
234         UpdateLight();
235     }
236
237     public void keyTyped(KeyEvent e) {
238     }
239
240     public void keyPressed(KeyEvent e) {
241         if (ren.VisibleActorCount() == 0)
242             return;
243         char keyChar = e.getKeyChar();
244
245         ctrlPressed = (e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK ? 1 : 0;
246         shiftPressed = (e.getModifiers() & InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK ? 1 : 0;
247
248         iren.SetEventInformationFlipY(lastX, lastY, ctrlPressed, shiftPressed, keyChar, 0, String.valueOf(keyChar));
249
250         Lock();
251         iren.KeyPressEvent();
252         iren.CharEvent();
253         UnLock();
254     }
255
256     public void keyReleased(KeyEvent e) {
257     }
258
259     private class DelayAction implements ActionListener {
260         public void actionPerformed(ActionEvent evt) {
261             Lock();
262             iren.TimerEvent();
263             UpdateLight();
264             UnLock();
265         }
266     }
267 }