From a818c567fc4aaf4622ff9d2286d5a4be203e1a20 Mon Sep 17 00:00:00 2001 From: zv0n Date: Sat, 30 Jan 2021 16:43:43 +0100 Subject: [PATCH] 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. --- sdlpp/sdlpp_textrenderer.cpp | 51 ++++++++++++++++++++++++------------ sdlpp/sdlpp_textrenderer.hpp | 18 +++++++++---- 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/sdlpp/sdlpp_textrenderer.cpp b/sdlpp/sdlpp_textrenderer.cpp index c611686..816c48c 100644 --- a/sdlpp/sdlpp_textrenderer.cpp +++ b/sdlpp/sdlpp_textrenderer.cpp @@ -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 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 diff --git a/sdlpp/sdlpp_textrenderer.hpp b/sdlpp/sdlpp_textrenderer.hpp index 7f33c70..db0716d 100644 --- a/sdlpp/sdlpp_textrenderer.hpp +++ b/sdlpp/sdlpp_textrenderer.hpp @@ -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; @@ -33,11 +33,19 @@ public: virtual std::shared_ptr< RenderObject > copySelf() override; private: - virtual void copyTo(std::shared_ptr other) override; + 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