/***********************************************************************

GECCO, a platform for command and control gaming

Copyright (C) 2001 Henrik Bäärnhielm, Andreas Enblom, Niklas
Hallenfur, Karl Hasselström, Henrik Hägerström, Oskar Linde, Jing Fu
Zi and Jon Åslund

This program 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 2 of the License, or (at
your option) any later version.

This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.

***********************************************************************/

package gecco.client;

import java.awt.*;
import java.awt.image.*;
import java.util.*;

/**
 *  This class contains the symbols (Images) of the game. Every piece type
 *  should have a symbol. For every symbol added to the collection, a
 *  semi-transparent variant of the same symbol is created, used for ghost
 *  pieces.
 *
 * @author     Andreas Enblom
 * @created    June 27, 2001
 */
public class Symbols {
    /**
     *  The symbols, hashed on type.
     */
    static Hashtable pieceSymbols = new Hashtable();
    /**
     *  The ghost symbols, hashed on type.
     */
    static Hashtable ghostSymbols = new Hashtable();


    /**
     *  Returns the symbol (Image) of the given type, or NULL if there is no
     *  symbol for that type.
     *
     * @param  type  Description of Parameter
     * @return       The PieceSymbol value
     */
    public static Image getPieceSymbol(String type) {
        return (Image) pieceSymbols.get(type);
    }


    /**
     *  Gets the AllPieceTypes attribute of the Symbols class
     *
     * @return    The AllPieceTypes value
     */
    public static Enumeration getAllPieceTypes() {
        return pieceSymbols.keys();
    }


    /**
     *  Return the ghost symbol of the given type, or NULL if there is no symbol
     *  for that type.
     *
     * @param  type  Description of Parameter
     * @return       The GhostSymbol value
     */
    public static Image getGhostSymbol(String type) {
        return (Image) ghostSymbols.get(type);
    }


    /**
     *  Adds a symbol for the given piece type. A semi-transparent (ghost)
     *  version of the image is produced.
     *
     * @param  type   The piece type.
     * @param  image  The symbol of the piece type.
     */
    public static void addPieceSymbol(String type, Image image) {
        pieceSymbols.put(type, image);

        // create semi-transparent symbol
        int width = -1;
        int height = -1;
        // XXX wait for the image to load completely
        while ((width == -1) || (height == -1)) {
            width = image.getWidth(null);
            height = image.getHeight(null);
        }
        BufferedImage ghost = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        ghost.getGraphics().drawImage(image, 0, 0, width, height, null);
        for (int x = 0; x < width; ++x) {
            for (int y = 0; y < height; ++y) {
                Color oldColor = new Color(ghost.getRGB(x, y), true);
                Color newColor = new Color(oldColor.getRed(),
                        oldColor.getGreen(),
                        oldColor.getBlue(),
                        oldColor.getAlpha() / 2);
                ghost.setRGB(x, y, newColor.getRGB());
            }
        }
        ghostSymbols.put(type, ghost);
    }


    /**
     *  Removes the symbol assigned to the given piece type (if any)..
     *
     * @param  type  Description of Parameter
     */
    public static void removePieceSymbol(String type) {
        pieceSymbols.remove(type);
        ghostSymbols.remove(type);
    }
}

