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")
{ {
@ -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,7 +498,10 @@ 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:
// Compute 1st line image width that fits inside the the container width to get the origin position
if (direction_ == "horizontal")
{
for ( unsigned int i = 0; i < text_[0].size( ); ++i ) for ( unsigned int i = 0; i < text_[0].size( ); ++i )
{ {
Font::GlyphInfo glyph; Font::GlyphInfo glyph;
@ -505,11 +512,13 @@ void ReloadableScrollingText::draw( )
imageWidth += glyph.minX; imageWidth += glyph.minX;
} }
if ( (imageWidth + glyph.advance)*scale > imageMaxWidth ) int char_width = static_cast<int>( glyph.rect.w?glyph.rect.w:glyph.advance );
if ( (imageWidth + char_width) * scale * scaleX_ > imageMaxWidth )
{ {
break; break;
} }
imageWidth += glyph.advance; imageWidth += char_width;
/*printf("textData_[%d]=%c, glyph.advance= %f - %d\n", i, textData_[i], glyph.advance, glyph.advance); /*printf("textData_[%d]=%c, glyph.advance= %f - %d\n", i, textData_[i], glyph.advance, glyph.advance);
printf("imageWidth=%f \n", imageWidth);*/ printf("imageWidth=%f \n", imageWidth);*/
@ -520,6 +529,7 @@ void ReloadableScrollingText::draw( )
Logger::write(Logger::ZONE_WARNING, "Text", ss.str());*/ Logger::write(Logger::ZONE_WARNING, "Text", ss.str());*/
} }
} }
}
float oldWidth = baseViewInfo.Width; float oldWidth = baseViewInfo.Width;
float oldHeight = baseViewInfo.Height; float oldHeight = baseViewInfo.Height;
@ -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;
} }