2010-09-17 17:15:21 +02:00
|
|
|
#include "Text.hpp"
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
Text::Text( const char * t, int s, GXColor c )
|
|
|
|
: GuiText( t, s, c )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
|
|
|
maxWidth = 400;
|
|
|
|
linestodraw = 9;
|
|
|
|
curLineStart = 0;
|
|
|
|
FirstLineOffset = 0;
|
|
|
|
wText = NULL;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !text )
|
2010-09-17 17:15:21 +02:00
|
|
|
return;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
wText = new ( std::nothrow ) wString( text );
|
|
|
|
if ( !wText )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( wText->size() == 0 )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
wText->push_back( L' ' );
|
|
|
|
wText->push_back( 0 );
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
textWidth = ( font ? font : fontSystem )->getWidth( wText->data(), currentSize );
|
2010-09-17 17:15:21 +02:00
|
|
|
delete [] text;
|
|
|
|
text = NULL;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
SetMaxWidth( maxWidth );
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
Text::Text( const wchar_t * t, int s, GXColor c )
|
|
|
|
: GuiText( ( wchar_t * ) NULL, s, c )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
|
|
|
maxWidth = 400;
|
|
|
|
linestodraw = 9;
|
|
|
|
curLineStart = 0;
|
|
|
|
FirstLineOffset = 0;
|
|
|
|
wText = NULL;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !t )
|
2010-09-17 17:15:21 +02:00
|
|
|
return;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
wText = new ( std::nothrow ) wString( t );
|
|
|
|
if ( !wText )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( wText->size() == 0 )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
wText->push_back( L' ' );
|
|
|
|
wText->push_back( 0 );
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
textWidth = ( font ? font : fontSystem )->getWidth( wText->data(), currentSize );
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
SetMaxWidth( maxWidth );
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Text::~Text()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( wText )
|
2010-09-17 17:15:21 +02:00
|
|
|
delete wText;
|
|
|
|
wText = NULL;
|
|
|
|
|
|
|
|
TextLines.clear();
|
|
|
|
ClearDynamicText();
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
void Text::SetText( const char * t )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
wchar_t * tmp = charToWideChar( t );
|
|
|
|
if ( !tmp )
|
2010-09-17 17:15:21 +02:00
|
|
|
return;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( wText )
|
2010-09-17 17:15:21 +02:00
|
|
|
delete wText;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
wText = new ( std::nothrow ) wString( tmp );
|
|
|
|
if ( !wText )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( wText->size() == 0 )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
wText->push_back( L' ' );
|
|
|
|
wText->push_back( 0 );
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
textWidth = ( font ? font : fontSystem )->getWidth( wText->data(), currentSize );
|
2010-09-17 17:15:21 +02:00
|
|
|
|
|
|
|
delete [] tmp;
|
|
|
|
|
|
|
|
ClearDynamicText();
|
|
|
|
CalcLineOffsets();
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
void Text::SetText( const wchar_t * t )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !t )
|
2010-09-17 17:15:21 +02:00
|
|
|
return;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( wText )
|
2010-09-17 17:15:21 +02:00
|
|
|
delete wText;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
wText = new wString( t );
|
|
|
|
textWidth = ( font ? font : fontSystem )->getWidth( wText->data(), currentSize );
|
2010-09-17 17:15:21 +02:00
|
|
|
CalcLineOffsets();
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
void Text::SetMaxWidth( int w )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
|
|
|
maxWidth = w;
|
|
|
|
curLineStart = 0;
|
|
|
|
Refresh();
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
void Text::SetTextLine( int line )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( line < 0 )
|
|
|
|
line = 0;
|
|
|
|
else if ( line > ( int ) TextLines.size() - 1 )
|
|
|
|
line = TextLines.size() - 1;
|
2010-09-17 17:15:21 +02:00
|
|
|
|
|
|
|
curLineStart = line;
|
|
|
|
|
|
|
|
FillRows();
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( ( int ) textDyn.size() < linestodraw && curLineStart > 0 )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
|
|
|
PreviousLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
void Text::SetTextPos( int pos )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !wText )
|
2010-09-17 17:15:21 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
int diff = 10000;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
for ( u32 i = 0; i < TextLines.size(); i++ )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
int curDiff = abs( TextLines[i].LineOffset - pos );
|
|
|
|
if ( curDiff < diff )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
|
|
|
diff = curDiff;
|
|
|
|
curLineStart = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FillRows();
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( ( int ) textDyn.size() < linestodraw && curLineStart > 0 )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
|
|
|
PreviousLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t * Text::GetText()
|
|
|
|
{
|
|
|
|
return wText->c_str();
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
std::string Text::GetUTF8String( void ) const
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
|
|
|
return wText->toUTF8();
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int Text::GetLineOffset( int ind )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( TextLines.size() == 0 )
|
2010-09-17 17:15:21 +02:00
|
|
|
return 0;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( ind < 0 )
|
2010-09-17 17:15:21 +02:00
|
|
|
return TextLines[0].LineOffset;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( ind >= ( int ) TextLines.size() - 1 )
|
2010-09-17 17:15:21 +02:00
|
|
|
return TextLines[TextLines.size()-1].LineOffset;
|
|
|
|
|
|
|
|
return TextLines[ind].LineOffset;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
const wchar_t * Text::GetTextLine( int ind )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( filling || textDyn.size() == 0 )
|
2010-09-17 17:15:21 +02:00
|
|
|
return NULL;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( ind < 0 )
|
2010-09-17 17:15:21 +02:00
|
|
|
return textDyn[0];
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( ind >= ( int ) textDyn.size() )
|
2010-09-17 17:15:21 +02:00
|
|
|
return textDyn[textDyn.size()-1];
|
|
|
|
|
|
|
|
return textDyn[ind];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::Refresh()
|
|
|
|
{
|
|
|
|
CalcLineOffsets();
|
|
|
|
FillRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::NextLine()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !wText || ( curLineStart + 1 > ( ( int ) TextLines.size() - linestodraw ) ) )
|
2010-09-17 17:15:21 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
++curLineStart;
|
|
|
|
|
|
|
|
FillRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::PreviousLine()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !wText || curLineStart - 1 < 0 )
|
2010-09-17 17:15:21 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
--curLineStart;
|
|
|
|
|
|
|
|
FillRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::FillRows()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !wText )
|
2010-09-17 17:15:21 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
filling = true;
|
|
|
|
|
|
|
|
ClearDynamicText();
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
for ( int i = 0; i < linestodraw && i < ( int ) TextLines.size(); i++ )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( i >= ( int ) textDyn.size() )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
textDyn.resize( i + 1 );
|
2010-09-17 17:15:21 +02:00
|
|
|
textDyn[i] = new wchar_t[maxWidth];
|
|
|
|
}
|
|
|
|
int offset = TextLines[curLineStart+i].LineOffset;
|
2010-09-19 01:16:05 +02:00
|
|
|
int count = TextLines[curLineStart+i].CharCount + 1;
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
for ( int n = 0; n < count && offset + n < ( int ) wText->size(); n++ )
|
|
|
|
textDyn[i][n] = wText->at( offset + n );
|
2010-09-17 17:15:21 +02:00
|
|
|
|
|
|
|
textDyn[i][count] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
filling = false;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::CalcLineOffsets()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !wText )
|
2010-09-17 17:15:21 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
TextLines.clear();
|
|
|
|
|
|
|
|
TextLine TmpLine;
|
|
|
|
TmpLine.CharCount = 0;
|
|
|
|
TmpLine.LineOffset = 0;
|
|
|
|
TmpLine.width = 0;
|
|
|
|
|
|
|
|
const wchar_t * origTxt = wText->c_str();
|
|
|
|
int ch = 0;
|
|
|
|
int lastSpace = -1;
|
|
|
|
int lastSpaceIndex = -1;
|
|
|
|
int currWidth = 0;
|
|
|
|
int i = 0;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( origTxt[ch] )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
currWidth += fontSystem->getCharWidth( origTxt[ch], currentSize, ch > 0 ? origTxt[ch-1] : 0x0000 );
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( currWidth >= maxWidth )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( lastSpace > 0 )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
|
|
|
ch = lastSpace;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
TmpLine.CharCount = ch - TmpLine.LineOffset;
|
2010-09-17 17:15:21 +02:00
|
|
|
TmpLine.width = currWidth;
|
2010-09-19 01:16:05 +02:00
|
|
|
TextLines.push_back( TmpLine );
|
2010-09-17 17:15:21 +02:00
|
|
|
currWidth = 0;
|
|
|
|
lastSpace = -1;
|
|
|
|
i = -1;
|
2010-09-19 01:16:05 +02:00
|
|
|
TmpLine.LineOffset = ch + 1;
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
else if ( origTxt[ch] == '\n' )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
TmpLine.CharCount = ch - TmpLine.LineOffset;
|
2010-09-17 17:15:21 +02:00
|
|
|
TmpLine.width = currWidth;
|
2010-09-19 01:16:05 +02:00
|
|
|
TextLines.push_back( TmpLine );
|
2010-09-17 17:15:21 +02:00
|
|
|
currWidth = 0;
|
|
|
|
lastSpace = -1;
|
|
|
|
i = -1;
|
2010-09-19 01:16:05 +02:00
|
|
|
TmpLine.LineOffset = ch + 1;
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
else if ( origTxt[ch] == ' ' )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
|
|
|
lastSpace = ch;
|
|
|
|
lastSpaceIndex = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
ch++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
TmpLine.CharCount = ch - TmpLine.LineOffset;
|
2010-09-17 17:15:21 +02:00
|
|
|
TmpLine.width = currWidth;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( TmpLine.CharCount > 0 )
|
|
|
|
TextLines.push_back( TmpLine );
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Text::Draw()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( textDyn.size() == 0 )
|
2010-09-17 17:15:21 +02:00
|
|
|
return;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !this->IsVisible() )
|
|
|
|
return;
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
GXColor c = color;
|
|
|
|
c.a = this->GetAlpha();
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int newSize = size * GetScale();
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( newSize != currentSize )
|
|
|
|
{
|
|
|
|
currentSize = newSize;
|
2010-09-17 17:15:21 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( wText )
|
|
|
|
textWidth = ( font ? font : fontSystem )->getWidth( wText->data(), currentSize );
|
|
|
|
}
|
2010-09-17 17:15:21 +02:00
|
|
|
|
|
|
|
u16 lineheight = newSize + 6;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
for ( u32 i = 0; i < textDyn.size(); i++ )
|
2010-09-17 17:15:21 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !filling )
|
|
|
|
( font ? font : fontSystem )->drawText( this->GetLeft(), this->GetTop() + i*lineheight, 0, textDyn[i], currentSize, c, style, 0, maxWidth );
|
2010-09-17 17:15:21 +02:00
|
|
|
}
|
|
|
|
}
|