Subversion Repositories spk

Rev

Blame | Last modification | View Log | RSS feed

//#include "stdafx.h"
#include "DisplayCustom.h"
#include "RenderText.h"
#include "RenderTexture.h"
#include "RenderSprite.h"
//#include "RenderVideo.h"


CDisplayCustom::CDisplayCustom ( COverlay *overlay, MyDirect3DDevice9 *device, CyString filename, CyString name, int width, int height, CyStringList *varibles ) : CDisplay ( overlay, device, name )
{
        m_iMaxAlpha = 255;
        m_iFadeIn = 0;

        SStringList *curVari = varibles->Head();

        FILE *id;

        int sizeX = -1, sizeY = -1;
        
        if ( !fopen_s ( &id, filename.c_str(), "r" ) )
        {
                CyString section;
                while ( !feof ( id ) )
                {
                        CyString line;
                        line.GetEndOfLine ( id, NULL, false );

                        if ( line.Empty() )
                                continue;
                        if ( line.Left(2) == "//" )
                                continue;

                        line = line.GetToken ( "//", 1, 1 );

                        // first, find if its a block
                        if ( line.IsIn ( "{" ) )
                        {
                                // parse the block
                                CyStringList strings;

                                CyString cmd = ParseBlock ( id, &line, &strings );

                                // parse strings for varibles
                                for ( SStringList *it = strings.Head(); it; it = it->next )
                                {
                                        if ( !it->str.IsIn ( "$" ) )
                                                continue;
                                        it->str = ParseVaribles ( it->str, varibles );
                                }
                                
                                CRenderObject *ro = NULL;
                                if ( cmd.Left(5) == "FUNC_" )
                                {
                                        SFunction *func = new SFunction;
                                        func->sFunction = cmd.upper();
                                        func->sFunction.Erase ( 0, 5 );
                                        func->sBlock = line;
                                        func->lStrings = strings;

                                        // now get the varibles passed
                                        if ( func->sFunction.IsIn ( "(" ) )
                                        {
                                                CyString vari = func->sFunction.GetToken ( "(", 2 ).GetToken ( ")", 1, 1 );
                                                func->sFunction = func->sFunction.GetToken ( "(", 1, 1 );

                                                int num = 0;
                                                vari.RemoveChar ( ' ' );
                                                CyString *varis = vari.SplitToken ( ',', &num );
                                                for ( int i = 0; i < num; i++ )
                                                        func->lVaribles.PushBack ( CyString("$") + varis[i] + "$" );
                                        }

                                        m_lFunctions.push_back ( func );
                                }
                                else if ( cmd == "TEXTSMALL" )
                                        ro = (CRenderObject *)new CRenderText ( 20 );
                                else if ( cmd == "TEXTMEDIUM" )
                                        ro = (CRenderObject *)new CRenderText ( 35 );
                                else if ( cmd == "TEXTLARGE" )
                                        ro = (CRenderObject *)new CRenderText ( 50 );
                                else if ( cmd == "TEXTHUGE" )
                                        ro = (CRenderObject *)new CRenderText ( 80 );
                                else if ( cmd == "TEXTCUSTOM" )
                                        ro = (CRenderObject *)new CRenderText ( -1 );
                                else if ( cmd == "TEXTURE" )
                                        ro = (CRenderObject *)new CRenderTexture ( device );
//                              else if ( cmd == "VIDEO" )
//                                      ro = (CRenderObject *)new CRenderVideo ();
                                else if ( cmd == "SPRITE" )
                                {
                                        ro = (CRenderObject *)new CRenderSprite ( device );
                                        if ( !((CRenderSprite *)ro)->Init() )
                                                ro = NULL;
                                }
                                else if ( (cmd == "BORDER") || (cmd == "INPUT") || (cmd == "CLOSEON") )
                                {
                                        SCloseOn *close = NULL;
                                        if ( cmd == "BORDER" )
                                                m_bNoBorder = false;
                                        else if ( cmd == "CLOSEON" )
                                        {                               
                                                close = new SCloseOn;
                                                m_lClose.push_back ( close );
                                        }

                                        int num = 0;
                                        CyString *str = line.SplitToken ( ';', &num );
                                        for ( int i = 0; i < num; i++ )
                                        {
                                                CyString c = str[i].GetToken ( ":", 1, 1 );
                                                CyString r = str[i].GetToken ( ":", 2 );
                                                if ( r.IsIn("\"") )
                                                        r = ReplaceString ( r, &strings );
                                                r = ParseVaribles (r, varibles);
                                                if ( cmd == "BORDER" )
                                                        ParseBorder ( c, r );
                                                else if ( cmd == "INPUT" )
                                                        ParseInput ( c, r );
                                                else if ( cmd == "CLOSEON" )
                                                        ParseCloseEvent ( close, c, r );
                                        }
                                }

                                if ( ro )
                                {
                                        ro->SetDevice ( device );

                                        DoBlock ( ro, line, &strings, varibles, section );

                                        AddObject ( ro );

                                        ro->FinishedLoad();
                                }
                        }
                        else
                        {
                                CyString cmd = line.GetToken ( 1, ' ' ).ToUpper();
                                CyString rest = ParseVaribles ( line.GetToken ( 2, -1, ' ' ), varibles );

                                if ( (cmd == "VARIABLE:") || (cmd == "VARIBLE:") )
                                {
                                        if ( curVari )
                                        {
                                                curVari->data = CyString("$") + rest + "$";
                                                curVari = curVari->next;
                                        }
                                }
                                else if ( cmd == "SOUND:" )
                                        CreateSound ( rest.GetToken ( ",", 2, 2 ), ParseNumber ( rest.GetToken ( ",", 1, 1 ) ) );
                                else if ( line.lower() == "noborder" )
                                        m_bNoBorder = true;
                                else if ( cmd == "DISPLAYFOR:" )
                                {
                                        m_lTimeout = ParseNumber ( rest );
                                        if ( m_lTimeout != -1 )
                                                m_lTimeout += timeGetTime();
                                }
                                else if ( cmd == "TEXTURE:" )
                                {
                                        CRenderTexture *t = new CRenderTexture ( device );
                                        t->OpenTextureFile ( rest.GetToken ( 3, ',' ).RemoveFirstSpace() );
                                        t->SetPosition ( ParseNumber ( rest.GetToken ( 1, ',' ) ), ParseNumber ( rest.GetToken ( 2, ',' ) ) );
                                        AddObject ( t );
                                }
                                else if ( cmd == "SIZE:" )
                                {
                                        if ( rest.lower() == "full" )
                                        {
                                                sizeX = width;
                                                sizeY = height;
                                        }
                                        else
                                        {
                                                sizeX = ParseNumber ( rest.GetToken ( 1, ',' ) );
                                                sizeY = ParseNumber ( rest.GetToken ( 2, ',' ) );
                                        }

                                        varibles->PushBack ( CyString::Number(sizeX), CyString("$GUI.SIZEX$") );
                                        varibles->PushBack ( CyString::Number(sizeY), CyString("$GUI.SIZEY$") );
                                }
                        }
                }

                fclose ( id );
        }

        if ( (sizeX != -1) && (sizeY != -1) )
                SetSize ( sizeX, sizeY, width, height );
        else
                SetSize ( 520, 300, width, height );

        if ( !m_iFadeIn )
                m_iAlpha = m_iMaxAlpha;
        else
                m_iAlpha = 0;
}

