/*
 * part of SpaceWars project
 * Burton Rosenberg
 * May 2004
 */
 
 
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.event.* ;
import javax.swing.* ;

class TheController extends JPanel 
implements ActionListener,  ChangeListener, MouseListener {
        // button panel and controller for spacewars
        
        private static final String CHECKBOX_TITLE = "Stars" ;
        private static final String BUTTON_TITLE = "Thrust" ;
        private static final double CONVERT_ANGLE_FOLDOVER = 1.15 ; //small  (15%) overlap
 
        JSlider theSlider ;
        JButton theButton ;
        JCheckBox theCheckBox ;
        TheModel tm ;
                
        double b = - Math.PI * CONVERT_ANGLE_FOLDOVER ; 
        double a = - 2.0 * b / 100.0 ;
   
        TheController(TheModel tm) {
                this.tm = tm ;
                tm.setTheController(this) ;
                theButton = new JButton(BUTTON_TITLE) ;
                theSlider = new JSlider() ;               
                theCheckBox = new JCheckBox(CHECKBOX_TITLE) ;
                theButton.addActionListener(this) ;
                theButton.addMouseListener(this) ;
                theSlider.addChangeListener(this) ;
                theCheckBox.addActionListener(this) ;
                this.add( theCheckBox ) ;
                this.add( theSlider ) ;
                this.add( theButton ) ;  
        }
       
       // JButton listener
       public void actionPerformed( ActionEvent ae ) {
//System.out.println("TheController:" + ae ) ;               

            // making the check box took care of the
            // problem that the stars calculations need
            // to wait for the view to have dimensions.
            if ( ae.getSource()==theCheckBox )
                tm.setEnableStars( theCheckBox.isSelected() ) ;
       }

       // JSlider listener
       public void stateChanged( ChangeEvent ce ) {
//System.out.println("TheController:" + ce ) ;     
           double theta = a*((double) theSlider.getValue()) + b ;       
           tm.setTheta(theta) ;          
       }
       
       // to implement depressed button
       public void mouseClicked( MouseEvent me ) {
//System.out.println("TheController:" + me ) ;                        
       }
       
       public void mouseEntered( MouseEvent me ) {
//System.out.println("TheController:" + me ) ;                             
       }
       
       public void mouseExited( MouseEvent me ) {
//System.out.println("TheController:" + me ) ;                          
       }
       
       public void mousePressed( MouseEvent me ) {
//System.out.println("TheController:" + me ) ;    
             tm.setThrust( true ) ;                        
       }
       
       public void mouseReleased( MouseEvent me ) {
//System.out.println("TheController:" + me ) ;     
             tm.setThrust( false ) ;                       
       }
       
}
