new text mode, ELLIPSIS, truncates one line within rect...

This commit is contained in:
2025-05-06 16:20:33 -05:00
parent 30e77633e4
commit 5ea7fcd145
2 changed files with 19 additions and 2 deletions

View File

@@ -261,7 +261,23 @@ static void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,st
}
}
if(mode == eTextMode::WRAP) {
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) + "...";
}
mode = eTextMode::LEFT_TOP;
}
if(mode == eTextMode::WRAP){
break_info_t break_info = options.break_info;
// It is better to pre-calculate line-wrapping and pass it in the options,
@@ -364,7 +380,7 @@ std::vector<snippet_t> draw_string_sel(sf::RenderTarget& dest_window,rectangle d
return params.snippets;
}
std::set<std::string> strings_to_cache = {" "};
std::set<std::string> strings_to_cache = {" ", "..."};
size_t string_length(std::string str, const TextStyle& style, short* height){
size_t total_width = 0;

View File

@@ -60,6 +60,7 @@ enum class eTextMode {
CENTRE,
LEFT_TOP,
LEFT_BOTTOM,
ELLIPSIS,
};
std::vector<rectangle> draw_string_hilite(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,TextStyle style,std::vector<hilite_t> hilites,sf::Color hiliteClr);