1   package fi.jyu.mit.graphics;
2   
3   //import java.awt.Color;
4   import java.awt.event.*;
5   import java.awt.*;
6   
7   
8   /**
9    * Ohjain jolla voi pyöritellä olioita hiiren avulla
10   * @author Markus Kivioja
11   *
12   */
13  public class TouchPad extends BasicController implements MouseListener , MouseMotionListener{
14      
15      private static final long serialVersionUID = 1L;
16      private int dx, dy;
17      private SpacePanel panel;
18      private BasicShape circle = new Circle(0,0,4); 
19      private boolean move = false;
20      
21      /**
22       * Luo uuden kosketusohjaimen
23       * @param objectToControl olio jota ohjaimella ohjataan
24       * @param move täytyykö hiiren painikkeen olla painettuna, tosi/epätosi
25       */
26      public TouchPad(Transformable objectToControl, boolean move) {
27          this(objectToControl);
28          this.move = move; 
29      }
30      
31      /**
32       * Luo uuden kosketusohjaimen siten, että hiiren painikkeen ei tarvitse olla painettuna
33       * @param objectToControl olio jota ohjaimella ohjataan
34       */
35      public TouchPad(Transformable objectToControl) {
36          super(objectToControl);
37          this.setBounds(0, 0, 150, 150);
38          panel = new SpacePanel();
39          panel.setPreferredSize(new Dimension(150,150));
40          add(panel, BorderLayout.CENTER);    
41          //panel.setBackground(Color.RED);
42          panel.addMouseListener(this);
43          panel.addMouseMotionListener(this);
44          dx = getWidth();
45          dy = getHeight();
46          circle = new Circle(0,0,4); 
47          panel.add(circle);
48          panel.addComponentListener(new ComponentAdapter() {
49              public void componentResized(ComponentEvent e) {
50                  dx = panel.getWidth();
51                  dy = panel.getHeight();
52                  circle.setTransform(new TranslateMatrix(dx/2,dy/2,0));
53              }
54          });   
55      }
56  
57      /**
58       * Asettaa totuusarvon sille täytyykö hiiren painikkeen olla painettuna
59       * @param move täytyykö hiiren painikkeen olla painettuna, tosi/epätosi
60       */
61      public void setMove(boolean move) {
62          this.move = move;
63      }
64      
65      /**
66       * Laskee ja asettaa oliolle kiertomatriisin
67       * @param e hiiritapahtuma
68       */
69      private void countDir(MouseEvent e) {
70          double degz = -((double)(e.getX() - (dx/2))/dx * 180);
71          double degx = ((double)(e.getY() - (dy/2))/dy * 180);
72          RotMatrix Rx = new RotMatrix(Axis.X,degx);
73          RotMatrix Rz = new RotMatrix(Axis.Z,degz);
74          getObject().setRotator(Rx.multiply(Rz));
75      }
76      
77      /**
78       * Tapatumankäsittelijä hiiren liikuttamiselle
79       * @param e hiiritapahtuma
80       */
81      public void mouseMoved(MouseEvent e) {
82          if ( move ) countDir(e);
83      }
84  
85      /**
86       * Tapahtumankäsittelijä hiiren raahaamiselle
87       * @param e hiiritapahtuma
88       */
89      public void mouseDragged(MouseEvent e) {
90          if ( !move ) countDir(e);
91      }
92  
93      public void mouseEntered(MouseEvent e)  { /* */ }
94      public void mouseExited(MouseEvent e)   { /* */ }
95      public void mouseClicked(MouseEvent e)  { /* */ }
96      public void mouseReleased(MouseEvent e) { /* */ }
97      public void mousePressed(MouseEvent e)  { /* */ }
98  }
99