From 3b84c9024b5ebef1f72b1dd1ae6ce3bd8bc8b447 Mon Sep 17 00:00:00 2001 From: Charles Lohr Date: Tue, 19 Apr 2005 02:14:43 +0000 Subject: [PATCH] Fix: Don't let BitmapText spill outside it's wrap area. Is this a good way to do it? anyone have a better idea? --- stepmania/src/BitmapText.cpp | 62 +++++++++++++++++++++++++-- stepmania/src/BitmapText.h | 1 + stepmania/src/ScreenNetSelectBase.cpp | 4 +- 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/stepmania/src/BitmapText.cpp b/stepmania/src/BitmapText.cpp index 42d6545c8a..3d329d8c0d 100644 --- a/stepmania/src/BitmapText.cpp +++ b/stepmania/src/BitmapText.cpp @@ -409,12 +409,28 @@ void BitmapText::SetText( const CString& _sText, const CString& _sAlternateText, } else { - m_wTextLines.push_back( CStringToWstring(sCurLine) ); - sCurLine = sWord; - iCurLineWidth = iWidthWord; + AddLine( sCurLine, iCurLineWidth ); + if ( sCurLine.length() != 0 ) + { + sCurLine += " " + sWord; + iCurLineWidth += iWidthWord; + } + else + { + sCurLine = sWord; + iCurLineWidth = iWidthWord; + } } } - m_wTextLines.push_back( CStringToWstring(sCurLine) ); + int LastWidth = iCurLineWidth; + while ( sCurLine.length() > 0 ) + { + AddLine( sCurLine, iCurLineWidth ); + if ( LastWidth < iCurLineWidth ) + break; //Something's wrong (infinite loop) + else + LastWidth = iCurLineWidth; + } } } @@ -422,6 +438,44 @@ void BitmapText::SetText( const CString& _sText, const CString& _sAlternateText, UpdateBaseZoom(); } +/* This will get called only if the sAddition is already somewhat + broken down into lines. This is in place only to handle single + words that are too wide for the line. */ +void BitmapText::AddLine( CString &sAddition, int & iWidthPixels ) +{ + if ( ( iWidthPixels < m_iWrapWidthPixels ) || ( sAddition.length() == 0 ) ) + { + m_wTextLines.push_back( CStringToWstring( sAddition ) ); + sAddition = ""; + iWidthPixels = 0; + } + else + { + CString sCurrentLine; + int iCurrentLineWidth; + for ( unsigned i = 0; i < sAddition.length(); i++ ) + { + CString sCurrentChar = sAddition.substr( i, 1 ); + int iCurrentCharWidth = m_pFont->GetLineWidthInSourcePixels( CStringToWstring( sCurrentChar ) ); + + if ( iCurrentLineWidth + iCurrentCharWidth > m_iWrapWidthPixels ) + { + if ( sCurrentLine.length() != 0 ) + m_wTextLines.push_back( CStringToWstring( sCurrentLine ) ); + sCurrentLine = sCurrentChar; + iCurrentLineWidth = iCurrentCharWidth; + } + else + { + sCurrentLine += sCurrentChar; + iCurrentLineWidth += iCurrentCharWidth; + } + } + sAddition = sCurrentLine; + iWidthPixels = iCurrentLineWidth; + } +} + void BitmapText::SetMaxWidth( float fMaxWidth ) { m_fMaxWidth = fMaxWidth; diff --git a/stepmania/src/BitmapText.h b/stepmania/src/BitmapText.h index e7a0c2f56a..e3cfe26c61 100644 --- a/stepmania/src/BitmapText.h +++ b/stepmania/src/BitmapText.h @@ -86,6 +86,7 @@ protected: vector verts; vector tex; + void AddLine( CString & sAddition, int & iWidthPixels ); void BuildChars(); void DrawChars(); void UpdateBaseZoom(); diff --git a/stepmania/src/ScreenNetSelectBase.cpp b/stepmania/src/ScreenNetSelectBase.cpp index 6ef18e982a..27f5a34d61 100644 --- a/stepmania/src/ScreenNetSelectBase.cpp +++ b/stepmania/src/ScreenNetSelectBase.cpp @@ -79,7 +79,7 @@ void ScreenNetSelectBase::Init() vector wLines; m_textOutHidden.GetLines( wLines ); m_actualText = ""; - for (unsigned i = max(int(wLines.size()) - SHOW_CHAT_LINES, 0 ) ; i < wLines.size() ; ++i) + for (unsigned i = max(int(wLines.size()) - SHOW_CHAT_LINES + 1, 0 ) ; i < wLines.size() ; ++i) m_actualText += WStringToCString( wLines[i] )+'\n'; m_textChatOutput.SetText( m_actualText ); @@ -201,7 +201,7 @@ void ScreenNetSelectBase::HandleScreenMessage( const ScreenMessage SM ) vector wLines; m_textOutHidden.GetLines( wLines ); m_actualText = ""; - for (unsigned i = max(int(wLines.size()) - SHOW_CHAT_LINES, 0 ) ; i < wLines.size() ; ++i) + for (unsigned i = max(int(wLines.size()) - SHOW_CHAT_LINES + 1, 0 ) ; i < wLines.size() ; ++i) m_actualText += WStringToCString( wLines[i] )+'\n'; m_textChatOutput.SetText( m_actualText ); }