From c43360bbb1461206525a04d7011eaec308dbf106 Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Thu, 6 Nov 2003 06:20:35 +0000 Subject: [PATCH] BitmapText: line wrap automatically --- stepmania/src/BitmapText.cpp | 58 ++++++++++++++++++++++++++++++--- stepmania/src/BitmapText.h | 2 +- stepmania/src/ScreenEdit.cpp | 1 + stepmania/src/ScreenOptions.cpp | 2 +- 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/stepmania/src/BitmapText.cpp b/stepmania/src/BitmapText.cpp index 3b88ff3857..11b05ab512 100644 --- a/stepmania/src/BitmapText.cpp +++ b/stepmania/src/BitmapText.cpp @@ -230,7 +230,7 @@ void BitmapText::DrawChars() /* 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 * in sAlternateText, too, just use sText. */ -void BitmapText::SetText( CString sText, CString sAlternateText ) +void BitmapText::SetText( CString sText, CString sAlternateText, int iWrapWidthPixels ) { ASSERT( m_pFont ); @@ -239,13 +239,61 @@ void BitmapText::SetText( CString sText, CString sAlternateText ) if(m_szText == sText) return; - m_szText = sText; - /* Break the string into lines. */ + + // Break the string into lines. + // m_szTextLines.clear(); - split(CStringToWstring(m_szText), L"\n", m_szTextLines, false); - + + if( iWrapWidthPixels == -1 ) + { + split( CStringToWstring(sText), L"\n", m_szTextLines, false ); + } + 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 + // TODO: Move this wrapping logic into Font + CStringArray asWords; + split( sText, " ", asWords ); + + CString sCurLine; + int iCurLineWidth = 0; + + for( int i=0; iGetLineWidthInSourcePixels( 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 + { + m_szTextLines.push_back( CStringToWstring(sCurLine) ); + sCurLine = sWord; + iCurLineWidth = iWidthWord; + } + } + } + m_szTextLines.push_back( CStringToWstring(sCurLine) ); + } + BuildChars(); } diff --git a/stepmania/src/BitmapText.h b/stepmania/src/BitmapText.h index 73d0c8a9fa..89829be4a2 100644 --- a/stepmania/src/BitmapText.h +++ b/stepmania/src/BitmapText.h @@ -26,7 +26,7 @@ public: bool LoadFromFont( CString sFontName ); bool LoadFromNumbers( CString sTexturePath ) { return LoadFromTextureAndChars(sTexturePath,"0123456789%. :x"); }; bool LoadFromTextureAndChars( CString sTexturePath, CString sChars ); - void SetText( CString sText, CString sAlternateText = "" ); + void SetText( CString sText, CString sAlternateText = "", int iWrapWidthPixels = -1 ); void SetTextMaxWidth( float MaxWidth, const CString &text, const CString &alttext = "" ); int GetWidestLineWidthInSourcePixels() { return m_iWidestLineWidth; }; diff --git a/stepmania/src/ScreenEdit.cpp b/stepmania/src/ScreenEdit.cpp index 7c0a03df67..3c359b0f7f 100644 --- a/stepmania/src/ScreenEdit.cpp +++ b/stepmania/src/ScreenEdit.cpp @@ -280,6 +280,7 @@ ScreenEdit::ScreenEdit( CString sName ) : Screen( sName ) m_Clipboard.SetNumTracks( m_NoteFieldEdit.GetNumTracks() ); + GAMESTATE->m_PlayerOptions[PLAYER_1].Init(); // don't allow weird options in editor. It doesn't handle reverse well. GAMESTATE->m_PlayerOptions[PLAYER_1].m_sNoteSkin = "default"; // change noteskin back to default before loading player GAMESTATE->ResetNoteSkins(); diff --git a/stepmania/src/ScreenOptions.cpp b/stepmania/src/ScreenOptions.cpp index 1391d02810..3c6222497b 100644 --- a/stepmania/src/ScreenOptions.cpp +++ b/stepmania/src/ScreenOptions.cpp @@ -791,7 +791,7 @@ void ScreenOptions::OnChange() sLineName.Replace("\n-",""); sLineName.Replace("\n",""); sLineName.Replace(" ",""); - pText->SetText( THEME->GetMetric(m_sName,sLineName) ); + pText->SetText( THEME->GetMetric(m_sName,sLineName), "", 800 ); } } }