SDLPP: Use FontConfiguration to store font configuration
With this it is possible to only store 1 configuration object as opposed to storing font configuration inside every TextRenderer
This commit is contained in:
@@ -12,8 +12,18 @@ TextRenderer::TextRenderer( double x, double y, double w, double h,
|
||||
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 );
|
||||
changeText( text );
|
||||
}
|
||||
TextRenderer::TextRenderer( double x, double y, double w, double h,
|
||||
std::shared_ptr< Renderer > &r,
|
||||
const std::string &text,
|
||||
std::shared_ptr< FontConfiguration > config,
|
||||
int flags )
|
||||
: RectangleRender( x, y, w, h, r ) {
|
||||
position_flags = flags;
|
||||
saveFontConfig( config );
|
||||
changeText( text );
|
||||
}
|
||||
void TextRenderer::setText( std::shared_ptr< Font > font,
|
||||
const std::string &text, const std::string &color,
|
||||
@@ -22,13 +32,21 @@ void TextRenderer::setText( std::shared_ptr< Font > font,
|
||||
_text = text;
|
||||
setTextColor( font, color, outline_color, outline_size );
|
||||
}
|
||||
void TextRenderer::setText( const std::string &text,
|
||||
std::shared_ptr< FontConfiguration > config ) {
|
||||
_text = text;
|
||||
setTextColor( config );
|
||||
}
|
||||
void TextRenderer::setTextColor( std::shared_ptr< Font > font,
|
||||
const std::string &color,
|
||||
const std::string &outline_color,
|
||||
double outline_size ) {
|
||||
saveFontConfig( font, color, outline_color, outline_size );
|
||||
updateSizeAndPosition();
|
||||
updateDstRect();
|
||||
}
|
||||
void TextRenderer::setTextColor( std::shared_ptr< FontConfiguration > config ) {
|
||||
saveFontConfig( config );
|
||||
updateSizeAndPosition();
|
||||
}
|
||||
void TextRenderer::changeText( const std::string &text ) {
|
||||
_text = text;
|
||||
@@ -55,13 +73,14 @@ void TextRenderer::updateSizeAndPosition() {
|
||||
RectangleRender::updateSizeAndPosition();
|
||||
|
||||
int fontSize = 0.6 * getRect().h;
|
||||
last_font->changeFontSize( fontSize );
|
||||
int intOutline = last_outline_size;
|
||||
_config->getFont()->changeFontSize( fontSize );
|
||||
int intOutline = _config->getOutlineSize();
|
||||
if ( intOutline != -1 ) {
|
||||
intOutline = last_outline_size * fontSize;
|
||||
intOutline = _config->getOutlineSize() * fontSize;
|
||||
}
|
||||
setTexture( *last_font, _text, last_color, last_outline_color, intOutline );
|
||||
last_font->revertSize();
|
||||
setTexture( *_config->getFont(), _text, _config->getColor(),
|
||||
_config->getOutlineColor(), intOutline );
|
||||
_config->getFont()->revertSize();
|
||||
|
||||
updateDstRect();
|
||||
}
|
||||
@@ -118,9 +137,11 @@ 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;
|
||||
_config = std::make_shared< FontConfiguration >( font, color, outline_color,
|
||||
outline_size );
|
||||
}
|
||||
void TextRenderer::saveFontConfig(
|
||||
std::shared_ptr< FontConfiguration > config ) {
|
||||
_config = config;
|
||||
}
|
||||
} // namespace SDLPP
|
||||
|
||||
Reference in New Issue
Block a user