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