1 package fi.jyu.mit.graphics;
2
3 import java.awt.event.*;
5 import java.awt.*;
6
7
8
13 public class TouchPad extends BasicController implements MouseListener , MouseMotionListener{
14
15 private static final long serialVersionUID = 1L;
16 private int dx, dy;
17 protected SpacePanel panel;
18 private BasicShape circle = new Circle(0,0,4);
19 private boolean move = false;
20 private int lastY = 0;
21
22
27 public TouchPad(Transformable objectToControl, boolean move) {
28 this(objectToControl);
29 this.move = move;
30 }
31
32
36 public TouchPad(Transformable objectToControl) {
37 super(objectToControl);
38 this.setBounds(0, 0, 150, 150);
39 panel = new SpacePanel();
40 panel.setPreferredSize(new Dimension(150,150));
41 add(panel, BorderLayout.CENTER);
42 panel.addMouseListener(this);
44 panel.addMouseMotionListener(this);
45 dx = getWidth();
46 dy = getHeight();
47 circle = new Circle(0,0,4);
48 panel.add(circle);
49 panel.addComponentListener(new ComponentAdapter() {
50 @SuppressWarnings("synthetic-access")
51 @Override
52 public void componentResized(ComponentEvent e) {
53 dx = panel.getWidth();
54 dy = panel.getHeight();
55 circle.setTransform(new TranslateMatrix(dx/2,dy/2,0));
56 }
57 });
58 }
59
60
64 public void setMove(boolean move) {
65 this.move = move;
66 }
67
68
72 private void countDir(MouseEvent e) {
73 double degz = -((double)(e.getX() - (dx/2))/dx * 180);
74 double degx = ((double)(e.getY() - (dy/2))/dy * 180);
75 RotMatrix Rx = new RotMatrix(Axis.X,degx);
76 RotMatrix Rz = new RotMatrix(Axis.Z,degz);
77 getObject().setRotator(Rx.multiply(Rz));
78 }
79
80
84 @Override
85 public void mouseMoved(MouseEvent e) {
86 if ( move ) countDir(e);
87 }
88
89
93 @Override
94 public void mouseDragged(MouseEvent e) {
95 int button = Integer.parseInt(e.toString().charAt(e.toString().indexOf("Button")+6)+"");
96 int y;
97 if ( !move && button == 1) countDir(e);
98 else {
99 y = e.getY();
100 if (this.lastY < y) getObject().transform(new ScaleMatrix(0.95, 0.95, 0.95));
101 if (this.lastY > y) getObject().transform(new ScaleMatrix(1.05, 1.05, 1.05));
102 this.lastY = y;
103 }
104 }
105
106 @Override
107 public void mouseEntered(MouseEvent e) { }
108 @Override
109 public void mouseExited(MouseEvent e) { }
110 @Override
111 public void mouseClicked(MouseEvent e) { }
112 @Override
113 public void mouseReleased(MouseEvent e) { }
114 @Override
115 public void mousePressed(MouseEvent e) { }
116 }
117