/*
 * Author: Andrew Robberts
 *
 * Copyright (C) 2003 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
 *
 *
 * 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 (version 2 of the License)
 * 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.
 *
 */

#ifndef PS_EFFECT_OBJ_TEXT_HEADER
#define PS_EFFECT_OBJ_TEXT_HEADER

#include "pseffectobj.h"
#include <imesh/sprite2d.h>

struct iGraphics3D;
struct iGraphics2D;
struct iTextureManager;
struct iTextureWrapper;
struct iFont;
struct iParticle;

class psEffectObjText : public psEffectObj
{
public:
    psEffectObjText(iView *parentView);
    ~psEffectObjText();

    /** Creates a texture with the given text.
     *   \param newText The text to write to the texture.
     *   \return        True on success, false otherwise.
     */
    bool SetText(const char * newText);

    /** Creates a texture with the given text.
     *   \param newText The text to write to the texture.
     *   \return        True on success, false otherwise.
     */
    bool SetAttributedText(const char * newText) { return true; }

    /** Creates a texture with the given preformated text.
     *   \param rows The number of rows
     *   \param ...  Rows rows and rows, a row is a pointer to an EffectTextRow object
     *   \return        True on success, false otherwise.
     */
    bool SetText(int rows, ...);


    /** Checks if this effect obj has static text.
     *   \return    True if the text of this obj is static and can't be changed.
     */
    bool IsStaticText() const;
    
    // inheritted function overloads
    bool Load(iDocumentNode * node);
    bool Render(const csVector3 & up);
    bool Update(csTicks elapsed);
    psEffectObj * Clone() const;


    // Preformated objects
    enum EffectTextAlignment
    {
        ETA_LEFT,
        ETA_CENTER,
        ETA_RIGHT,

        ETA_COUNT
    };

    struct EffectTextRow
    {
        EffectTextRow(const char* text = "", int colour = 0, int align = 0 , bool shadow = false, bool outline = true, int outlinecolor = 0, int shadowcolor = 0)
        {
            this->text = text;
            this->colour = colour;
            this->align = align;
            this->hasShadow = shadow;
            this->hasOutline = outline;
            this->shadowColour = shadowcolor;
            this->outlineColour = outlinecolor;
        }

        csString text;
        int colour;
        bool hasShadow;
        int shadowColour;
        bool hasOutline;
        int outlineColour;

        int align;
    };

private:

    inline int HexToInt(char hex)
    {
        if (hex >= '0' && hex <= '9')
            return (hex - '0');
        if (hex >= 'A' && hex <= 'F')
            return (hex - 'A' + 10);
        if (hex >= 'a' && hex <= 'f')
            return (hex - 'a' + 10);
        return 0;
    }

    inline int ToPowerOf2(int n)
    {
        for (int a=sizeof(int) * 8-1; a>0; --a)
        {
            if (n & (1 << a))
            {
                if (n == (1 << a))
                    return n;
                else
                    return (1 << (a+1));
            }
        }
        return 1;
    };

    struct EffectTextElement : public EffectTextRow
    {
        EffectTextElement() : EffectTextRow(NULL,0,0)
        {

        }
        int width, height;
        int x, y;
    };

    /** Since text effect objs need their own material, they need their own mesh factory as well.
     *   \return    True on success, false otherwise.
     */
    bool CreateMesh();

    /** Sets the text of the obj, but doesn't check to see if it can be changed or not.
     *   \param newText     The new text of the obj.
     *   \return            True on success.
     */
    bool UnprotectedSetText(const char * newText);

    /** Draws array of text elements to the label's material
      *   \param textElems   An array of text chunks with formatting
      *   \return            True on success
      */
    bool RenderText(const csArray<psEffectObjText::EffectTextElement>& textElems);

    bool FormatedSetText(EffectTextRow* row);
    bool ApplyRow(const EffectTextElement* row);
    bool PrepareText();

    /** Calculates new texture coordinates.
     */
    void CalcTextureCoords();

    /** performs the post setup (after the effect obj has been loaded).
     *  Things like create mesh factory, etc are initialized here.
     */
    bool PostSetup();

    csString fontName;
    int fontSize;
    csRef<iFont> font;

    csRef<iGraphics3D> g3d;
    iGraphics2D * g2d;
    csRef<iTextureManager> txtmgr;
    csRef<iTextureWrapper> tex;
    csRef<iMaterialWrapper> mat;
    int texWidth;
    int texHeight;
    int paddingWidth;
    int paddingHeight;

    csRef<iSprite2DState> sprite; 
    csRef<iParticle> particle;
    
    bool staticText;
    bool antiAlias;

    /** True if multi-lined text should grow upwards.
     *  (used for entitylabels, so multi-lined labels do not grow into the entity)
     */
    bool growUpwards;
    /** If growUpwards is true, this contains the offset the text is moved up relativ to the entity.
     *  For a one-line text this is 0, after that the height of each additional line is added.
     */
    float yOffset;

    csColor textColour;

    float lastScale;
    float lastSpin;

    csString text;
};

#endif
