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:
parent
7f661630c0
commit
a818c567fc
@ -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,6 +53,16 @@ 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() {
|
||||
@ -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
|
||||
|
@ -14,18 +14,18 @@ public:
|
||||
TextRenderer( double x, double y, double w, double h,
|
||||
std::shared_ptr< Renderer > &r );
|
||||
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 = "FFFFFF",
|
||||
const std::string &outline_color = "000000",
|
||||
double outline_size = -1, int flags = SDLPP_TEXT_CENTER );
|
||||
void setText( Font &font, const std::string &text,
|
||||
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 setTextColor( Font &font, const std::string &color = "FFFFFF",
|
||||
void setTextColor( std::shared_ptr< Font > font,
|
||||
const std::string &color = "FFFFFF",
|
||||
const std::string &outline_color = "000000",
|
||||
double outline_size = -1 );
|
||||
// TODO maybe store Font and actually update texture here
|
||||
void changeText( const std::string &text );
|
||||
void setFlags( int flags );
|
||||
virtual void render() override;
|
||||
@ -35,9 +35,17 @@ public:
|
||||
private:
|
||||
virtual void copyTo( std::shared_ptr< RenderObject > other ) override;
|
||||
void updateDstRect();
|
||||
void saveFontConfig( std::shared_ptr< Font > font, const std::string &color,
|
||||
const std::string &outline_color,
|
||||
double outline_size );
|
||||
|
||||
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;
|
||||
};
|
||||
} // end of namespace SDLPP
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user