001    package swingui;
002    
003    import java.awt.GridLayout;
004    import java.awt.event.ItemEvent;
005    import java.awt.event.ItemListener;
006    
007    import javax.swing.JComboBox;
008    import javax.swing.JLabel;
009    import javax.swing.JPanel;
010    
011    import physics3d.Angle;
012    import physics3d.Vect3;
013    
014    import components.GameObject;
015    
016    /** The OrientationPanel contains a JComboBox that allows the user to change the orientation
017     * of the given component. Orientation can be 0/90/180/270. 
018     *  <p>
019     *  Like the other panels, this expects getObject() to be called; when this happens it 
020     *  updates the UI with information about the new object. 
021     */
022    public class OrientationPanel extends JPanel implements ItemListener {
023            /** a UID. for grins and giggles **/
024            private static final long serialVersionUID = 1L;
025            
026            /** the object whose orientation we display/modify **/
027            private GameObject obj; 
028            /** the orient box with 0/90/180/270 **/
029            private JComboBox orientBox;
030            
031            /** create a default OrientationPanel with nothing selected **/
032            public OrientationPanel() {
033                    super();
034                    
035                    //make components
036                    JLabel orientLabel = new JLabel(" Orientation: ");
037                    orientBox = new JComboBox(AngleSelect.DEFAULT_ANGLES);
038                    orientBox.addItemListener(this);
039                    
040                    //set to default (nothing selected)
041                    setObject(null);
042                    
043                    //add to layout
044                    add(orientLabel);
045                    add(orientBox);
046            }
047    
048            /** when the user selects a different orientation, change that of the object **/
049            public void itemStateChanged(ItemEvent e) {
050                    if (obj == null)
051                            return;
052                    AngleSelect as = (AngleSelect) orientBox.getSelectedItem();
053                    Angle a = as.getAngle();
054                    if (obj != null) {
055                            Vect3 tlf = obj.getTLF();
056                            obj.setOrientAngle(a);
057                            obj.setTLF(tlf);  //tlf is actually the same loc, but center changes for flippers
058                            //gui.getCanvas().display();
059                    }
060            }
061            
062            /** change the object that we are displaying info about **/
063            public void setObject(GameObject go) {
064                    obj = go; //set
065                    if (go != null)
066                            configureOrientBox();
067                    else
068                            orientBox.setSelectedIndex(-1); //no selection
069            }
070            
071            //selects correct angle for this object in combo box
072            private void configureOrientBox() {
073                    Angle a = obj.getOrientAngle();
074                    if (a.equals(Angle.ZERO)) {
075                            orientBox.setSelectedItem(AngleSelect.zero);
076                    } else if (a.equals(Angle.DEG_90)) {
077                            orientBox.setSelectedItem(AngleSelect.ninety);
078                    } else if (a.equals(Angle.DEG_180)) {
079                            orientBox.setSelectedItem(AngleSelect.one_eighty);
080                    } else if (a.equals(Angle.DEG_270)) {
081                            orientBox.setSelectedItem(AngleSelect.two_seventy);
082                    } else {
083                            throw new RuntimeException("wtf? angle="+a);
084                    }
085            }
086            
087            public String getName() { return "Orientation"; }
088    
089    }