Subversion Repositories spk

Rev

Blame | Last modification | View Log | RSS feed

//#include "stdafx.h"
#include "RenderText.h"

#include "../Hook/MyDirect3DDevice9.h"

CRenderText::CRenderText ( int size ) : CRenderObject ()
{
        m_iSize = size;
        m_font = NULL;
        m_iX = m_iY = 0;
        m_iWidth = 520;
        m_iHeight = size;
        m_iAlign = DT_LEFT;
        m_bBreakLine = false;

        m_iColour[0] = 0;
        m_iColour[1] = 0;
        m_iColour[2] = 255;
}

CRenderText::~CRenderText ()
{
        if ( m_font )
                m_font->Release ();
        m_font = NULL;
}

bool CRenderText::CheckMouse ( int x, int y, int offsetx, int offsety )
{
        int iX = offsetx + m_iX;
        int iY = offsety + m_iY;

        if ( x < iX )
                return false;
        if ( y < iY )
                return false;
        if ( x > (iX + m_iWidth) )
                return false;
        if ( y > (iY + m_iHeight) )
                return false;

        return true;
}


void CRenderText::ParseSetting ( COverlay *overlay, CyString cmd, CyString rest, CyString section )
{
        cmd.ToUpper();
        if ( section == "ANIMATE" )
        {
                if ( cmd == "TEXT" )
                {
                        SAnimateText *text = new SAnimateText;
                        text->iStartTime = ConvertAnimationTime ( rest.GetToken ( ",", 1, 1 ) );
                        text->sText = rest.GetToken ( ",", 2 );
                        m_lAnimations.push_back ( text );
                }
                else if ( cmd == "COLOUR" )
                {
                        SAnimateColour *c = new SAnimateColour;
                        c->iStartTime = ConvertAnimationTime ( rest.GetToken ( ",", 1, 1 ) );
                        c->iR = rest.GetToken ( ",", 2, 2 ).ConvertEquation();
                        c->iG = rest.GetToken ( ",", 3, 3 ).ConvertEquation();
                        c->iB = rest.GetToken ( ",", 4, 4 ).ConvertEquation();
                        m_lAnimations.push_back ( c );
                }
                else
                        CRenderObject::ParseSetting ( overlay, cmd, rest, section );
        }
        else if ( (section == "MOUSEOVER") || (section == "ONCLICK") )
        {
                if ( cmd == "APPEND" )
                {
                        SMOTextAppend *c = new SMOTextAppend;
                        c->sText = rest;
                        AddMouseEvent ( section, c );
                }
                else if ( cmd == "PREPEND" )
                {
                        SMOTextPrepend *c = new SMOTextPrepend;
                        c->sText = rest;
                        AddMouseEvent ( section, c );
                }
                else
                        CRenderObject::ParseSetting ( overlay, cmd, rest, section );
        }
        else if ( section.Empty() )
        {
                if ( cmd == "LENGTH" )
                        m_iWidth = rest.ConvertEquation();
                else if ( cmd == "FONT" )
                        InitFont ( m_pDevice, TEXT(rest.c_str()), m_iSize );
                else if ( cmd == "ALIGN" )
                {
                        rest.ToUpper();
                        if ( rest == "CENTER" )
                                m_iAlign = DT_CENTER;
                        else if ( rest == "LEFT" )
                                m_iAlign = DT_LEFT;
                        else if ( rest == "RIGHT" )
                                m_iAlign = DT_RIGHT;
                }
                else if ( (cmd == "SIZE") && (m_iSize == -1) )
                        m_iSize = rest.ConvertEquation();
                else if ( cmd == "LINES" )
                {
                        int l = rest.ConvertEquation();
                        if ( l < 1 )
                                l = 1;
                        m_iHeight = m_iSize * l;
                        if ( l > 1 )
                                m_bBreakLine = true;
                }
                else if ( cmd == "COLOUR" )
                {
                        m_iColour[0] = rest.GetToken ( ",", 1, 1 ).ConvertEquation();
                        m_iColour[1] = rest.GetToken ( ",", 2, 2 ).ConvertEquation();
                        m_iColour[2] = rest.GetToken ( ",", 3, 3 ).ConvertEquation();
                }
                else if ( cmd == "TEXT" )
                        m_sText = rest;
                else
                        CRenderObject::ParseSetting ( overlay, cmd, rest, section );
        }
        else
                CRenderObject::ParseSetting ( overlay, cmd, rest, section );
}