CyString CDisplayCustom::ParseBlock ( FILE *id, CyString *str, CyStringList *strings )
{
        strings->Clear();

        CyString rest;
        CyString line = *str;
        int depth = -1;
        while ( !feof(id) )
        {
                if ( (line.Left(2) == "//") || (line.Empty()) )
                {
                        line.GetEndOfLine ( id, NULL, false );
                        continue;
                }

                line = line.GetToken ( "//", 1, 1 );
                line.RemoveChar ( 9 );

                if ( line.IsIn ( "{" ) )
                        ++depth;
                if ( line.IsIn ("}") )
                {
                        if ( !depth )
                        {
                                rest += line.GetToken ( "}", 1, 1 );
                                break;
                        }
                        else
                        {
                                rest += line.GetToken ( "}", 1, 1 );
                                rest += "};";
                                line = line.GetToken ( "}", 2 );
                                --depth;

                                if ( !line.Empty() )
                                {
                                        line.GetEndOfLine ( id, NULL, false );
                                        continue;
                                }
                        }
                }

                rest += line;
                line.GetEndOfLine ( id, NULL, false );
        }

        CyString cmd = rest.GetToken ( "{", 1, 1 ).RemoveSpaces();
        rest = rest.GetToken ( "{", 2 );

        // parse strings
        CyString done;
        while ( rest.IsIn("\"") )
        {
                done += rest.GetToken ( "\"", 1, 1 );
                CyString s = rest.GetToken ( "\"", 2, 2 );

                if ( s.Empty() )
                        break;

                strings->PushBack ( s );
                done += "\"";
                done += "$STRING";
                done += (long)strings->Count();
                done += "\"";

                rest = rest.GetToken ( "\"", 3 );
        }

        if ( !rest.Empty() )
                done += rest;
        done.RemoveChar ( ' ' );

        *str = done;
        return cmd;
}

