/*****************************************************************************
 * $LastChangedDate: 2011-04-09 21:58:06 -0400 (Sat, 09 Apr 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   HUD text objects.
 *//*
 * LEGAL:   COPYRIGHT (C) 2007 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#ifndef HUD_HUD_TEXT_HH
#define HUD_HUD_TEXT_HH 1

#include "base/array_tiny.hh"
using namespace base;
#include "math/module.hh"
#include "math/types_trig.hh"
using namespace math;
#include "gfx/rgba.hh"
using namespace gfx;
namespace view { class View; }
using namespace view;
#include "hud/font.hh"
#include <osgText/Text>

namespace hud {

////////////////////////////////////////////////////////////////////////////
/// @brief Defines a string of text to be displayed in HUD.
///
/// HudText has attributes such as 2D position, orientation, font name/size,
/// color, color gradients, and 3D rotation for animation.
///
class HudText : public HudObject
{
PREVENT_COPYING(HudText)
typedef HudObject Parent;
friend class Hud;
public:
    /// Text orientation.
    enum eOrientation
    {
        eOrientation_BOTTOM_LEFT,   // default
        eOrientation_BOTTOM_RIGHT,  // same order as OSG setColorGradientCorners()
        eOrientation_TOP_LEFT,
        eOrientation_TOP_RIGHT,
        eOrientation_CENTER,
    };

    /// For setting RGBA array passed to SetGradientColors().
    enum eGradientColor
    {
        eGradientColor_TOP_LEFT,
        eGradientColor_BOTTOM_LEFT,
        eGradientColor_BOTTOM_RIGHT,
        eGradientColor_TOP_RIGHT,
    };

    /// @brief Specify which kind of text animation.
    enum eAnimation
    {
        eAnimation_NONE,
        eAnimation_FLIP,     ///< gradually rotate text from flat to standing up
    };

    /// @brief Args for HudText() ctor.
    struct Args
    {
        Args( void );
        string          mText;
        Vector2         mPos;
        Font            mFont;
        eOrientation    mOrientation;
        RGBA            mColor;
        Vector2         mOffset;
        eAnimation      mAnimation;
    };
                    HudText( const Args& args );
                    ~HudText();
    void            SetText( const string& text );
    void            SetText( const char text[] );
    void            SetPosition3D( const WorldVertex& pos3 );
    void            SetFontSize( const fp fontSize );
    void            SetColor( const RGBA rgba );
    void            SetGradientColors4( const TinyArray<4,RGBA>& colors4 );
    RefPtr<Node>    GetNode( void ) { return mGeode.get(); }
    void            Rotate( const uint axis, const Radian rad );

private:
    Vector2         OrientPosition( const Args& args );
    virtual void    Update( View& view );
    void            UpdatePosition3D( View& view, const Milliseconds elapsedMilliseconds );
    void            UpdateAnimation( const Milliseconds elapsedMilliseconds );

private:
    struct Animation
    {
        Animation( const HudText::Args& args );
        const eAnimation    mAnimation;
        const Milliseconds  mStartMilliseconds;
        Radian              mFlipRotation;
    };

private:
    /// @brief Optional definition of 2D box in 3D terms.
    /// Becomes valid after SetPosition3D() is called.
    struct Definition3D
    {
        Definition3D( const Vector2& offset2 )
        : mValid(false), mPos3(0,0,0), mOffset2(offset2), mLastTime(0) { }
        bool            mValid;         ///< if text object has a 3D position
        WorldVertex     mPos3;          ///< 3D position
        const Vector2   mOffset2;       ///< to adjust font
        Milliseconds    mLastTime;      ///< last time position was recomputed
    };

private:
    RefPtr<osg::Geode>      mGeode;     ///< geometry node
    RefPtr<osgText::Text>   mOsgText;   ///< drawable node
    Definition3D            m3D;        ///< 3D position enabled by SetPosition3D()
    Animation               mAnimation;
public:
    DECLARE_TYPESIG(TYPESIG_HUD_TEXT)
};

} // namespace hud

#endif // HUD_HUD_TEXT_HH