void CRenderText::render ( MyDirect3DDevice9 *device, int x, int y, LPD3DXSPRITE sprite )
{
        // set size of text
        RECT rct;
        rct.left = m_iX + x;
        rct.right = rct.left + m_iWidth;
        rct.top = m_iY + y;
        rct.bottom = rct.top + m_iHeight;

        // set the colour for the text
        short *c = m_iColour;
        CyString text = m_sText;

        CLinkList<SMouseOver> *mouseList = NULL;

        for ( int i = 0; i < 2; i++ )
        {
                mouseList = NULL;
                if ( (i == 1) && (m_bClicked) )
                        mouseList = &m_lMouseClick;
                else if ( (i == 0) && (m_bMouseOver) )
                        mouseList = &m_lMouseOver;

                if ( mouseList )
                {
                        for ( SMouseOver *mo = mouseList->First(); mo; mo = mouseList->Next() )
                        {
                                if ( mo->iType == MO_COLOUR )
                                        c = ((SMOColour *)mo)->iColour;
                                else if ( mo->iType == MO_TEXTAPPEND )
                                        text += ((SMOTextAppend *)mo)->sText;
                                else if ( mo->iType == MO_TEXTPREPEND )
                                        text = ((SMOTextPrepend *)mo)->sText + text;
                        }
                }
        }

        if ( text.IsIn("$") )
                text = ParseVaribles ( text, &m_lVaribles );

        D3DCOLOR fontColor = D3DCOLOR_ARGB ( m_iAlpha, c[0], c[1], c[2] );

        // set the alignment
        int align = m_iAlign;
        if ( m_bBreakLine )
                align += DT_WORDBREAK;

        // set the transformation position
        D3DXMATRIX mat;
        D3DXMatrixIdentity ( &mat );
        sprite->SetTransform ( &mat );

        // draw the text
        m_font->DrawText( sprite, text.c_str(), -1, &rct, align, fontColor );   
}

void CRenderText::InitFont ( MyDirect3DDevice9 *device, LPCTSTR fontStr, int size )
{
        D3DXCreateFont( device, size, 0, FW_BOLD, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, fontStr, &m_font );
        m_iHeight = size;
}


void CRenderText::StartAnimation ( SAnimate *anim )
{
        if ( anim->iType == ANIM_CHANGETEXT )
        {
                SAnimateText *text = (SAnimateText *)anim;
                m_sText = text->sText;
                EndAnimation ( anim );
        }
        else if ( anim->iType == ANIM_CHANGECOLOUR )
        {
                SAnimateColour *colour = (SAnimateColour *)anim;
                m_iColour[0] = colour->iR;
                m_iColour[1] = colour->iG;
                m_iColour[2] = colour->iB;
                EndAnimation ( anim );
        }
        else
                CRenderObject::StartAnimation ( anim );
}

/*
###############################################################
########                Render Text Header            #########
###############################################################
*/

CRenderTextHeader::CRenderTextHeader ( MyDirect3DDevice9 *device, LPCTSTR fontStr, CyString header ) : CRenderText ()
{
        InitFont ( device, fontStr, 50 );
        m_iAlign = DT_CENTER;
        m_sText = header;
}

CRenderTextNormal::CRenderTextNormal ( MyDirect3DDevice9 *device, LPCTSTR fontStr, CyString header ) : CRenderText ()
{
        InitFont ( device, fontStr, 20 );
        m_iAlign = DT_CENTER | DT_WORDBREAK;
        m_sText = header;
}