001         package com.croftsoft.apps.mars.controller;
002    
003         import java.awt.*;
004         import java.awt.event.*;
005    
006         import com.croftsoft.core.lang.NullArgumentException;
007         import com.croftsoft.core.media.sound.AudioClipCache;
008    
009         import com.croftsoft.apps.mars.UserData;
010    
011         /*********************************************************************
012         * Toggles sound.
013         *
014         * @version
015         *   2003-04-04
016         * @since
017         *   2003-04-02
018         * @author
019         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
020         *********************************************************************/
021    
022         public final class  SoundController
023           extends KeyAdapter
024         //////////////////////////////////////////////////////////////////////
025         //////////////////////////////////////////////////////////////////////
026         {
027    
028         private final AudioClipCache  audioClipCache;
029    
030         private final UserData        userData;
031    
032         //////////////////////////////////////////////////////////////////////
033         //////////////////////////////////////////////////////////////////////
034    
035         public  SoundController (
036           AudioClipCache  audioClipCache,
037           UserData        userData,
038           Component       component )
039         //////////////////////////////////////////////////////////////////////
040         {
041           NullArgumentException.check (
042             this.audioClipCache = audioClipCache );
043    
044           NullArgumentException.check ( this.userData = userData );
045    
046           component.addKeyListener ( this );
047    
048           component.requestFocus ( );
049         }
050    
051         //////////////////////////////////////////////////////////////////////
052         // interface KeyListener methods
053         //////////////////////////////////////////////////////////////////////
054    
055         public void  keyPressed ( KeyEvent  keyEvent )
056         //////////////////////////////////////////////////////////////////////
057         {
058           if ( keyEvent.getKeyCode ( ) == KeyEvent.VK_S )
059           {
060             boolean  muted = !userData.isMuted ( );
061    
062             userData.setMuted ( muted );
063    
064             audioClipCache.setMuted ( muted );
065    
066             System.out.println ( "Sound " + ( muted ? "off" : "on" ) );
067           }
068         }
069    
070         //////////////////////////////////////////////////////////////////////
071         //////////////////////////////////////////////////////////////////////
072         }