correct 2 bugs in scrolling text: not displayed if waitEndTime<=0 and reset current position to 0 correctly

Signed-off-by: Vincent-FK <vincent.buso@funkey-project.com>
This commit is contained in:
Vincent-FK 2020-03-14 11:36:38 +01:00
parent 7ee88a1bc9
commit cfb2dbd880

View File

@ -86,6 +86,11 @@ void ReloadableScrollingText::update(float dt)
{ {
//currentPosition_ += scrollingSpeed_ * dt * scaleX_; //currentPosition_ += scrollingSpeed_ * dt * scaleX_;
currentPosition_ += (scrollForward_?1.0f:-1.0f) * scrollingSpeed_ * dt * scaleX_; currentPosition_ += (scrollForward_?1.0f:-1.0f) * scrollingSpeed_ * dt * scaleX_;
// Sanity check
if(currentPosition_ < -startPosition_ * scaleX_){
currentPosition_ = -startPosition_ * scaleX_;
}
} }
else if (direction_ == "vertical") else if (direction_ == "vertical")
{ {
@ -176,7 +181,7 @@ void ReloadableScrollingText::reloadTexture( )
if (systemMode_) if (systemMode_)
{ {
// check the master collection for the system artifact // check the master collection for the system artifact
loadText( collectionName, type_, type_, "", true ); loadText( collectionName, type_, type_, "", true );
// check collection for the system artifact // check collection for the system artifact
@ -193,7 +198,7 @@ void ReloadableScrollingText::reloadTexture( )
if (selectedItem->leaf) // item is a leaf if (selectedItem->leaf) // item is a leaf
{ {
// check the master collection for the artifact // check the master collection for the artifact
loadText( collectionName, type_, basename, "", false ); loadText( collectionName, type_, basename, "", false );
// check the collection for the artifact // check the collection for the artifact
@ -206,7 +211,7 @@ void ReloadableScrollingText::reloadTexture( )
else // item is a submenu else // item is a submenu
{ {
// check the master collection for the artifact // check the master collection for the artifact
loadText( collectionName, type_, basename, "", false ); loadText( collectionName, type_, basename, "", false );
// check the collection for the artifact // check the collection for the artifact
@ -219,7 +224,7 @@ void ReloadableScrollingText::reloadTexture( )
if (text_.empty( )) if (text_.empty( ))
{ {
loadText( selectedItem->name, type_, type_, "", true ); loadText( selectedItem->name, type_, type_, "", true );
} }
} }
@ -420,7 +425,7 @@ void ReloadableScrollingText::loadText( std::string collection, std::string type
return; return;
} }
std::string line; std::string line;
while(std::getline(includeStream, line)) while(std::getline(includeStream, line))
{ {
@ -454,7 +459,7 @@ void ReloadableScrollingText::draw( )
{ {
Component::draw( ); Component::draw( );
if (!text_.empty( ) && waitEndTime_ <= 0.0f && baseViewInfo.Alpha > 0.0f) if (!text_.empty( ) && baseViewInfo.Alpha > 0.0f)
{ {
Font *font; Font *font;
@ -463,7 +468,6 @@ void ReloadableScrollingText::draw( )
else // If not, use the general font settings else // If not, use the general font settings
font = fontInst_; font = fontInst_;
//SDL_Texture *t = font->getTexture( );
SDL_Surface *t = font->getTexture( ); SDL_Surface *t = font->getTexture( );
float imageHeight = 0; float imageHeight = 0;
@ -494,31 +498,37 @@ void ReloadableScrollingText::draw( )
float scale = 1.0f; float scale = 1.0f;
// determine image width that can fit the container from 1st line // Horizontal mode only:
for ( unsigned int i = 0; i < text_[0].size( ); ++i ) // Compute 1st line image width that fits inside the the container width to get the origin position
if (direction_ == "horizontal")
{ {
Font::GlyphInfo glyph; for ( unsigned int i = 0; i < text_[0].size( ); ++i )
if ( font->getRect( text_[0][i], glyph ) ) {
{ Font::GlyphInfo glyph;
if ( glyph.minX < 0 ) if ( font->getRect( text_[0][i], glyph ) )
{ {
imageWidth += glyph.minX; if ( glyph.minX < 0 )
} {
imageWidth += glyph.minX;
}
if ( (imageWidth + glyph.advance)*scale > imageMaxWidth ) int char_width = static_cast<int>( glyph.rect.w?glyph.rect.w:glyph.advance );
{
break;
}
imageWidth += glyph.advance;
/*printf("textData_[%d]=%c, glyph.advance= %f - %d\n", i, textData_[i], glyph.advance, glyph.advance); if ( (imageWidth + char_width) * scale * scaleX_ > imageMaxWidth )
printf("imageWidth=%f \n", imageWidth);*/ {
} break;
else{ }
/*std::stringstream ss; imageWidth += char_width;
ss << "Could not find Glyph info for char: " << textData_[i];
Logger::write(Logger::ZONE_WARNING, "Text", ss.str());*/ /*printf("textData_[%d]=%c, glyph.advance= %f - %d\n", i, textData_[i], glyph.advance, glyph.advance);
} printf("imageWidth=%f \n", imageWidth);*/
}
else{
/*std::stringstream ss;
ss << "Could not find Glyph info for char: " << textData_[i];
Logger::write(Logger::ZONE_WARNING, "Text", ss.str());*/
}
}
} }
float oldWidth = baseViewInfo.Width; float oldWidth = baseViewInfo.Width;
@ -576,7 +586,8 @@ void ReloadableScrollingText::draw( )
{ {
SDL_Rect charRect = glyph.rect; SDL_Rect charRect = glyph.rect;
rect.h = static_cast<int>( charRect.h * scale * scaleY_ ); rect.h = static_cast<int>( charRect.h * scale * scaleY_ );
rect.w = static_cast<int>( charRect.w?charRect.w:glyph.advance * scale * scaleX_ ); int char_width = static_cast<int>( charRect.w?charRect.w:glyph.advance );
rect.w = static_cast<int>( char_width * scale * scaleX_ );
rect.y = static_cast<int>( yOrigin ); rect.y = static_cast<int>( yOrigin );
/*if (font->getAscent( ) < glyph.maxY) /*if (font->getAscent( ) < glyph.maxY)
@ -586,19 +597,19 @@ void ReloadableScrollingText::draw( )
rect.y += static_cast<int>( (font->getAscent( ) - glyph.maxY) * scale * scaleY_ ); rect.y += static_cast<int>( (font->getAscent( ) - glyph.maxY) * scale * scaleY_ );
// Check if glyph falls partially outside the box at the back end // Check if glyph falls partially outside the box at the back end
if ((rect.x + static_cast<int>( glyph.advance * scale * scaleX_ )) >= (static_cast<int>( xOrigin ) + imageMaxWidth)) if ((rect.x + static_cast<int>( char_width * scale * scaleX_ )) >= (static_cast<int>( xOrigin ) + imageMaxWidth))
{ {
rect.w = static_cast<int>( xOrigin ) + static_cast<int>( imageMaxWidth ) - rect.x; rect.w = static_cast<int>( xOrigin ) + static_cast<int>( imageMaxWidth ) - rect.x;
charRect.w = static_cast<int>( rect.w / scale / scaleX_ ); charRect.w = static_cast<int>( rect.w / scale / scaleX_ );
} }
// Print the glyph if it falls (partially) within the box // Print the glyph if it falls (partially) within the box
if ( position + glyph.advance * scale * scaleX_ > currentPosition_ ) if ( position + char_width * scale * scaleX_ > currentPosition_ )
{ {
// Check if glyph falls partially outside the box at the front end // Check if glyph falls partially outside the box at the front end
if ( position < currentPosition_ ) if ( position < currentPosition_ )
{ {
rect.w = static_cast<int>( glyph.advance * scale * scaleX_ + position - currentPosition_ ); rect.w = static_cast<int>( char_width * scale * scaleX_ + position - currentPosition_ );
charRect.x = static_cast<int>( charRect.x + charRect.w - rect.w / scale / scaleX_ ); charRect.x = static_cast<int>( charRect.x + charRect.w - rect.w / scale / scaleX_ );
charRect.w = static_cast<int>( rect.w / scale / scaleX_ ); charRect.w = static_cast<int>( rect.w / scale / scaleX_ );
} }
@ -607,7 +618,7 @@ void ReloadableScrollingText::draw( )
SDL::renderCopy(t, baseViewInfo.Alpha, &charRect, &rect, baseViewInfo); SDL::renderCopy(t, baseViewInfo.Alpha, &charRect, &rect, baseViewInfo);
rect.x += rect.w; rect.x += rect.w;
} }
else if ((rect.x + static_cast<int>( glyph.advance * scale * scaleX_ )) >= (static_cast<int>( xOrigin ) + imageMaxWidth)) else if ((rect.x + static_cast<int>( char_width * scale * scaleX_ )) >= (static_cast<int>( xOrigin ) + imageMaxWidth))
{ {
rect.x = static_cast<int>( xOrigin ) + static_cast<int>( imageMaxWidth ) + 10; // Stop handling the rest of the string rect.x = static_cast<int>( xOrigin ) + static_cast<int>( imageMaxWidth ) + 10; // Stop handling the rest of the string
} }
@ -615,7 +626,7 @@ void ReloadableScrollingText::draw( )
rect.x += glyph.advance * scale * scaleX_; rect.x += glyph.advance * scale * scaleX_;
}*/ }*/
} }
position += glyph.advance * scale * scaleX_; position += char_width * scale * scaleX_;
} }
} }
@ -634,7 +645,9 @@ void ReloadableScrollingText::draw( )
{ {
imageWidth += glyph.minX; imageWidth += glyph.minX;
} }
imageWidth += glyph.advance;
int char_width = static_cast<int>( glyph.rect.w?glyph.rect.w:glyph.advance );
imageWidth += char_width;
} }
} }
} }
@ -667,6 +680,7 @@ void ReloadableScrollingText::draw( )
currentPosition_ <= -startPosition_ * scaleX_) currentPosition_ <= -startPosition_ * scaleX_)
{ {
waitStartTime_ = startTime_; waitStartTime_ = startTime_;
currentPosition_ = -startPosition_ * scaleX_;
scrollForward_ = true; scrollForward_ = true;
} }
@ -889,7 +903,7 @@ bool ReloadableScrollingText::mustRender( )
{ {
if ( Component::mustRender( ) ) return true; if ( Component::mustRender( ) ) return true;
if (!text_.empty( ) && waitEndTime_ <= 0.0f && baseViewInfo.Alpha > 0.0f) if (!text_.empty( ) && baseViewInfo.Alpha > 0.0f)
{ {
return true; return true;
} }