SDLPP: TextRenderer remembers last font/color/outline

This is used for changeText() and updateSizeAndPosition() so programmer
doesn't have to pass the text configuration with every change of text/size.
This commit is contained in:
2021-01-30 16:43:43 +01:00
parent 7f661630c0
commit a818c567fc
2 changed files with 47 additions and 22 deletions
+34 -17
View File
@@ -5,36 +5,34 @@ TextRenderer::TextRenderer( double x, double y, double w, double h,
std::shared_ptr< Renderer > &r )
: RectangleRender( x, y, w, h, r ) {}
TextRenderer::TextRenderer( double x, double y, double w, double h,
std::shared_ptr< Renderer > &r, Font &font,
std::shared_ptr< Renderer > &r,
std::shared_ptr< Font > font,
const std::string &text, const std::string &color,
const std::string &outline_color, double outline_size,
int flags )
const std::string &outline_color,
double outline_size, int flags )
: RectangleRender( x, y, w, h, r ) {
position_flags = flags;
setText( font, text, color, outline_color, outline_size );
saveFontConfig( font, color, outline_color, outline_size );
}
void TextRenderer::setText( Font &font, const std::string &text,
const std::string &color,
void TextRenderer::setText( std::shared_ptr< Font > font,
const std::string &text, const std::string &color,
const std::string &outline_color,
double outline_size ) {
_text = text;
setTextColor( font, color, outline_color, outline_size );
}
void TextRenderer::setTextColor( Font &font, const std::string &color,
void TextRenderer::setTextColor( std::shared_ptr< Font > font,
const std::string &color,
const std::string &outline_color,
double outline_size ) {
int fontSize = 0.6 * getRect().h;
font.changeFontSize(fontSize);
int intOutline = outline_size;
if(intOutline != -1) {
intOutline = outline_size * fontSize;
}
setTexture( font, _text, color, outline_color, intOutline );
font.revertSize();
saveFontConfig( font, color, outline_color, outline_size );
updateSizeAndPosition();
updateDstRect();
}
void TextRenderer::changeText( const std::string &text ) {
_text = text;
updateSizeAndPosition();
}
void TextRenderer::setFlags( int flags ) {
position_flags = flags;
@@ -55,16 +53,26 @@ void TextRenderer::render() {
}
void TextRenderer::updateSizeAndPosition() {
RectangleRender::updateSizeAndPosition();
int fontSize = 0.6 * getRect().h;
last_font->changeFontSize( fontSize );
int intOutline = last_outline_size;
if ( intOutline != -1 ) {
intOutline = last_outline_size * fontSize;
}
setTexture( *last_font, _text, last_color, last_outline_color, intOutline );
last_font->revertSize();
updateDstRect();
}
std::shared_ptr< RenderObject > TextRenderer::copySelf() {
auto ret = std::make_shared< TextRenderer >( *this );
copyTo(ret);
copyTo( ret );
return ret;
}
void TextRenderer::copyTo(std::shared_ptr<RenderObject> other) {
RectangleRender::copyTo(other);
void TextRenderer::copyTo( std::shared_ptr< RenderObject > other ) {
RectangleRender::copyTo( other );
}
void TextRenderer::updateDstRect() {
@@ -106,4 +114,13 @@ void TextRenderer::updateDstRect() {
dst_rect.y = rect.y + rect.h - dst_rect.h;
}
}
void TextRenderer::saveFontConfig( std::shared_ptr< Font > font,
const std::string &color,
const std::string &outline_color,
double outline_size ) {
last_font = font;
last_color = color;
last_outline_color = outline_color;
last_outline_size = outline_size;
}
} // namespace SDLPP