optimize the last change
This commit is contained in:
@@ -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){
|
||||
|
@@ -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);
|
||||
|
@@ -10,6 +10,7 @@
|
||||
#include <cmath>
|
||||
#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 = ' ';
|
||||
|
@@ -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) {
|
||||
|
Reference in New Issue
Block a user