1   package fi.jyu.mit.graphics;
2   
3   import java.awt.*;
4   import java.awt.event.*;
5   import javax.swing.*;
6   
7   /**
8    * Ohjain jolla voi kiertää olioita akseliensa ympäri,
9    * painike jokaiselle akselille molempiin suuntiin
10   * @author Markus Kivioja
11   *
12   */
13  public class ControlButtons extends BasicController implements ActionListener {
14      
15      private static final long serialVersionUID = 1L;
16  
17      private boolean autoOn = false;
18      private Rotator autoRotate;
19      private Matrix lastRot = null;
20      private final JPanel xPanel = new JPanel();
21      private final JPanel yPanel = new JPanel();
22      private final JPanel zPanel = new JPanel();
23      private final JPanel autoPanel = new JPanel();
24      private final JCheckBox auto = new JCheckBox("Auto");
25      
26      /**
27       * Luo uuden painikeohjaimen
28       * @param objectToControl olio jota tällä ohjaimella ohjataan
29       */
30      public ControlButtons(Transformable objectToControl) {
31          super(objectToControl);
32          this.setBounds(150, 350, 150, 150);
33          
34          this.xPanel.setLayout(new BorderLayout());
35          this.yPanel.setLayout(new BorderLayout());
36          this.zPanel.setLayout(new BorderLayout());
37          this.autoPanel.setLayout(new BorderLayout());
38          this.setLayout(new FlowLayout());
39          
40          this.autoPanel.add(auto, BorderLayout.CENTER);
41          
42          new TransformButton("KierraX").set(new RotMatrix(Axis.X, -4), xPanel, BorderLayout.WEST, this);
43          new TransformButton("KierraX").set(new RotMatrix(Axis.X,  4), xPanel, BorderLayout.EAST, this);
44          new TransformButton("KierraY").set(new RotMatrix(Axis.Y, -4), yPanel, BorderLayout.WEST, this);
45          new TransformButton("KierraY").set(new RotMatrix(Axis.Y,  4), yPanel, BorderLayout.EAST, this);
46          new TransformButton("KierraZ").set(new RotMatrix(Axis.Z, -4), zPanel, BorderLayout.WEST, this);
47          new TransformButton("KierraZ").set(new RotMatrix(Axis.Z,  4), zPanel, BorderLayout.EAST, this);
48          
49          this.auto.addItemListener(new ItemListener() {
50              public void itemStateChanged(ItemEvent tap) {
51                  if (tap.getStateChange() == ItemEvent.SELECTED) {
52                      autoOn = true;
53                      if ( lastRot != null )
54                          autoRotate = new Rotator(getObject(), lastRot, 20);
55                  }   
56                  if (tap.getStateChange() == ItemEvent.DESELECTED) {
57                      autoOn = false;
58                      if (autoRotate != null) autoRotate.cancel();
59                  }
60              }
61          });
62      
63          add(xPanel);
64          add(yPanel);
65          add(zPanel);
66          add(autoPanel);
67      }
68      
69      /**
70       * Tapahtumankäsittelijä joka käsittelee painikkeiden painallukset
71       * @param e käsiteltävä tapahtuma
72       */
73      public void actionPerformed(ActionEvent e) {
74          Object causer = e.getSource();
75          if ( ! ( causer instanceof TransformButton) ) return;
76          rotate(((TransformButton)causer).getTransform());
77      }
78      
79      /**
80       * Kiertää tämän ohjaimen ohjaamaa oliota annetulla kiertomatriisilla
81       * @param rotM kiertomatriisi
82       */
83      private void rotate(Matrix rotM) {
84          lastRot = rotM;
85          if (autoOn) {
86              if (autoRotate != null) autoRotate.cancel();
87              autoRotate = new Rotator(getObject(), rotM, 20);
88          }
89          else 
90              //getObject().transform(rotM);
91              getObject().setRotator(getObject().getRotator().multiply(rotM));
92      }
93  }
94