Subversion Repositories AndroidProjects

Rev

Rev 66 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.pingK.game;

import com.gebauz.framework.util.Sprite2D;
import com.gebauz.framework.util.Texture;
import com.gebauz.framework.util.Vector4;
import com.gebauz.pingK.R;
import com.gebauz.pingK.R.drawable;

/**
 * Not the most optimized class, but quick and dirty until a real font engine gets implemented
 */

public class Font
{
        private class CharacterData
        {
                public char c;
                public float x;
                public float y;
                public float w;
                public float h;
               
                public CharacterData(char _c, float _x, float _y, float _w, float _h)
                {
                        c = _c;
                        x = _x;
                        y = _y;
                        w = _w;
                        h = _h;
                }
        }
       
        public static final float CHARACTER_SIZE = 8.0f;
       
        private Sprite2D mFontSprite = new Sprite2D();
        private char mCharacters[] =
        {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '=', '-'
        };
        private CharacterData mCharacterData[] = new CharacterData[mCharacters.length];

        public Font()
        {
                mFontSprite.init(R.drawable.font);
                mFontSprite.pivotX = 0.0f;
                mFontSprite.pivotY = 0.0f;
               
                mFontSprite.getTexture().setFiltering(Texture.Filter.NEAREST, Texture.Filter.NEAREST);
                mFontSprite.setColor(GameConsts.PINGK_COLOR);
               
                float x = 0.0f;
                for (int i = 0; i < mCharacters.length; i++)
                {
                        mCharacterData[i] = new CharacterData(mCharacters[i], x, 0, CHARACTER_SIZE, CHARACTER_SIZE);
                        x += CHARACTER_SIZE;
                }
        }
       
        public void update(float deltaTime)
        {
               
        }
       
        public void render()
        {
                //drawText("TEST 0 1 2", 10, 10, 2.0f);
        }
       
        public void drawText(String text, float x, float y, float scale, Vector4 color)
        {
                String actualText = text.toUpperCase();
               
                float posX = x;
                mFontSprite.setColor(color);
               
                for (int i = 0; i < actualText.length(); i++)
                {
                        char c = actualText.charAt(i);
                        int index = getCharacterIndex(c);
                       
                        if (index == -1)
                        {
                                posX += (CHARACTER_SIZE * scale);
                                continue;
                        }
                       
                        CharacterData data = mCharacterData[index];
                       
                        mFontSprite.setTextureArea(data.x, data.y, data.w, data.h);
                        mFontSprite.x = posX;
                        mFontSprite.y = y;
                        mFontSprite.w = CHARACTER_SIZE * scale;
                        mFontSprite.h = CHARACTER_SIZE * scale;
                       
                        mFontSprite.render();
                       
                        posX += (CHARACTER_SIZE * scale);
                }
        }
       
        public void drawText(String text, float x, float y, float scale)
        {
                drawText(text, x, y, scale, GameConsts.PINGK_COLOR);
        }
       
        private int getCharacterIndex(char character)
        {
                for (int i = 0; i < mCharacters.length; i++)
                {
                        if (character == mCharacters[i])
                                return i;
                }
                return -1;
        }
}