2003-02-16 04:01:45 +00:00
|
|
|
#include "global.h"
|
2001-11-03 10:52:42 +00:00
|
|
|
#include "BitmapText.h"
|
|
|
|
|
#include "IniFile.h"
|
2002-03-30 20:00:13 +00:00
|
|
|
#include "FontManager.h"
|
2002-05-01 19:14:55 +00:00
|
|
|
#include "RageLog.h"
|
2002-07-27 19:29:51 +00:00
|
|
|
#include "RageException.h"
|
|
|
|
|
#include "RageTimer.h"
|
2002-11-11 04:53:31 +00:00
|
|
|
#include "RageDisplay.h"
|
|
|
|
|
#include "ThemeManager.h"
|
|
|
|
|
#include "GameConstantsAndTypes.h"
|
2002-12-29 22:55:26 +00:00
|
|
|
#include "Font.h"
|
2003-11-06 07:23:22 +00:00
|
|
|
#include "ActorUtil.h" // for HandleParams
|
2002-06-14 22:25:22 +00:00
|
|
|
|
2003-01-10 03:26:41 +00:00
|
|
|
/* XXX:
|
|
|
|
|
* We need some kind of font modifier string for metrics. For example,
|
|
|
|
|
* "valign=top;spacing = x+5,y+2"
|
|
|
|
|
*
|
|
|
|
|
* Better, we could go all the way, drop all of the actor-specific font aliases,
|
|
|
|
|
* and do "font=header2;valign=top;...".
|
|
|
|
|
*
|
|
|
|
|
* However, let's wait until Pango support is in, so we can figure out how to
|
|
|
|
|
* integrate font selection at the same time.
|
|
|
|
|
*/
|
2003-01-05 08:47:48 +00:00
|
|
|
/*
|
|
|
|
|
* Forward planning:
|
|
|
|
|
*
|
|
|
|
|
* This can't handle full CJK fonts; it's not reasonable to load a 2000+ glyph bitmap
|
|
|
|
|
* font into memory. We want to be able to fall back on a real font renderer when
|
|
|
|
|
* we're missing characters. However, we make use of custom glyphs, and we don't want
|
|
|
|
|
* custom glyphs and fallback fonts to be mutually exclusive.
|
|
|
|
|
*
|
|
|
|
|
* So, if we have a fallback font renderer active, and we're missing any characters at
|
|
|
|
|
* all, send the text to the fallback renderer. If it can't render a character
|
|
|
|
|
* (because it's a special-use character), render up to that character, render the
|
|
|
|
|
* special character ourself, then start again on the next character. The data
|
|
|
|
|
* rendered can be put into textures and put into the verts/tex lists as if it came
|
|
|
|
|
* from a regular font (nothing says we can't put more than one character per quad)
|
|
|
|
|
* and DrawChars won't have to be changed at all.
|
|
|
|
|
*
|
|
|
|
|
* Some mechanism to hint which fallback font size/style a given font wants, to make
|
|
|
|
|
* a best effort to line up font types. This could be a setting in the font INI.
|
|
|
|
|
*/
|
2002-12-29 22:55:26 +00:00
|
|
|
#define RAINBOW_COLOR(n) THEME->GetMetricC("BitmapText",ssprintf("RainbowColor%i", n+1))
|
2002-04-16 17:31:00 +00:00
|
|
|
|
2002-08-27 23:31:41 +00:00
|
|
|
const int NUM_RAINBOW_COLORS = 7;
|
2002-10-28 05:30:45 +00:00
|
|
|
RageColor RAINBOW_COLORS[NUM_RAINBOW_COLORS];
|
2001-11-03 10:52:42 +00:00
|
|
|
|
|
|
|
|
BitmapText::BitmapText()
|
|
|
|
|
{
|
2002-08-27 23:31:41 +00:00
|
|
|
// Loading these theme metrics is slow, so only do it ever 20th time.
|
|
|
|
|
static int iReloadCounter = 0;
|
|
|
|
|
if( iReloadCounter%20==0 )
|
|
|
|
|
{
|
2002-12-29 22:55:26 +00:00
|
|
|
for(int i = 0; i < NUM_RAINBOW_COLORS; ++i)
|
|
|
|
|
RAINBOW_COLORS[i] = RAINBOW_COLOR(i);
|
2002-08-27 23:31:41 +00:00
|
|
|
}
|
|
|
|
|
iReloadCounter++;
|
|
|
|
|
|
2002-03-30 20:00:13 +00:00
|
|
|
m_pFont = NULL;
|
2002-02-02 05:11:12 +00:00
|
|
|
|
2002-04-16 17:31:00 +00:00
|
|
|
m_bRainbow = false;
|
2003-11-06 07:23:22 +00:00
|
|
|
|
|
|
|
|
m_iWrapWidthPixels = -1;
|
2003-12-17 09:47:03 +00:00
|
|
|
m_fMaxWidth = 0;
|
2004-03-20 02:59:08 +00:00
|
|
|
|
|
|
|
|
SetShadowLength( 4 );
|
2002-02-02 05:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BitmapText::~BitmapText()
|
|
|
|
|
{
|
2002-03-30 20:00:13 +00:00
|
|
|
if( m_pFont )
|
2003-01-04 05:17:51 +00:00
|
|
|
FONT->UnloadFont( m_pFont );
|
2001-11-25 04:31:44 +00:00
|
|
|
}
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2002-08-13 23:26:46 +00:00
|
|
|
bool BitmapText::LoadFromFont( CString sFontFilePath )
|
2001-11-25 04:31:44 +00:00
|
|
|
{
|
2004-05-30 23:23:39 +00:00
|
|
|
CHECKPOINT_M( ssprintf("BitmapText::LoadFromFont(%s)", sFontFilePath.c_str()) );
|
|
|
|
|
|
2002-10-13 22:58:31 +00:00
|
|
|
if( m_pFont ) {
|
2003-01-04 05:17:51 +00:00
|
|
|
FONT->UnloadFont( m_pFont );
|
2002-10-13 22:58:31 +00:00
|
|
|
m_pFont = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
m_pFont = FONT->LoadFont( sFontFilePath );
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2003-01-04 07:55:42 +00:00
|
|
|
BuildChars();
|
|
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool BitmapText::LoadFromTextureAndChars( CString sTexturePath, CString sChars )
|
|
|
|
|
{
|
2003-10-22 06:37:31 +00:00
|
|
|
CHECKPOINT_M( ssprintf("BitmapText::LoadFromTextureAndChars(\"%s\",\"%s\")", sTexturePath.c_str(), sChars.c_str()) );
|
2002-09-03 22:31:06 +00:00
|
|
|
|
2002-10-16 19:31:17 +00:00
|
|
|
if( m_pFont ) {
|
2003-01-04 05:17:51 +00:00
|
|
|
FONT->UnloadFont( m_pFont );
|
2002-10-16 19:31:17 +00:00
|
|
|
m_pFont = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
m_pFont = FONT->LoadFont( sTexturePath, sChars );
|
2002-09-03 06:33:08 +00:00
|
|
|
|
2003-01-04 07:55:42 +00:00
|
|
|
BuildChars();
|
|
|
|
|
|
2002-03-30 20:00:13 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2003-01-04 07:55:42 +00:00
|
|
|
void BitmapText::BuildChars()
|
|
|
|
|
{
|
|
|
|
|
/* If we don't have a font yet, we'll do this when it loads. */
|
|
|
|
|
if(m_pFont == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
2003-01-21 23:10:22 +00:00
|
|
|
/* calculate line lengths and widths */
|
2003-11-24 03:02:26 +00:00
|
|
|
m_size.x = 0;
|
2003-01-21 23:10:22 +00:00
|
|
|
|
2003-02-12 19:34:53 +00:00
|
|
|
m_iLineWidths.clear();
|
2003-11-06 07:23:22 +00:00
|
|
|
for( unsigned l=0; l<m_wTextLines.size(); l++ ) // for each line
|
2003-01-21 23:10:22 +00:00
|
|
|
{
|
2003-11-06 07:23:22 +00:00
|
|
|
m_iLineWidths.push_back(m_pFont->GetLineWidthInSourcePixels( m_wTextLines[l] ));
|
2003-11-24 03:02:26 +00:00
|
|
|
m_size.x = max( m_size.x, m_iLineWidths.back() );
|
2003-01-21 23:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
2003-01-04 07:55:42 +00:00
|
|
|
verts.clear();
|
|
|
|
|
tex.clear();
|
|
|
|
|
|
2003-11-06 07:23:22 +00:00
|
|
|
if(m_wTextLines.empty()) return;
|
2003-01-08 02:32:30 +00:00
|
|
|
|
2003-11-24 02:31:41 +00:00
|
|
|
m_size.y = float(m_pFont->GetHeight() * m_wTextLines.size());
|
2003-01-04 07:55:42 +00:00
|
|
|
unsigned i;
|
2003-01-08 02:32:30 +00:00
|
|
|
int MinSpacing = 0;
|
|
|
|
|
|
|
|
|
|
/* The height (from the origin to the baseline): */
|
|
|
|
|
int Padding = max(m_pFont->GetLineSpacing(), MinSpacing) - m_pFont->GetHeight();
|
|
|
|
|
|
|
|
|
|
/* There's padding between every line: */
|
2003-11-24 02:31:41 +00:00
|
|
|
m_size.y += Padding * (m_wTextLines.size()-1);
|
2003-01-04 07:55:42 +00:00
|
|
|
|
|
|
|
|
int iY; // the top position of the first row of characters
|
|
|
|
|
switch( m_VertAlign )
|
|
|
|
|
{
|
|
|
|
|
case align_top: iY = 0; break;
|
2003-11-24 02:31:41 +00:00
|
|
|
case align_middle: iY = -(int)roundf(m_size.y/2.0f); break;
|
|
|
|
|
case align_bottom: iY = -(int)m_size.y; break;
|
2003-01-04 07:55:42 +00:00
|
|
|
default: ASSERT( false ); return;
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-06 07:23:22 +00:00
|
|
|
for( i=0; i<m_wTextLines.size(); i++ ) // foreach line
|
2003-01-04 07:55:42 +00:00
|
|
|
{
|
2003-01-08 02:32:30 +00:00
|
|
|
iY += m_pFont->GetHeight();
|
2003-11-06 07:23:22 +00:00
|
|
|
const wstring &szLine = m_wTextLines[i];
|
2003-01-04 07:55:42 +00:00
|
|
|
const int iLineWidth = m_iLineWidths[i];
|
|
|
|
|
|
|
|
|
|
int iX;
|
|
|
|
|
switch( m_HorizAlign )
|
|
|
|
|
{
|
|
|
|
|
case align_left: iX = 0; break;
|
2003-01-10 02:54:07 +00:00
|
|
|
case align_center: iX = -(int)roundf(iLineWidth/2.0f); break;
|
2003-01-04 07:55:42 +00:00
|
|
|
case align_right: iX = -iLineWidth; break;
|
|
|
|
|
default: ASSERT( false ); return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for( unsigned j=0; j<szLine.size(); j++ ) // for each character in the line
|
|
|
|
|
{
|
2003-07-07 01:29:18 +00:00
|
|
|
RageSpriteVertex v[4];
|
2003-01-04 07:55:42 +00:00
|
|
|
const glyph &g = m_pFont->GetGlyph(szLine[j]);
|
|
|
|
|
|
|
|
|
|
/* set vertex positions */
|
2003-01-10 03:48:21 +00:00
|
|
|
v[0].p = RageVector3( iX+g.hshift, iY+g.fp->vshift, 0 ); // top left
|
|
|
|
|
v[1].p = RageVector3( iX+g.hshift, iY+g.fp->vshift+g.height, 0 ); // bottom left
|
|
|
|
|
v[2].p = RageVector3( iX+g.hshift+g.width, iY+g.fp->vshift+g.height, 0 ); // bottom right
|
|
|
|
|
v[3].p = RageVector3( iX+g.hshift+g.width, iY+g.fp->vshift, 0 ); // top right
|
2003-01-04 07:55:42 +00:00
|
|
|
|
|
|
|
|
/* Advance the cursor. */
|
|
|
|
|
iX += g.hadvance;
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2003-01-04 07:55:42 +00:00
|
|
|
/* set texture coordinates */
|
|
|
|
|
v[0].t = RageVector2( g.rect.left, g.rect.top );
|
|
|
|
|
v[1].t = RageVector2( g.rect.left, g.rect.bottom );
|
|
|
|
|
v[2].t = RageVector2( g.rect.right, g.rect.bottom );
|
|
|
|
|
v[3].t = RageVector2( g.rect.right, g.rect.top );
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2003-01-04 07:55:42 +00:00
|
|
|
verts.insert(verts.end(), &v[0], &v[4]);
|
2003-01-19 00:55:14 +00:00
|
|
|
tex.push_back(g.GetTexture());
|
2003-01-04 07:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
2003-01-08 02:32:30 +00:00
|
|
|
/* The amount of padding a line needs: */
|
|
|
|
|
iY += Padding;
|
2003-01-04 07:55:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BitmapText::DrawChars()
|
|
|
|
|
{
|
2004-03-06 07:15:59 +00:00
|
|
|
// bail if cropped all the way
|
|
|
|
|
if( m_pTempState->crop.left + m_pTempState->crop.right >= 1 ||
|
|
|
|
|
m_pTempState->crop.top + m_pTempState->crop.bottom >= 1 )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const int iNumGlyphs = tex.size();
|
|
|
|
|
int iStartGlyph = (int) roundf( SCALE( m_pTempState->crop.left, 0.f, 1.f, 0, (float) iNumGlyphs ) );
|
|
|
|
|
int iEndGlyph = (int) roundf( SCALE( m_pTempState->crop.right, 0.f, 1.f, (float) iNumGlyphs, 0 ) );
|
|
|
|
|
iStartGlyph = clamp( iStartGlyph, 0, iNumGlyphs );
|
|
|
|
|
iEndGlyph = clamp( iEndGlyph, 0, iNumGlyphs );
|
|
|
|
|
|
|
|
|
|
if( m_pTempState->fade.top > 0 ||
|
|
|
|
|
m_pTempState->fade.bottom > 0 ||
|
|
|
|
|
m_pTempState->fade.left > 0 ||
|
|
|
|
|
m_pTempState->fade.right > 0 )
|
|
|
|
|
{
|
|
|
|
|
/* Handle fading by tweaking the alpha values of the verteces. */
|
|
|
|
|
|
|
|
|
|
/* Actual size of the fade on each side: */
|
|
|
|
|
const RectF &FadeDist = m_pTempState->fade;
|
|
|
|
|
RectF FadeSize = FadeDist;
|
|
|
|
|
|
|
|
|
|
/* If the cropped size is less than the fade distance, clamp. */
|
|
|
|
|
const float HorizRemaining = 1.0f - (m_pTempState->crop.left + m_pTempState->crop.right);
|
|
|
|
|
if( FadeDist.left+FadeDist.right > 0 &&
|
|
|
|
|
HorizRemaining < FadeDist.left+FadeDist.right )
|
|
|
|
|
{
|
|
|
|
|
const float LeftPercent = FadeDist.left/(FadeDist.left+FadeDist.right);
|
|
|
|
|
FadeSize.left = LeftPercent * HorizRemaining;
|
|
|
|
|
FadeSize.right = (1.0f-LeftPercent) * HorizRemaining;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const RageColor &FadeColor = m_pTempState->fadecolor;
|
|
|
|
|
|
|
|
|
|
/* We fade from 0 to LeftColor, then from RightColor to 0. (We won't fade all the way to
|
|
|
|
|
* 0 if the crop is beyond the outer edge.) */
|
|
|
|
|
const RageColor RightColor = scale( FadeSize.right, FadeDist.right, 0, RageColor(1,1,1,1), FadeColor );
|
|
|
|
|
const RageColor LeftColor = scale( FadeSize.left, FadeDist.left, 0, RageColor(1,1,1,1), FadeColor );
|
|
|
|
|
|
|
|
|
|
const float StartFadeLeftPercent = m_pTempState->crop.left;
|
|
|
|
|
const float StopFadeLeftPercent = m_pTempState->crop.left + FadeSize.left;
|
|
|
|
|
const float fLeftFadeStartGlyph = SCALE( StartFadeLeftPercent, 0.f, 1.f, 0, (float) iNumGlyphs );
|
|
|
|
|
const float fLeftFadeStopGlyph = SCALE( StopFadeLeftPercent, 0.f, 1.f, 0, (float) iNumGlyphs );
|
|
|
|
|
|
|
|
|
|
const float StartFadeRightPercent = 1-(m_pTempState->crop.right + FadeSize.right);
|
|
|
|
|
const float StopFadeRightPercent = 1-(m_pTempState->crop.right);
|
|
|
|
|
const float fRightFadeStartGlyph = SCALE( StartFadeRightPercent, 0.f, 1.f, 0, (float) iNumGlyphs );
|
|
|
|
|
const float fRightFadeStopGlyph = SCALE( StopFadeRightPercent, 0.f, 1.f, 0, (float) iNumGlyphs );
|
|
|
|
|
|
|
|
|
|
for( int start = iStartGlyph; start < iEndGlyph; ++start )
|
|
|
|
|
{
|
|
|
|
|
int i = start*4;
|
|
|
|
|
|
|
|
|
|
if( FadeSize.left > 0.001f )
|
|
|
|
|
{
|
|
|
|
|
/* Add .5, so we fade wrt. the center of the vert, not the left side.
|
|
|
|
|
* TODO: fade all channels, not just alpha */
|
|
|
|
|
float fPercent = SCALE( start+0.5f, fLeftFadeStartGlyph, fLeftFadeStopGlyph, 0.0f, 1.0f );
|
|
|
|
|
fPercent = clamp( fPercent, 0.0f, 1.0f );
|
|
|
|
|
fPercent *= LeftColor.a;
|
|
|
|
|
|
2004-03-06 08:53:49 +00:00
|
|
|
verts[i+0].c.a = (unsigned char)( verts[i+0].c.a * fPercent ); // top left
|
|
|
|
|
verts[i+1].c.a = (unsigned char)( verts[i+1].c.a * fPercent ); // bottom left
|
|
|
|
|
verts[i+2].c.a = (unsigned char)( verts[i+2].c.a * fPercent ); // bottom right
|
|
|
|
|
verts[i+3].c.a = (unsigned char)( verts[i+3].c.a * fPercent ); // top right
|
2004-03-06 07:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( FadeSize.right > 0.001f )
|
|
|
|
|
{
|
|
|
|
|
float fPercent = SCALE( start+0.5f, fRightFadeStartGlyph, fRightFadeStopGlyph, 1.0f, 0.0f );
|
|
|
|
|
fPercent = clamp( fPercent, 0.0f, 1.0f );
|
|
|
|
|
fPercent *= RightColor.a;
|
|
|
|
|
|
2004-03-06 08:53:49 +00:00
|
|
|
verts[i+0].c.a = (unsigned char)( verts[i+0].c.a * fPercent ); // top left
|
|
|
|
|
verts[i+1].c.a = (unsigned char)( verts[i+1].c.a * fPercent ); // bottom left
|
|
|
|
|
verts[i+2].c.a = (unsigned char)( verts[i+2].c.a * fPercent ); // bottom right
|
|
|
|
|
verts[i+3].c.a = (unsigned char)( verts[i+3].c.a * fPercent ); // top right
|
2004-03-06 07:15:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-09-13 19:47:28 +00:00
|
|
|
|
2004-03-06 07:15:59 +00:00
|
|
|
for( int start = iStartGlyph; start < iEndGlyph; )
|
2003-01-04 07:55:42 +00:00
|
|
|
{
|
2004-03-06 07:15:59 +00:00
|
|
|
int end = start;
|
|
|
|
|
while( end < iEndGlyph && tex[end] == tex[start] )
|
2003-01-04 07:55:42 +00:00
|
|
|
end++;
|
2004-02-04 09:55:18 +00:00
|
|
|
DISPLAY->ClearAllTextures();
|
|
|
|
|
DISPLAY->SetTexture( 0, tex[start] );
|
2003-09-13 19:47:28 +00:00
|
|
|
RageSpriteVertex &start_vertex = verts[start*4];
|
|
|
|
|
int iNumVertsToDraw = (end-start)*4;
|
|
|
|
|
DISPLAY->DrawQuads( &start_vertex, iNumVertsToDraw );
|
2003-01-04 07:55:42 +00:00
|
|
|
|
2003-01-05 01:32:08 +00:00
|
|
|
start = end;
|
2003-01-04 07:55:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2003-02-17 01:06:03 +00:00
|
|
|
/* sText is UTF-8. If not all of the characters in sText are available in the
|
|
|
|
|
* font, sAlternateText will be used instead. If there are unavailable characters
|
2003-02-12 20:28:28 +00:00
|
|
|
* in sAlternateText, too, just use sText. */
|
2003-11-06 06:20:35 +00:00
|
|
|
void BitmapText::SetText( CString sText, CString sAlternateText, int iWrapWidthPixels )
|
2002-03-30 20:00:13 +00:00
|
|
|
{
|
2002-09-02 21:59:58 +00:00
|
|
|
ASSERT( m_pFont );
|
2003-01-06 07:43:14 +00:00
|
|
|
|
2003-02-12 20:19:20 +00:00
|
|
|
if(StringWillUseAlternate(sText, sAlternateText))
|
|
|
|
|
sText = sAlternateText;
|
2003-02-12 19:34:53 +00:00
|
|
|
|
2003-11-06 07:23:22 +00:00
|
|
|
if( iWrapWidthPixels == -1 ) // wrap not specified
|
|
|
|
|
iWrapWidthPixels = m_iWrapWidthPixels;
|
|
|
|
|
|
|
|
|
|
if(m_sText == sText && iWrapWidthPixels==m_iWrapWidthPixels)
|
2003-01-04 07:55:42 +00:00
|
|
|
return;
|
2003-11-06 07:23:22 +00:00
|
|
|
m_sText = sText;
|
|
|
|
|
m_iWrapWidthPixels = iWrapWidthPixels;
|
2002-09-22 20:34:11 +00:00
|
|
|
|
2003-11-06 06:20:35 +00:00
|
|
|
|
|
|
|
|
// Break the string into lines.
|
|
|
|
|
//
|
2003-11-06 07:23:22 +00:00
|
|
|
m_wTextLines.clear();
|
2003-11-06 06:20:35 +00:00
|
|
|
|
|
|
|
|
if( iWrapWidthPixels == -1 )
|
|
|
|
|
{
|
2003-11-06 07:23:22 +00:00
|
|
|
split( CStringToWstring(sText), L"\n", m_wTextLines, false );
|
2003-11-06 06:20:35 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// Break sText into lines that don't exceed iWrapWidthPixels
|
|
|
|
|
// (if only one word fits on the line, it may be larger than iWrapWidthPixels ).
|
|
|
|
|
//
|
|
|
|
|
// TODO: Investigate whether this works in all languages
|
2003-11-06 21:47:12 +00:00
|
|
|
/* It doesn't. I can add Japanese wrapping, at least. We could handle hyphens
|
|
|
|
|
* and soft hyphens and pretty easily, too. -glenn */
|
2003-11-06 06:20:35 +00:00
|
|
|
// TODO: Move this wrapping logic into Font
|
|
|
|
|
CStringArray asWords;
|
|
|
|
|
split( sText, " ", asWords );
|
|
|
|
|
|
|
|
|
|
CString sCurLine;
|
|
|
|
|
int iCurLineWidth = 0;
|
|
|
|
|
|
2003-11-06 21:47:12 +00:00
|
|
|
/* Note that GetLineWidthInSourcePixels does not include horizontal overdraw
|
|
|
|
|
* right now (eg. italic fonts), so it's possible to go slightly over. */
|
|
|
|
|
for( unsigned i=0; i<asWords.size(); i++ )
|
2003-11-06 06:20:35 +00:00
|
|
|
{
|
2003-11-06 21:47:12 +00:00
|
|
|
const CString &sWord = asWords[i];
|
2003-11-06 06:20:35 +00:00
|
|
|
int iWidthWord = m_pFont->GetLineWidthInSourcePixels( CStringToWstring(sWord) );
|
|
|
|
|
|
|
|
|
|
if( sCurLine.empty() )
|
|
|
|
|
{
|
|
|
|
|
sCurLine = sWord;
|
|
|
|
|
iCurLineWidth = iWidthWord;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CString sToAdd = " " + sWord;
|
|
|
|
|
int iWidthToAdd = m_pFont->GetLineWidthInSourcePixels(L" ") + iWidthWord;
|
|
|
|
|
if( iCurLineWidth + iWidthToAdd <= iWrapWidthPixels ) // will fit on current line
|
|
|
|
|
{
|
|
|
|
|
sCurLine += sToAdd;
|
|
|
|
|
iCurLineWidth += iWidthToAdd;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-11-06 07:23:22 +00:00
|
|
|
m_wTextLines.push_back( CStringToWstring(sCurLine) );
|
2003-11-06 06:20:35 +00:00
|
|
|
sCurLine = sWord;
|
|
|
|
|
iCurLineWidth = iWidthWord;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-11-06 07:23:22 +00:00
|
|
|
m_wTextLines.push_back( CStringToWstring(sCurLine) );
|
2003-11-06 06:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
2003-01-04 07:55:42 +00:00
|
|
|
BuildChars();
|
2003-12-17 09:47:03 +00:00
|
|
|
UpdateBaseZoom();
|
2001-11-27 22:47:30 +00:00
|
|
|
}
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2003-12-17 09:47:03 +00:00
|
|
|
void BitmapText::SetMaxWidth( float MaxWidth )
|
|
|
|
|
{
|
|
|
|
|
m_fMaxWidth = MaxWidth;
|
|
|
|
|
UpdateBaseZoom();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BitmapText::UpdateBaseZoom()
|
|
|
|
|
{
|
|
|
|
|
if( m_fMaxWidth == 0 )
|
|
|
|
|
{
|
|
|
|
|
this->SetBaseZoomX( 1 );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2003-05-19 23:48:51 +00:00
|
|
|
|
2003-11-24 03:02:26 +00:00
|
|
|
const float Width = GetUnzoomedWidth();
|
|
|
|
|
|
2003-05-19 23:48:51 +00:00
|
|
|
/* Avoid division by zero. */
|
2003-11-24 03:02:26 +00:00
|
|
|
if( !Width )
|
|
|
|
|
return;
|
2003-05-19 23:48:51 +00:00
|
|
|
|
2003-12-17 09:47:03 +00:00
|
|
|
/* Never decrease the zoom. */
|
|
|
|
|
const float Zoom = min( 1, m_fMaxWidth/Width );
|
|
|
|
|
this->SetBaseZoomX( Zoom );
|
2003-05-19 23:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
2003-02-12 20:19:20 +00:00
|
|
|
bool BitmapText::StringWillUseAlternate(CString sText, CString sAlternateText) const
|
|
|
|
|
{
|
|
|
|
|
ASSERT( m_pFont );
|
|
|
|
|
|
|
|
|
|
/* Can't use the alternate if there isn't one. */
|
|
|
|
|
if(!sAlternateText.size()) return false;
|
|
|
|
|
|
|
|
|
|
/* False if the alternate isn't needed. */
|
|
|
|
|
if(m_pFont->FontCompleteForString(CStringToWstring(sText))) return false;
|
|
|
|
|
|
|
|
|
|
/* False if the alternate is also incomplete. */
|
|
|
|
|
if(!m_pFont->FontCompleteForString(CStringToWstring(sAlternateText))) return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-22 09:31:32 +00:00
|
|
|
void BitmapText::CropToWidth( int iMaxWidthInSourcePixels )
|
|
|
|
|
{
|
|
|
|
|
iMaxWidthInSourcePixels = max( 0, iMaxWidthInSourcePixels );
|
|
|
|
|
|
2003-11-06 07:23:22 +00:00
|
|
|
for( unsigned l=0; l<m_wTextLines.size(); l++ ) // for each line
|
2002-08-22 09:31:32 +00:00
|
|
|
{
|
|
|
|
|
while( m_iLineWidths[l] > iMaxWidthInSourcePixels )
|
|
|
|
|
{
|
2003-11-06 07:23:22 +00:00
|
|
|
m_wTextLines[l].erase(m_wTextLines[l].end()-1, m_wTextLines[l].end());
|
|
|
|
|
m_iLineWidths[l] = m_pFont->GetLineWidthInSourcePixels( m_wTextLines[l] );
|
2002-08-22 09:31:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
2003-01-21 23:10:22 +00:00
|
|
|
|
|
|
|
|
BuildChars();
|
2002-08-22 09:31:32 +00:00
|
|
|
}
|
2001-11-27 22:47:30 +00:00
|
|
|
|
2004-02-14 03:38:34 +00:00
|
|
|
bool BitmapText::EarlyAbortDraw()
|
|
|
|
|
{
|
|
|
|
|
return m_wTextLines.empty();
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
// draw text at x, y using colorTop blended down to colorBottom, with size multiplied by scale
|
|
|
|
|
void BitmapText::DrawPrimitives()
|
2001-11-27 22:47:30 +00:00
|
|
|
{
|
2002-04-28 20:42:32 +00:00
|
|
|
|
2003-05-22 05:28:37 +00:00
|
|
|
Actor::SetRenderStates(); // set Actor-specified render states
|
2002-11-11 04:53:31 +00:00
|
|
|
DISPLAY->SetTextureModeModulate();
|
2003-05-15 06:09:19 +00:00
|
|
|
|
|
|
|
|
/* Draw if we're not fully transparent or the zbuffer is enabled */
|
2003-10-07 04:26:14 +00:00
|
|
|
if( m_pTempState->diffuse[0].a != 0 )
|
2002-01-16 10:01:32 +00:00
|
|
|
{
|
2002-08-18 16:19:26 +00:00
|
|
|
//////////////////////
|
|
|
|
|
// render the shadow
|
|
|
|
|
//////////////////////
|
2004-03-20 02:59:08 +00:00
|
|
|
if( m_fShadowLength != 0 )
|
2002-08-18 16:19:26 +00:00
|
|
|
{
|
|
|
|
|
DISPLAY->PushMatrix();
|
2003-05-22 05:28:37 +00:00
|
|
|
DISPLAY->TranslateWorld( m_fShadowLength, m_fShadowLength, 0 ); // shift by 5 units
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2003-10-07 04:26:14 +00:00
|
|
|
RageColor dim(0,0,0,0.5f*m_pTempState->diffuse[0].a); // semi-transparent black
|
2002-08-19 20:02:30 +00:00
|
|
|
|
2003-01-04 07:55:42 +00:00
|
|
|
for( unsigned i=0; i<verts.size(); i++ )
|
|
|
|
|
verts[i].c = dim;
|
|
|
|
|
DrawChars();
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2002-08-18 16:19:26 +00:00
|
|
|
DISPLAY->PopMatrix();
|
|
|
|
|
}
|
2002-05-29 09:47:24 +00:00
|
|
|
|
2002-08-18 16:19:26 +00:00
|
|
|
//////////////////////
|
|
|
|
|
// render the diffuse pass
|
|
|
|
|
//////////////////////
|
|
|
|
|
if( m_bRainbow )
|
2002-05-29 09:47:24 +00:00
|
|
|
{
|
2002-12-19 23:07:20 +00:00
|
|
|
int color_index = int(RageTimer::GetTimeSinceStart() / 0.200) % NUM_RAINBOW_COLORS;
|
2003-01-04 07:55:42 +00:00
|
|
|
for( unsigned i=0; i<verts.size(); i+=4 )
|
2002-08-18 16:19:26 +00:00
|
|
|
{
|
2002-10-28 05:30:45 +00:00
|
|
|
const RageColor color = RAINBOW_COLORS[color_index];
|
2003-01-04 07:55:42 +00:00
|
|
|
for( unsigned j=i; j<i+4; j++ )
|
|
|
|
|
verts[j].c = color;
|
2002-08-18 16:19:26 +00:00
|
|
|
|
|
|
|
|
color_index = (color_index+1)%NUM_RAINBOW_COLORS;
|
|
|
|
|
}
|
2002-05-29 09:47:24 +00:00
|
|
|
}
|
2002-08-18 16:19:26 +00:00
|
|
|
else
|
2002-05-29 09:47:24 +00:00
|
|
|
{
|
2003-01-04 07:55:42 +00:00
|
|
|
for( unsigned i=0; i<verts.size(); i+=4 )
|
2002-08-18 16:19:26 +00:00
|
|
|
{
|
2003-10-07 04:26:14 +00:00
|
|
|
verts[i+0].c = m_pTempState->diffuse[0]; // top left
|
|
|
|
|
verts[i+1].c = m_pTempState->diffuse[2]; // bottom left
|
|
|
|
|
verts[i+2].c = m_pTempState->diffuse[3]; // bottom right
|
|
|
|
|
verts[i+3].c = m_pTempState->diffuse[1]; // top right
|
2002-08-18 16:19:26 +00:00
|
|
|
}
|
2002-05-29 09:47:24 +00:00
|
|
|
}
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2003-01-04 07:55:42 +00:00
|
|
|
DrawChars();
|
2002-08-18 16:19:26 +00:00
|
|
|
}
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2002-12-29 22:55:26 +00:00
|
|
|
/* render the glow pass */
|
2003-10-07 04:26:14 +00:00
|
|
|
if( m_pTempState->glow.a > 0.0001f )
|
2002-01-16 10:01:32 +00:00
|
|
|
{
|
2003-10-07 04:26:14 +00:00
|
|
|
DISPLAY->SetTextureModeGlow(m_pTempState->glowmode);
|
2002-08-19 20:02:30 +00:00
|
|
|
|
2003-01-04 07:55:42 +00:00
|
|
|
for( unsigned i=0; i<verts.size(); i++ )
|
2003-10-07 04:26:14 +00:00
|
|
|
verts[i].c = m_pTempState->glow;
|
2003-01-04 07:55:42 +00:00
|
|
|
DrawChars();
|
2002-01-16 10:01:32 +00:00
|
|
|
}
|
2002-11-16 08:54:15 +00:00
|
|
|
}
|
2003-01-04 07:55:42 +00:00
|
|
|
|
|
|
|
|
/* Rebuild when these change. */
|
|
|
|
|
void BitmapText::SetHorizAlign( HorizAlign ha )
|
|
|
|
|
{
|
|
|
|
|
if(ha == m_HorizAlign) return;
|
|
|
|
|
Actor::SetHorizAlign(ha);
|
|
|
|
|
BuildChars();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BitmapText::SetVertAlign( VertAlign va )
|
|
|
|
|
{
|
|
|
|
|
if(va == m_VertAlign) return;
|
|
|
|
|
Actor::SetVertAlign(va);
|
|
|
|
|
BuildChars();
|
|
|
|
|
}
|
2003-11-06 07:23:22 +00:00
|
|
|
|
2004-01-10 20:34:18 +00:00
|
|
|
void BitmapText::HandleCommand( const ParsedCommand &command )
|
2003-11-06 07:23:22 +00:00
|
|
|
{
|
|
|
|
|
HandleParams;
|
|
|
|
|
|
2004-01-10 20:34:18 +00:00
|
|
|
const CString& sName = sParam(0);
|
2003-11-06 07:23:22 +00:00
|
|
|
|
|
|
|
|
// Commands that go in the tweening queue:
|
|
|
|
|
// Commands that take effect immediately (ignoring the tweening queue):
|
|
|
|
|
if( sName=="wrapwidthpixels" ) SetWrapWidthPixels( iParam(1) );
|
2003-12-17 09:47:03 +00:00
|
|
|
else if( sName=="maxwidth" ) SetMaxWidth( fParam(1) );
|
2003-11-06 07:23:22 +00:00
|
|
|
else
|
|
|
|
|
{
|
2004-01-10 20:34:18 +00:00
|
|
|
Actor::HandleCommand( command );
|
2003-11-06 07:23:22 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CheckHandledParams;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BitmapText::SetWrapWidthPixels( int iWrapWidthPixels )
|
|
|
|
|
{
|
2003-11-06 21:47:12 +00:00
|
|
|
ASSERT( m_pFont ); // always load a font first
|
2003-11-06 07:23:22 +00:00
|
|
|
if( m_iWrapWidthPixels == iWrapWidthPixels )
|
|
|
|
|
return;
|
|
|
|
|
SetText( m_sText, "", iWrapWidthPixels );
|
|
|
|
|
}
|
2004-06-08 00:47:53 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* (c) 2003-2004 Chris Danford
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, and/or sell copies of the Software, and to permit persons to
|
|
|
|
|
* whom the Software is furnished to do so, provided that the above
|
|
|
|
|
* copyright notice(s) and this permission notice appear in all copies of
|
|
|
|
|
* the Software and that both the above copyright notice(s) and this
|
|
|
|
|
* permission notice appear in supporting documentation.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
|
|
|
|
|
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
|
|
|
|
|
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
|
|
|
|
|
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
|
|
|
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
|
|
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*/
|