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:
parent
1f7a80d7d2
commit
73f67a3f47
51
sdlpp/sdlpp_fontconfiguration.hpp
Normal file
51
sdlpp/sdlpp_fontconfiguration.hpp
Normal file
@ -0,0 +1,51 @@
|
||||
#ifndef SDLPP_HPP_FONT_CONFIGURATION
|
||||
#define SDLPP_HPP_FONT_CONFIGURATION
|
||||
|
||||
#include "sdlpp_common.hpp"
|
||||
#include "sdlpp_font.hpp"
|
||||
|
||||
namespace SDLPP {
|
||||
class SDLPPSCOPE FontConfiguration {
|
||||
public:
|
||||
FontConfiguration() = delete;
|
||||
FontConfiguration( std::shared_ptr< Font > font, const std::string &color,
|
||||
const std::string &outline_color, double outline_size ) {
|
||||
_font = font;
|
||||
_color = color;
|
||||
_outline_color = outline_color;
|
||||
_outline_size = outline_size;
|
||||
}
|
||||
|
||||
const std::shared_ptr< Font > &getFont() {
|
||||
return _font;
|
||||
}
|
||||
|
||||
const std::string &getColor() {
|
||||
return _color;
|
||||
}
|
||||
|
||||
const std::string &getOutlineColor() {
|
||||
return _outline_color;
|
||||
}
|
||||
|
||||
const double &getOutlineSize() {
|
||||
return _outline_size;
|
||||
}
|
||||
|
||||
void setColor( const std::string &color ) {
|
||||
_color = color;
|
||||
}
|
||||
|
||||
void setOutlineColor( const std::string &outline_color ) {
|
||||
_outline_color = outline_color;
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr< Font > _font;
|
||||
std::string _color;
|
||||
std::string _outline_color;
|
||||
double _outline_size;
|
||||
};
|
||||
} // namespace SDLPP
|
||||
|
||||
#endif
|
@ -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
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include "sdlpp_common.hpp"
|
||||
#include "sdlpp_rectrenderer.hpp"
|
||||
#include "sdlpp_fontconfiguration.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
@ -18,14 +19,21 @@ public:
|
||||
const std::string &text, const std::string &color = "FFFFFF",
|
||||
const std::string &outline_color = "000000",
|
||||
double outline_size = -1, int flags = SDLPP_TEXT_CENTER );
|
||||
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 = SDLPP_TEXT_CENTER );
|
||||
void setText( std::shared_ptr< Font > font, const std::string &text,
|
||||
const std::string &color = "FFFFFF",
|
||||
const std::string &outline_color = "000000",
|
||||
double outline_size = -1 );
|
||||
void setText( const std::string &text,
|
||||
std::shared_ptr< FontConfiguration > config );
|
||||
void setTextColor( std::shared_ptr< Font > font,
|
||||
const std::string &color = "FFFFFF",
|
||||
const std::string &outline_color = "000000",
|
||||
double outline_size = -1 );
|
||||
void setTextColor( std::shared_ptr< FontConfiguration > config );
|
||||
void changeText( const std::string &text );
|
||||
void setFlags( int flags );
|
||||
virtual void render() override;
|
||||
@ -38,14 +46,11 @@ private:
|
||||
void saveFontConfig( std::shared_ptr< Font > font, const std::string &color,
|
||||
const std::string &outline_color,
|
||||
double outline_size );
|
||||
|
||||
void saveFontConfig( std::shared_ptr< FontConfiguration > config );
|
||||
std::string _text{};
|
||||
int position_flags = 0;
|
||||
SDL_Rect dst_rect{};
|
||||
std::shared_ptr< Font > last_font;
|
||||
std::string last_color;
|
||||
std::string last_outline_color;
|
||||
double last_outline_size;
|
||||
std::shared_ptr< FontConfiguration > _config;
|
||||
};
|
||||
} // end of namespace SDLPP
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user