/*
 *  Copyright 2002-2009 Mikhail Kryshen <mikhail@kryshen.net>
 *
 *  This file is part of BMTron.
 *
 *  BMTron is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  BMTron is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with BMTron.  If not, see <http://www.gnu.org/licenses/>.
 */
 
package kryshen.bmtron;

import java.awt.AWTEventMulticaster;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Button extends Component implements MouseListener {
    private final Game bmt;
    private Image image;
    private boolean active = false;
    private boolean pressed = false;

    private ActionListener actionListener = null;

    Button(Image img, Game bmt) {
        this.bmt = bmt;
        image = img;
        setSize(getPreferredSize());

	setBackground(bmt.skin.buttonBackColor);
	setForeground(bmt.skin.buttonTextColor);
	setFont(bmt.skin.buttonFont);

        addMouseListener(this);       
    }
    
    public void setImage(Image img) {
        image = img;
        repaint();
    }
    
    public void paint(Graphics g) {
        int offset;
        if (pressed) offset = 1; 
	else offset = 0;
    
        g.drawImage(image, offset + 1, offset + 1, this);
        
        if (pressed) {
            g.setColor(Color.black);
            g.drawRect(offset, offset, getSize().width - 1, 
		       getSize().height - 1);
        }

        if (active) 
	    g.setColor(bmt.skin.activeButtonBorderColor); 
	else 
	    g.setColor(bmt.skin.buttonBorderColor);

        g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
    }
    
    public void mouseEntered(MouseEvent e) { active = true; repaint(); }
    public void mouseExited(MouseEvent e) { active = false; repaint(); }
    
    public void mousePressed(MouseEvent e) { pressed = true; repaint(); }    
    public void mouseReleased(MouseEvent e) { pressed = false; repaint(); }
    
    public void mouseClicked(MouseEvent e) {
            if (actionListener != null) {
            actionListener.actionPerformed(new ActionEvent(this, 0, null));
        }
    }

    public synchronized void addActionListener(ActionListener l) {
        actionListener = AWTEventMulticaster.add(actionListener, l);
    }
    
    public synchronized void removeActionListener(ActionListener l) {
        actionListener = AWTEventMulticaster.remove(actionListener, l);
    }
    
    public Dimension getPreferredSize() {
        return new Dimension(image.getWidth(this) + 2, 
			     image.getHeight(this) + 2);
    }
}
