///////////////////////////////////////////////////////////////////////////////////////////
// TextSprite.cpp
//
//  Implementation of the TextSprite class, which represents a rectangular object on the 
// screen, displaying a string of symbols from a texture.
//
//  TextSprite derives from Sprite, and uses it to perform texture segmentation and 
// drawing. It calculates the sprite frames from the offsets of letters in a character 
// string.

#include "StdAfx.h"
#include "TextSprite.h"

namespace Sunlight
{
    namespace DirectX
    {
        namespace Graphics
        {
            TextSprite::TextSprite(SpriteManager *manager, System::Drawing::Rectangle dimensions, 
                Texture *texture, System::Drawing::Point sourcePosition, int framesAcross,
                String *characterSet) :
                Sprite(manager, dimensions, texture, sourcePosition, framesAcross),
                CharacterSet(characterSet),
                Text(NULL)
            {
                DefaultCharacter = CharacterSet->Chars[0];
            }

            // Gets a rectangle enclosing the object.
            Drawing::Rectangle TextSprite::get_Bounds()
            {
                if (Text == NULL)
                    return Drawing::Rectangle(Left, Top, 0, 0);

                return Drawing::Rectangle(Left, Top, Text->Length * Width, Height);
            }
            // Draw this sprite using its SpriteManager.
            void TextSprite::Draw()
            {
                int OldLeft = Left;

                for (int i = 0; i < Text->Length; i++)
                {
                    wchar_t c = Text->Chars[i];
                    int index = CharacterSet->IndexOf(c);

                    if (index == -1)
                        index = CharacterSet->IndexOf(DefaultCharacter);

                    if (index != -1)
                    {
                        Left = OldLeft + i * Width;
                        CurrentFrame = index;
                        Sprite::Draw();
                    }
                }
                
                Left = OldLeft;
            }
        }
    }
}