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      protected boolean autoOn = false;
18      private Rotator autoRotate;
19      protected 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              @Override
51              public void itemStateChanged(ItemEvent tap) {
52                  if (tap.getStateChange() == ItemEvent.SELECTED) {
53                      autoOn = true;
54                      if ( lastRot != null )
55                          setAutoRotate(new Rotator(getObject(), lastRot, 20));
56                  }   
57                  if (tap.getStateChange() == ItemEvent.DESELECTED) {
58                      autoOn = false;
59                      if (getAutoRotate() != null) getAutoRotate().cancel();
60                  }
61              }
62          });
63      
64          add(xPanel);
65          add(yPanel);
66          add(zPanel);
67          add(autoPanel);
68      }
69      
70      /**
71       * Tapahtumankäsittelijä joka käsittelee painikkeiden painallukset
72       * @param e käsiteltävä tapahtuma
73       */
74      @Override
75      public void actionPerformed(ActionEvent e) {
76          Object causer = e.getSource();
77          if ( ! ( causer instanceof TransformButton) ) return;
78          rotate(((TransformButton)causer).getTransform());
79      }
80      
81      /**
82       * Kiertää tämän ohjaimen ohjaamaa oliota annetulla kiertomatriisilla
83       * @param rotM kiertomatriisi
84       */
85      private void rotate(Matrix rotM) {
86          lastRot = rotM;
87          if (autoOn) {
88              if (getAutoRotate() != null) getAutoRotate().cancel();
89              setAutoRotate(new Rotator(getObject(), rotM, 20));
90          }
91          else 
92              //getObject().transform(rotM);
93              getObject().setRotator(getObject().getRotator().multiply(rotM));
94      }
95  
96      public Rotator getAutoRotate() {
97          return autoRotate;
98      }
99  
100     public void setAutoRotate(Rotator autoRotate) {
101         this.autoRotate = autoRotate;
102     }
103 }
104