Files
itgmania212121/src/BitmapText.cpp
T

888 lines
25 KiB
C++
Raw Normal View History

2003-02-16 04:01:45 +00:00
#include "global.h"
2001-11-03 10:52:42 +00:00
#include "BitmapText.h"
2005-11-06 18:03:47 +00:00
#include "XmlFile.h"
#include "FontManager.h"
2002-05-01 19:14:55 +00:00
#include "RageLog.h"
2002-07-27 19:29:51 +00:00
#include "RageTimer.h"
#include "RageDisplay.h"
#include "ThemeManager.h"
2002-12-29 22:55:26 +00:00
#include "Font.h"
2006-08-20 19:32:57 +00:00
#include "ActorUtil.h"
2005-01-26 11:21:43 +00:00
#include "LuaBinding.h"
2005-09-22 16:49:51 +00:00
#include "Foreach.h"
2005-01-26 11:21:43 +00:00
REGISTER_ACTOR_CLASS( BitmapText )
2002-06-14 22:25:22 +00:00
2005-01-16 20:37:08 +00:00
/*
* XXX: Changing a whole array of diffuse colors every frame (several times) is a waste,
* when we're usually setting them all to the same value. Rainbow and fading are annoying
* to optimize, but rarely used. Iterating over every character in Draw() is dumb.
*/
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;...".
2003-01-05 08:47:48 +00:00
*/
#define NUM_RAINBOW_COLORS THEME->GetMetricI("BitmapText","NumRainbowColors")
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
static vector<RageColor> RAINBOW_COLORS;
2001-11-03 10:52:42 +00:00
BitmapText::BitmapText()
{
// Loading these theme metrics is slow, so only do it ever 20th time.
static int iReloadCounter = 0;
if( iReloadCounter%20==0 )
{
RAINBOW_COLORS.resize( NUM_RAINBOW_COLORS );
for( unsigned i = 0; i < RAINBOW_COLORS.size(); ++i )
2002-12-29 22:55:26 +00:00
RAINBOW_COLORS[i] = RAINBOW_COLOR(i);
}
iReloadCounter++;
m_pFont = NULL;
2008-03-28 12:24:04 +00:00
m_bUppercase = false;
2002-02-02 05:11:12 +00:00
m_bRainbowScroll = false;
2006-12-07 07:49:22 +00:00
m_bJitter = false;
2003-11-06 07:23:22 +00:00
m_iWrapWidthPixels = -1;
m_fMaxWidth = 0;
2005-04-15 12:25:59 +00:00
m_fMaxHeight = 0;
2006-01-24 01:12:54 +00:00
m_iVertSpacing = 0;
m_bHasGlowAttribute = false;
m_StrokeColor = RageColor(1,1,1,1);
SetShadowLength( 4 );
2002-02-02 05:11:12 +00:00
}
BitmapText::~BitmapText()
{
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
BitmapText & BitmapText::operator=(const BitmapText &cpy)
2005-09-27 05:38:14 +00:00
{
Actor::operator=(cpy);
2005-09-27 05:38:14 +00:00
2005-07-15 03:50:56 +00:00
#define CPY(a) a = cpy.a
2008-04-12 22:01:05 +00:00
CPY( m_bUppercase );
2005-07-15 03:50:56 +00:00
CPY( m_sText );
CPY( m_wTextLines );
CPY( m_iLineWidths );
CPY( m_iWrapWidthPixels );
CPY( m_fMaxWidth );
CPY( m_fMaxHeight );
CPY( m_bRainbowScroll );
2006-12-07 07:49:22 +00:00
CPY( m_bJitter );
2006-01-25 09:32:24 +00:00
CPY( m_iVertSpacing );
2005-07-15 03:50:56 +00:00
CPY( m_aVertices );
CPY( m_vpFontPageTextures );
CPY( m_mAttributes );
CPY( m_bHasGlowAttribute );
CPY( m_StrokeColor );
2005-07-15 03:50:56 +00:00
#undef CPY
2005-09-27 05:38:14 +00:00
if( m_pFont )
FONT->UnloadFont( m_pFont );
2005-07-16 03:10:59 +00:00
if( cpy.m_pFont != NULL )
m_pFont = FONT->CopyFont( cpy.m_pFont );
else
m_pFont = NULL;
return *this;
}
BitmapText::BitmapText( const BitmapText &cpy ):
Actor( cpy )
{
m_pFont = NULL;
*this = cpy;
2005-07-15 03:50:56 +00:00
}
2005-02-09 05:27:51 +00:00
void BitmapText::LoadFromNode( const XNode* pNode )
2005-02-09 05:27:51 +00:00
{
2006-01-22 01:00:06 +00:00
RString sText;
2005-08-25 01:17:26 +00:00
pNode->GetAttrValue( "Text", sText );
2006-01-22 01:00:06 +00:00
RString sAltText;
2005-08-25 01:17:26 +00:00
pNode->GetAttrValue( "AltText", sAltText );
2005-02-09 05:27:51 +00:00
ThemeManager::EvaluateString( sText );
ThemeManager::EvaluateString( sAltText );
2006-01-22 01:00:06 +00:00
RString sFont;
if( !ActorUtil::GetAttrPath(pNode, "File", sFont) )
{
if( !pNode->GetAttrValue("Font", sFont) ) // accept "File" for backward compatibility
RageException::Throw( "%s: BitmapText: missing the File attribute",
ActorUtil::GetWhere(pNode).c_str() );
2007-02-15 11:27:17 +00:00
sFont = THEME->GetPathF( "", sFont );
}
2007-02-15 11:27:17 +00:00
LoadFromFont( sFont );
2005-02-09 05:27:51 +00:00
SetText( sText, sAltText );
Actor::LoadFromNode( pNode );
2005-02-09 05:27:51 +00:00
}
2006-01-22 01:00:06 +00:00
bool BitmapText::LoadFromFont( const RString& sFontFilePath )
2001-11-25 04:31:44 +00:00
{
CHECKPOINT_M( ssprintf("BitmapText::LoadFromFont(%s)", sFontFilePath.c_str()) );
2005-01-16 20:37:08 +00:00
if( m_pFont )
{
2003-01-04 05:17:51 +00:00
FONT->UnloadFont( m_pFont );
m_pFont = NULL;
}
m_pFont = FONT->LoadFont( sFontFilePath );
2002-01-16 10:01:32 +00:00
this->SetStrokeColor( m_pFont->GetDefaultStrokeColor() );
2003-01-04 07:55:42 +00:00
BuildChars();
return true;
}
2006-01-22 01:00:06 +00:00
bool BitmapText::LoadFromTextureAndChars( const RString& sTexturePath, const RString& sChars )
{
CHECKPOINT_M( ssprintf("BitmapText::LoadFromTextureAndChars(\"%s\",\"%s\")", sTexturePath.c_str(), sChars.c_str()) );
2005-01-16 20:37:08 +00:00
if( m_pFont )
{
2003-01-04 05:17:51 +00:00
FONT->UnloadFont( m_pFont );
m_pFont = NULL;
}
m_pFont = FONT->LoadFont( sTexturePath, sChars );
2002-09-03 06:33:08 +00:00
2003-01-04 07:55:42 +00:00
BuildChars();
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. */
2005-01-16 20:37:08 +00:00
if( m_pFont == NULL )
2003-01-04 07:55:42 +00:00
return;
2003-01-21 23:10:22 +00:00
/* calculate line lengths and widths */
m_size.x = 0;
2003-01-21 23:10:22 +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] ));
m_size.x = max( m_size.x, m_iLineWidths.back() );
2003-01-21 23:10:22 +00:00
}
2007-03-16 03:15:24 +00:00
/* Ensure that the width is always even. This maintains pixel alignment; fX below will
* always be an integer. */
m_size.x = QuantizeUp( m_size.x, 2.0f );
m_aVertices.clear();
m_vpFontPageTextures.clear();
2003-01-04 07:55:42 +00:00
2005-01-16 20:37:08 +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-08 02:32:30 +00:00
/* The height (from the origin to the baseline): */
2006-01-24 01:04:53 +00:00
int iPadding = m_pFont->GetLineSpacing() - m_pFont->GetHeight();
2006-01-24 01:12:54 +00:00
iPadding += m_iVertSpacing;
2003-01-08 02:32:30 +00:00
/* There's padding between every line: */
2006-06-25 04:00:34 +00:00
m_size.y += iPadding * int(m_wTextLines.size()-1);
2003-01-04 07:55:42 +00:00
// the top position of the first row of characters
int iY = lrintf(-m_size.y/2.0f);
2003-01-04 07:55:42 +00:00
2004-09-21 07:53:39 +00:00
for( unsigned 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();
wstring sLine = m_wTextLines[i];
2005-09-22 16:49:51 +00:00
if( m_pFont->IsRightToLeft() )
reverse( sLine.begin(), sLine.end() );
2003-01-04 07:55:42 +00:00
const int iLineWidth = m_iLineWidths[i];
float fX = SCALE( m_fHorizAlign, 0.0f, 1.0f, -m_size.x/2.0f, +m_size.x/2.0f - iLineWidth );
int iX = lrintf( fX );
2003-01-04 07:55:42 +00:00
for( unsigned j = 0; j < sLine.size(); ++j )
2003-01-04 07:55:42 +00:00
{
RageSpriteVertex v[4];
const glyph &g = m_pFont->GetGlyph( sLine[j] );
2003-01-04 07:55:42 +00:00
2005-09-22 09:08:11 +00:00
if( m_pFont->IsRightToLeft() )
iX -= g.m_iHadvance;
2003-01-04 07:55:42 +00:00
/* set vertex positions */
2006-01-24 01:00:49 +00:00
v[0].p = RageVector3( iX+g.m_fHshift, iY+g.m_pPage->m_fVshift, 0 ); // top left
v[1].p = RageVector3( iX+g.m_fHshift, iY+g.m_pPage->m_fVshift+g.m_fHeight, 0 ); // bottom left
v[2].p = RageVector3( iX+g.m_fHshift+g.m_fWidth, iY+g.m_pPage->m_fVshift+g.m_fHeight, 0 ); // bottom right
v[3].p = RageVector3( iX+g.m_fHshift+g.m_fWidth, iY+g.m_pPage->m_fVshift, 0 ); // top right
2003-01-04 07:55:42 +00:00
/* Advance the cursor. */
iX += g.m_iHadvance;
2002-01-16 10:01:32 +00:00
2003-01-04 07:55:42 +00:00
/* set texture coordinates */
2006-01-24 01:00:49 +00:00
v[0].t = RageVector2( g.m_TexRect.left, g.m_TexRect.top );
v[1].t = RageVector2( g.m_TexRect.left, g.m_TexRect.bottom );
v[2].t = RageVector2( g.m_TexRect.right, g.m_TexRect.bottom );
v[3].t = RageVector2( g.m_TexRect.right, g.m_TexRect.top );
2002-01-16 10:01:32 +00:00
m_aVertices.insert( m_aVertices.end(), &v[0], &v[4] );
m_vpFontPageTextures.push_back( g.GetFontPageTextures() );
2003-01-04 07:55:42 +00:00
}
2003-01-08 02:32:30 +00:00
/* The amount of padding a line needs: */
2006-01-24 01:02:21 +00:00
iY += iPadding;
2003-01-04 07:55:42 +00:00
}
}
void BitmapText::DrawChars( bool bUseStrokeTexture )
2003-01-04 07:55:42 +00:00
{
2004-03-06 07:15:59 +00:00
// bail if cropped all the way
2005-01-01 22:10:09 +00:00
if( m_pTempState->crop.left + m_pTempState->crop.right >= 1 ||
2004-03-06 07:15:59 +00:00
m_pTempState->crop.top + m_pTempState->crop.bottom >= 1 )
return;
const int iNumGlyphs = m_vpFontPageTextures.size();
2007-01-03 05:05:28 +00:00
int iStartGlyph = lrintf( SCALE( m_pTempState->crop.left, 0.f, 1.f, 0, (float) iNumGlyphs ) );
int iEndGlyph = lrintf( SCALE( m_pTempState->crop.right, 0.f, 1.f, (float) iNumGlyphs, 0 ) );
2004-03-06 07:15:59 +00:00
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 )
{
2005-01-01 22:10:09 +00:00
/* Handle fading by tweaking the alpha values of the vertices. */
2004-03-06 07:15:59 +00:00
/* 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. */
2005-08-25 01:26:59 +00:00
const float fHorizRemaining = 1.0f - (m_pTempState->crop.left + m_pTempState->crop.right);
2004-03-06 07:15:59 +00:00
if( FadeDist.left+FadeDist.right > 0 &&
2005-08-25 01:26:59 +00:00
fHorizRemaining < FadeDist.left+FadeDist.right )
2004-03-06 07:15:59 +00:00
{
const float LeftPercent = FadeDist.left/(FadeDist.left+FadeDist.right);
2005-08-25 01:26:59 +00:00
FadeSize.left = LeftPercent * fHorizRemaining;
FadeSize.right = (1.0f-LeftPercent) * fHorizRemaining;
2004-03-06 07:15:59 +00:00
}
/* 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.) */
2005-08-25 00:47:10 +00:00
const float fRightAlpha = SCALE( FadeSize.right, FadeDist.right, 0, 1, 0 );
const float fLeftAlpha = SCALE( FadeSize.left, FadeDist.left, 0, 1, 0 );
2004-03-06 07:15:59 +00:00
2005-08-25 00:47:10 +00:00
const float fStartFadeLeftPercent = m_pTempState->crop.left;
const float fStopFadeLeftPercent = m_pTempState->crop.left + FadeSize.left;
const float fLeftFadeStartGlyph = SCALE( fStartFadeLeftPercent, 0.f, 1.f, 0, (float) iNumGlyphs );
const float fLeftFadeStopGlyph = SCALE( fStopFadeLeftPercent, 0.f, 1.f, 0, (float) iNumGlyphs );
2004-03-06 07:15:59 +00:00
2005-08-25 00:47:10 +00:00
const float fStartFadeRightPercent = 1-(m_pTempState->crop.right + FadeSize.right);
const float fStopFadeRightPercent = 1-(m_pTempState->crop.right);
const float fRightFadeStartGlyph = SCALE( fStartFadeRightPercent, 0.f, 1.f, 0, (float) iNumGlyphs );
const float fRightFadeStopGlyph = SCALE( fStopFadeRightPercent, 0.f, 1.f, 0, (float) iNumGlyphs );
2004-03-06 07:15:59 +00:00
for( int start = iStartGlyph; start < iEndGlyph; ++start )
{
int i = start*4;
float fAlpha = 1.0f;
2004-03-06 07:15:59 +00:00
if( FadeSize.left > 0.001f )
{
2005-01-01 22:07:52 +00:00
/* Add .5, so we fade wrt. the center of the vert, not the left side. */
2004-03-06 07:15:59 +00:00
float fPercent = SCALE( start+0.5f, fLeftFadeStartGlyph, fLeftFadeStopGlyph, 0.0f, 1.0f );
fPercent = clamp( fPercent, 0.0f, 1.0f );
2005-08-25 00:47:10 +00:00
fAlpha *= fPercent * fLeftAlpha;
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 );
2005-08-25 00:47:10 +00:00
fAlpha *= fPercent * fRightAlpha;
2004-03-06 07:15:59 +00:00
}
for( int j = 0; j < 4; ++j )
m_aVertices[i+j].c.a = (unsigned char)( m_aVertices[i+j].c.a * fAlpha );
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 && m_vpFontPageTextures[end] == m_vpFontPageTextures[start] )
2003-01-04 07:55:42 +00:00
end++;
bool bHaveATexture = !bUseStrokeTexture || (bUseStrokeTexture && m_vpFontPageTextures[start]->m_pTextureStroke);
if( bHaveATexture )
{
DISPLAY->ClearAllTextures();
if( bUseStrokeTexture )
DISPLAY->SetTexture( TextureUnit_1, m_vpFontPageTextures[start]->m_pTextureStroke->GetTexHandle() );
else
DISPLAY->SetTexture( TextureUnit_1, m_vpFontPageTextures[start]->m_pTextureMain->GetTexHandle() );
/* Don't bother setting texture render states for text. We never go outside of 0..1. */
/* We should call SetTextureRenderStates because it does more than just setting
* the texture wrapping state. If setting the wrapping state is found to be slow,
* there should probably be a "don't care" texture wrapping mode set in Actor. -Chris */
Actor::SetTextureRenderStates();
RageSpriteVertex &start_vertex = m_aVertices[start*4];
int iNumVertsToDraw = (end-start)*4;
DISPLAY->DrawQuads( &start_vertex, iNumVertsToDraw );
}
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
/* 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. */
2006-01-22 01:00:06 +00:00
void BitmapText::SetText( const RString& _sText, const RString& _sAlternateText, int iWrapWidthPixels )
{
ASSERT( m_pFont );
2006-01-22 01:00:06 +00:00
RString sNewText = StringWillUseAlternate(_sText,_sAlternateText) ? _sAlternateText : _sText;
2008-03-28 12:24:04 +00:00
if( m_bUppercase )
sNewText.MakeUpper();
2003-11-06 07:23:22 +00:00
if( iWrapWidthPixels == -1 ) // wrap not specified
iWrapWidthPixels = m_iWrapWidthPixels;
2005-01-16 20:37:08 +00:00
if( m_sText == sNewText && iWrapWidthPixels==m_iWrapWidthPixels )
2003-01-04 07:55:42 +00:00
return;
2005-01-16 07:08:58 +00:00
m_sText = sNewText;
2003-11-06 07:23:22 +00:00
m_iWrapWidthPixels = iWrapWidthPixels;
ClearAttributes();
SetTextInternal();
}
2002-09-22 20:34:11 +00:00
void BitmapText::SetTextInternal()
{
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( m_iWrapWidthPixels == -1 )
2003-11-06 06:20:35 +00:00
{
2005-12-20 08:35:47 +00:00
split( RStringToWstring(m_sText), L"\n", m_wTextLines, false );
2003-11-06 06:20:35 +00:00
}
else
{
//
// Break sText into lines that don't exceed iWrapWidthPixels
2004-09-08 01:21:52 +00:00
// (if only one word fits on the line, it may be larger than iWrapWidthPixels).
2003-11-06 06:20:35 +00:00
//
// 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
2006-01-22 01:00:06 +00:00
vector<RString> asLines;
split( m_sText, "\n", asLines, false );
2003-11-06 06:20:35 +00:00
2004-09-08 01:21:52 +00:00
for( unsigned line = 0; line < asLines.size(); ++line )
2003-11-06 06:20:35 +00:00
{
2006-01-22 01:00:06 +00:00
vector<RString> asWords;
2004-09-08 01:21:52 +00:00
split( asLines[line], " ", asWords );
2003-11-06 06:20:35 +00:00
2006-01-22 01:00:06 +00:00
RString sCurLine;
2004-09-08 01:21:52 +00:00
int iCurLineWidth = 0;
for( unsigned i=0; i<asWords.size(); i++ )
2003-11-06 06:20:35 +00:00
{
2006-01-22 01:00:06 +00:00
const RString &sWord = asWords[i];
2005-12-20 08:35:47 +00:00
int iWidthWord = m_pFont->GetLineWidthInSourcePixels( RStringToWstring(sWord) );
2004-09-08 01:21:52 +00:00
if( sCurLine.empty() )
{
sCurLine = sWord;
iCurLineWidth = iWidthWord;
continue;
}
2006-01-22 01:00:06 +00:00
RString sToAdd = " " + sWord;
2003-11-06 06:20:35 +00:00
int iWidthToAdd = m_pFont->GetLineWidthInSourcePixels(L" ") + iWidthWord;
if( iCurLineWidth + iWidthToAdd <= m_iWrapWidthPixels ) // will fit on current line
2004-09-08 00:42:07 +00:00
{
sCurLine += sToAdd;
iCurLineWidth += iWidthToAdd;
}
else
2004-09-08 00:42:07 +00:00
{
2005-12-20 08:35:47 +00:00
m_wTextLines.push_back( RStringToWstring(sCurLine) );
sCurLine = sWord;
iCurLineWidth = iWidthWord;
2004-09-08 00:42:07 +00:00
}
2003-11-06 06:20:35 +00:00
}
2005-12-20 08:35:47 +00:00
m_wTextLines.push_back( RStringToWstring(sCurLine) );
2003-11-06 06:20:35 +00:00
}
}
2003-01-04 07:55:42 +00:00
BuildChars();
UpdateBaseZoom();
}
2001-11-03 10:52:42 +00:00
2006-01-24 01:12:54 +00:00
void BitmapText::SetVertSpacing( int iSpacing )
{
m_iVertSpacing = iSpacing;
BuildChars();
}
2005-04-15 12:25:59 +00:00
void BitmapText::SetMaxWidth( float fMaxWidth )
{
2005-04-15 12:25:59 +00:00
m_fMaxWidth = fMaxWidth;
UpdateBaseZoom();
}
void BitmapText::SetMaxHeight( float fMaxHeight )
{
m_fMaxHeight = fMaxHeight;
UpdateBaseZoom();
}
void BitmapText::SetUppercase( bool b )
{
m_bUppercase = b;
BuildChars();
}
void BitmapText::UpdateBaseZoom()
{
if( m_fMaxWidth == 0 )
{
this->SetBaseZoomX( 1 );
2005-04-15 12:25:59 +00:00
}
else
{
const float fWidth = GetUnzoomedWidth();
if( fWidth != 0 ) // don't divide by 0
{
/* Never decrease the zoom. */
const float fZoom = min( 1, m_fMaxWidth/fWidth );
this->SetBaseZoomX( fZoom );
}
}
2003-05-19 23:48:51 +00:00
2005-04-15 12:25:59 +00:00
if( m_fMaxHeight == 0 )
{
this->SetBaseZoomY( 1 );
2005-04-15 12:25:59 +00:00
}
else
{
const float fHeight = GetUnzoomedHeight();
if( fHeight != 0 ) // don't divide by 0
{
/* Never decrease the zoom. */
const float fZoom = min( 1, m_fMaxHeight/fHeight );
this->SetBaseZoomY( fZoom );
}
2005-04-15 12:25:59 +00:00
}
2003-05-19 23:48:51 +00:00
}
2006-01-22 01:00:06 +00:00
bool BitmapText::StringWillUseAlternate( const RString& sText, const RString& sAlternateText ) const
2003-02-12 20:19:20 +00:00
{
ASSERT( m_pFont );
/* Can't use the alternate if there isn't one. */
2005-01-16 20:37:08 +00:00
if( !sAlternateText.size() )
return false;
2003-02-12 20:19:20 +00:00
/* False if the alternate isn't needed. */
2005-12-20 08:35:47 +00:00
if( m_pFont->FontCompleteForString(RStringToWstring(sText)) )
2005-01-16 20:37:08 +00:00
return false;
2003-02-12 20:19:20 +00:00
/* False if the alternate is also incomplete. */
2005-12-20 08:35:47 +00:00
if( !m_pFont->FontCompleteForString(RStringToWstring(sAlternateText)) )
2005-01-16 20:37:08 +00:00
return false;
2003-02-12 20:19:20 +00:00
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 )
{
m_wTextLines[l].erase( m_wTextLines[l].end()-1, m_wTextLines[l].end() );
2003-11-06 07:23:22 +00:00
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
}
2005-08-25 00:57:53 +00:00
bool BitmapText::EarlyAbortDraw() const
{
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()
{
2005-02-27 08:31:41 +00:00
Actor::SetGlobalRenderStates(); // set Actor-specified render states
2007-02-13 04:59:05 +00:00
DISPLAY->SetTextureMode( TextureUnit_1, TextureMode_Modulate );
/* 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
{
2005-08-25 00:47:10 +00:00
//
// render the shadow
2005-08-25 00:47:10 +00:00
//
if( m_fShadowLengthX != 0 || m_fShadowLengthY != 0 )
{
DISPLAY->PushMatrix();
DISPLAY->TranslateWorld( m_fShadowLengthX, m_fShadowLengthY, 0 );
2002-08-19 20:02:30 +00:00
RageColor c = m_ShadowColor;
c.a *= m_pTempState->diffuse[0].a;
for( unsigned i=0; i<m_aVertices.size(); i++ )
m_aVertices[i].c = c;
DrawChars( false );
2002-01-16 10:01:32 +00:00
DISPLAY->PopMatrix();
}
2002-05-29 09:47:24 +00:00
2008-05-05 22:34:34 +00:00
//
// render the stroke
//
2008-07-04 22:44:20 +00:00
if( m_StrokeColor.a > 0 )
2008-05-05 22:34:34 +00:00
{
2008-07-04 22:44:20 +00:00
RageColor c = m_StrokeColor;
c.a *= m_pTempState->diffuse[0].a;
for( unsigned i=0; i<m_aVertices.size(); i++ )
m_aVertices[i].c = c;
DrawChars( true );
2008-05-05 22:34:34 +00:00
}
2005-08-25 00:47:10 +00:00
//
// render the diffuse pass
2005-08-25 00:47:10 +00:00
//
if( m_bRainbowScroll )
2002-05-29 09:47:24 +00:00
{
int color_index = int(RageTimer::GetTimeSinceStartFast() / 0.200) % RAINBOW_COLORS.size();
for( unsigned i=0; i<m_aVertices.size(); i+=4 )
{
const RageColor color = RAINBOW_COLORS[color_index];
2003-01-04 07:55:42 +00:00
for( unsigned j=i; j<i+4; j++ )
m_aVertices[j].c = color;
color_index = (color_index+1) % RAINBOW_COLORS.size();
}
2002-05-29 09:47:24 +00:00
}
else
2002-05-29 09:47:24 +00:00
{
size_t i = 0;
map<size_t,Attribute>::const_iterator iter = m_mAttributes.begin();
while( i < m_aVertices.size() )
{
// Set the colors up to the next attribute.
size_t iEnd = iter == m_mAttributes.end()? m_aVertices.size():iter->first*4;
iEnd = min( iEnd, m_aVertices.size() );
for( ; i < iEnd; i += 4 )
{
m_aVertices[i+0].c = m_pTempState->diffuse[0]; // top left
m_aVertices[i+1].c = m_pTempState->diffuse[2]; // bottom left
m_aVertices[i+2].c = m_pTempState->diffuse[3]; // bottom right
m_aVertices[i+3].c = m_pTempState->diffuse[1]; // top right
}
if( iter == m_mAttributes.end() )
break;
// Set the colors according to this attribute.
const Attribute &attr = iter->second;
++iter;
if( attr.length < 0 )
iEnd = iter == m_mAttributes.end()? m_aVertices.size():iter->first*4;
else
iEnd = i + attr.length*4;
iEnd = min( iEnd, m_aVertices.size() );
for( ; i < iEnd; i += 4 )
{
m_aVertices[i+0].c = attr.diffuse[0]; // top left
m_aVertices[i+1].c = attr.diffuse[2]; // bottom left
m_aVertices[i+2].c = attr.diffuse[3]; // bottom right
m_aVertices[i+3].c = attr.diffuse[1]; // top right
}
}
2002-05-29 09:47:24 +00:00
}
2002-01-16 10:01:32 +00:00
2006-12-07 07:49:22 +00:00
// apply jitter to verts
vector<RageVector3> vGlyphJitter;
if( m_bJitter )
{
2007-01-03 05:05:28 +00:00
int iSeed = lrintf( RageTimer::GetTimeSinceStartFast()*8 );
2006-12-07 07:49:22 +00:00
RandomGen rnd( iSeed );
for( unsigned i=0; i<m_aVertices.size(); i+=4 )
{
RageVector3 jitter( rnd()%2, rnd()%3, 0 );
vGlyphJitter.push_back( jitter );
m_aVertices[i+0].p += jitter; // top left
m_aVertices[i+1].p += jitter; // bottom left
m_aVertices[i+2].p += jitter; // bottom right
m_aVertices[i+3].p += jitter; // top right
}
}
DrawChars( false );
2006-12-07 07:49:22 +00:00
// undo jitter to verts
if( m_bJitter )
{
ASSERT( vGlyphJitter.size() == m_aVertices.size()/4 );
for( unsigned i=0; i<m_aVertices.size(); i+=4 )
{
const RageVector3 &jitter = vGlyphJitter[i/4];;
m_aVertices[i+0].p -= jitter; // top left
m_aVertices[i+1].p -= jitter; // bottom left
m_aVertices[i+2].p -= jitter; // bottom right
m_aVertices[i+3].p -= jitter; // top right
}
}
}
2002-01-16 10:01:32 +00:00
2002-12-29 22:55:26 +00:00
/* render the glow pass */
if( m_pTempState->glow.a > 0.0001f || m_bHasGlowAttribute )
2002-01-16 10:01:32 +00:00
{
2007-02-13 04:59:05 +00:00
DISPLAY->SetTextureMode( TextureUnit_1, TextureMode_Glow );
2002-08-19 20:02:30 +00:00
size_t i = 0;
map<size_t,Attribute>::const_iterator iter = m_mAttributes.begin();
while( i < m_aVertices.size() )
{
// Set the glow up to the next attribute.
size_t iEnd = iter == m_mAttributes.end()? m_aVertices.size():iter->first*4;
iEnd = min( iEnd, m_aVertices.size() );
for( ; i < iEnd; ++i )
m_aVertices[i].c = m_pTempState->glow;
if( iter == m_mAttributes.end() )
break;
// Set the glow according to this attribute.
const Attribute &attr = iter->second;
++iter;
if( attr.length < 0 )
iEnd = iter == m_mAttributes.end()? m_aVertices.size():iter->first*4;
else
iEnd = i + attr.length*4;
iEnd = min( iEnd, m_aVertices.size() );
for( ; i < iEnd; ++i )
m_aVertices[i].c = attr.glow;
}
DrawChars( false );
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. */
2007-03-01 08:14:33 +00:00
void BitmapText::SetHorizAlign( float f )
2003-01-04 07:55:42 +00:00
{
float fHorizAlign = m_fHorizAlign;
2007-03-01 08:14:33 +00:00
Actor::SetHorizAlign(f);
if( fHorizAlign == m_fHorizAlign )
return;
2003-01-04 07:55:42 +00:00
BuildChars();
}
2003-11-06 07:23:22 +00:00
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;
m_iWrapWidthPixels = iWrapWidthPixels;
SetTextInternal();
}
BitmapText::Attribute BitmapText::GetDefaultAttribute() const
{
Attribute attr;
for( int i = 0; i < 4; ++i )
attr.diffuse[i] = GetDiffuses( i );
attr.glow = GetGlow();
return attr;
}
void BitmapText::AddAttribute( size_t iPos, const Attribute &attr )
{
// Fixup position for new lines.
int iLines = 0;
size_t iAdjustedPos = iPos;
FOREACH_CONST( wstring, m_wTextLines, line )
{
size_t length = line->length();
if( length >= iAdjustedPos )
break;
iAdjustedPos -= length;
++iLines;
}
m_mAttributes[iPos-iLines] = attr;
m_bHasGlowAttribute = m_bHasGlowAttribute || attr.glow.a > 0.0001f;
}
void BitmapText::ClearAttributes()
{
m_mAttributes.clear();
m_bHasGlowAttribute = false;
}
void BitmapText::Attribute::FromStack( lua_State *L, int iPos )
{
if( lua_type(L, iPos) != LUA_TTABLE )
return;
lua_pushvalue( L, iPos );
const int iTab = lua_gettop( L );
// Get the length.
lua_getfield( L, iTab, "Length" );
length = lua_tointeger( L, -1 );
lua_settop( L, iTab );
// Get the diffuse colors.
lua_getfield( L, iTab, "Diffuses" );
if( !lua_isnil(L, -1) )
{
for( int i = 1; i <= 4; ++i )
{
lua_rawgeti( L, -i, i );
diffuse[i-1].FromStack( L, -1 );
}
}
lua_settop( L, iTab );
// Get a single diffuse color.
lua_getfield( L, iTab, "Diffuse" );
if( !lua_isnil(L, -1) )
{
diffuse[0].FromStack( L, -1 );
diffuse[1] = diffuse[2] = diffuse[3] = diffuse[0];
}
lua_settop( L, iTab );
// Get the glow color.
lua_getfield( L, iTab, "Glow" );
glow.FromStack( L, -1 );
lua_settop( L, iTab - 1 );
2003-11-06 07:23:22 +00:00
}
2004-06-08 00:47:53 +00:00
2005-06-20 04:06:26 +00:00
// lua start
2005-09-06 04:36:20 +00:00
#include "FontCharAliases.h"
2005-06-20 05:02:03 +00:00
class LunaBitmapText: public Luna<BitmapText>
2005-06-20 04:06:26 +00:00
{
public:
2006-12-07 07:49:22 +00:00
static int wrapwidthpixels( T* p, lua_State *L ) { p->SetWrapWidthPixels( IArg(1) ); return 0; }
static int maxwidth( T* p, lua_State *L ) { p->SetMaxWidth( FArg(1) ); return 0; }
static int maxheight( T* p, lua_State *L ) { p->SetMaxHeight( FArg(1) ); return 0; }
static int vertspacing( T* p, lua_State *L ) { p->SetVertSpacing( IArg(1) ); return 0; }
2005-09-06 04:36:20 +00:00
static int settext( T* p, lua_State *L )
{
2006-01-22 01:00:06 +00:00
RString s = SArg(1);
2007-08-21 00:26:49 +00:00
RString sAlt;
2005-09-06 04:36:20 +00:00
// XXX: Lua strings should simply use "\n" natively. However, some
// settext calls may be made from GetMetric() calls to other strings,
// and it's confusing for :: to work in some strings and not others.
// Eventually, all strings should be Lua expressions, but until then
// continue to support this.
s.Replace("::","\n");
FontCharAliases::ReplaceMarkers( s );
2007-08-21 00:26:49 +00:00
if( lua_gettop(L) > 1 )
{
sAlt = SArg(2);
sAlt.Replace("::","\n");
FontCharAliases::ReplaceMarkers( sAlt );
}
p->SetText( s, sAlt );
return 0;
2005-09-06 04:36:20 +00:00
}
static int rainbowscroll( T* p, lua_State *L ) { p->SetRainbowScroll( BArg(1) ); return 0; }
2006-12-07 07:49:22 +00:00
static int jitter( T* p, lua_State *L ) { p->SetJitter( BArg(1) ); return 0; }
static int GetText( T* p, lua_State *L ) { lua_pushstring( L, p->GetText() ); return 1; }
static int AddAttribute( T* p, lua_State *L )
{
size_t iPos = IArg(1);
BitmapText::Attribute attr = p->GetDefaultAttribute();
attr.FromStack( L, 2 );
p->AddAttribute( iPos, attr );
return 0;
}
2007-08-19 00:10:33 +00:00
static int ClearAttributes( T* p, lua_State *L ) { p->ClearAttributes(); return 0; }
static int strokecolor( T* p, lua_State *L ) { RageColor c; c.FromStackCompat( L, 1 ); p->SetStrokeColor( c ); return 0; }
2008-03-28 12:24:04 +00:00
static int uppercase( T* p, lua_State *L ) { p->SetUppercase( BArg(1) ); return 0; }
2005-06-20 04:06:26 +00:00
2006-09-27 20:30:29 +00:00
LunaBitmapText()
2005-06-20 04:06:26 +00:00
{
ADD_METHOD( wrapwidthpixels );
ADD_METHOD( maxwidth );
ADD_METHOD( maxheight );
2006-01-24 01:12:54 +00:00
ADD_METHOD( vertspacing );
ADD_METHOD( settext );
ADD_METHOD( rainbowscroll );
2006-12-07 07:49:22 +00:00
ADD_METHOD( jitter );
ADD_METHOD( GetText );
ADD_METHOD( AddAttribute );
2007-08-19 00:10:33 +00:00
ADD_METHOD( ClearAttributes );
ADD_METHOD( strokecolor );
2008-03-28 12:24:04 +00:00
ADD_METHOD( uppercase );
2005-06-20 04:06:26 +00:00
}
};
LUA_REGISTER_DERIVED_CLASS( BitmapText, Actor )
// lua end
2004-06-08 00:47:53 +00:00
/*
* (c) 2003-2007 Chris Danford, Charles Lohr, Steve Checkoway
2004-06-08 00:47:53 +00:00
* 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.
*/