-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputMonitor.java
120 lines (85 loc) · 2.34 KB
/
InputMonitor.java
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package display3D;
import java.awt.event.*;
/**
* monitors mouse input on a canvass and adjusts the canvass' camera accordingly
* @author Luke
*
*/
public class InputMonitor implements MouseListener, MouseMotionListener,MouseWheelListener{
private int mouseDragX , mouseDragY;
private int mouseX , mouseY;
private Camera3D camera;
private Scene3D scene;
public InputMonitor( Camera3D camera , Scene3D scene){
this.camera =camera;
this.scene = scene;
this.scene.addMouseListener(this);
this.scene.addMouseMotionListener(this);
this.scene.addMouseWheelListener(this);
}
public int [] getDrag(){
int [] result = new int[]{ mouseDragX , mouseDragY };
mouseX -= mouseDragX;
mouseY -= mouseDragY;
mouseDragX = 0;
mouseDragY = 0;
return result;
}
public void mouseDragged(MouseEvent e) {
mouseDragX = -(e.getX() - mouseX);
mouseDragY = -(e.getY() - mouseY);
//get the mouse drag amount
int [] drag = getDrag();
if( !e.isShiftDown() ){
double deltaPhi = ((double)drag[0])/100.;
double deltaTheta = ((double)drag[1])/100.;
camera.rotatePhi(deltaPhi);
camera.rotateTheta(deltaTheta);
}else{
Vect foc = camera.getFocus();
Vect[] xy = camera.getCamXYVectors();
foc = foc.add( xy[0].scale( 1./camera.getZoom() * ((double)drag[0])) );
foc = foc.add( xy[1].scale( 1./camera.getZoom() * ((double)-drag[1])) );
camera.setFocus(foc);
}
scene.repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
if( e.isControlDown() ){
camera.setLocation(1 , 0 , 0);
//camera.setFocus(new Vect(0,0,0));
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
public void mouseReleased(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
mouseDragX = 0;
mouseDragY = 0;
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
double tics = -e.getPreciseWheelRotation();
if( tics > 0 )
camera.setZoom(camera.getZoom()/0.9);
else
camera.setZoom(camera.getZoom()/1.1);
scene.repaint();
}
}