/* @(#)GameTile.java   9 November 2006 */

/* Useful imports */

import edu.neu.ccs.*;
import edu.neu.ccs.gui.*;
import edu.neu.ccs.codec.*;
import edu.neu.ccs.console.*;
import edu.neu.ccs.filter.*;
import edu.neu.ccs.jpf.*;
import edu.neu.ccs.parser.*;
import edu.neu.ccs.pedagogy.*;
import edu.neu.ccs.quick.*;
import edu.neu.ccs.util.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;
import java.math.*;
import java.beans.*;
import java.lang.reflect.*;
import java.net.URL;
import java.util.regex.*;
import java.text.ParseException;


/**
 * <p>Class <code>GameTile</code> is a
 * <code>PaintableComponent</code> that is designed
 * to easily control whether the internal paintable
 * is painted and whether the object should respond
 * to mouse actions.</p>
 */
public class GameTile
    extends PaintableComponent
{
    /** Whether or not the tile should respond to mouse actions. */
    private boolean active = true;
    
    /** The inner paintable within the game time. */
    private Paintable inner = null;
    
    /** The tile that encapsulates the inner paintable. */
    private Tile tile = null;
    
    
    /**
     * <p>Constructs a game tile using the given paintable
     * which should be non-null,
     * the given tile background,
     * the given tile width and height,
     * the initial painted setting for the paintable,
     * and the initial mouse activity setting.</p>
     * 
     * <p>The default bounds of the given paintable will
     * be set to the rectangle (0,0,width,height).</p>
     * 
     * <p>The game tile will be opaque so that prior to
     * painting the paintable the background color will
     * be painted.</p>
     */
    public GameTile(
        Paintable paintable,
        Color background,
        int width,
        int height,
        boolean painted,
        boolean active)
    {
        super(new Tile(paintable), background, true);
        
        inner = paintable;
        tile  = (Tile) getPaintable();
        
        XRect bounds = new XRect(0, 0, width, height);
        tile.setDefaultBounds2D(bounds);
        
        setPainted(painted);
        setActive(active);
    }
    
    
    /**
     * <p>Returns whether the tile is painted
     * when the component is repainted.</p>
     */
    public boolean isPainted() {
        return tile.isVisible();
    }
    
    
    /**
     * <p>Sets whether the tile is painted
     * when the component is repainted.</p>
     */
    public void setPainted(boolean painted) {
        boolean current = isPainted();
        
        if (painted != current) {
            tile.setVisible(painted);
            repaint();
        }
    }
    
    
    /**
     * <p>Returns whether or not the tile should respond
     * to mouse actions.</p>
     * 
     * <p>It is up to the mouse actions to use this call
     * to determine whether or not to be active.</p>
     */
    public boolean isActive() { return active; }
    
    
    /**
     * <p>Sets whether or not the tile should respond
     * to mouse actions.</p>
     */
    public void setActive(boolean active) { this.active = active; }
    
    
    /**
     * Returns the inner Paintable
     * encapsulated in a Tile
     * within this GameTile.
     */
    public Paintable getInnerPaintable() {
        return inner;
    }
    
    
    /**
     * Tests the equality of two game tiles by testing
     * if their inner paintables are identical.
     */
    public boolean isEqualTo(GameTile gametile) {
        if (gametile == null)
            return false;
        
        return getInnerPaintable() == gametile.getInnerPaintable();
    }
    
}

