summaryrefslogtreecommitdiffstats
path: root/src/com/mbien/opencl/demos/joglinterop/UserSceneInteraction.java
blob: bec2d2866fed932ee1792fd1ddc16bb492f4d0e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.mbien.opencl.demos.joglinterop;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.media.opengl.GL2;

/**
 * Utility class for interacting with a scene. Supports rotation and zoom around origin.
 * @author Michael Bien
 */
public class UserSceneInteraction {

    private float z = -20;
    private float rotx = 45;
    private float roty = 30;

    private Point dragstart;
    private enum MOUSE_MODE { DRAG_ROTATE, DRAG_ZOOM }
    private MOUSE_MODE dragmode = MOUSE_MODE.DRAG_ROTATE;


    public void init(Component component) {
        initMouseListeners(component);
    }

    private void initMouseListeners(Component component) {
        component.addMouseMotionListener(new MouseMotionAdapter() {

            @Override
            public void mouseDragged(MouseEvent e) {

                if (dragstart != null) {
                    switch (dragmode) {
                        case DRAG_ROTATE:
                            rotx += e.getY() - dragstart.getY();
                            roty += e.getX() - dragstart.getX();
                            break;
                        case DRAG_ZOOM:
                            z += (e.getY() - dragstart.getY()) / 5.0f;
                            break;
                    }
                }

                dragstart = e.getPoint();
            }
        });
        component.addMouseWheelListener(new MouseWheelListener() {

            public void mouseWheelMoved(MouseWheelEvent e) {
                z += e.getWheelRotation()*5;
            }

        });
        component.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                switch (e.getButton()) {
                    case (MouseEvent.BUTTON1):
                        dragmode = MOUSE_MODE.DRAG_ROTATE;
                        break;
                    case (MouseEvent.BUTTON2):
                        dragmode = MOUSE_MODE.DRAG_ZOOM;
                        break;
                    case (MouseEvent.BUTTON3):
                        dragmode = MOUSE_MODE.DRAG_ZOOM;
                        break;
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                switch (e.getButton()) {
                    case (MouseEvent.BUTTON1):
                        dragmode = MOUSE_MODE.DRAG_ZOOM;
                        break;
                    case (MouseEvent.BUTTON2):
                        dragmode = MOUSE_MODE.DRAG_ROTATE;
                        break;
                    case (MouseEvent.BUTTON3):
                        dragmode = MOUSE_MODE.DRAG_ROTATE;
                        break;
                }

                dragstart = null;
            }
        });
    }


    public void interact(GL2 gl) {
        gl.glTranslatef(0, 0, z);
        gl.glRotatef(rotx, 1f, 0f, 0f);
        gl.glRotatef(roty, 0f, 1.0f, 0f);
    }


}