Jogamp
50ffd6bcce72a27a84ba0d9376ab29b45f503479
[jocl-demos.git] / src / com / mbien / opencl / demos / joglinterop / UserSceneInteraction.java
1 package com.mbien.opencl.demos.joglinterop;
2
3 import java.awt.Component;
4 import java.awt.Point;
5 import java.awt.event.MouseAdapter;
6 import java.awt.event.MouseEvent;
7 import java.awt.event.MouseMotionAdapter;
8 import java.awt.event.MouseWheelEvent;
9 import java.awt.event.MouseWheelListener;
10 import javax.media.opengl.GL2;
11
12 /**
13  * Utility class for interacting with a scene. Supports rotation and zoom around origin.
14  * @author Michael Bien
15  */
16 public class UserSceneInteraction {
17
18     private float z = -150;
19     private float rotx;
20     private float roty;
21
22     private Point dragstart;
23     private enum MOUSE_MODE { DRAG_ROTATE, DRAG_ZOOM }
24     private MOUSE_MODE dragmode = MOUSE_MODE.DRAG_ROTATE;
25
26
27     public void init(Component component) {
28         initMouseListeners(component);
29     }
30
31     private void initMouseListeners(Component component) {
32         component.addMouseMotionListener(new MouseMotionAdapter() {
33
34             @Override
35             public void mouseDragged(MouseEvent e) {
36
37                 if (dragstart != null) {
38                     switch (dragmode) {
39                         case DRAG_ROTATE:
40                             rotx += e.getY() - dragstart.getY();
41                             roty += e.getX() - dragstart.getX();
42                             break;
43                         case DRAG_ZOOM:
44                             z += (e.getY() - dragstart.getY()) / 5.0f;
45                             break;
46                     }
47                 }
48
49                 dragstart = e.getPoint();
50             }
51         });
52         component.addMouseWheelListener(new MouseWheelListener() {
53
54             public void mouseWheelMoved(MouseWheelEvent e) {
55                 z += e.getWheelRotation()*5;
56             }
57             
58         });
59         component.addMouseListener(new MouseAdapter() {
60
61             @Override
62             public void mousePressed(MouseEvent e) {
63                 switch (e.getButton()) {
64                     case (MouseEvent.BUTTON1):
65                         dragmode = MOUSE_MODE.DRAG_ROTATE;
66                         break;
67                     case (MouseEvent.BUTTON2):
68                         dragmode = MOUSE_MODE.DRAG_ZOOM;
69                         break;
70                     case (MouseEvent.BUTTON3):
71                         dragmode = MOUSE_MODE.DRAG_ZOOM;
72                         break;
73                 }
74             }
75
76             @Override
77             public void mouseReleased(MouseEvent e) {
78                 switch (e.getButton()) {
79                     case (MouseEvent.BUTTON1):
80                         dragmode = MOUSE_MODE.DRAG_ZOOM;
81                         break;
82                     case (MouseEvent.BUTTON2):
83                         dragmode = MOUSE_MODE.DRAG_ROTATE;
84                         break;
85                     case (MouseEvent.BUTTON3):
86                         dragmode = MOUSE_MODE.DRAG_ROTATE;
87                         break;
88                 }
89
90                 dragstart = null;
91             }
92         });
93     }
94
95
96     public void interact(GL2 gl) {
97         gl.glTranslatef(0, 0, z);
98         gl.glRotatef(rotx, 1f, 0f, 0f);
99         gl.glRotatef(roty, 0f, 1.0f, 0f);
100     }
101
102
103 }
http://JogAmp.org git info: FAQ, tutorial and man pages.