CyString CDisplayCustom::ReplaceString ( CyString s, CyStringList *strings )
{
        int num = 1;
        for ( SStringList *it = strings->Head(); it; it = it->next, num++ )
        {
                CyString str = CyString("\"$STRING") + (long)num + "\"";
                CyString newstr = it->str;
                newstr = newstr.FindReplace ( ",", "$COMMA$" );
                s = s.FindReplace ( str, newstr );
        }


        return s;
}

void CDisplayCustom::ParseBorder ( CyString cmd, CyString rest )
{
        cmd.ToUpper();
        if ( cmd == "TEXTURE" )
                SetBorderTexture ( rest );
        else if ( cmd == "ALPHA" )
                m_iMaxAlpha = ParseNumber ( rest );
        else if ( cmd == "FADEIN" )
                m_iFadeIn = ParseNumber ( rest );
        else if ( cmd == "RENDERAFTER" )
                m_iFadeState = FADE_AFTER;
        else if ( cmd == "FADEALLOBJECTS" )
                m_iFadeState = FADE_ALL;
        else if ( cmd == "SIZE" )
                m_iBorderSizeX = m_iBorderSizeY = ParseNumber ( rest );
}

void CDisplayCustom::ParseCloseEvent ( SCloseOn *close, CyString cmd, CyString rest )
{
        cmd.ToUpper();
        if ( cmd == "START" )
                close->iStart = rest.ConvertEquation();
        else if ( cmd == "END" )
                close->iEnd = rest.ConvertEquation();
        else if ( cmd == "LEFTCLICK" )
                close->iMouseCommands |= COMMAND_LEFTCLICK;
        else if ( cmd == "RIGHTCLICK" )
                close->iMouseCommands |= COMMAND_RIGHTCLICK;
        else if ( cmd == "LEFTCLICKIN" )
                close->iMouseCommands |= COMMAND_LEFTCLICKIN;
        else if ( cmd == "RIGHTCLICKIN" )
                close->iMouseCommands |= COMMAND_RIGHTCLICKIN;
        else if ( cmd == "KEY_ENTER" )
                close->iKeyCommands |= COMMAND_KEY_ENTER;
        else if ( cmd == "KEY_SPACE" )
                close->iKeyCommands |= COMMAND_KEY_SPACE;
}

