From 4cd07098cdfbaa265091d126315ac8ca05761109 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Tue, 13 May 2025 17:01:10 -0500 Subject: [PATCH] optimize the last change --- src/gfx/render_text.cpp | 31 ++++++++++++++++++------------- src/gfx/render_text.hpp | 1 + src/scenedit/scen.btnmg.cpp | 4 +++- src/scenedit/scen.graphics.cpp | 2 +- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/gfx/render_text.cpp b/src/gfx/render_text.cpp index 7d7cc1cb..b5fbd62f 100644 --- a/src/gfx/render_text.cpp +++ b/src/gfx/render_text.cpp @@ -223,6 +223,23 @@ void draw_scale_aware_text(sf::RenderTarget& dest_window, sf::Text str_to_draw) } } +std::string truncate_with_ellipsis(std::string str, const TextStyle& style, int width) { + size_t length = string_length(str, style); + if(length > width){ + size_t ellipsis_length = string_length("...", style); + + size_t need_to_clip = length - width + ellipsis_length; + int clip_idx = str.size() - 1; + // clip_idx should never reach 0 + for(; clip_idx >= 0; --clip_idx){ + size_t clip_length = string_length(str.substr(clip_idx), style); + if(clip_length >= need_to_clip) break; + } + str = str.substr(0, clip_idx) + "..."; + } + return str; +} + static void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,text_params_t& options) { if(str.empty()) return; // Nothing to do! short line_height = options.style.lineHeight; @@ -262,19 +279,7 @@ static void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,st } if(mode == eTextMode::ELLIPSIS){ - size_t length = string_length(str, options.style); - if(length > dest_rect.width()){ - size_t ellipsis_length = string_length("...", options.style); - - size_t need_to_clip = length - dest_rect.width() + ellipsis_length; - int clip_idx = str.size() - 1; - // clip_idx should never reach 0 - for(; clip_idx >= 0; --clip_idx){ - size_t clip_length = string_length(str.substr(clip_idx), options.style); - if(clip_length >= need_to_clip) break; - } - str = str.substr(0, clip_idx) + "..."; - } + str = truncate_with_ellipsis(str, options.style, dest_rect.width()); mode = eTextMode::LEFT_TOP; } if(mode == eTextMode::WRAP){ diff --git a/src/gfx/render_text.hpp b/src/gfx/render_text.hpp index ab8dc3ef..0adc822f 100644 --- a/src/gfx/render_text.hpp +++ b/src/gfx/render_text.hpp @@ -71,6 +71,7 @@ void clear_scale_aware_text(sf::RenderTexture& texture); void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,eTextMode mode,TextStyle style,bool right_align = false); void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,eTextMode mode,TextStyle style, break_info_t break_info,bool right_align = false); break_info_t calculate_line_wrapping(rectangle dest_rect, std::string str, TextStyle style); +std::string truncate_with_ellipsis(std::string str, const TextStyle& style, int width); size_t string_length(std::string str, const TextStyle& style, short* height = nullptr); inline void round_vec(sf::Vector2f& vec) { vec.x = std::round(vec.x); diff --git a/src/scenedit/scen.btnmg.cpp b/src/scenedit/scen.btnmg.cpp index 4b351024..53b033ba 100644 --- a/src/scenedit/scen.btnmg.cpp +++ b/src/scenedit/scen.btnmg.cpp @@ -10,6 +10,7 @@ #include #include "scen.btnmg.hpp" #include "dialogxml/widgets/scrollbar.hpp" +#include "gfx/render_text.hpp" extern rectangle right_sbar_rect; @@ -84,7 +85,8 @@ void set_rb(short slot, eRBAction action, int n, std::string label, bool do_draw right_button_status.resize(slot + 1); right_button_status[slot].action = action; right_button_status[slot].i = n; - right_button_status[slot].label = label; + static TextStyle style; + right_button_status[slot].label = truncate_with_ellipsis(label, style, right_buttons[0].width()); for(char& c : right_button_status[slot].label) { if(c == '|') c = ' '; diff --git a/src/scenedit/scen.graphics.cpp b/src/scenedit/scen.graphics.cpp index ede8873a..92285fcb 100644 --- a/src/scenedit/scen.graphics.cpp +++ b/src/scenedit/scen.graphics.cpp @@ -516,7 +516,7 @@ void draw_rb_slot (short which,short mode) { style.colour = Colours::GREEN; style.lineHeight = 12; - win_draw_string(mainPtr(),text_rect,right_button_status[which].label,eTextMode::ELLIPSIS,style); + win_draw_string(mainPtr(),text_rect,right_button_status[which].label,eTextMode::LEFT_TOP,style); } void set_up_terrain_buttons(bool reset) {