package com.gebauz.Bauzoid.graphics;
import com.gebauz.Bauzoid.graphics.sprite.AtlasSprite;
import com.gebauz.Bauzoid.graphics.sprite.AtlasSpriteInstance;
import com.gebauz.Bauzoid.math.Vector4;
public class Font extends GraphicsObject
{
public static class CharacterInfo
{
private char mCharacter
;
private AtlasSpriteInstance mSpriteInstance =
null;
public float offsetX =
0;
public float offsetY =
0;
public float width
;
public CharacterInfo
(char c
)
{
mCharacter = c
;
}
public final char getCharacter
()
{
return mCharacter
;
}
public void setSpriteInstance
(AtlasSpriteInstance instance
)
{
mSpriteInstance = instance
;
}
public final AtlasSpriteInstance getSpriteInstance
()
{
return mSpriteInstance
;
}
public void render
()
{
if (mSpriteInstance ==
null)
return;
mSpriteInstance.
render();
}
}
private CharacterInfo
[] mCharacters =
null;
private float mAscent = 0.0f
;
private AtlasSprite mSprite =
null;
public Font(Graphics graphics
)
{
super(graphics
);
}
public void dispose
()
{
mCharacters =
null;
}
public void setAscent
(float ascent
)
{
mAscent = ascent
;
}
public void setCharacters
(CharacterInfo
[] characters
)
{
mCharacters = characters
;
}
public void setSprite
(AtlasSprite sprite
)
{
mSprite = sprite
;
}
public void drawText
(String text,
float x,
float y, Vector4 color
)
{
drawText
(text, x, y, color, 1.0f
);
}
public void drawText
(String text,
float x,
float y, Vector4 color,
float scale
)
{
float posX = x
;
float posY = y
;
for (int i =
0; i
< text.
length(); i++
)
{
if (text.
charAt(i
) ==
'\n')
{
posY += mAscent
* scale
;
posX = x
;
continue;
}
int n = getCharacterIndex
(text.
charAt(i
));
if (n == -
1)
{
n = getCharacterIndex
(Character.
toLowerCase(text.
charAt(i
)));
if (n == -
1)
{
n = getCharacterIndex
(Character.
toUpperCase(text.
charAt(i
)));
if (n == -
1)
continue;
}
}
CharacterInfo c = mCharacters
[n
];
if (c.
mSpriteInstance !=
null)
{
c.
mSpriteInstance.
x = posX
;
c.
mSpriteInstance.
y = posY
;
c.
mSpriteInstance.
color = color
;
c.
mSpriteInstance.
w = mSprite.
getRegionWidth(c.
mSpriteInstance.
getRegionIndex()) * scale
;
c.
mSpriteInstance.
h = mSprite.
getRegionHeight(c.
mSpriteInstance.
getRegionIndex()) * scale
;
c.
mSpriteInstance.
pivotX =
0;
c.
mSpriteInstance.
pivotY =
0;
c.
render();
}
posX += c.
width * scale
;
}
}
public float getTextWidth
(String text,
float scale
)
{
float maxWidth =
0;
float width =
0;
float posX =
0;
float posY =
0;
for (int i =
0; i
< text.
length(); i++
)
{
if (text.
charAt(i
) ==
'\n')
{
posY += mAscent
* scale
;
posX =
0;
continue;
}
int n = getCharacterIndex
(text.
charAt(i
));
if (n == -
1)
{
n = getCharacterIndex
(Character.
toLowerCase(text.
charAt(i
)));
if (n == -
1)
{
n = getCharacterIndex
(Character.
toUpperCase(text.
charAt(i
)));
if (n == -
1)
continue;
}
}
CharacterInfo c = mCharacters
[n
];
/* if (c.mSpriteInstance != null)
{
width = posX + mSprite.getRegionWidth(c.mSpriteInstance.getRegionIndex()) * scale;
}
else*/
{
width = posX + c.
width * scale
;
}
if (maxWidth
< width
)
{
maxWidth = width
;
}
posX += c.
width * scale
;
}
return maxWidth
;
}
public float getTextHeight
(String text,
float scale
)
{
float maxHeight =
0;
float posY =
0;
for (int i =
0; i
< text.
length(); i++
)
{
if (text.
charAt(i
) ==
'\n')
{
posY += mAscent
* scale
;
continue;
}
int n = getCharacterIndex
(text.
charAt(i
));
if (n == -
1)
{
n = getCharacterIndex
(Character.
toLowerCase(text.
charAt(i
)));
if (n == -
1)
{
n = getCharacterIndex
(Character.
toUpperCase(text.
charAt(i
)));
if (n == -
1)
continue;
}
}
CharacterInfo c = mCharacters
[n
];
float charHeight =
this.
mAscent * scale
;
/* if (c.mSpriteInstance != null)
{
charHeight = mSprite.getRegionHeight(c.mSpriteInstance.getRegionIndex()) * scale;
}*/
if ((posY + charHeight
) > maxHeight
)
{
maxHeight = posY + charHeight
;
}
}
return maxHeight
;
}
public int getCharacterIndex
(char c
)
{
for (int i =
0; i
< mCharacters.
length; i++
)
{
if (mCharacters
[i
].
getCharacter() == c
)
return i
;
}
return -
1;
}
public CharacterInfo getCharacter
(char c
)
{
int i = getCharacterIndex
(c
);
if (i
!= -
1)
return mCharacters
[i
];
return null;
}
}