void CDisplayCustom::ParseInput ( CyString cmd, CyString rest )
{
        cmd.ToUpper();
        if ( cmd == "MOUSECURSOR" )
                SetCursorTexture ( rest );
        else if ( cmd == "HALTMOUSE" )
                m_bHaltMouse = true;
        else if ( cmd == "CONTAINMOUSE" )
                m_bContainMouse = true;
}


void CDisplayCustom::DoBlock ( CRenderObject *ro, CyString line, CyStringList *strings, CyStringList *varibles, CyString &section )
{
        int num = 0;
        CyString *str = line.SplitToken ( ';', &num );

        for ( int i = 0; i < num; i++ )
        {
                CyString c = str[i].GetToken ( ":", 1, 1 ), r;
                if ( c.Empty() )
                        continue;

                if ( c.IsIn ("{") )
                {
                        if ( c.IsIn ("}") )
                        {
                                r = CyString("{") + c.GetToken ( "{", 2 ).GetToken ( "}", 1, 1 ) + "}";
                                c = c.GetToken ( "{", 1, 1 );
                        }
                        else
                        {
                                if ( section.Empty() )
                                        section = c.GetToken ( "{", 1, 1 );
                                else
                                        section += CyString(":") + c.GetToken ( "{", 1, 1 );

                                c = c.GetToken ( "{", 2 );
                                r = str[i].GetToken ( ":", 2 );
                        }
                }
                else if ( c.IsIn ( "}" ) )
                {
                        if ( section.IsIn (":") )
                                section = section.GetToken ( ":", 1, section.NumToken(":") - 1 );
                        else
                                section = "";
                        continue;
                }
                else
                {
                        r = str[i].GetToken ( ":", 2 );
                        if ( r.IsIn ( "}" ) )
                        {
                                r = r.GetToken ( "}", 1, 1 );
                                if ( section.IsIn (":") )
                                        section = section.GetToken ( ":", 1, section.NumToken(":") - 1 );
                                else
                                        section = "";
                        }
                }

                if ( r.IsIn("\"") )
                        r = ReplaceString ( r, strings );

                if ( c == "CALL" )
                {
                        CyString func = r.GetToken ( ",", 1, 1 ).upper();
                        for ( SFunction *f = m_lFunctions.First(); f; f = m_lFunctions.Next() )
                        {
                                if ( f->sFunction == func )
                                {
                                        int num = 0;
                                        CyString *varis = r.GetToken ( ",", 2 ).SplitToken ( ',', &num );
                                        int i = 0;

                                        int stringSize = varibles->Count();
                                        for ( SStringList *sl = f->lVaribles.Head(); (sl) && (i < num); sl = sl->next, i++ )
                                                varibles->PushBack ( varis[i], sl->str );

                                        CyString oldsection = section;
                                        DoBlock ( ro, f->sBlock, &f->lStrings, varibles, section );
                                        section = oldsection;

                                        for ( i = varibles->Count(); i > stringSize; i-- )
                                                varibles->PopBack();
                                        break;
                                }
                        }
                }
                else
                        ro->ParseSetting ( m_pOverlay, c, ParseVaribles (r, varibles), section );
        }
}
int CDisplayCustom::ParseNumber ( CyString n )
{
        // equations
        if ( n.IsAnyIn ( "+-/*" ) )
                return n.ConvertEquation ();

        return n.ToInt();
}

float CDisplayCustom::Update ( DWORD tNow )
{
        float dt = CDisplay::Update( tNow );
        if ( !dt )
                return 0;

        if ( m_iFadeIn )
        {
                if ( m_iStartDisplay >= m_iFadeIn )
                {
                        m_iFadeIn = 0;
                        m_iAlpha = m_iMaxAlpha;
                        m_iFadeState = FADE_NONE;
                }
                else
                {
                        float t = (float)m_iStartDisplay / (float)m_iFadeIn;
                        m_iAlpha = (int) (t * (float)m_iMaxAlpha);
                }
        }

        return dt;
}