/* $Id$
 *
 * Copyright (c) 1999 Xenonsoft Limited. All Rights Reserved.
 *
 * This software is protected by copyright. You are hereby notified from
 * now by reading this message. This software is also the confidential 
 * and proprietary information of Xenonsoft Limited.  ("Confidential
 * Information"). 
 *
 * This software is distributed under the Xenonsoft Public end user 
 * License ("XPeL"), where the machine-readable source code is provided 
 * under the "Open Source" model.
 * For more information, please read the file "LICENSE-XPL.txt" 
 */

// File:`UserKeyControlDialog.java'
//
//
// DESCRIPTION
//	 User Defined Key Control Dialog
// 
// Author
//
//	Peter Pilgrim
//	Thu Aug 05 19:38:34 BST 1999
// 
// RCS HEADER ``UserKeyControlDialog.java''
// 
//	$Author$
//	$Date$
//	$Revision$    $State$    $Locker$
//

package xenon.gamekit;

import java.awt.*;				// for Component, Panel
import java.awt.event.*;			// for KeyEvent, InputEvent
import java.io.Serializable;

import xenon.swing.*;				// for RowColumnLayout


/**
 * A default GUI control dialog that allows game key to be user defined.
 * 
 * @author Peter Pilgrim, Thu Aug 05 19:38:52 BST 1999
 * @version $Id$
 */
public class UserKeyControlDialog extends Dialog
implements ActionListener
{
    public final static int	CANCEL_BUTTON=0;
    public final static int	OK_BUTTON=1;
    
    /** the key change buttons */
    protected Button		bt_ok;
    protected Button		bt_cancel;
    protected Button		bt_reset;

    /** the user defined key custom controls */
    protected DefaultUserKeyControl keyControl;

    protected int		response=-1;
    protected UserKeyTuple []   userkeys;
    protected UserKeyTuple []   saved_userkeys;

    /**
     * constructs a GUI control dialog for user defined game keys
     * that does not allow duplicate user defined keys 
     *
     * @param frame the parent frame
     * @param userkeys the user defined key 
     */
    public UserKeyControlDialog( Frame frame, UserKeyTuple [] userkeys )
    {
	this( frame, "User Defined Key Control Dialog", userkeys );
    }

    /**
     * constructs a GUI control dialog for user defined game keys
     * that does not allow duplicate user defined keys 
     *
     * @param frame the parent frame
     * @param title the title string
     * @param userkeys the user defined key 
     */
    public UserKeyControlDialog( Frame frame, String title, 
				 UserKeyTuple [] userkeys )
    {
	this( frame, title, userkeys, false );
    }
    
    /**
     * constructs a GUI control dialog for user defined game keys
     *
     * @param frame the parent frame
     * @param title the title string
     * @param userkeys the user defined key 
     * @param duplicate if true then allow duplicate user defined keys
     */
    public UserKeyControlDialog( Frame frame, String title, 
				 UserKeyTuple [] userkeys, boolean duplicates )
    {
	super( frame, title, true /*modal*/ );

	setLayout( new BorderLayout( 2, 2 ) );
	setForeground( Color.white );
	setBackground( new Color( 96, 140, 255 ));

	this.userkeys = userkeys;
	keyControl = new DefaultUserKeyControl( userkeys );
	int N = userkeys.length;
	saved_userkeys = new UserKeyTuple[N];
	for ( int q=0; q<N; ++q) 
	    saved_userkeys[q] = (UserKeyTuple)userkeys[q].clone();
	add( keyControl.getControl(), BorderLayout.CENTER );


	Font buttonFont = new Font( "Monospaced", Font.BOLD, 12 );
	Panel panel = new Panel();
	panel.setLayout( new FlowLayout( FlowLayout.RIGHT ) );
	
	bt_reset = new Button( "Reset" );
	bt_reset.setFont( buttonFont );
	bt_reset.addActionListener( this );	
	panel.add( bt_reset );

	bt_cancel = new Button( "Cancel" );
	bt_cancel.setFont( buttonFont );
	bt_cancel.addActionListener( this );
	panel.add( bt_cancel );

	bt_ok = new Button( "Ok" );
	bt_ok.setFont( buttonFont );
	bt_ok.addActionListener( this );
	panel.add( bt_ok );
	
	add( panel, BorderLayout.SOUTH );

	pack();
    }

    /** returns the last response of the dialog */
    public int getResponse()
    {
	return (response);
    }
    
    /** 
     * show the user key control modal dialog
     * @returns the last response of the dialog 
     */
    public int showDialog()
    {
	setVisible( true );
	return (response);
    }

    public void setVisible( boolean visible )
    {
        if (visible) {
	    response = -1;
            // Center the dialog on the screen
            Dimension size = new Dimension(getSize());
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
	    if ( size.width < 350 )
		size.width = 350;
            int x = (screen.width-size.width)/2;
            int y = (screen.height-size.height)/2;
            setBounds(x, y, size.width, size.height);
        }

        super.setVisible(visible);
    }

    
    /** implementation defined */
    public void actionPerformed( ActionEvent evt ) 
    {
	Object src = evt.getSource();
	if ( src == bt_ok ) {
	    response = OK_BUTTON;
	    setVisible(false);
	}
	else if ( src == bt_cancel ) {
	    response = CANCEL_BUTTON;
	    for ( int q=0; q<userkeys.length; ++q) 
		userkeys[q].keyCode = saved_userkeys[q].keyCode;
	    setVisible(false);
	}
	else if ( src == bt_reset ) {
	    keyControl.resetToDefaultKeys();
	}
    }
    

    public static void main( String [] args )
    {
	UserKeyTuple keys [] = {
	    new UserKeyTuple( "Move Left",    KeyEvent.VK_A ), 
	    new UserKeyTuple( "Move Right",   KeyEvent.VK_S ), 
	    new UserKeyTuple( "Thrust Ship",  KeyEvent.VK_SHIFT ), 
	    new UserKeyTuple( "Fire Missile", KeyEvent.VK_ENTER ), 
	};
	
	Frame frame = new Frame("Test Frame");
	frame.setLayout( new BorderLayout() );
	UserKeyControlDialog keyDialog = 
	    new UserKeyControlDialog( frame, "Test User Key Control Dialog", keys );
	keyDialog.showDialog();
	System.exit(0);
    }
    
}

// fini
